run

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package run provides wrappers around os/exec types that support stubbing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchAny

func MatchAny(cmd *Cmd) bool

MatchAny is a matcher that matches any command.

Types

type Client

type Client struct {
	Executor Executor
}

Client is an abstraction around os/exec to support stubbing.

func NewClient

func NewClient() *Client

NewClient returns a new Client.

func (*Client) Command

func (c *Client) Command(name string, arg ...string) *Cmd

Command returns the Cmd struct to execute the named program with the given arguments.

func (*Client) CommandContext

func (c *Client) CommandContext(ctx context.Context, name string, arg ...string) *Cmd

CommandContext is like Command but includes a context.

func (*Client) IsStubbed

func (c *Client) IsStubbed() bool

IsStubbed returns true if the executor is configured for stubbing.

func (*Client) RegisterStub

func (c *Client) RegisterStub(matcher Matcher, responder Responder) *Client

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

func (c *Client) WithStubbing() *Client

WithStubbing configures stubbing and returns the receiver.

type Cmd

type Cmd struct {
	*exec.Cmd
	// contains filtered or unexported fields
}

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

func (c *Cmd) DebugString() string

DebugString the command string plus a truncated preview of stdin.

func (*Cmd) ExitCode added in v0.5.0

func (c *Cmd) ExitCode() int

ExitCode returns the exit code for the command.

func (*Cmd) Output

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

Output runs the command and returns its standard output.

func (*Cmd) PeekStdin added in v0.4.1

func (c *Cmd) PeekStdin() ([]byte, error)

PeekStdin reads Stdin without moving the read offset to EOF.

func (*Cmd) Run

func (c *Cmd) Run() error

Run starts the specified command and waits for it to complete.

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

type ExitError struct {
	*exec.ExitError
	// contains filtered or unexported fields
}

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

func NewExitError(code int) *ExitError

NewExitError returns a new exit error for code.

func (*ExitError) Code added in v0.5.0

func (e *ExitError) Code() int

Code returns the exit code.

func (*ExitError) Error added in v0.5.0

func (e *ExitError) Error() string

Error returns the error message.

func (*ExitError) Unwrap added in v0.5.1

func (e *ExitError) Unwrap() error

type Matcher

type Matcher func(cmd *Cmd) bool

Matcher is function that matches commands.

func MatchAll added in v0.4.0

func MatchAll(matchers ...Matcher) Matcher

MatchAll returns a matcher that returns true if all matcher args match.

func MatchRegexp

func MatchRegexp(s string) Matcher

MatchRegexp returns a matcher that matches against regular expressions.

func MatchStdin added in v0.4.0

func MatchStdin(s string) Matcher

MatchStdin returns a matcher that matches against command stdin.

func MatchString

func MatchString(s string) Matcher

MatchString returns a matcher that matches against command strings.

type Responder

type Responder func(cmd *Cmd) (stdout []byte, stderr []byte, err error)

Responder is a function that returns stubbed command output.

func ErrorResponse

func ErrorResponse(err error) Responder

ErrorResponse creates a responder that returns err.

func MuxResponse added in v0.5.0

func MuxResponse(stdout []byte, stderr []byte, code int) Responder

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

func RegexpResponse(pattern string, index int) Responder

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

func StderrResponse(stderr []byte, code int) Responder

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

func StdoutResponse(stdout []byte, code int) Responder

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

func StringResponse(s string) Responder

StringResponse creates a responder that returns the given string via stdout.

type Stub

type Stub struct {
	Matched   bool
	Matcher   Matcher
	Responder Responder
}

Stub is a stubbed Cmd execution.

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) Output

func (e *StubExecutor) Output(cmd *Cmd) ([]byte, error)

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.

Jump to

Keyboard shortcuts

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