clitest

package
v2.32.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: AGPL-3.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UpdateGoldenFiles = flag.Bool("update", false, "update .golden files")

UpdateGoldenFiles indicates golden files should be updated. To update the golden files: make gen/golden-files

Functions

func CreateTemplateVersionSource

func CreateTemplateVersionSource(t *testing.T, responses *echo.Responses) string

CreateTemplateVersionSource writes the echo provisioner responses into a new temporary testing directory.

func HandlersOK

func HandlersOK(t *testing.T, cmd *serpent.Command)

HandlersOK asserts that all commands have a handler. Without a handler, the command has no default behavior. Even for non-root commands (like 'groups' or 'users'), a handler is required. These handlers are likely just the 'help' handler, but this must be explicitly set.

func New

func New(t testing.TB, args ...string) (*serpent.Invocation, config.Root)

New creates a CLI instance with a configuration pointed to a temporary testing directory. The invocation is set up to use a global config directory for the given testing.TB, and keyring usage disabled.

func NewWithClock added in v2.32.0

func NewWithClock(t testing.TB, clk quartz.Clock, args ...string) (*serpent.Invocation, config.Root)

NewWithClock is like New, but injects the given clock for tests that are time-dependent.

func NewWithCommand

func NewWithCommand(
	t testing.TB, cmd *serpent.Command, args ...string,
) (*serpent.Invocation, config.Root)

func NewWithDefaultKeyringCommand added in v2.29.0

func NewWithDefaultKeyringCommand(t testing.TB, cmd *serpent.Command, args ...string,
) (*serpent.Invocation, config.Root)

func Run

func Run(t *testing.T, inv *serpent.Invocation)

Run runs the command and asserts that there is no error.

func SetupConfig

func SetupConfig(t *testing.T, client *codersdk.Client, root config.Root)

SetupConfig applies the URL and SessionToken of the client to the config.

func Start

func Start(t *testing.T, inv *serpent.Invocation)

Start runs the command in a goroutine and cleans it up when the test completed.

func StartWithAssert added in v2.9.0

func StartWithAssert(t *testing.T, inv *serpent.Invocation, assertCallback func(t *testing.T, err error))

StartWithAssert starts the given invocation and calls assertCallback with the resulting error when the invocation completes. If assertCallback is nil, expected shutdown errors are silently tolerated.

func TestCommandHelp

func TestCommandHelp(t *testing.T, getRoot func(t *testing.T) *serpent.Command, cases []CommandHelpCase)

TestCommandHelp will test the help output of the given commands using golden files.

func TestGoldenFile added in v2.10.0

func TestGoldenFile(t *testing.T, fileName string, actual []byte, replacements map[string]string)

TestGoldenFile will test the given bytes slice input against the golden file with the given file name, optionally using the given replacements.

Types

type CommandHelpCase

type CommandHelpCase struct {
	Name string
	Cmd  []string
}

func DefaultCases

func DefaultCases() []CommandHelpCase

type ErrorWaiter

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

func StartWithWaiter

func StartWithWaiter(t *testing.T, inv *serpent.Invocation) *ErrorWaiter

StartWithWaiter runs the command in a goroutine but returns the error instead of asserting it. This is useful for testing error cases.

func (*ErrorWaiter) Cancel

func (w *ErrorWaiter) Cancel()

func (*ErrorWaiter) RequireAs

func (w *ErrorWaiter) RequireAs(want interface{})

func (*ErrorWaiter) RequireContains

func (w *ErrorWaiter) RequireContains(s string)

func (*ErrorWaiter) RequireError

func (w *ErrorWaiter) RequireError()

func (*ErrorWaiter) RequireIs

func (w *ErrorWaiter) RequireIs(want error)

func (*ErrorWaiter) RequireSuccess

func (w *ErrorWaiter) RequireSuccess()

func (*ErrorWaiter) Wait

func (w *ErrorWaiter) Wait() error

type FakeSignalNotifier added in v2.4.0

type FakeSignalNotifier struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewFakeSignalNotifier added in v2.4.0

func NewFakeSignalNotifier(t *testing.T) *FakeSignalNotifier

func (*FakeSignalNotifier) AssertStopped added in v2.4.0

func (f *FakeSignalNotifier) AssertStopped()

func (*FakeSignalNotifier) Notify added in v2.4.0

func (f *FakeSignalNotifier) Notify()

func (*FakeSignalNotifier) NotifyContext added in v2.4.0

func (f *FakeSignalNotifier) NotifyContext(parent context.Context, signals ...os.Signal) (ctx context.Context, stop context.CancelFunc)

func (*FakeSignalNotifier) Stop added in v2.4.0

func (f *FakeSignalNotifier) Stop()

type Output added in v2.31.1

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

Output captures stdout and stderr from an invocation and formats them with prefixes for golden file testing, preserving their interleaved order.

func Capture added in v2.31.1

func Capture(inv *serpent.Invocation) *Output

Capture sets up stdout and stderr writers on the invocation that prefix each line with "out: " or "err: " while preserving their order.

func (*Output) Golden added in v2.31.1

func (o *Output) Golden() []byte

Golden returns the formatted output with lines prefixed by "err: " or "out: ".

func (*Output) Stderr added in v2.31.1

func (o *Output) Stderr() string

Stderr returns the unprefixed stderr content.

func (*Output) Stdout added in v2.31.1

func (o *Output) Stdout() string

Stdout returns the unprefixed stdout content for parsing (e.g., JSON).

Jump to

Keyboard shortcuts

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