cmdtest

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cmdtest provides shared test helpers for CLI command tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureErr added in v0.8.3

func CaptureErr(t *testing.T, f *cmdutil.Factory, args ...string) error

CaptureErr executes a CLI command, asserts it errors, and returns the error.

func CaptureOutput added in v0.8.3

func CaptureOutput(t *testing.T, f *cmdutil.Factory, args ...string) string

CaptureOutput executes a CLI command and returns the combined stdout/stderr.

func Dedent added in v0.8.3

func Dedent(s string) string

Dedent strips the common leading whitespace from a multi-line string. Leading/trailing blank lines are also trimmed. This allows writing expected output indented inside test functions.

func EnableAnalytics added in v1.7.0

func EnableAnalytics(t *testing.T) string

EnableAnalytics undoes NewTestServer's DO_NOT_TRACK for tests of opt-out-gated behavior. The CLI config moves to a fresh XDG_CONFIG_HOME, returned, and FUS events go to a local sink, so nothing leaves the machine.

func Error

func Error(w http.ResponseWriter, status int, message string)

Error writes an API error response.

func ExtractID

func ExtractID(path, prefix string) string

ExtractID extracts an ID from a path like /app/rest/builds/id:123/something

func JSON

func JSON(w http.ResponseWriter, v any)

JSON writes a JSON response with 200 OK.

func RunCmd

func RunCmd(t *testing.T, args ...string)

RunCmd executes a CLI command using the default mock factory and asserts no error.

func RunCmdExpectErr

func RunCmdExpectErr(t *testing.T, want string, args ...string)

RunCmdExpectErr executes a CLI command and asserts an error containing want.

func RunCmdWithFactory

func RunCmdWithFactory(t *testing.T, f *cmdutil.Factory, args ...string)

RunCmdWithFactory executes a CLI command using a specific factory and asserts no error.

func RunCmdWithFactoryExpectErr

func RunCmdWithFactoryExpectErr(t *testing.T, f *cmdutil.Factory, want string, args ...string)

RunCmdWithFactoryExpectErr executes a CLI command using a specific factory and asserts an error containing want.

func Text

func Text(w http.ResponseWriter, s string)

Text writes a plain text response.

Types

type OnboardingCall added in v1.7.0

type OnboardingCall struct {
	Endpoint string
	Body     string
}

OnboardingCall is one request to an /app/fus/onboarding/cli/ endpoint; Body is "" when it had none.

type OnboardingCalls added in v1.7.0

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

OnboardingCalls records the onboarding endpoint calls a TestServer gets.

func (*OnboardingCalls) Calls added in v1.7.0

func (c *OnboardingCalls) Calls() []OnboardingCall

Calls returns the calls so far.

func (*OnboardingCalls) Reset added in v1.7.0

func (c *OnboardingCalls) Reset()

Reset forgets the calls so far.

func (*OnboardingCalls) Respond added in v1.7.0

func (c *OnboardingCalls) Respond(status int)

Respond changes the status the onboarding endpoints answer with.

type TestServer

type TestServer struct {
	*httptest.Server
	Factory *cmdutil.Factory
	// contains filtered or unexported fields
}

TestServer wraps httptest.Server for easy API testing.

func NewTestServer

func NewTestServer(t *testing.T) *TestServer

NewTestServer creates a test server and configures a Factory with a mock client.

func SetupMockClient

func SetupMockClient(t *testing.T) *TestServer

SetupMockClient creates a test server with all common API endpoints pre-registered.

func (*TestServer) CloneFactory

func (ts *TestServer) CloneFactory() *cmdutil.Factory

CloneFactory returns a new Factory that shares the same ClientFunc and IOStreams but has its own flag storage, making it safe for parallel subtests.

func (*TestServer) Handle

func (ts *TestServer) Handle(pattern string, h http.HandlerFunc)

Handle registers a handler for "METHOD /path" pattern.

func (*TestServer) HandleOnboarding added in v1.7.0

func (ts *TestServer) HandleOnboarding(status int) *OnboardingCalls

HandleOnboarding answers every onboarding endpoint with status and records the calls.

Jump to

Keyboard shortcuts

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