fakeexec

package
v0.133.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Call

type Call struct {
	Dir  string
	Name string
	Args []string
}

Call is one recorded invocation of the runner seam.

func (Call) Line

func (c Call) Line() string

Line renders the call as a shell-ish string: "name arg1 arg2". Rule matching and the Assert helpers all work against this rendering, so a match string like "git reset --hard" or "go mod vendor" reads naturally.

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 New

func New() *Fake

New returns a Fake whose unmatched commands succeed with empty output.

func (*Fake) AssertCalled

func (f *Fake) AssertCalled(t testing.TB, match string)

AssertCalled fails t if no recorded call contains match.

func (*Fake) AssertNotCalled

func (f *Fake) AssertNotCalled(t testing.TB, match string)

AssertNotCalled fails t if any recorded call contains match.

func (*Fake) AssertOrder

func (f *Fake) AssertOrder(t testing.TB, matches ...string)

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

func (f *Fake) Calls() []Call

Calls returns a snapshot of every recorded invocation, in order.

func (*Fake) Count

func (f *Fake) Count(match string) int

Count returns how many recorded calls contain match.

func (*Fake) FailOn

func (f *Fake) FailOn(match string, err error) *Fake

FailOn registers a rule that makes commands containing match return ("", err) — i.e. a non-zero exit.

func (*Fake) Lines

func (f *Fake) Lines() []string

Lines returns the recorded calls rendered via Call.Line, in order.

func (*Fake) OnDo

func (f *Fake) OnDo(match string, hook func(Call) error) *Fake

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

func (f *Fake) OnReturn(match, out string, err error) *Fake

OnReturn registers a rule: a command whose rendered line contains match returns (out, err). Rules are tried in registration order; the first match wins.

func (*Fake) Reset

func (f *Fake) Reset()

Reset clears recorded calls (rules are kept).

func (*Fake) Run

func (f *Fake) Run(dir, name string, args ...string) (string, error)

Run is the seam. Assign it where a run(dir, name, args...) (string, error) function value is expected.

func (*Fake) SetDefault

func (f *Fake) SetDefault(out string, err error) *Fake

SetDefault sets the (out, err) returned for commands that match no rule. Useful to make an unconfigured command fail loudly instead of silently succeeding.

Jump to

Keyboard shortcuts

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