exec

package
v0.0.0-...-e5c115e Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommandOutput

func CommandOutput(name string, args ...string) ([]byte, error)

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

func CommandRun(name string, args ...string) error

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

type Cmd struct {
	Name string
	Args []string
}

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

func (c Cmd) ArgsMatch(args ...string) bool

ArgsMatch returns true if the command's args match the provided ones exactly.

func (Cmd) Matches

func (c Cmd) Matches(name string, args ...string) bool

Matches returns true if the command's name and args match the provided ones exactly.

func (Cmd) Output

func (c Cmd) Output() ([]byte, error)

Output internally calls exec.Command(c.Name, c.Args...).Output() and returns the result. This can be used to execute certain commands in tests while mocking others.

func (Cmd) Run

func (c Cmd) Run() error

Run internally calls exec.Command(c.Name, c.Args...).Run() and returns potential error. This can be used to execute certain commands in tests while mocking others.

type CommandOutputFunc

type CommandOutputFunc func(cmd Cmd) ([]byte, error)

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

type CommandRunFunc func(cmd Cmd) error

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.

func (*ExitError) Error

func (e *ExitError) Error() string

Error implements error.

type FakeProcessState

type FakeProcessState struct {
	ExitStatus   int
	ErrorMessage string
}

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

type ProcessState interface {
	String() string
	ExitCode() int
}

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.

Jump to

Keyboard shortcuts

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