clitest

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 25 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 clitest runs chronos commands against a fake API in tests. Use it from external test packages (package schedules_test), since it imports the whole command tree.

Index

Constants

View Source
const APIKey = "chrns_testkey0123456789abcdefghijklmnop"

Variables

This section is empty.

Functions

func ChildCommand added in v0.4.0

func ChildCommand(steps ...string) []string

ChildCommand returns the command line that runs the test binary as a child command, once the package's TestMain calls ServeChild. The child runs steps in order:

out:<text>   write text and a newline to stdout
err:<text>   the same on stderr
bytes:<n>    write n bytes to stdout, in lines of 64
stdin        copy stdin to stdout
sleep:<d>    sleep for the duration d
spew         write to stdout until a write fails, for at most 10s
hold-stdout  leave a process running that holds stdout open for 3s
exit:<n>     exit with code n

Any other step is written to stdout as "arg <step>".

func DecodeError

func DecodeError(t *testing.T, stderr string) exit.Outcome

func Fixture

func Fixture(name string) string

Fixture returns testdata/<name>.json: an API object as the API returns it.

func FixtureWith

func FixtureWith(t *testing.T, name string, fields map[string]any) string

FixtureWith returns Fixture(name) with fields set over its own. Numbers keep every digit, so large integers stay exact.

func Golden

func Golden(t *testing.T, name, got string)

Golden compares got with testdata/<name>.golden in the calling package. Run go test with -update to rewrite it.

func Now

func Now() time.Time

Now is the fixed time tests run at unless Options.Now is set.

func Query

func Query(t *testing.T, req Request) url.Values

func ServeChild added in v0.4.0

func ServeChild()

ServeChild runs the child and exits when the test binary was started by ChildCommand, and returns otherwise.

func WantExit

func WantExit(t *testing.T, res Result, want exit.Code)

Types

type API

type API struct {
	URL string
	// contains filtered or unexported fields
}

API replays responses in order, repeating the last one, and records every request. A request to an API with no responses fails the test.

func NewAPI

func NewAPI(t *testing.T, responses ...Response) *API

func (*API) Env

func (a *API) Env() map[string]string

Env points the CLI at this API with the test key.

func (*API) Requests

func (a *API) Requests() []Request

func (*API) ServeHTTP

func (a *API) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Failure

type Failure struct {
	Status      int
	Code        string
	Message     string
	FieldErrors string
}

Failure is an API error response. FieldErrors is a JSON array, or empty for none.

func (Failure) Response

func (f Failure) Response() Response

Response returns the error envelope the API sends for f.

type Keychain

type Keychain struct {
	Err error
	// contains filtered or unexported fields
}

Keychain is an in-memory keychain. With Err set, every call returns Err.

func (*Keychain) Delete

func (k *Keychain) Delete(service, account string) error

func (*Keychain) Get

func (k *Keychain) Get(service, account string) (string, error)

func (*Keychain) Set

func (k *Keychain) Set(service, account, secret string) error

type Options

type Options struct {
	Args []string
	// Env is the whole environment. CHRONOS_CONFIG_DIR defaults to a fresh
	// temporary directory.
	Env   map[string]string
	Stdin string
	// In replaces Stdin, for a stdin that blocks.
	In        io.Reader
	StdinTTY  bool
	StdoutTTY bool
	StderrTTY bool
	Width     int
	// Secret is what a hidden prompt reads.
	Secret string
	Now    time.Time
	// Context defaults to the test's context.
	Context context.Context
	// ClientOptions are appended to the defaults, which turn off request
	// retries so failures surface at once.
	ClientOptions []chronos.Option
	// Extend adds test-only commands to the root command.
	Extend func(f *app.Factory, root *cobra.Command)
	// Keychain defaults to an empty in-memory keychain, so tests never touch
	// the system one.
	Keychain config.Keychain
	// Browser stands in for opening the browser. Nil means there is none.
	Browser func(url string) error
	// LoginTimeout defaults to five seconds, so a test that waits for a
	// browser that never answers fails quickly.
	LoginTimeout time.Duration
	// PingTimeout defaults to two seconds, so a ping to a server that never
	// answers fails quickly.
	PingTimeout time.Duration
}

type Request

type Request struct {
	Method     string
	RequestURI string
	Header     http.Header
	Body       string
}

type Response

type Response struct {
	Status int
	Header map[string]string
	Body   string
	// Delay holds the response back, to make a request time out.
	Delay time.Duration
}

func Created

func Created(data string) Response

func OK

func OK(data string) Response

OK wraps data in the API's success envelope.

func Page

func Page(data string, paging chronos.Paging) Response

Page wraps data in the API's list envelope.

type Result

type Result struct {
	Stdout string
	Stderr string
	Code   exit.Code
}

func Run

func Run(t *testing.T, opts Options) Result

Jump to

Keyboard shortcuts

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