commandtest

package
v0.0.0-...-6f3d572 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cmp

func Cmp[T any](t *testing.T, prefix string, want, got T, opts ...cmp.Option)

Cmp compares the two provided fields.

func CmpError

func CmpError(t *testing.T, funcString string, wantErr, err error, opts ...cmp.Option)

CmpError compares the provided errors

func CmpPanic

func CmpPanic[T any](t *testing.T, funcString string, f func() T, want interface{}, opts ...cmp.Option) T

CmpPanic runs the provided function, `f`, and verifies the proper panic value is recoverd.

func FilepathAbs

func FilepathAbs(t *testing.T, s ...string) string

FilepathAbs returns the absolute, joined filepath for the provided strings. Fails the test if unsuccessful.

func StubEnv

func StubEnv(t *testing.T, m map[string]string)

StubEnv stubs the environment variable used throughout this package.

func StubGetwd

func StubGetwd(t *testing.T, wd string, err error)

StubGetwd uses the provided string and error when calling command.GetwdProcessor.

func StubValue

func StubValue[T any](t *testing.T, originalValue *T, newValue T)

StubValue stubs the originalValue with newValue for the duration of the test run.

func TempFile

func TempFile(t *testing.T, pattern string) *os.File

TempFile creates a temporary file and fails the test if not successful.

func Write

func Write(t *testing.T, iow io.Writer, contents []string)

Write writes the provided contents (joined by newline characters) to iow.

Types

type Changeable

type Changeable interface {
	// Changed returns whether or not the undelrying command object has changed.
	Changed() bool
}

Changeable is an interface for commands that can be changed. Note: this is really just using a function from the `sourcerer.CLI` interface.

type CompleteTestCase

type CompleteTestCase struct {
	// Node is the root `Node` of the command to test.
	Node command.Node
	// Args is the list of arguments provided to the command.
	// Remember that args requires a dummy command argument (e.g. "cmd ")
	// since `COMP_LINE` includes that.
	Args string
	// PassthroughArgs are the passthrough args provided to the command autocompletion.
	PassthroughArgs []string
	// Env is the map of os environment variables to stub. If nil, this is not stubbed.
	Env map[string]string
	// OS is the OS to use for the test.
	OS command.OS

	// Want is the expected `Autocompletion` object produced by the test.
	Want *command.Autocompletion
	// WantErr is the error that should be returned.
	WantErr error
	// WantData is the `Data` object that should be constructed.
	WantData *command.Data
	// SkipDataCheck skips the check on `WantData`.
	SkipDataCheck bool
	// DataCmpOpts is the set of cmp.Options that should be used
	// when comparing data.
	DataCmpOpts cmp.Options

	// RunResponses are the stubbed responses to return from exec.Cmd.Run.
	RunResponses []*FakeRun

	// WantRunContents are the set of shell commands that should have been run.
	WantRunContents []*RunContents
}

CompleteTestCase is a test case object for testing command autocompletion.

func (*CompleteTestCase) GetEnv

func (ctc *CompleteTestCase) GetEnv() map[string]string

type ExecuteTestCase

type ExecuteTestCase struct {
	// Node is the root `Node` of the command to test.
	Node command.Node
	// Args is the list of arguments provided to the command.
	Args []string
	// Env is the map of os environment variables to stub. If nil, this is not stubbed.
	Env map[string]string
	// OS is the OS to use for the test.
	OS command.OS

	// WantData is the `Data` object that should be constructed.
	WantData *command.Data
	// SkipDataCheck skips the check on `WantData`.
	SkipDataCheck bool
	// DataCmpOpts is the set of cmp.Options that should be used
	// when comparing data.
	DataCmpOpts cmp.Options
	// WantExecuteData is the `ExecuteData` object that should be constructed.
	WantExecuteData *command.ExecuteData
	// WantStdout is the data that should be sent to stdout.
	WantStdout string
	// WantStderr is the data that should be sent to stderr.
	WantStderr string
	// WantErr is the error that should be returned.
	WantErr error
	// WantPanic is the object that should be passed to panic (or nil if no panic expected).
	WantPanic interface{}

	// WantRunContents are the set of shell commands that should have been run.
	WantRunContents []*RunContents

	// RequiresSetup indicates whether or not the command requires setup
	RequiresSetup bool
	// SetupContents is the contents of the setup file provided to the command.
	SetupContents []string

	// RunResponses are the stubbed responses to return from exec.Cmd.Run.
	RunResponses []*FakeRun
}

ExecuteTestCase is a test case object for testing command execution.

func (*ExecuteTestCase) GetEnv

func (etc *ExecuteTestCase) GetEnv() map[string]string

type FakeOS

type FakeOS struct{}

FakeOS is a fake `command.OS` interface implementer that can be used for testing purposes.

func (*FakeOS) SetEnvVar

func (*FakeOS) SetEnvVar(variable, value string) string

func (*FakeOS) UnsetEnvVar

func (*FakeOS) UnsetEnvVar(variable string) string

type FakeRun

type FakeRun struct {
	Stdout []string
	Stderr []string
	Err    error
	F      func(t *testing.T)
}

FakeRun is a fake shell command run.

type Output

type Output struct {
	command.Output
	// contains filtered or unexported fields
}

Output is a fake `Output` object that can be used for testing.

func NewOutput

func NewOutput() *Output

NewOutput returns a new `Output` object.

func (*Output) Close

func (fo *Output) Close()

Close closes the fake output channel.

func (*Output) GetStderr

func (fo *Output) GetStderr() string

GetStderr returns all of the data that was written to the stderr channel.

func (*Output) GetStderrByCalls

func (fo *Output) GetStderrByCalls() []string

GetStderrByCalls returns all of the individual calls made to stdout.

func (*Output) GetStdout

func (fo *Output) GetStdout() string

GetStdout returns all of the data that was written to the stdout channel.

func (*Output) GetStdoutByCalls

func (fo *Output) GetStdoutByCalls() []string

GetStdoutByCalls returns all of the individual calls made to stdout.

type RunContents

type RunContents struct {
	Name          string
	Args          []string
	Dir           string
	StdinContents string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL