testutil

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 22 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 testutil provides testing utilities and helpers for the AhaSend CLI:

  • Command execution helpers that capture output and inject a mock client
  • A home directory per test, so tests never read the real configuration

Fixtures of API responses live in the mocks package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureRequest added in v0.3.0

func CaptureRequest[R any](mocked interface {
	On(string, ...any) *mock.Call
}, method string, captured *R, response any, args ...any)

CaptureRequest makes mocked answer the call method, made with args and then a request of type R, with response and no error, and store the request in captured.

func HangUpURL added in v0.3.0

func HangUpURL(t *testing.T) string

HangUpURL returns the URL of a local listener that closes every connection before answering, so each request fails to reach the API. The listener keeps its port for the whole test: a closed port could be taken by another test's server and answer the request.

func IdempotencyKeyArg added in v0.3.0

func IdempotencyKeyArg(key string) interface{}

IdempotencyKeyArg matches a request option that sets the Idempotency-Key header to key.

func IndentedJSON added in v0.3.0

func IndentedJSON(t *testing.T, body string) string

IndentedJSON returns body in the form JSON output prints an API response: indented by two spaces and followed by a newline.

func InjectClient added in v0.3.0

func InjectClient(t *testing.T, c client.AhaSendClient) *bool

InjectClient installs c as the authenticated client for the rest of the test. The returned flag reports whether a command asked for the client, which lets tests assert that validation ran before authentication.

func OpenError added in v0.3.0

func OpenError(t *testing.T, path string) string

OpenError returns the error text of opening the missing file path, as this operating system words it.

func RefuseAuth added in v0.3.0

func RefuseAuth(t *testing.T) *bool

RefuseAuth makes a command that asks for the authenticated client, for the rest of the test, fail the test by name and stop with an error, with no client to call, so a command that reaches authentication fails the test that runs it rather than the whole test binary. The returned flag reports whether a command asked, for tests that check that validation runs before authentication.

func ResponseHandler added in v0.3.0

func ResponseHandler(t *testing.T, format string, writer, errWriter io.Writer) printer.ResponseHandler

ResponseHandler returns the response handler for format that prints results to writer and errors to errWriter.

func RunCommand added in v0.3.0

func RunCommand(t *testing.T, cmd *cobra.Command, format, stdin string, args ...string) (string, string, error)

RunCommand runs a leaf command with a response handler for format installed in context, as the root command does, stdin as its input, and stdout and stderr captured apart.

func RunCommandCombined added in v0.3.0

func RunCommandCombined(t *testing.T, cmd *cobra.Command, format string, args ...string) (string, error)

RunCommandCombined runs a leaf command like RunCommand, with no input and stderr written into the same buffer as stdout.

func RunPlain added in v0.3.0

func RunPlain(t *testing.T, cmd *cobra.Command, args ...string) (string, error)

RunPlain runs a leaf command with plain output and no input, and returns its stdout.

func RunWithClient added in v0.3.0

func RunWithClient(t *testing.T, c client.AhaSendClient, cmd *cobra.Command, format string, args ...string) (string, error)

RunWithClient installs c as the authenticated client and runs a leaf command like RunCommandCombined.

func StatError added in v0.3.0

func StatError(t *testing.T, path string) string

StatError returns the error text of reading the details of the missing file path, as this operating system words it.

func UseNewHome added in v0.3.0

func UseNewHome(t *testing.T) string

UseNewHome points the home directory at a new empty directory.

func WireJSON added in v0.3.0

func WireJSON(t *testing.T, v any) map[string]any

WireJSON marshals v the way the SDK sends it and decodes it into a map.

func WriteConfig added in v0.3.0

func WriteConfig(t *testing.T, home, config string) string

WriteConfig writes config as the CLI's config file under home and returns the file's path.

func WriteProfiles added in v0.3.0

func WriteProfiles(t *testing.T, home, apiURL, accountID string, names ...string) string

WriteProfiles writes a config file under home with a profile for each of names, the first one the default, and returns the file's path. Each profile points at apiURL, such as a local test server, with accountID and the API key "aha-sk-" followed by its name.

func WriteTempFile added in v0.3.0

func WriteTempFile(t *testing.T, name, content string) string

WriteTempFile writes content to name in a test directory and returns its path.

Types

type Terminal added in v0.3.0

type Terminal struct {
	Stdin *os.File
	// contains filtered or unexported fields
}

Terminal is a pseudo-terminal for tests of prompts: the command reads its stdin from Stdin, a terminal, and the test types into it.

func NewTerminal added in v0.3.0

func NewTerminal(t *testing.T) *Terminal

NewTerminal opens a pseudo-terminal that closes when the test ends.

func (*Terminal) Echoes added in v0.3.0

func (term *Terminal) Echoes(t *testing.T) bool

Echoes reports whether the terminal shows what is typed.

func (*Terminal) PressHidden added in v0.3.0

func (term *Terminal) PressHidden(t *testing.T, keys string)

PressHidden waits like TypeHidden, then types keys as they are, such as "\x04" for Ctrl+D.

func (*Terminal) Shown added in v0.3.0

func (term *Terminal) Shown(t *testing.T) func() string

Shown returns what was written to the terminal, Stdin, which a command can write to as its stderr, as the terminal received it: each newline written is a carriage return and a newline. Reading starts at the first call; the returned function returns what was received so far.

func (*Terminal) Start added in v0.3.0

func (term *Terminal) Start(t *testing.T, cmd *cobra.Command, format string, args ...string) (wait func() (string, string, error))

Start runs a leaf command like RunCommand, with the terminal as its stdin, while the test types into the terminal. The returned function waits for the command to end and returns its stdout, stderr and error; it fails the test when the command has not ended after a few seconds.

func (*Terminal) Type added in v0.3.0

func (term *Terminal) Type(t *testing.T, line string)

Type types line and Enter, whether the terminal echoes them or not.

func (*Terminal) TypeHidden added in v0.3.0

func (term *Terminal) TypeHidden(t *testing.T, line string)

TypeHidden waits until the terminal stops echoing what is typed, as it does while a prompt hides its answer, then types line and Enter. It fails the test when the terminal still echoes after a few seconds.

func (*Terminal) WaitForEcho added in v0.3.0

func (term *Terminal) WaitForEcho(t *testing.T)

WaitForEcho waits until the terminal shows what is typed again, as it does once a hidden prompt is over. It fails the test when the terminal still hides input after a few seconds.

func (*Terminal) WaitForHiding added in v0.3.0

func (term *Terminal) WaitForHiding(t *testing.T)

WaitForHiding waits until the terminal stops echoing what is typed, as it does while a prompt hides its answer. It fails the test when the terminal still echoes after a few seconds.

Jump to

Keyboard shortcuts

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