goldens

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package goldens provides a uniform golden-file assertion helper for tests that compare byte-for-byte output against stored fixtures.

Usage

func TestFoo(t *testing.T) {
    got := render(...)
    goldens.Assert(t, "testdata/foo.golden", got)
}

On the first run (no fixture file) the test fails with a clear message instructing the developer to run with -update:

go test -run TestFoo -update

The -update flag (or GOGRAPH_UPDATE_GOLDENS=1) causes Assert to overwrite the fixture file with the current output and mark the test as passed. The write is atomic (temp file + rename) so an interrupted run never corrupts an existing golden.

Path resolution

path is resolved relative to the caller's package directory (using runtime/debug to locate the file). Absolute paths are used as-is; relative paths are resolved against the directory that contains the calling test file (i.e. the package root from the test's perspective).

Concurrency

Assert and UpdateRequested are safe for concurrent use; the flag is read once at package init time and never modified.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

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

Assert compares got against the golden file at path. If the file does not exist or its content differs from got, the test fails with a unified diff. When -update or GOGRAPH_UPDATE_GOLDENS=1 is set the file is overwritten with got and the test continues.

path may be absolute or relative. Relative paths are resolved relative to the directory of the calling test's source file, which is equivalent to the package root under which the test runs.

func UpdateRequested

func UpdateRequested() bool

UpdateRequested reports whether the test binary was started with -update or the environment variable GOGRAPH_UPDATE_GOLDENS=1. It is safe to call before flag.Parse (e.g. from TestMain); the environment variable path is always available.

Types

This section is empty.

Jump to

Keyboard shortcuts

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