Documentation
¶
Overview ¶
Package fake is a scriptable, contract-verified test double for invoke.Environment.
The fake simulates a small POSIX-ish target: a virtual filesystem, a minimal shell and builtin vocabulary, and full transfer semantics between the real host filesystem and the virtual one. It passes the same invoketest contract suite the real providers pass, so tests written against it exercise contract-accurate lifecycle, error, and stream behavior — not a mock that can be programmed into impossible states.
Consumer-registered handlers take precedence over the builtin vocabulary; commands that are neither registered nor builtin fail with invoke.ErrNotFound, exactly as a missing binary does on a real target.
What the shell runs ¶
invoke.Shell scripts are interpreted, not executed, and the interpreter covers a subset: sequencing with ; and &&, single and double quotes, $NAME and ${NAME} expansion, $(command) substitution, redirection to /dev/null (flush or spaced) and between the two output streams, cd, exit, and exec.
A script reaching outside that subset is refused by name rather than run wrongly. Pipelines, || lists, redirection to a file, input redirection, backquotes, newline-separated commands, background commands, comments, globs, backslash escapes, arithmetic expansion, the ${ operator forms, and the special and positional parameters ($?, $1, $$) are all refused. Start refuses such a script before any process exists, with an error wrapping invoke.ErrNotSupported; a script it cannot see into, such as one nested in quotes as sh -c 'a | b', is refused when it runs, with the construct named on standard error and exit status 93. The refusal is the point: an unrecognized character is just another character to a tokenizer, so an unsupported construct would otherwise be run as something else — `false || echo rescued` exiting 1 having printed nothing, where every real target exits 0 having printed — and a test asserting that would assert the opposite of the truth.
The builtin vocabulary ¶
The builtins are a vocabulary rather than an implementation: they cover the forms the contract suite and ordinary shell-outs use, and a form outside them — a test operator beyond the set below, most utility flags — fails loudly on standard error rather than being answered falsely. What the fake answers without a handler:
- sh -c, running the script through this same interpreter
- echo (no flags) and printf (%s and %% conversions, the common backslash escapes)
- cat, reading standard input; file arguments are ignored
- test, with the one-argument form, an optional leading !, and the unary -e, -d, -f, -L, -n, -z, and -t; test and cd do not follow symbolic links
- true, false, sleep SECONDS, pwd, and uname (which reports Linux, matching Environment.OS)
- find PATH -maxdepth 0 -perm MODE, and dd if=/dev/zero with optional bs= and count=
- mkdir [-p], touch, and rm [-r|-f|-rf], each with the failure modes a real target has
Inside scripts, cd (to $HOME with no argument), exit (numeric statuses only) and exec COMMAND (which runs COMMAND and ends the script with its status) are the shell's own. Anything else — ls, cp, grep, env, chmod — is an unknown command: invoke.ErrNotFound from Start, the shell's exit 127 from a script. Register your own with Environment.Handle, which overrides any builtin of the same name and is reachable everywhere the name can be written. A script needing more than the subset belongs in a handler, or on a real target.
The builtins, and handlers registered under a bare name, live in /usr/bin, which is where LookPath reports them. Start finds a bare name only when the PATH the command runs with lists /usr/bin: a PATH in Command.Env, or the base environment's. A handler registered under a path lives at that path, and is reached by it whatever PATH says. A script's own changes to PATH are not followed.
The filesystem ¶
One store holds the target's files, directories and symbolic links, and everything that touches the target goes through it: the builtins mutate it directly, Upload and Download reach it through the shared transfer engine the real providers use, and Environment.FS reads it as a live io/fs.FS for assertions. A file an upload wrote is the file test -f finds and the one fs.ReadFile returns.
What the fake does not simulate ¶
TTY allocation is refused with invoke.ErrNotSupported, as Environment.Capabilities reports. Files carry permission bits but no owner, and no modification time: ModTime is always the zero time, so an assertion on mtimes needs a real target.
Index ¶
- type Environment
- func (e *Environment) Calls() []invoke.Command
- func (e *Environment) Capabilities() invoke.Capabilities
- func (e *Environment) Close() error
- func (e *Environment) Download(ctx context.Context, remotePath, localPath string, ...) error
- func (e *Environment) FS() fs.FS
- func (e *Environment) Handle(name string, h Handler)
- func (e *Environment) LookPath(ctx context.Context, name string) (string, error)
- func (e *Environment) OS() invoke.TargetOS
- func (e *Environment) Start(ctx context.Context, cmd invoke.Command, stdio invoke.IO) (invoke.Process, error)
- func (e *Environment) Upload(ctx context.Context, localPath, remotePath string, ...) error
- type Handler
- type Option
- type Session
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
Environment is a simulated execution target.
func New ¶
func New(opts ...Option) *Environment
New returns a fresh simulated target with an empty /tmp, a small base environment, and no recorded calls.
func (*Environment) Calls ¶
func (e *Environment) Calls() []invoke.Command
Calls returns the commands started so far, in order. Probe and cleanup commands issued by test helpers appear too: the record is honest.
func (*Environment) Capabilities ¶
func (e *Environment) Capabilities() invoke.Capabilities
Capabilities reports the simulated target's features: signal delivery and symlink-preserving transfers work; TTY allocation is not simulated.
func (*Environment) Close ¶
func (e *Environment) Close() error
Close marks the fake closed and terminates simulated processes still running. It is idempotent.
func (*Environment) Download ¶
func (e *Environment) Download(ctx context.Context, remotePath, localPath string, opts ...invoke.TransferOption) error
Download copies a virtual file or tree out to the host, with the same semantics as Upload.
func (*Environment) FS ¶
func (e *Environment) FS() fs.FS
FS returns a read-only io/fs.FS view of the fake target's filesystem, for asserting on target state with standard tooling (fs.ReadFile, fs.WalkDir, fstest.TestFS). Paths are unrooted io/fs paths: the target's /tmp/app.txt is "tmp/app.txt", and "." is the root.
The view is live — it reflects mutations as commands and transfers make them — and implements fs.ReadLinkFS, so symlinks are inspectable.
func (*Environment) Handle ¶
func (e *Environment) Handle(name string, h Handler)
Handle registers a handler for commands whose Path equals name, overriding any builtin of the same name. A handler is reachable everywhere the name can be written: started directly, invoked from a invoke.Shell script or a $(...) substitution, and through the path LookPath reports for it. Calls records commands given to Start; a script's internal invocations belong to the script.
func (*Environment) LookPath ¶
LookPath resolves name against the fake's handlers and builtins. The answer is itself resolvable: what LookPath reports, Start accepts.
func (*Environment) OS ¶
func (e *Environment) OS() invoke.TargetOS
OS reports the simulated target's operating system.
func (*Environment) Start ¶
func (e *Environment) Start(ctx context.Context, cmd invoke.Command, stdio invoke.IO) (invoke.Process, error)
Start launches a simulated process for cmd.
func (*Environment) Upload ¶
func (e *Environment) Upload(ctx context.Context, localPath, remotePath string, opts ...invoke.TransferOption) error
Upload copies a host file or tree into the virtual filesystem through the shared transfer engine, so it behaves as local's and ssh's do: per-file atomicity, merging into an existing directory, modes preserved or forced umask-proof, the symlink policy with containment, special files refused by name, root-relative progress and the configured concurrency. Close stops it, and it then fails wrapping invoke.ErrClosed.
type Handler ¶
Handler simulates one command: it reads and writes the session streams and returns the exit code. Honoring ctx cancellation keeps a scripted command responsive to the lifecycle machinery, which handles the resulting classification (cancellation, Close, signals) itself.
cmd is the invocation as written. From Start it is the caller's Command unchanged, its Dir possibly empty and its Env only the overlay the caller set; from a script it carries the Path and Args the script wrote, the shell's current directory as Dir, and no Env. The state to act on is the Session's: on both routes Session.Dir is the directory the command runs in and Session.Env the full environment.
type Session ¶
type Session struct {
// Stdin is the invocation's standard input (never nil; empty when
// the caller wired none).
Stdin io.Reader
// Stdout and Stderr are the invocation's output streams (never
// nil; discarding when the caller wired none).
Stdout io.Writer
Stderr io.Writer
// Dir is the working directory the command runs in: Command.Dir,
// or / when that is empty, from Start; the shell's current
// directory from a script.
Dir string
// Env is the resolved environment: the fake's base environment with
// the command's overlay applied, in "KEY=VALUE" form.
Env []string
}
Session is the execution state a Handler runs with.