Documentation
¶
Overview ¶
Package fakeexec provides an in-memory fake for the command-runner seam used across the fleet to shell out to git / go / docker:
run(dir, name string, args ...string) (string, error)
Production code keeps a swappable package var (e.g. `var sh = run`) and calls it instead of os/exec directly. Tests swap in Fake.Run to assert *orchestration* — which commands run, in what order, and how the code reacts to their exit status — without invoking the real toolchain or touching the network. Side-effect hooks let a faked `go mod vendor` simulate writing files so downstream logic (change detection, staging) still sees them.
It exists so every fleet app can self-test its own shell-outs the same way, with no third-party dependency: this package imports only the standard library.
Usage:
f := fakeexec.New().
OnDo("go mod vendor", func(c fakeexec.Call) error {
return os.WriteFile(filepath.Join(c.Dir, "vendor/modules.txt"), data, 0o644)
}).
FailOn("go build", errors.New("undefined: Foo")).
OnReturn("git status --porcelain", " M go.mod\n", nil)
old := sh
sh = f.Run
t.Cleanup(func() { sh = old })
// ... exercise the code under test ...
f.AssertOrder(t, "go mod vendor", "go build")
f.AssertCalled(t, "git add -A")
A Fake is safe for concurrent use.
Index ¶
- type Call
- type Fake
- func (f *Fake) AssertCalled(t testing.TB, match string)
- func (f *Fake) AssertNotCalled(t testing.TB, match string)
- func (f *Fake) AssertOrder(t testing.TB, matches ...string)
- func (f *Fake) Calls() []Call
- func (f *Fake) Count(match string) int
- func (f *Fake) FailOn(match string, err error) *Fake
- func (f *Fake) Lines() []string
- func (f *Fake) OnDo(match string, hook func(Call) error) *Fake
- func (f *Fake) OnReturn(match, out string, err error) *Fake
- func (f *Fake) Reset()
- func (f *Fake) Run(dir, name string, args ...string) (string, error)
- func (f *Fake) SetDefault(out string, err error) *Fake
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake is an in-memory runner. Configure outcomes with the On* / FailOn methods, then pass Fake.Run wherever a run(dir, name, args...) (string, error) seam is expected. Commands no rule matches succeed with empty output by default; override that with SetDefault.
func (*Fake) AssertCalled ¶
AssertCalled fails t if no recorded call contains match.
func (*Fake) AssertNotCalled ¶
AssertNotCalled fails t if any recorded call contains match.
func (*Fake) AssertOrder ¶
AssertOrder fails t unless a command containing each match appears, in the given relative order (matched against first occurrences). Intervening commands are allowed — it asserts ordering, not adjacency.
func (*Fake) FailOn ¶
FailOn registers a rule that makes commands containing match return ("", err) — i.e. a non-zero exit.
func (*Fake) OnDo ¶
OnDo registers a rule that runs hook (a simulated side effect, e.g. writing a file a real command would have written) and returns ("", hook's error).
func (*Fake) OnReturn ¶
OnReturn registers a rule: a command whose rendered line contains match returns (out, err). Rules are tried in registration order; the first match wins.