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
- func ChildCommand(steps ...string) []string
- func DecodeError(t *testing.T, stderr string) exit.Outcome
- func Fixture(name string) string
- func FixtureWith(t *testing.T, name string, fields map[string]any) string
- func Golden(t *testing.T, name, got string)
- func Now() time.Time
- func Query(t *testing.T, req Request) url.Values
- func ServeChild()
- func WantExit(t *testing.T, res Result, want exit.Code)
- type API
- type Failure
- type Keychain
- type Options
- type Request
- type Response
- type Result
Constants ¶
const APIKey = "chrns_testkey0123456789abcdefghijklmnop"
Variables ¶
This section is empty.
Functions ¶
func ChildCommand ¶ added in v0.4.0
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 FixtureWith ¶
FixtureWith returns Fixture(name) with fields set over its own. Numbers keep every digit, so large integers stay exact.
func Golden ¶
Golden compares got with testdata/<name>.golden in the calling package. Run go test with -update to rewrite it.
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.
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.
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.
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
}