Documentation
¶
Overview ¶
Package execx abstracts process execution so callers can be tested with a fake instead of spawning real processes.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cmd ¶
type Cmd struct {
// Name is the binary to execute.
Name string
// Args are the command-line arguments (excluding Name).
Args []string
// Dir is the working directory; empty means the current directory.
Dir string
// Env entries (KEY=VALUE) are appended to the parent environment.
Env []string
// Stdin is optional input for the process.
Stdin io.Reader
// Stdout, when non-nil, receives the process's standard output as it is
// produced (unbuffered by the runner). Result.Stdout is then empty. When
// nil, output is captured into Result.Stdout.
Stdout io.Writer
// Stderr is the same as Stdout for standard error and Result.Stderr.
Stderr io.Writer
}
Cmd describes a process to run.
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake is a Runner for tests. It records every Cmd and replays scripted results in order. It is safe for concurrent use.
func (*Fake) Run ¶
Run records cmd and returns the next scripted result, or an error when the script is exhausted.
func (*Fake) Script ¶
func (f *Fake) Script(results ...ScriptedResult)
Script appends results to the queue of responses.
type Result ¶
type Result struct {
// Stdout is the captured standard output.
Stdout string
// Stderr is the captured standard error.
Stderr string
// ExitCode is the process exit code.
ExitCode int
}
Result is the outcome of a process that ran to completion.
type Runner ¶
type Runner interface {
// Run executes cmd and waits for it to finish.
Run(ctx context.Context, cmd Cmd) (Result, error)
}
Runner runs external commands.
A non-zero exit code is NOT an error: it is reported in Result.ExitCode so callers decide how to react. The error return is reserved for failing to start the process (for example a missing binary), context cancellation, and I/O failures.
type ScriptedResult ¶
type ScriptedResult struct {
// Result is returned by the call.
Result Result
// Err, if set, is returned as the call's error.
Err error
}
ScriptedResult is one queued response for Fake.