test

package
v0.0.0-...-69b9b6e Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: Apache-2.0 Imports: 18 Imported by: 2

Documentation

Overview

Package test is the main entrypoint for Tigron.

Index

Constants

View Source
const (
	// FilePermissionsDefault specifies the default creation mode for temporary files.
	// Note that umask will affect these.
	FilePermissionsDefault = 0o644
	// DirPermissionsDefault specifies the default creation mode for temporary directories.
	// Note that umask will affect these.
	DirPermissionsDefault = 0o755
)

Variables

This section is empty.

Functions

func Customize

func Customize(testable Testable)

Customize TODO.

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, t tig.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

ConfigKey FIXME consider getting rid of this?

type ConfigValue

type ConfigValue string

ConfigValue FIXME consider getting rid of this?

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)
	// T returns the current testing object
	T() tig.T
	// contains filtered or unexported methods
}

func NewGenericCommand

func NewGenericCommand() CustomizableCommand

type Data

type Data interface {
	Temp() DataTemp
	Labels() DataLabels
	Identifier(suffix ...string) string
}

Data is meant to hold information about a test: - first, key-value data that the test implementer wants to carry around - this is Labels() - second, some commonly useful immutable test properties (eg: a way to generate unique identifiers for that test) - third, ability to manipulate test files inside managed temporary directories Note that Data Labels are inherited from parent test to subtest. This is not true for Identifier and Temp().Dir(), which are bound to the test itself, though temporary files can be accessed by subtests if their location is passed around (in Labels).

func WithLabels

func WithLabels(in map[string]string) Data

WithLabels returns a Data object with specific key value labels set.

type DataLabels

type DataLabels interface {
	// Get returns the value of the requested label.
	Get(key string) string
	// Set will save the label `key` with `value`.
	Set(key, value string)
}

DataLabels holds key-value test information set by the test authors. Note that retrieving a non-existent label will return the empty string.

type DataTemp

type DataTemp interface {
	// Load will retrieve the content stored in the file
	// Asserts on failure.
	Load(key ...string) string
	// Save will store the content in the file, ensuring parent dir exists, and return the path.
	// Asserts on failure.
	Save(data string, key ...string) string
	// SaveToWriter allows to write to the file as a writer.
	// This is particularly useful for encoding functions like pem.Encode.
	SaveToWriter(writer func(file io.Writer) error, key ...string) string
	// Path will return the absolute path for the asset, whether it exists or not.
	Path(key ...string) string
	// Exists asserts that the object exist.
	Exists(key ...string)
	// Dir ensures the directory under temp is created, and returns the path.
	// Asserts on failure.
	Dir(key ...string) string
}

DataTemp allows test authors to easily manipulate test fixtures / temporary files.

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()

func (*GenericCommand) Clone

func (gc *GenericCommand) Clone() TestableCommand

func (*GenericCommand) Feed

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

func (*GenericCommand) PrependArgs

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

func (*GenericCommand) Run

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

func (*GenericCommand) Setenv

func (gc *GenericCommand) Setenv(key, value string)

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() tig.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) WithFeeder

func (gc *GenericCommand) WithFeeder(fun func() io.Reader)

func (*GenericCommand) WithPseudoTTY

func (gc *GenericCommand) WithPseudoTTY()

func (*GenericCommand) WithTimeout

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

func (*GenericCommand) WithWhitelist

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

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() tig.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 tig.T) CustomizableCommand
	AmbientRequirements(testCase *Case, t tig.T)
}

Testable TODO.

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 will allocate a new pty and set the command stdin and stdout to it.
	WithPseudoTTY()
	// WithCwd allows specifying the working directory for the command.
	WithCwd(path string)
	// WithTimeout defines the execution timeout for a command.
	WithTimeout(timeout time.Duration)
	// Setenv allows to override a specific env variable directly for a specific command instead of test-wide
	Setenv(key, value string)
	// WithFeeder allows passing a reader to be fed to the command stdin.
	WithFeeder(fun func() io.Reader)
	// Feed allows passing a reader to be fed to the command stdin.
	Feed(r io.Reader)
	// 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()
	// 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.

Jump to

Keyboard shortcuts

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