Documentation
¶
Overview ¶
Package runnertest is task-8's test double for internal/runner.Runner: a scriptable Fake that matches an expected argv and returns canned output or an injected error, plus AllowRealProcess for the small number of tests that must start a real process. Every consumer of internal/runner depends on the runner.Runner interface, never on internal/runner.Real directly, so a unit test substitutes Fake here instead.
Fake also has task-8's fail-call-N mode (decision 23): FailCall makes one chosen call fail on its own, standalone, and Fake implements internal/testsweep.Failer so internal/testsweep.Sweep can drive it once per call a happy-path body makes, to cover every error return a multi-call body reaches without a hand-written failure test per call. See FailCall's doc comment and internal/testsweep's package doc for usage.
Index ¶
- func AllowRealProcess(t testing.TB)
- type Call
- type Fake
- func (f *Fake) CallCount() int
- func (f *Fake) Calls() []Call
- func (f *Fake) Detach(dir, name string, args ...string) (int, error)
- func (f *Fake) Expect(match func(Call) bool, result runner.Result, err error) *Fake
- func (f *Fake) ExpectArgv(argv []string, result runner.Result, err error) *Fake
- func (f *Fake) FailCall(callNum int, failErr error)
- func (f *Fake) Interactive(_ context.Context, dir, name string, args ...string) error
- func (f *Fake) Run(_ context.Context, dir, name string, args ...string) (runner.Result, error)
- func (f *Fake) RunOpts(_ context.Context, dir string, opts runner.RunOptions, name string, ...) (runner.Result, error)
- func (f *Fake) RunWithInput(_ context.Context, dir string, input []byte, name string, args ...string) (runner.Result, error)
- func (f *Fake) Start(_ context.Context, dir, name string, args ...string) (runner.Handle, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllowRealProcess ¶
AllowRealProcess lets t start a real process through github.com/sneat-dev/wb/internal/runner.Real despite task-24's runtime guard. Only a file already on internal/quality/testdata/unit_tier.pending or unit_tier.allow may call it -- the static check in internal/quality fails an unreviewed new call the same way it fails any other pending-list violation.
It works by setting the runner package's own allow-process environment variable through t.Setenv, so it is restored automatically at t's cleanup and (like every other t.Setenv call in this repository) refuses to coexist with t.Parallel().
Types ¶
type Call ¶
type Call struct {
Op string // "Run", "RunWithInput", "RunOpts", "Start", "Detach" or "Interactive"
Dir string
Name string
Args []string
// Input is the stdin RunWithInput was given, or opts.Stdin for RunOpts.
// Empty for every other Op.
Input []byte
// Opts is the RunOptions a RunOpts call was given. Zero value for every
// other Op.
Opts runner.RunOptions
}
Call records one Run/RunWithInput/RunOpts/Start/Detach/Interactive invocation the Fake received.
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake is a scriptable, in-memory runner.Runner. Configure expected calls with Expect before exercising the code under test, then assert on Calls. An unmatched call fails the test immediately through t, rather than silently succeeding: a fake that answers a call nobody scripted would hide the exact case task-8's contract tests exist to catch.
Fake also has task-8's fail-call-N mode (decision 23): FailCall makes one chosen call number fail regardless of its scripted result, on its own or driven by internal/testsweep.Sweep across every call a happy path makes. See CallCount and FailCall, and the package example.
func (*Fake) CallCount ¶
CallCount reports how many calls the Fake has answered so far, across Run, Start, Detach and Interactive combined, in the order it answered them. It implements internal/testsweep.Failer.
func (*Fake) Expect ¶
Expect adds one scripted response: the next call for which match returns true is answered with result and err (either may be zero). Scripts are tried in the order they were added and each is consumed on its first match, so a test that expects the same argv twice with different results calls Expect twice.
func (*Fake) ExpectArgv ¶
ExpectArgv is Expect's common case: match an exact argv (name followed by args).
func (*Fake) FailCall ¶
FailCall arranges for the callNum'th call (1-indexed, counting Run, Start, Detach and Interactive together in the order the Fake answers them) to fail with failErr instead of returning its scripted result; every other call keeps returning its own script unchanged, so FailCall fails exactly call N and passes the rest. The call FailCall targets must still be scripted via Expect/ExpectArgv first -- FailCall replaces that call's result, not the argv match that catches an unexpected call.
It works standalone, or driven once per call number by internal/testsweep.Sweep, which is why Fake implements internal/testsweep.Failer. A callNum of 0 disables the override; calling FailCall again replaces the previous target rather than adding a second one.
func (*Fake) Interactive ¶
Interactive implements runner.Runner.
func (*Fake) RunOpts ¶
func (f *Fake) RunOpts(_ context.Context, dir string, opts runner.RunOptions, name string, args ...string) (runner.Result, error)
RunOpts implements runner.Runner. opts is recorded on Call.Opts (and its Stdin duplicated onto Call.Input alongside RunWithInput's), so a test can assert on the environment/stdin/WaitDelay a call was given.
func (*Fake) RunWithInput ¶
func (f *Fake) RunWithInput(_ context.Context, dir string, input []byte, name string, args ...string) (runner.Result, error)
RunWithInput implements runner.Runner. The input the caller passed is recorded on Call.Input, so a test can assert on it via Calls/ExpectArgv's match function alongside the argv.