Documentation ¶
Index ¶
- func CommandOutput(name string, args ...string) ([]byte, error)
- func CommandRun(name string, args ...string) error
- func FakeCommandOutput(fn CommandOutputFunc) func()
- func FakeCommandRun(fn CommandRunFunc) func()
- type Cmd
- type CommandOutputFunc
- type CommandRunFunc
- type ExitError
- type FakeProcessState
- type ProcessState
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CommandOutput ¶
CommandOutput runs the a command with given args and returns its standard output. Any returned error will usually be of type *ExitError.
In the normal case, this just internally calls exec.Command(name, args...).Output() and returns the result.
In tests the behaviour can be changed. See the documentation of the FakeCommandOutput func.
func CommandRun ¶
CommandRun runs the a command with given args. Any returned error will usually be of type *ExitError.
In the normal case, this just internally calls exec.Command(name, args...).Run().
In tests the behaviour can be changed. See the documentation of the FakeCommandRun func.
func FakeCommandOutput ¶
func FakeCommandOutput(fn CommandOutputFunc) func()
FakeCommandOutput replaces all calls of CommandOutput with given fn in tests. The returned func must be called after the tests are finished to restore CommandOutput to avoid unexpected behaviour.
fakeCommandOutputFn := func(name string, args ...string) ([]byte, error) { if name == "foo" && exec.ArgsMatch(args, "--bar", "baz") { return []byte(`the output`), nil } return nil, &exec.ExitError{ ProcessState: &exec.FakeProcessState{ExitStatus: 1}, } } restore := FakeCommandOutput(fakeCommandOutputFn) defer restore()
func FakeCommandRun ¶
func FakeCommandRun(fn CommandRunFunc) func()
FakeCommandRun replaces all calls of CommandRun with given fn in tests. The returned func must be called after the tests are finished to restore CommandRun to avoid unexpected behaviour.
fakeCommandRunFn := func(name string, args ...string) error { if name == "foo" && exec.ArgsMatch(args, "--bar", "baz") { return nil } return &exec.ExitError{ ProcessState: &exec.FakeProcessState{ExitStatus: 1}, } } restore := FakeCommandRun(fakeCommandRunFn) defer restore()
Types ¶
type Cmd ¶
Cmd is a container type which wrap the command name and args. It has some methods attached to it which help matching commands in tests.
func (Cmd) ArgsMatch ¶
ArgsMatch returns true if the command's args match the provided ones exactly.
func (Cmd) Matches ¶
Matches returns true if the command's name and args match the provided ones exactly.
type CommandOutputFunc ¶
CommandOutputFunc is a func which takes a command name and an optional number of args and produces a byte slice of output or an error.
type CommandRunFunc ¶
CommandRunFunc is a func which takes a command name and an optional number of args and runs it, returning any errors.
type ExitError ¶
type ExitError struct { ProcessState Stderr []byte }
ExitError has the same structure as exec.ExitError with the only difference that the embedded ProcessState is an interface instead of *os.ProcessState so that it can be faked in tests.
type FakeProcessState ¶
FakeProcessState is a fake process state which lets users fake the exit status of a command in tests.
func (*FakeProcessState) ExitCode ¶
func (p *FakeProcessState) ExitCode() int
ExitCode implements ProcessState.
func (*FakeProcessState) String ¶
func (p *FakeProcessState) String() string
String implements fmt.Stringer.
type ProcessState ¶
ProcessState is the interface satisfied by os.ProcessState containing only methods that might be usful in tests. This is used by ExitError so the ProcessState can be mocked in tests, e.g. if it is required to test behaviour based on different exit codes.