execx

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 9 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 execx abstracts process execution so callers can be tested with a fake instead of spawning real processes.

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 binary to execute.
	Name string
	// Args are the command-line arguments (excluding Name).
	Args []string
	// Dir is the working directory; empty means the current directory.
	Dir string
	// Env entries (KEY=VALUE) are appended to the parent environment.
	Env []string
	// Stdin is optional input for the process.
	Stdin io.Reader
	// Stdout, when non-nil, receives the process's standard output as it is
	// produced (unbuffered by the runner). Result.Stdout is then empty. When
	// nil, output is captured into Result.Stdout.
	Stdout io.Writer
	// Stderr is the same as Stdout for standard error and Result.Stderr.
	Stderr io.Writer
}

Cmd describes a process to run.

type Fake

type Fake struct {
	// contains filtered or unexported fields
}

Fake is a Runner for tests. It records every Cmd and replays scripted results in order. It is safe for concurrent use.

func (*Fake) Calls

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

Calls returns a copy of all commands received so far.

func (*Fake) Run

func (f *Fake) Run(_ context.Context, cmd Cmd) (Result, error)

Run records cmd and returns the next scripted result, or an error when the script is exhausted.

func (*Fake) Script

func (f *Fake) Script(results ...ScriptedResult)

Script appends results to the queue of responses.

type OSRunner

type OSRunner struct{}

OSRunner is a Runner backed by os/exec.

func (*OSRunner) Run

func (*OSRunner) Run(ctx context.Context, cmd Cmd) (Result, error)

Run executes cmd, capturing stdout and stderr separately. Only the command name and args are logged, never env values, since env may carry secrets.

type Result

type Result struct {
	// Stdout is the captured standard output.
	Stdout string
	// Stderr is the captured standard error.
	Stderr string
	// ExitCode is the process exit code.
	ExitCode int
}

Result is the outcome of a process that ran to completion.

func Emit

func Emit(cmd Cmd, res Result) Result

Emit applies the Fake's streaming semantics to res for cmd, for test runners that route commands themselves: output goes to cmd.Stdout and cmd.Stderr when set and is cleared from the returned Result.

func (Result) OK

func (r Result) OK() bool

OK reports whether the process exited with code 0.

type Runner

type Runner interface {
	// Run executes cmd and waits for it to finish.
	Run(ctx context.Context, cmd Cmd) (Result, error)
}

Runner runs external commands.

A non-zero exit code is NOT an error: it is reported in Result.ExitCode so callers decide how to react. The error return is reserved for failing to start the process (for example a missing binary), context cancellation, and I/O failures.

type ScriptedResult

type ScriptedResult struct {
	// Result is returned by the call.
	Result Result
	// Err, if set, is returned as the call's error.
	Err error
}

ScriptedResult is one queued response for Fake.

Jump to

Keyboard shortcuts

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