Documentation
¶
Overview ¶
Package run provides wrappers around os/exec types that support stubbing.
Index ¶
- func MatchAny(cmd *Cmd) bool
- type Client
- func (c *Client) Command(name string, arg ...string) *Cmd
- func (c *Client) CommandContext(ctx context.Context, name string, arg ...string) *Cmd
- func (c *Client) IsStubbed() bool
- func (c *Client) RegisterStub(matcher Matcher, responder Responder) *Client
- func (c *Client) VerifyStubs(t testable)
- func (c *Client) WithStubbing() *Client
- type Cmd
- type Executor
- type ExitError
- type Matcher
- type Responder
- func ErrorResponse(err error) Responder
- func MuxResponse(stdout []byte, stderr []byte, code int) Responder
- func RegexpResponse(pattern string, index int) Responder
- func StderrResponse(stderr []byte, code int) Responder
- func StdoutResponse(stdout []byte, code int) Responder
- func StringResponse(s string) Responder
- type Stub
- type StubExecutor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
Executor Executor
}
Client is an abstraction around os/exec to support stubbing.
func (*Client) Command ¶
Command returns the Cmd struct to execute the named program with the given arguments.
func (*Client) CommandContext ¶
CommandContext is like Command but includes a context.
func (*Client) RegisterStub ¶
RegisterStub registers a new stub for the given matcher/responder pair.
func (*Client) VerifyStubs ¶
func (c *Client) VerifyStubs(t testable)
VerifyStubs fails the test if there are unmatched stubs.
func (*Client) WithStubbing ¶
WithStubbing configures stubbing and returns the receiver.
type Cmd ¶
Cmd is a wrapper around os/exec.Cmd that supports stubbing. All functionality is delegated to [Client.Executor].
func (*Cmd) DebugString ¶ added in v0.4.1
DebugString the command string plus a truncated preview of stdin.
type Executor ¶
type Executor interface {
// ExitCode returns the exit code of the exited process, or -1
// if the process hasn't exited or was terminated by a signal.
ExitCode(cmd *Cmd) int
// Output runs the command and returns its standard output.
Output(cmd *Cmd) ([]byte, error)
// Run starts the specified command and waits for it to complete.
Run(cmd *Cmd) error
}
Executor is an interface representing the ability to execute an external command.
var ( // DefaultExecutor is the default implementation of Executor used by new Clients. // Each of it's methods simply delegate to the underlying [os/exec.Cmd]. DefaultExecutor Executor = &defaultExecutor{} )
type ExitError ¶ added in v0.5.0
ExitError is a light wrapper for exec.ExitError that allows for easily accessing and stubbing the exit code.
The reason we don't use exec.ExitError directly is that it stores the exit code inside `os.ProcessState`, which in turn uses `syscall.WaitStatus`, and WaitStatus is implemented differently per-system.
func NewExitError ¶ added in v0.5.0
NewExitError returns a new exit error for code.
type Matcher ¶
Matcher is function that matches commands.
func MatchAll ¶ added in v0.4.0
MatchAll returns a matcher that returns true if all matcher args match.
func MatchRegexp ¶
MatchRegexp returns a matcher that matches against regular expressions.
func MatchStdin ¶ added in v0.4.0
MatchStdin returns a matcher that matches against command stdin.
func MatchString ¶
MatchString returns a matcher that matches against command strings.
type Responder ¶
Responder is a function that returns stubbed command output.
func ErrorResponse ¶
ErrorResponse creates a responder that returns err.
func MuxResponse ¶ added in v0.5.0
MuxResponse creates a responder that returns values for both stdout and stderr. If the provided exit code is non-zero, then an exit error will also be returned.
func RegexpResponse ¶
RegexpResponse creates a responder that returns the match index for the given regular expression pattern. Panics if there is no match for index.
For example:
responder := RegexpResponse(`echo (\w+)$`, 1)
buf, err := responder(client.Command("echo", "howdy"))
// buf == []byte("howdy")
// err == nil
func StderrResponse ¶ added in v0.5.0
StderrResponse creates a responder that returns the given bytes via stderr. If the provided exit code is non-zero, then an exit error will also be returned.
func StdoutResponse ¶ added in v0.5.0
StdoutResponse creates a responder that returns the given bytes via stdout. If the provided exit code is non-zero, then an exit error will also be returned.
func StringResponse ¶
StringResponse creates a responder that returns the given string via stdout.
type StubExecutor ¶
type StubExecutor struct {
Commands []*Cmd
// contains filtered or unexported fields
}
StubExecutor is an implementation of Executor that executes stubbed commands.
func NewStubExecutor ¶
func NewStubExecutor() *StubExecutor
func (*StubExecutor) ExitCode ¶ added in v0.5.0
func (e *StubExecutor) ExitCode(cmd *Cmd) int
func (*StubExecutor) RegisterStub ¶
func (e *StubExecutor) RegisterStub(matcher Matcher, responder Responder) *StubExecutor
RegisterStub registers a new stub for the given matcher/responder pair.
func (*StubExecutor) Run ¶
func (e *StubExecutor) Run(cmd *Cmd) error
func (*StubExecutor) VerifyStubs ¶
func (e *StubExecutor) VerifyStubs(test testable)
VerifyStubs fails the test if there are unmatched stubs.