cmdtest

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 11 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 runs a leaf command in process against a test server, as cmd.Execute would. It sits apart from pkg/testutil, which client's own tests import, because it builds a client.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoLogin = errors.New("not logged in")

ErrNoLogin is what NoClient fails with.

Functions

func ClientFactory

func ClientFactory(ts *httptest.Server) func() (*client.AlpaconClient, error)

ClientFactory returns a client factory aimed at ts, so a test needs no login in HOME.

func NoClient

func NoClient() (*client.AlpaconClient, error)

NoClient fails as a missing login does.

func Run

func Run(cmd *cobra.Command, args ...string) (failure string, code int)

Run executes cmd with args and returns the failure cmd.Execute would print and its exit code. It captures nothing else, so it is safe under t.Parallel.

func RunCaptured

func RunCaptured(build Build, args ...string) (stdout, stderr string, code int)

RunCaptured runs the command build returns on test streams and returns what it wrote to each; the failure is appended to stderr. It swaps nothing, so it is safe under t.Parallel.

func RunJSON

func RunJSON(build Build, args ...string) (stdout, stderr string, code int)

RunJSON is RunCaptured under --output json.

Types

type Build

type Build func(ios *utils.IOStreams) *cobra.Command

Build constructs the command under test on the streams it is handed.

type Fake

type Fake struct {
	*httptest.Server
	// contains filtered or unexported fields
}

Fake is a test server that answers fixed routes and counts the requests each one got.

func Server

func Server(t *testing.T, routes map[string]Reply) *Fake

Server answers each "METHOD /path" key of routes with its Reply, ignoring the query string, and fails the test on any other request.

func (*Fake) Factory

func (f *Fake) Factory() func() (*client.AlpaconClient, error)

Factory returns a client factory aimed at f.

func (*Fake) Hits

func (f *Fake) Hits(route string) int

Hits returns how many requests route ("METHOD /path") got.

type Reply

type Reply struct {
	Status  int
	Body    string
	Inspect func(*http.Request)
}

Reply is the status and JSON body a Server answers a route with.

Jump to

Keyboard shortcuts

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