testctx

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TestCmd

type TestCmd struct {
	Cmd     *exec.Cmd
	Factory *TestCmdFactory

	StdoutListener opctx.LineListener
	StderrListener opctx.LineListener
	FileListeners  map[string]opctx.LineListener
}

TestCmd is a test implementation of the opctx.Cmd interface, used by TestCmdFactory.

func (*TestCmd) AddRealTimeFileListener

func (c *TestCmd) AddRealTimeFileListener(path string, listener opctx.LineListener) error

AddRealTimeFileListener implements the opctx.Cmd interface.

func (*TestCmd) GetArgs

func (c *TestCmd) GetArgs() []string

GetArgs implements the opctx.Cmd interface.

func (*TestCmd) Run

func (c *TestCmd) Run(ctx context.Context) error

Run implements the opctx.Cmd interface.

func (*TestCmd) RunAndGetOutput

func (c *TestCmd) RunAndGetOutput(ctx context.Context) (string, error)

RunAndGetOutput implements the opctx.Cmd interface.

func (*TestCmd) SetDescription

func (c *TestCmd) SetDescription(description string) opctx.Cmd

SetDescription implements the opctx.Cmd interface.

func (*TestCmd) SetLongRunning

func (c *TestCmd) SetLongRunning(progressTitle string) opctx.Cmd

SetLongRunning implements the opctx.Cmd interface.

func (*TestCmd) SetRealTimeStderrListener

func (c *TestCmd) SetRealTimeStderrListener(listener opctx.LineListener) error

SetRealTimeStderrListener implements the opctx.Cmd interface.

func (*TestCmd) SetRealTimeStdoutListener

func (c *TestCmd) SetRealTimeStdoutListener(listener opctx.LineListener) error

SetRealTimeStdoutListener implements the opctx.Cmd interface.

func (*TestCmd) SetStderr

func (c *TestCmd) SetStderr(_ io.Writer)

SetStderr implements the opctx.Cmd interface.

func (*TestCmd) SetStdin

func (c *TestCmd) SetStdin(_ io.Reader)

SetStdin implements the opctx.Cmd interface.

func (*TestCmd) SetStdout

func (c *TestCmd) SetStdout(_ io.Writer)

SetStdout implements the opctx.Cmd interface.

func (*TestCmd) Start

func (c *TestCmd) Start(ctx context.Context) error

Start implements the opctx.Cmd interface.

func (*TestCmd) Wait

func (c *TestCmd) Wait(ctx context.Context) error

Wait implements the opctx.Cmd interface.

type TestCmdFactory

type TestCmdFactory struct {
	RunHandler             func(*exec.Cmd) error
	RunAndGetOutputHandler func(*exec.Cmd) (string, error)

	Stdout          string
	Stderr          string
	ListenableFiles map[string]string
	// contains filtered or unexported fields
}

TestCmdFactory is a test implementation of the opctx.CmdFactory interface, intended for use in unit tests. It allows for simulating command execution and capturing output without actually running any commands.

func NewTestCmdFactory

func NewTestCmdFactory() *TestCmdFactory

Creates a new instance of TestCmdFactory, with no commands present in the default search path.

func (*TestCmdFactory) Command

func (f *TestCmdFactory) Command(cmd *exec.Cmd) (opctx.Cmd, error)

Command implements the opctx.CmdFactory interface.

func (*TestCmdFactory) CommandInSearchPath

func (f *TestCmdFactory) CommandInSearchPath(name string) bool

CommandInSearchPath implements the opctx.CmdFactory interface.

func (*TestCmdFactory) RegisterCommandInSearchPath

func (f *TestCmdFactory) RegisterCommandInSearchPath(name string)

Registers a command as being in the search path; used by tests to simulate preconditions for code-under-test that checks for the presence of commands, or executes commands.

type TestCtx

type TestCtx struct {
	//nolint:containedctx // We are intentionally embedding a context.Context.
	Ctx context.Context

	CmdFactory *TestCmdFactory

	AllPromptsAcceptedValue bool
	DryRunValue             bool
	PromptsAllowedValue     bool
	VerboseValue            bool

	Events []*TestEvent
	// contains filtered or unexported fields
}

Test implementation of opctx.Ctx; allows for injecting simulated conditions.

func NewCtx

func NewCtx(options ...TestCtxOption) *TestCtx

Creates a new test implementation of opctx.Ctx with an in-memory filesystem.

func (*TestCtx) AllPromptsAccepted

func (t *TestCtx) AllPromptsAccepted() bool

AllPromptsAccepted implements the opctx.Prompter interface.

func (*TestCtx) Command

func (t *TestCtx) Command(cmd *exec.Cmd) (opctx.Cmd, error)

Command implements the opctx.CmdFactory interface.

func (*TestCtx) CommandInSearchPath

func (t *TestCtx) CommandInSearchPath(name string) bool

CommandInSearchPath implements the opctx.CmdFactory interface.

func (*TestCtx) ConfirmAutoResolution

func (t *TestCtx) ConfirmAutoResolution(text string) bool

ConfirmAutoResolution implements the opctx.Prompter interface.

func (*TestCtx) Deadline

func (t *TestCtx) Deadline() (deadline time.Time, ok bool)

Deadline implements the context.Context interface.

func (*TestCtx) Done

func (t *TestCtx) Done() <-chan struct{}

Done implements the context.Context interface.

func (*TestCtx) DryRun

func (t *TestCtx) DryRun() bool

DryRun implements the opctx.Ctx interface.

func (*TestCtx) Err

func (t *TestCtx) Err() error

Err implements the context.Context interface.

func (*TestCtx) Event

func (t *TestCtx) Event(name string, args ...any)

Event implements the opctx.EventListener interface.

func (*TestCtx) FS

func (t *TestCtx) FS() opctx.FS

FS implements the opctx.FileSystemFactory interface.

func (*TestCtx) OSEnv

func (t *TestCtx) OSEnv() opctx.OSEnv

OSEnv implements the opctx.OSEnvFactory interface.

func (*TestCtx) PromptsAllowed

func (t *TestCtx) PromptsAllowed() bool

PromptsAllowed implements the opctx.Prompter interface.

func (*TestCtx) StartEvent

func (t *TestCtx) StartEvent(name string, args ...any) opctx.Event

StartEvent implements the opctx.EventListener interface.

func (*TestCtx) Value

func (t *TestCtx) Value(key any) any

Value implements the context.Context interface.

func (*TestCtx) Verbose

func (t *TestCtx) Verbose() bool

Verbose implements the opctx.Verbosity interface.

type TestCtxOption

type TestCtxOption func(*TestCtx)

TestCtxOption is a function that modifies the TestCtx instance.

func WithFS

func WithFS(fs opctx.FS) TestCtxOption

WithFS sets the filesystem to the provided opctx.FS implementation.

func WithHostFS

func WithHostFS() TestCtxOption

WithHostFS sets the filesystem to the host OS filesystem.

func WithOSEnv

func WithOSEnv(osEnv opctx.OSEnv) TestCtxOption

WithOSEnv sets the OSEnv to the provided opctx.OSEnv implementation.

type TestEvent

type TestEvent struct {
	Name  string
	Ended bool

	LongRunningDescription string

	LastUnitsCompleted int64
	LastTotalUnits     int64
}

TestEvent is a test implementation of the Event interface. It tracks updates to its state from function calls, allowing for test verification.

func (*TestEvent) End

func (t *TestEvent) End()

End implements the opctx.Event interface.

func (*TestEvent) SetLongRunning

func (t *TestEvent) SetLongRunning(title string)

SetLongRunning implements the opctx.Event interface.

func (*TestEvent) SetProgress

func (t *TestEvent) SetProgress(unitsComplete int64, totalUnits int64)

SetProgress implements the opctx.Event interface.

type TestOSEnv

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

TestOSEnv is a test implementation of the OSEnv interface that allows tests to control the environment without mutating the host environment.

func NewTestOSEnv

func NewTestOSEnv() *TestOSEnv

Constructs a new TestOSEnv instance with the default working directory initialized to "/".

func (*TestOSEnv) Chdir

func (env *TestOSEnv) Chdir(dir string) error

Chdir implements the opctx.OSEnv interface.

func (*TestOSEnv) Getenv

func (env *TestOSEnv) Getenv(key string) string

Getenv implements the opctx.OSEnv interface.

func (*TestOSEnv) Getwd

func (env *TestOSEnv) Getwd() (string, error)

Getwd implements the opctx.OSEnv interface.

func (*TestOSEnv) IsCurrentUserMemberOf

func (env *TestOSEnv) IsCurrentUserMemberOf(groupName string) (isMember bool, err error)

IsCurrentUserMemberOf implements the opctx.OSEnv interface.

func (*TestOSEnv) LookupGroupID

func (env *TestOSEnv) LookupGroupID(groupName string) (gid int, err error)

LookupGroupID implements the opctx.OSEnv interface.

func (*TestOSEnv) SetEnv

func (env *TestOSEnv) SetEnv(key, value string)

SetEnv sets a test environment variable on the TestOSEnv instance.

Note that [SetEnv] is not part of the opctx.OSEnv interface, so it is only available when working with TestOSEnv directly. The intended usage pattern in tests is:

env := NewTestOSEnv()
env.SetEnv("KEY", "VALUE")
ctx := NewCtx(
    ctx,
    WithOSEnv(env),
)

This allows tests to configure environment variables without mutating the real host environment while still passing an opctx.OSEnv-compatible value into NewCtx via WithOSEnv.

Jump to

Keyboard shortcuts

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