Documentation
¶
Index ¶
- type Call
- type CmdDumpStyle
- type Command
- type Error
- type ExitError
- type LoggingWriter
- type MockOptions
- type MockRunner
- func (m *MockRunner) ExpectCmd(cmd Command) *Call
- func (m *MockRunner) ExpectSubprocessCmd(cmd Command) *Call
- func (m *MockRunner) PrepareSubprocess(cmd Command, options ...RunOption) Subprocess
- func (m *MockRunner) Run(cmd Command, options ...RunOption) error
- func (m *MockRunner) Test(t *testing.T)
- type RealRunner
- type RunOption
- type RunOptions
- type Runner
- type Subprocess
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Call ¶
Call decorates testify's Call struct with additional methods for simulating stdout / stderr output from a mocked command
func (*Call) ExitsNonZero ¶
ExitsNonZero configures the mock command to exit with a non-zero exit code
func (*Call) WithStderr ¶
WithStderr configures the mock command to write the given data to stderr
func (*Call) WithStdout ¶
WithStdout configures the mock command to write the given data to stdout
type CmdDumpStyle ¶
type CmdDumpStyle int
CmdDumpStyle how to style commands when they are printed to the console
const ( Default CmdDumpStyle = iota Pretty Spew )
Default prints the command using "%v" Pretty formats commands using PrettyFormat Spew uses the spew library to spew the entire struct
type Command ¶
type Command struct {
Prog string // Prog Main CLI program to execute
Args []string // Args Arguments to pass to program
Env []string // Env List of environment variables, eg []string{ "FOO=BAR", "BAZ=QUUX" }, to set when executing
Dir string // Dir Directory where command should be run
PristineEnv bool // PristineEnv When true, set only supplied Env vars without inheriting current process's env vars
}
Command encapsulates a shell command
func CmdFromArgs ¶
CmdFromArgs Convenience function to build a shell.Command from a list of arguments
Eg. CmdFromArgs("FOO=BAR", "ls", "-al", ".") ->
Command{
Env: []string{"FOO=BAR"},
Prog: "ls",
Args: []string{"-al", "."},
Dir: ""
}
func CmdFromFmt ¶
CmdFromFmt Convenience function to build a shell.Command from a format string and arguments
Eg. CmdFromFmt("HOME=%s FOO=BAR ls -al %s", "/tmp", "Documents") ->
Command{
Env: []string{"HOME=/tmp", "FOO=BAR"},
Prog: "ls",
Args: []string{"-al", "Documents},
Dir: ""
}
func (Command) PrettyFormat ¶
PrettyFormat converts a command into a simple string for easy inspection. Eg.
&Command{
Prog: []string{"echo"},
Args: []string{"foo", "bar", "baz"},
Dir: "/tmp",
Env: []string{"A=B", "C=D"}
}
-> "A=B C=D echo foo bar baz"
type Error ¶
type Error struct {
Command Command // the command that generated this error e
// contains filtered or unexported fields
}
Error is a generic error that is returned in situations other than the command failing. (eg. if the Command's Directory does not exist)
type ExitError ¶
type ExitError struct {
Command Command // the command that generated this error
ExitCode int // exit code of command
Stderr string // stderr output
}
ExitError is returned when a command fails
type LoggingWriter ¶ added in v0.0.36
type LoggingWriter struct {
// contains filtered or unexported fields
}
LoggingWriter an io.Writer that logs messages that are sent to it with Write() and optionally forwards to another io.Writer
func NewLoggingWriter ¶ added in v0.0.36
type MockOptions ¶
type MockOptions struct {
VerifyOrder bool // VerifyOrder If true, verify commands are run in the order they were declared
DumpStyle CmdDumpStyle // DumpStyle how to style the dump
IgnoreEnvVars []string // Array of environment variable names to strip when matching shell.Command arguments
IgnoreDir bool // If true, ignore Dir field of shell.Command arguments
}
options for a MockRunner
type MockRunner ¶
MockRunner is an implementation of Runner interface for use with testify/mock.
func DefaultMockRunner ¶
func DefaultMockRunner() *MockRunner
DefaultMockRunner returns a new mock runner instance with default settings
func NewMockRunner ¶
func NewMockRunner(options MockOptions) *MockRunner
NewMockRunner constructor for MockRunner
func (*MockRunner) ExpectCmd ¶
func (m *MockRunner) ExpectCmd(cmd Command) *Call
ExpectCmd sets an expectation for a command that should be run.
func (*MockRunner) ExpectSubprocessCmd ¶ added in v0.0.44
func (m *MockRunner) ExpectSubprocessCmd(cmd Command) *Call
ExpectSubprocessCmd sets an expectation for a command run from a Subprocess.
func (*MockRunner) PrepareSubprocess ¶ added in v0.0.44
func (m *MockRunner) PrepareSubprocess(cmd Command, options ...RunOption) Subprocess
PrepareSubprocess creates a Subprocess with a Start method that has the same mock behavior as Run. Note that this isn't sufficient to fully mock parallel execution; this exists largely to maintain type compatibility with the very real implementation offered by RealRunner.
func (*MockRunner) Run ¶
func (m *MockRunner) Run(cmd Command, options ...RunOption) error
Run Instead of executing the command, logs an info message and registers the call with testify mock
func (*MockRunner) Test ¶
func (m *MockRunner) Test(t *testing.T)
Test decorates Testify's mock.Mock#Test() function by adding a cleanup hook to the test object that dumps the set of expected command matchers to stderr in the event of a test failure. This is useful because most command matchers are functions and so Testify can't generate a pretty diff for them; you end up with:
(shell.Command={...}) not matched by func(Command) bool
type RealRunner ¶
type RealRunner struct{}
RealRunner is an implementation of the Runner interface that actually executes shell commands
func (*RealRunner) PrepareSubprocess ¶ added in v0.0.44
func (r *RealRunner) PrepareSubprocess(cmd Command, options ...RunOption) Subprocess
PrepareSubprocess sets up a Subprocess to run a Command asynchronously
type RunOption ¶ added in v0.0.32
type RunOption func(*RunOptions)
RunOption can be used to configure RunOptions for a Run invocation
type RunOptions ¶
type RunOptions struct {
// optional logger to use for logging this command
Logger *zerolog.Logger
// optional level at which command should be logged
LogLevel zerolog.Level
// optional level at which command output (stdout/stderr) should be logged
OutputLogLevel zerolog.Level
// optional writer where stdout should be written
Stdout io.Writer
// optional writer where stderr should be written
Stderr io.Writer
// if false, do not send stdout to logging system
LogStdout bool
}
RunOptions are option for a RunWith() invocation
type Runner ¶
type Runner interface {
// Run runs a Command, streaming stdout and stderr to the log at debug level.
// Behavior can be customized by passing in one or more RunOption functions
Run(cmd Command, opts ...RunOption) error
// PrepareSubprocess sets up a Subprocess to run a Command, similar to Run but asynchronous.
PrepareSubprocess(cmd Command, opts ...RunOption) Subprocess
}
Runner is an interface for running shell commands. It exists to support mocking shell commands in unit tests.
https://joshrendek.com/2014/06/go-lang-mocking-exec-dot-command-using-interfaces/
type Subprocess ¶ added in v0.0.44
type Subprocess interface {
// Start begins running the actual Command the Subprocess is configured for
Start() error
// Wait synchronously blocks until normal completion of the Command
Wait() error
// Stop signals the process to exit, and synchronously waits until it does; after three seconds it will be forcibly
// killed and an error returned
Stop() error
}
Subprocess allows running a shell command in parallel with Thelma's main process