Documentation
¶
Overview ¶
Package cmdtest provides shared test helpers for CLI command tests.
Index ¶
- func CaptureErr(t *testing.T, f *cmdutil.Factory, args ...string) error
- func CaptureOutput(t *testing.T, f *cmdutil.Factory, args ...string) string
- func Dedent(s string) string
- func EnableAnalytics(t *testing.T) string
- func Error(w http.ResponseWriter, status int, message string)
- func ExtractID(path, prefix string) string
- func JSON(w http.ResponseWriter, v any)
- func RunCmd(t *testing.T, args ...string)
- func RunCmdExpectErr(t *testing.T, want string, args ...string)
- func RunCmdWithFactory(t *testing.T, f *cmdutil.Factory, args ...string)
- func RunCmdWithFactoryExpectErr(t *testing.T, f *cmdutil.Factory, want string, args ...string)
- func Text(w http.ResponseWriter, s string)
- type OnboardingCall
- type OnboardingCalls
- type TestServer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CaptureErr ¶ added in v0.8.3
CaptureErr executes a CLI command, asserts it errors, and returns the error.
func CaptureOutput ¶ added in v0.8.3
CaptureOutput executes a CLI command and returns the combined stdout/stderr.
func Dedent ¶ added in v0.8.3
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
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 RunCmdExpectErr ¶
RunCmdExpectErr executes a CLI command and asserts an error containing want.
func RunCmdWithFactory ¶
RunCmdWithFactory executes a CLI command using a specific factory and asserts no error.
func RunCmdWithFactoryExpectErr ¶
RunCmdWithFactoryExpectErr executes a CLI command using a specific factory and asserts an error containing want.
Types ¶
type OnboardingCall ¶ added in v1.7.0
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.