Documentation
¶
Overview ¶
Package mock provides test doubles for the cmdexec.Executor interface.
Index ¶
- type Call
- type Executor
- func (m *Executor) AddResult(stdout, stderr string, err error)
- func (m *Executor) Run(_ context.Context, name string, args ...string) (string, string, error)
- func (m *Executor) RunWithStdin(_ context.Context, stdin io.Reader, name string, args ...string) (string, string, error)
- func (m *Executor) Start(_ context.Context, name string, args ...string) (*cmdexec.Process, error)
- type Pipes
- type RecordedCall
- type Recorder
- type RecordingExecutor
- func (r *RecordingExecutor) Calls() []RecordedCall
- func (r *RecordingExecutor) Dump() string
- func (r *RecordingExecutor) Run(ctx context.Context, name string, args ...string) (string, string, error)
- func (r *RecordingExecutor) RunWithStdin(ctx context.Context, stdin io.Reader, name string, args ...string) (string, string, error)
- func (r *RecordingExecutor) Start(ctx context.Context, name string, args ...string) (*cmdexec.Process, error)
- type Result
- type StreamResult
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 ¶
AddResult enqueues a result to be returned by the next Run or RunWithStdin call. Only used when OnCall is nil.
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
CloseWithError closes the pipe at the given index with an error, causing the reader to return that error.
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.
type RecordedCall ¶
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) IsIntegration ¶
IsIntegration returns true when running against real executors.
type RecordingExecutor ¶
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.
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.