test

package
v0.0.0-...-2feac61 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Customize

func Customize(testable Testable)

Types

type Butler

type Butler func(data Data, helpers Helpers)

A Butler is the function signature meant to be attached to a Setup or Cleanup routine for a Case or Requirement.

type Case

type Case struct {
	// Description contains a human-readable short desc, used as a seed for the identifier and as a title for the test
	Description string
	// NoParallel disables parallel execution if set to true
	// This obviously implies that all tests run in parallel, by default. This is a design choice.
	NoParallel bool
	// Env contains a map of environment variables to use as a base for all commands run in Setup, Command and Cleanup
	// Note that the environment is inherited by subtests
	Env map[string]string
	// Data contains test specific data, accessible to all operations, also inherited by subtests
	Data Data
	// Config contains specific information meaningful to the binary being tested.
	// It is also inherited by subtests
	Config Config

	// Requirement
	Require *Requirement
	// Setup
	Setup Butler
	// Command
	Command Executor
	// Expected
	Expected Manager
	// Cleanup
	Cleanup Butler

	// SubTests
	SubTests []*Case
	// contains filtered or unexported fields
}

Case describes an entire test-case, including data, setup and cleanup routines, command and expectations.

func (*Case) Run

func (test *Case) Run(t *testing.T)

Run prepares and executes the test, and any possible subtests.

type Comparator

type Comparator func(stdout string, info string, t *testing.T)

A Comparator is the function signature to implement for the Output property of an Expected.

type Config

type Config interface {
	Write(key ConfigKey, value ConfigValue) Config
	Read(key ConfigKey) ConfigValue
}

Config is meant to hold information relevant to the binary (eg: flags defining certain behaviors, etc.)

func WithConfig

func WithConfig(key ConfigKey, value ConfigValue) Config

WithConfig returns a config object with a certain config property set.

type ConfigKey

type ConfigKey string

type ConfigValue

type ConfigValue string

type CustomizableCommand

type CustomizableCommand interface {
	TestableCommand

	PrependArgs(args ...string)
	// WithBlacklist allows to filter out unwanted variables from the embedding environment -
	// default it pass any that is defined by WithEnv
	WithBlacklist(env []string)
	// contains filtered or unexported methods
}

CustomizableCommand is an interface meant for people who want to heavily customize the base command of their test case.

type Data

type Data interface {
	// Get returns the value of a certain key for custom data
	Get(key string) string
	// Set will save `value` for `key`
	Set(key string, value string) Data

	// Identifier returns the test identifier that can be used to name resources
	Identifier(suffix ...string) string
	// TempDir returns the test temporary directory
	TempDir() string
}

Data is meant to hold information about a test: - first, any random key value data that the test implementer wants to carry / modify - this is test data - second, some commonly useful immutable test properties (a way to generate unique identifiers for that test, temporary directory, etc.) Note that Data is inherited, from parent test to subtest (except for Identifier and TempDir of course).

func WithData

func WithData(key string, value string) Data

WithData returns a data object with a certain key value set.

type Evaluator

type Evaluator func(data Data, helpers Helpers) (bool, string)

An Evaluator is a function that decides whether a test should run or not.

type Executor

type Executor func(data Data, helpers Helpers) TestableCommand

An Executor is the function signature meant to be attached to the Command property of a Case.

func Command

func Command(args ...string) Executor

Command is the simplest way to express a test.TestableCommand for very basic cases where access to test data is not necessary.

type Expected

type Expected struct {
	// ExitCode
	ExitCode int
	// Errors contains any error that (once serialized) should be seen in stderr
	Errors []error
	// Output function to match against stdout
	Output Comparator
}

Expected expresses the expected output of a command.

type GenericCommand

type GenericCommand struct {
	Config  Config
	TempDir string
	Env     map[string]string
	// contains filtered or unexported fields
}

GenericCommand is a concrete Command implementation.

func (*GenericCommand) Background

func (gc *GenericCommand) Background(timeout time.Duration)

func (*GenericCommand) Clone

func (gc *GenericCommand) Clone() TestableCommand

func (*GenericCommand) PrependArgs

func (gc *GenericCommand) PrependArgs(args ...string)

func (*GenericCommand) Run

func (gc *GenericCommand) Run(expect *Expected)

func (*GenericCommand) Signal

func (gc *GenericCommand) Signal(sig os.Signal) error

func (*GenericCommand) Stderr

func (gc *GenericCommand) Stderr() string

func (*GenericCommand) T

func (gc *GenericCommand) T() *testing.T

func (*GenericCommand) WithArgs

func (gc *GenericCommand) WithArgs(args ...string)

func (*GenericCommand) WithBinary

func (gc *GenericCommand) WithBinary(binary string)

func (*GenericCommand) WithBlacklist

func (gc *GenericCommand) WithBlacklist(env []string)

func (*GenericCommand) WithCwd

func (gc *GenericCommand) WithCwd(path string)

func (*GenericCommand) WithPseudoTTY

func (gc *GenericCommand) WithPseudoTTY(writers ...func(*os.File) error)

func (*GenericCommand) WithStdin

func (gc *GenericCommand) WithStdin(r io.Reader)

func (*GenericCommand) WithWrapper

func (gc *GenericCommand) WithWrapper(binary string, args ...string)

type Helpers

type Helpers interface {
	// Ensure runs a command and verifies it is succeeding
	Ensure(args ...string)
	// Anyhow runs a command and ignores its result
	Anyhow(args ...string)
	// Fail runs a command and verifies it failed
	Fail(args ...string)
	// Capture runs a command, verifies it succeeded, and returns stdout
	Capture(args ...string) string
	// Err runs a command, and returns stderr regardless of its outcome
	// This is mostly useful for debugging
	Err(args ...string) string

	// Command will return a populated command from the default internal command, with the provided arguments,
	// ready to be Run or further configured
	Command(args ...string) TestableCommand
	// Custom will return a bare command, without configuration nor defaults (still has the Env)
	Custom(binary string, args ...string) TestableCommand

	// Read return the config value associated with a key
	Read(key ConfigKey) ConfigValue
	// Write saves a value in the config
	Write(key ConfigKey, value ConfigValue)

	// T returns the current testing object
	T() *testing.T
}

Helpers provides a set of helpers to run commands with simple expectations, available at all stages of a test (Setup, Cleanup, etc...)

type Manager

type Manager func(data Data, helpers Helpers) *Expected

A Manager is the function signature meant to produce expectations for a command.

func Expects

func Expects(exitCode int, errors []error, output Comparator) Manager

Expects is provided as a simple helper covering "expectations" for simple use-cases where access to the test data is not necessary.

type Requirement

type Requirement struct {
	// Check is expected to verify if the requirement is fulfilled, and return a boolean and an explanatory message
	Check Evaluator
	// Setup, if provided, will be run before any test-specific Setup routine, in the order that requirements
	// have been declared
	Setup Butler
	// Cleanup, if provided, will be run after any test-specific Cleanup routine, in the reverse order that
	// requirements have been declared
	Cleanup Butler
}

A Requirement offers a way to verify random conditions to decide if a test should be skipped or run. It can also (optionally) provide custom Setup and Cleanup routines.

type Testable

type Testable interface {
	CustomCommand(testCase *Case, t *testing.T) CustomizableCommand
	AmbientRequirements(testCase *Case, t *testing.T)
}

type TestableCommand

type TestableCommand interface {
	// WithBinary specifies what binary to execute
	WithBinary(binary string)
	// WithArgs specifies the args to pass to the binary. Note that WithArgs can be used multiple times and is additive.
	WithArgs(args ...string)
	// WithWrapper allows wrapping a command with another command (for example: `time`)
	WithWrapper(binary string, args ...string)
	WithPseudoTTY(writers ...func(*os.File) error)
	// WithStdin allows passing a reader to be used for stdin for the command
	WithStdin(r io.Reader)
	// WithCwd allows specifying the working directory for the command
	WithCwd(path string)
	// Clone returns a copy of the command
	Clone() TestableCommand

	// Run does execute the command, and compare the output with the provided expectation.
	// Passing nil for `Expected` will just run the command regardless of outcome.
	// An empty `&Expected{}` is (of course) equivalent to &Expected{Exit: 0}, meaning the command is
	// verified to be successful
	Run(expect *Expected)
	// Background allows starting a command in the background
	Background(timeout time.Duration)
	// Signal sends a signal to a backgrounded command
	Signal(sig os.Signal) error
	// Stderr allows retrieving the raw stderr output of the command once it has been run
	Stderr() string
}

The TestableCommand interface represents a low-level command to execute, typically to be compared with an Expected A TestableCommand can be used as a Case Command obviously, but also as part of a Setup or Cleanup routine, and as the basis of any type of helper. For more powerful use-cases outside of test cases, see below CustomizableCommand.

Directories

Path Synopsis
pty
Package pty.
Package pty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳