sandbox

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pipeline

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

Pipeline manages execution of multiple stages with piped I/O.

func NewPipeline

func NewPipeline(stages ...*PipelineStage) *Pipeline

NewPipeline constructs a Pipeline with the given stages.

func (*Pipeline) CaptureStderr

func (p *Pipeline) CaptureStderr() *bytes.Buffer

CaptureStderr configures stderr capture and returns the buffer.

func (*Pipeline) CaptureStdout

func (p *Pipeline) CaptureStdout() *bytes.Buffer

CaptureStdout configures stdout capture and returns the buffer.

func (*Pipeline) Run

func (p *Pipeline) Run(ctx context.Context) *PipelineResult

Run executes all stages in the pipeline sequentially with stdout piped from each stage to stdin of the next. Returns a PipelineResult containing any errors and captured output.

func (*Pipeline) RunWithTimeout

func (p *Pipeline) RunWithTimeout(ctx context.Context, timeout time.Duration) *PipelineResult

RunWithTimeout executes the pipeline with a deadline. If the deadline is exceeded before completion, execution is cancelled and context.DeadlineExceeded is returned in the result.

type PipelineResult

type PipelineResult struct {
	Err      error
	ExitCode int
	Stdout   []byte
	Stderr   []byte
}

PipelineResult holds the outcome of pipeline execution.

type PipelineStage

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

PipelineStage represents a single stage in a pipeline.

func Stage

func Stage(name string, runner Runner) *PipelineStage

Stage constructs a PipelineStage with the given name and runner.

func StageWithName

func StageWithName(name string, p *Process) *PipelineStage

StageWithName constructs a PipelineStage with the given name, wrapping the provided Process.

type Process

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

Process manages execution of a Runner function with configurable I/O streams and piping support. It allows tests to run functions in isolation with piped input/output between multiple processes.

func NewProcess

func NewProcess(fn Runner, isTTY bool) *Process

NewProcess constructs a Process bound to a Runner function with the specified TTY mode. The context parameter is reserved for future use.

func NewProducer

func NewProducer(interval time.Duration, lines []string) *Process

NewProducer constructs a Process that emits the provided byte buffer to stdout. It is useful for testing stages that consume input.

func (*Process) CaptureStderr

func (p *Process) CaptureStderr() *bytes.Buffer

CaptureStderr configures stderr capture and returns the buffer.

func (*Process) CaptureStdout

func (p *Process) CaptureStdout() *bytes.Buffer

CaptureStdout configures stdout capture and returns the buffer.

func (*Process) Close

func (p *Process) Close() error

Close closes the process stdin writer. This signals EOF to the process, allowing it to complete reading.

func (*Process) Run

func (p *Process) Run(ctx context.Context) *ProcessResult

Run executes the process runner synchronously. It wires configured streams, invokes the runner with the provided arguments, and closes any process-owned writers and readers when complete. Returns a ProcessResult containing the exit code, error, and captured output.

func (*Process) RunWithIO added in v0.2.0

func (p *Process) RunWithIO(ctx context.Context, r io.Reader) *ProcessResult

func (*Process) SetArgs

func (p *Process) SetArgs(args []string)

SetArgs sets the command-line arguments for the process.

func (*Process) SetStderr

func (p *Process) SetStderr(w io.Writer)

SetStderr sets the error stream for the process.

func (*Process) SetStdin

func (p *Process) SetStdin(r io.Reader)

SetStdin sets the input stream for the process.

func (*Process) SetStdout

func (p *Process) SetStdout(w io.Writer)

SetStdout sets the output stream for the process.

func (*Process) StderrPipe

func (p *Process) StderrPipe() io.Reader

StderrPipe returns a reader connected to the process stderr. Writing to the process stderr will be readable from the returned reader.

func (*Process) StdoutPipe

func (p *Process) StdoutPipe() io.Reader

StdoutPipe returns a reader connected to the process stdout. Writing to the process stdout will be readable from the returned reader.

func (*Process) Write

func (p *Process) Write(b []byte) (int, error)

Write writes data to the process stdin. It creates a stdin pipe on first call if one does not exist. This allows continuous writing to the process while it runs concurrently.

type ProcessResult

type ProcessResult struct {
	Err      error
	ExitCode int
	Stdout   []byte
	Stderr   []byte
}

ProcessResult holds the outcome of process execution including any error, exit code, and captured stdout and stderr output.

type Runner

type Runner func(ctx context.Context, stream *toolkit.Stream) (int, error)

Runner is a function signature for executing code within an isolated test environment. It receives a context, standard I/O streams, and command-line arguments, returning an error on failure.

type Sandbox

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

Sandbox bundles common test setup used by package tests. It contains a testing.T, a context carrying a test logger, a test env, a test clock, a hasher, and a temporary "jail" directory that acts as an isolated filesystem.

func NewSandbox

func NewSandbox(t *testing.T, options *SandboxOptions, opts ...SandboxOption) *Sandbox

NewSandbox constructs a Sandbox and applies given options. Cleanup is registered with t.Cleanup so callers do not need to call a cleanup function.

func (*Sandbox) AbsPath

func (sandbox *Sandbox) AbsPath(rel string) string

AbsPath returns an absolute path. When the sandbox Jail is set and rel is relative the path is made relative to the Jail. Otherwise the function returns the absolute form of rel.

func (*Sandbox) Advance

func (sandbox *Sandbox) Advance(d time.Duration)

Advance advances the sandbox test clock by the given duration.

func (*Sandbox) AtomicWriteFile

func (sandbox *Sandbox) AtomicWriteFile(rel string, data []byte, perm os.FileMode) error

func (*Sandbox) Context

func (sandbox *Sandbox) Context() context.Context

Context returns the sandbox context.

func (*Sandbox) DumpJailTree

func (sandbox *Sandbox) DumpJailTree(maxDepth int)

DumpJailTree logs a tree of files and directories rooted at the sandbox's Jail. Only directories with no children and files are logged. maxDepth limits recursion depth; maxDepth <= 0 means unlimited depth.

func (*Sandbox) GetHome

func (sandbox *Sandbox) GetHome() (string, error)

GetHome returns the sandbox home.

func (*Sandbox) GetJail

func (sandbox *Sandbox) GetJail() string

func (*Sandbox) Getwd

func (sandbox *Sandbox) Getwd() string

Getwd returns the sandbox working directory.

func (*Sandbox) Mkdir

func (sandbox *Sandbox) Mkdir(rel string, all bool) error

func (*Sandbox) MustReadFile

func (sandbox *Sandbox) MustReadFile(rel string) []byte

MustReadFile reads a file under the Jail and fails the test on error.

func (*Sandbox) MustWriteFile

func (sandbox *Sandbox) MustWriteFile(path string, data []byte, perm os.FileMode)

MustWriteFile writes data under the Jail and fails the test on error.

func (*Sandbox) Now

func (sandbox *Sandbox) Now() time.Time

Now returns the current time from the sandbox test clock.

func (*Sandbox) ReadFile

func (sandbox *Sandbox) ReadFile(rel string) ([]byte, error)

ReadFile reads a file located under the sandbox Jail. The path is interpreted relative to the Jail root.

func (*Sandbox) ResolvePath

func (sandbox *Sandbox) ResolvePath(rel string) string

ResolvePath returns an absolute path with symlinks resolved, ensuring the path remains within the sandbox Jail. It expands the path and evaluates any symbolic links before confining it to the jail boundary.

func (*Sandbox) Setwd

func (sandbox *Sandbox) Setwd(dir string)

Setwd sets the sandbox working directory.

func (*Sandbox) WriteFile

func (sandbox *Sandbox) WriteFile(rel string, data []byte, perm os.FileMode) error

WriteFile writes data to a path under the sandbox Jail, creating parent directories as needed. perm is applied to the file.

type SandboxOption

type SandboxOption func(f *Sandbox)

SandboxOption is a function used to modify a Sandbox during construction.

func WithClock

func WithClock(t0 time.Time) SandboxOption

WithClock returns a SandboxOption that sets the test clock to the provided time.

func WithEnv

func WithEnv(key, val string) SandboxOption

WithEnv returns a SandboxOption that sets a single environment variable in the sandbox's Env.

func WithEnvMap

func WithEnvMap(m map[string]string) SandboxOption

WithEnvMap returns a SandboxOption that seeds multiple environment variables from a map.

func WithFixture

func WithFixture(fixture string, path string) SandboxOption

WithFixture returns a SandboxOption that copies a fixture directory from the embedded package data into the provided path within the sandbox Jail. Example fixtures are "empty" or "example".

func WithWd

func WithWd(rel string) SandboxOption

WithWd returns a SandboxOption that sets the sandbox working directory.

type SandboxOptions

type SandboxOptions struct {
	// Data is an embedded filesystem containing test fixtures.
	Data embed.FS
	// Home is the home directory for the user. If empty defaults to
	// /home/$USER. If the user is root the default is /.root.
	Home string
	// User is the username. Defaults to testuser.
	User string
}

SandboxOptions holds optional settings provided to NewSandbox.

type StageOption

type StageOption func(s *PipelineStage)

StageOption configures a PipelineStage.

Jump to

Keyboard shortcuts

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