mockexec

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package mockexec provides a configurable mock for shell command execution in functional tests. It is designed to run as a test service alongside the mock HTTP server — configure it via `spec.testing.config.services` with `type: exec`.

Mock rules match commands by exact string or regex and return predefined stdout, stderr, and exit codes. Unmatched commands can fall through to real execution or fail with an error (configurable via Passthrough).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallRecord

type CallRecord struct {
	Command string
	Args    []string
	Matched bool
	Rule    *Rule
}

CallRecord records a command that was intercepted.

type MockExec

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

MockExec intercepts shell commands and returns predefined responses.

func New

func New(rules []Rule, opts ...Option) (*MockExec, error)

New creates a new MockExec with the given rules. Rules are matched in order — the first matching rule wins.

func (*MockExec) Calls

func (m *MockExec) Calls() []CallRecord

Calls returns a copy of all recorded call records.

func (*MockExec) Reset

func (m *MockExec) Reset()

Reset clears recorded calls.

func (*MockExec) Rules

func (m *MockExec) Rules() []Rule

Rules returns the configured rules (read-only slice).

func (*MockExec) RunFunc

func (m *MockExec) RunFunc() shellexec.RunFunc

RunFunc returns a shellexec.RunFunc that can be injected into context via shellexec.WithRunFunc. Matched commands return mock responses; unmatched commands either pass through to real execution or fail, depending on config.

type Option

type Option func(*MockExec)

Option configures a MockExec.

func WithPassthrough

func WithPassthrough(allow bool) Option

WithPassthrough allows unmatched commands to execute normally via the real shellexec.Run. When false (default), unmatched commands return an error.

type Rule

type Rule struct {
	// Command is an exact command string to match. Mutually exclusive with Pattern.
	Command string `json:"command,omitempty" yaml:"command,omitempty" doc:"Exact command string to match" maxLength:"1000"`

	// Pattern is a regex pattern to match against the command. Mutually exclusive with Command.
	Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty" doc:"Regex pattern to match against command" maxLength:"500"`

	// Stdout is the mock standard output to return.
	Stdout string `json:"stdout,omitempty" yaml:"stdout,omitempty" doc:"Mock stdout response" maxLength:"100000"`

	// Stderr is the mock standard error to return.
	Stderr string `json:"stderr,omitempty" yaml:"stderr,omitempty" doc:"Mock stderr response" maxLength:"100000"`

	// ExitCode is the mock exit code (default: 0).
	ExitCode int `json:"exitCode,omitempty" yaml:"exitCode,omitempty" doc:"Mock exit code" maximum:"255"`
	// contains filtered or unexported fields
}

Rule defines a single mock command response.

func (*Rule) Compile

func (r *Rule) Compile() error

Compile pre-compiles the regex pattern if set. Returns an error for invalid patterns.

func (*Rule) Matches

func (r *Rule) Matches(command string) bool

Matches returns true if the rule matches the given command string.

Jump to

Keyboard shortcuts

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