cmdtest

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

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 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 JSONStatus

func JSONStatus(w http.ResponseWriter, status int, v any)

JSONStatus writes a JSON response with specified status code.

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 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.

Jump to

Keyboard shortcuts

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