Documentation
¶
Overview ¶
Package cmdexec provides a testable abstraction for running external commands.
Index ¶
- type Executor
- type LogFunc
- type LoggingExecutor
- type MockCall
- type MockExecutor
- type MockResult
- type OSExecutor
- 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)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Executor ¶
type Executor interface {
// Run executes the named command with the given arguments and returns
// the captured stdout and stderr. If the command exits with a non-zero
// status, the returned error wraps an *exec.ExitError.
Run(ctx context.Context, name string, args ...string) (stdout string, stderr string, err error)
// RunWithStdin is like Run but pipes the given reader to the command's stdin.
RunWithStdin(ctx context.Context, stdin io.Reader, name string, args ...string) (stdout string, stderr string, err error)
}
Executor runs external commands and captures their output.
type LogFunc ¶
LogFunc is called before each command execution with the context and the full command string (name + args joined by spaces).
type LoggingExecutor ¶
LoggingExecutor wraps another Executor and calls a LogFunc before each command invocation.
type MockCall ¶
type MockCall struct {
Name string
Args []string
Stdin string // captured stdin content (empty if no stdin)
}
MockCall records a single invocation of MockExecutor.
type MockExecutor ¶
type MockExecutor 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) MockResult
Calls []MockCall
// contains filtered or unexported fields
}
MockExecutor 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 MockExecutor and result dispatch must be based on command arguments rather than call order.
func (*MockExecutor) AddResult ¶
func (m *MockExecutor) 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.
type MockResult ¶
MockResult holds the return values for a single MockExecutor call.
type OSExecutor ¶
type OSExecutor struct{}
OSExecutor runs commands using os/exec.
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 NewMockRecorder ¶
func NewMockRecorder() *Recorder
NewMockRecorder returns a Recorder backed by a MockExecutor.
func (*Recorder) IsIntegration ¶
IsIntegration returns true when running against real executors.
type RecordingExecutor ¶
type RecordingExecutor struct {
Inner 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.