Documentation
¶
Overview ¶
Package proc provides a small abstraction over os/exec so production code can take a dependency on Runner instead of calling exec.Command directly. Tests substitute Fake to script command output and assert on the call shape without spawning real subprocesses.
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 executable to run (e.g. "git").
Name string
// Args are the arguments passed to Name.
Args []string
// Dir, if non-empty, sets the subprocess's working directory.
Dir string
// Env, if non-nil, replaces the subprocess's environment. nil
// inherits the parent process's environment.
Env []string
// Stdin, if non-empty, is written to the subprocess's stdin.
Stdin []byte
}
Cmd describes a command invocation. Zero values are sane defaults: no working directory override, no extra environment, empty stdin.
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake is a Runner that replays scripted responses based on Matcher rules and records every call it observes. Fake is safe for concurrent use.
When no rule matches an incoming Cmd, Run returns an error describing the unexpected call. This makes tests fail loudly when the code under test invokes commands the test did not anticipate.
func (*Fake) Calls ¶
Calls returns a copy of every call observed so far, in invocation order. Args slices are also copied so callers can mutate the result safely.
func (*Fake) On ¶
On registers resp as the response to commands matching m. Rules are evaluated in registration order; the first match wins.
type Matcher ¶
Matcher decides whether a Cmd should produce a given Response.
func MatchExact ¶
MatchExact returns a Matcher that requires Name and Args (in order) to equal the given values. Dir and Env are not compared.
func MatchPrefix ¶
MatchPrefix returns a Matcher that requires Name to equal name and the first len(args) entries of Cmd.Args to equal args. Useful for matching "git diff ..." regardless of trailing arguments.
type Response ¶
Response describes a scripted reply Fake should return for a matched call. Err, when non-nil, is returned as the second return from Run; otherwise Result is returned with err == nil.
type Result ¶
Result captures the outcome of running a Cmd. ExitCode is 0 on success, non-zero on failure, and -1 when the process could not be started or was killed before reporting status (e.g. context cancel).
type Runner ¶
type Runner interface {
// Run executes cmd and returns the result. Run returns an error
// only when the command could not be invoked at all (e.g. binary
// not found, context canceled). A non-zero ExitCode is reported
// via Result, not error, so callers can decide whether non-zero
// is a failure for their use case.
Run(ctx context.Context, cmd Cmd) (Result, error)
}
Runner runs commands. Implementations must be safe for concurrent use.