golden

package
v0.0.0-...-638dac1 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package golden provides a framework for writing file-based golden tests.

The primary entry-point is Corpus. Define a new corpus in an ordinary Go test body and call Corpus.Run to execute it.

Corpora can be "refreshed" automatically to update the golden test corpus with new data generated by the test instead of comparing it. To do this, run the test with the environment variable that Corpus.Refresh names set to a file glob for all test files to regenerate expectations for.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareAndDiff

func CompareAndDiff(got, want string) string

CompareAndDiff is a CompareFunc that returns a colorized diff of the two strings if they are not equal.

Types

type CompareFunc

type CompareFunc func(got, want string) string

CompareFunc is a comparison function between strings, used in Output.

Returns empty string if the strings match, otherwise returns an error message.

type Corpus

type Corpus struct {
	// The root of the test data directory. This path is relative to the directory of
	// the file that calls [Corpus.Run].
	Root string

	// An environment variable to check with regards to whether to run in "refresh"
	// mode or not.
	Refresh string

	// The file extensions (without a dot) of files which define a test case,
	// e.g. "proto".
	Extensions []string

	// Possible outputs of the test, which are found using Outputs.Extension.
	// If the file for a particular output is missing, it is implicitly treated
	// as being expected to be empty (i.e., if the file Output[n].Extension
	// specifies does not exist, then Output[n].Compare is passed the empty string
	// as the "want" value).
	Outputs []Output
}

A corpus describes a test data corpus. This is essentially a way for doing table-driven tests where the "table" is in your file system.

func (Corpus) Run

func (c Corpus) Run(t *testing.T, test func(t *testing.T, path, text string, outputs []string))

Run executes a golden test.

The test function executes a single test case in the corpus, and writes the results to the entries of output, which will be the same length as Corpus.Outputs.

test should write to outputs as early as possible to ensure that, if test panics, successfully created test output can still be shown to the user.

type Output

type Output struct {
	// The extension of the output. This is a suffix to the name of the
	// testcase's main file; so if Corpus.Extension is "proto", and this is
	// "stderr", for a test "foo.proto" the test runner will look for files
	// named "foo.proto.stderr".
	Extension string

	// The comparison function for this output. If nil, defaults to
	// [CompareAndDiff].
	Compare CompareFunc
}

Output represents the output of a test case.

Jump to

Keyboard shortcuts

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