Documentation
¶
Index ¶
- type TestCmd
- func (c *TestCmd) AddRealTimeFileListener(path string, listener opctx.LineListener) error
- func (c *TestCmd) GetArgs() []string
- func (c *TestCmd) Run(ctx context.Context) error
- func (c *TestCmd) RunAndGetOutput(ctx context.Context) (string, error)
- func (c *TestCmd) SetDescription(description string) opctx.Cmd
- func (c *TestCmd) SetLongRunning(progressTitle string) opctx.Cmd
- func (c *TestCmd) SetRealTimeStderrListener(listener opctx.LineListener) error
- func (c *TestCmd) SetRealTimeStdoutListener(listener opctx.LineListener) error
- func (c *TestCmd) SetStderr(_ io.Writer)
- func (c *TestCmd) SetStdin(_ io.Reader)
- func (c *TestCmd) SetStdout(_ io.Writer)
- func (c *TestCmd) Start(ctx context.Context) error
- func (c *TestCmd) Wait(ctx context.Context) error
- type TestCmdFactory
- type TestCtx
- func (t *TestCtx) AllPromptsAccepted() bool
- func (t *TestCtx) Command(cmd *exec.Cmd) (opctx.Cmd, error)
- func (t *TestCtx) CommandInSearchPath(name string) bool
- func (t *TestCtx) ConfirmAutoResolution(text string) bool
- func (t *TestCtx) Deadline() (deadline time.Time, ok bool)
- func (t *TestCtx) Done() <-chan struct{}
- func (t *TestCtx) DryRun() bool
- func (t *TestCtx) Err() error
- func (t *TestCtx) Event(name string, args ...any)
- func (t *TestCtx) FS() opctx.FS
- func (t *TestCtx) OSEnv() opctx.OSEnv
- func (t *TestCtx) PromptsAllowed() bool
- func (t *TestCtx) StartEvent(name string, args ...any) opctx.Event
- func (t *TestCtx) Value(key any) any
- func (t *TestCtx) Verbose() bool
- type TestCtxOption
- type TestEvent
- type TestOSEnv
- func (env *TestOSEnv) Chdir(dir string) error
- func (env *TestOSEnv) Getenv(key string) string
- func (env *TestOSEnv) Getwd() (string, error)
- func (env *TestOSEnv) IsCurrentUserMemberOf(groupName string) (isMember bool, err error)
- func (env *TestOSEnv) LookupGroupID(groupName string) (gid int, err error)
- func (env *TestOSEnv) SetEnv(key, value string)
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) RunAndGetOutput ¶
RunAndGetOutput implements the opctx.Cmd interface.
func (*TestCmd) SetDescription ¶
SetDescription implements the opctx.Cmd interface.
func (*TestCmd) SetLongRunning ¶
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.
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 ¶
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 ¶
AllPromptsAccepted implements the opctx.Prompter interface.
func (*TestCtx) Command ¶
Command implements the opctx.CmdFactory interface.
func (*TestCtx) CommandInSearchPath ¶
CommandInSearchPath implements the opctx.CmdFactory interface.
func (*TestCtx) ConfirmAutoResolution ¶
ConfirmAutoResolution implements the opctx.Prompter interface.
func (*TestCtx) Deadline ¶
Deadline implements the context.Context interface.
func (*TestCtx) Done ¶
func (t *TestCtx) Done() <-chan struct{}
Done implements the context.Context interface.
func (*TestCtx) Event ¶
Event implements the opctx.EventListener interface.
func (*TestCtx) FS ¶
FS implements the opctx.FileSystemFactory interface.
func (*TestCtx) OSEnv ¶
OSEnv implements the opctx.OSEnvFactory interface.
func (*TestCtx) PromptsAllowed ¶
PromptsAllowed implements the opctx.Prompter interface.
func (*TestCtx) StartEvent ¶
StartEvent implements the opctx.EventListener interface.
func (*TestCtx) Value ¶
Value implements the context.Context interface.
func (*TestCtx) Verbose ¶
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) SetLongRunning ¶
SetLongRunning implements the opctx.Event interface.
func (*TestEvent) SetProgress ¶
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 ¶
Chdir implements the opctx.OSEnv interface.
func (*TestOSEnv) Getenv ¶
Getenv implements the opctx.OSEnv interface.
func (*TestOSEnv) Getwd ¶
Getwd implements the opctx.OSEnv interface.
func (*TestOSEnv) IsCurrentUserMemberOf ¶
IsCurrentUserMemberOf implements the opctx.OSEnv interface.
func (*TestOSEnv) LookupGroupID ¶
LookupGroupID implements the opctx.OSEnv interface.
func (*TestOSEnv) SetEnv ¶
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.