mock

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: GPL-3.0, LGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package mock provides test doubles for the cmdexec.Executor interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Call

type Call struct {
	Name  string
	Args  []string
	Stdin string // captured stdin content (empty if no stdin)
}

Call records a single invocation of Executor.

type Executor

type Executor struct {
	// OnCall, if set, is called to produce a result for each invocation.
	// The args slice contains the subcommand and its arguments
	// (e.g. ["inspect", "sind-dns"]). Stdin is non-empty for RunWithStdin calls.
	OnCall func(args []string, stdin string) Result

	// OnStart, if set, is called to produce a StreamResult for Start calls.
	// If nil, Start returns an error.
	OnStart func(args []string) StreamResult

	Calls []Call
	// contains filtered or unexported fields
}

Executor records all calls and returns preconfigured results. It is safe for concurrent use.

Results are dispatched in two modes:

  • If OnCall is set, it is called for every invocation to produce a result.
  • Otherwise, results queued via AddResult are returned in FIFO order.

OnCall is useful when multiple goroutines share a Executor and result dispatch must be based on command arguments rather than call order.

func (*Executor) AddResult

func (m *Executor) AddResult(stdout, stderr string, err error)

AddResult enqueues a result to be returned by the next Run or RunWithStdin call. Only used when OnCall is nil.

func (*Executor) Run

func (m *Executor) Run(_ context.Context, name string, args ...string) (string, string, error)

Run implements cmdexec.Executor.

func (*Executor) RunWithStdin

func (m *Executor) RunWithStdin(_ context.Context, stdin io.Reader, name string, args ...string) (string, string, error)

RunWithStdin implements cmdexec.Executor.

func (*Executor) Start added in v0.9.0

func (m *Executor) Start(_ context.Context, name string, args ...string) (*cmdexec.Process, error)

Start implements cmdexec.Executor.

type Pipes added in v0.9.0

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

Pipes manages io.Pipe pairs for mock streaming. Each OnStart call creates a new pipe and returns its reader. Use Write to feed data into a pipe by index, and CloseAll to shut them down.

func (*Pipes) CloseAll added in v0.9.0

func (p *Pipes) CloseAll()

CloseAll closes all pipe writers.

func (*Pipes) CloseWithError added in v0.9.0

func (p *Pipes) CloseWithError(index int, err error)

CloseWithError closes the pipe at the given index with an error, causing the reader to return that error.

func (*Pipes) Len added in v0.9.0

func (p *Pipes) Len() int

Len returns the number of pipes created so far.

func (*Pipes) OnStart added in v0.9.0

func (p *Pipes) OnStart(_ []string) StreamResult

OnStart is an OnStart callback that creates a new pipe per call.

func (*Pipes) Write added in v0.9.0

func (p *Pipes) Write(index int, data string)

Write sends data to the pipe at the given index.

type RecordedCall

type RecordedCall struct {
	Name   string
	Args   []string
	Stdout string
	Stderr string
	Err    error
}

RecordedCall captures one invocation of the executor.

type Recorder

type Recorder struct {
	*RecordingExecutor
	// contains filtered or unexported fields
}

Recorder holds a RecordingExecutor and the underlying mock (if any). In unit mode, mock is non-nil and AddResult configures responses. In integration mode, mock is nil and AddResult is a no-op.

func NewIntegrationRecorder

func NewIntegrationRecorder() *Recorder

NewIntegrationRecorder returns a Recorder backed by an OSExecutor.

func NewRecorder

func NewRecorder() *Recorder

NewRecorder returns a Recorder backed by a Executor.

func (*Recorder) AddResult

func (r *Recorder) AddResult(stdout, stderr string, err error)

AddResult queues a mock response. No-op in integration mode.

func (*Recorder) IsIntegration

func (r *Recorder) IsIntegration() bool

IsIntegration returns true when running against real executors.

type RecordingExecutor

type RecordingExecutor struct {
	Inner cmdexec.Executor
	// contains filtered or unexported fields
}

RecordingExecutor wraps another Executor and records all calls with their results. Useful for observing actual CLI I/O during tests.

func (*RecordingExecutor) Calls

func (r *RecordingExecutor) Calls() []RecordedCall

Calls returns a copy of all recorded calls.

func (*RecordingExecutor) Dump

func (r *RecordingExecutor) Dump() string

Dump returns a human-readable log of all recorded calls.

func (*RecordingExecutor) Run

func (r *RecordingExecutor) Run(ctx context.Context, name string, args ...string) (string, string, error)

Run implements cmdexec.Executor.

func (*RecordingExecutor) RunWithStdin

func (r *RecordingExecutor) RunWithStdin(ctx context.Context, stdin io.Reader, name string, args ...string) (string, string, error)

RunWithStdin implements cmdexec.Executor.

func (*RecordingExecutor) Start added in v0.9.0

func (r *RecordingExecutor) Start(ctx context.Context, name string, args ...string) (*cmdexec.Process, error)

Start implements cmdexec.Executor.

type Result

type Result struct {
	Stdout string
	Stderr string
	Err    error
}

Result holds the return values for a single Executor call.

type StreamResult added in v0.9.0

type StreamResult struct {
	Reader io.ReadCloser
	Err    error
}

StreamResult holds the return values for a single Executor.Start call.

Jump to

Keyboard shortcuts

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