testutil

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: Apache-2.0 Imports: 14 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 holds the test helpers that tests of many packages need and that need nothing of AgentKit: file trees, a git environment that ignores the machine's configuration, polling with a deadline, JSON, tar, golden files, the repository root, and running a program with `go run`.

It imports only the standard library, so the tests of every package, the root's and the nested modules' alike, can use it. Test doubles over core (a scripted provider, an echo tool) are in internal/testkit.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Environ

func Environ(drop ...string) map[string]string

Environ is the test process's environment as a map, without the variables named in drop.

func Eventually

func Eventually(t testing.TB, what string, cond func() bool)

Eventually fails t unless cond holds within five seconds; what names the condition in the failure.

func Golden

func Golden(t testing.TB, path string, got []byte)

Golden compares got with the file at path. Run with -update to rewrite the file instead; the diff is then reviewed, not blessed, since a golden regenerated from the output it checks proves nothing.

func Hermetic

func Hermetic(t *testing.T)

Hermetic points HOME, XDG_CONFIG_HOME and git's global and system configuration at empty places for the rest of the test, so that what git and the ignore rules read does not depend on whose machine runs it.

func MustJSON

func MustJSON(t testing.TB, v any) string

MustJSON is v encoded as JSON.

func Object

func Object(t testing.TB, b []byte) map[string]any

Object decodes b as a JSON object, numbers as json.Number, the form schema.Validate and the tools' Data take.

func RepoRoot

func RepoRoot(t testing.TB) string

RepoRoot is the repository's root directory: the nearest ancestor of the working directory whose go.mod declares the root module.

func SameJSON

func SameJSON(a, b []byte) bool

SameJSON reports whether a and b are the same JSON value, whatever their spacing and key order.

func Tar

func Tar(t testing.TB, files map[string]string) []byte

Tar is a tar stream of regular files, mode 0644, in sorted name order.

func Tree

func Tree(t testing.TB, files map[string]string) string

Tree is a new temporary directory holding files, as WriteTree writes them.

func Within

func Within(d time.Duration, cond func() bool) bool

Within polls cond until it holds or d has passed, and reports whether it held.

func WriteTree

func WriteTree(t testing.TB, dir string, files map[string]string)

WriteTree writes files, slash-separated paths relative to dir mapped to their contents, creating the directories they need.

Types

type Result

type Result struct {
	Stdout, Stderr string
	Exit           int
}

A Result is what a program run with GoRun printed and its exit code.

func GoRun

func GoRun(t testing.TB, dir, pkg string, env map[string]string) Result

GoRun runs `go run pkg` in dir (the test's directory when empty) with exactly the environment env, and returns what it printed and go run's exit code: 1 for a program that failed, whose own status go run reports on stderr as "exit status N". It skips the test when go is not on PATH.

Jump to

Keyboard shortcuts

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