testutil

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package testutil holds the fixture helpers the package tests share: the repo root (for tests that read the checked-in tree), the mkdir-then-write fixture writer, the drift gate (drift.go) and the golden compare with its -update rewrite. Test-only by convention: nothing outside *_test.go imports it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FirstDiff

func FirstDiff(want, got string) string

FirstDiff renders the first line where want and got differ.

func Golden

func Golden(t *testing.T, path, got string, update bool)

Golden compares got with the golden file at path. With update set it rewrites the file with got instead and reports nothing. The message names the command that rewrites the golden.

func RepoRoot

func RepoRoot(t *testing.T) string

RepoRoot walks up from the test's working directory to the repository root — the directory that carries the frozen bash tree (`.lok8s/lo`).

func WriteFile

func WriteFile(t *testing.T, path, content string)

WriteFile writes a fixture, creating its directory first; any failure fails the test.

Types

type Drift

type Drift struct {
	// Want is the canonical side. Got is the twin that must match it.
	Want, Got Tree
	// Sync names the command that brings Got in line with Want. It is
	// printed with every difference.
	Sync string
	// SyncBack names the command that brings Want in line with Got. It is
	// printed for an entry that exists only in Got. Empty means Sync.
	SyncBack string
	// Subset makes an entry that exists only in Got a non-difference.
	Subset bool
	// Max caps the number of differences reported. Zero means 20.
	Max int
}

Drift compares two Trees and fails the test on every difference.

func (Drift) Check

func (d Drift) Check(t *testing.T) int

Check reports the differences through t.Errorf, at most d.Max of them, and returns the total number found.

func (Drift) Diffs

func (d Drift) Diffs() []string

Diffs lists every difference, one message per entry, in key order. A side with no entries is reported first: two empty trees agree on nothing, so an empty read (a wrong root, a skip func that drops everything) must not pass as "no drift".

type Tree

type Tree struct {
	Name  string
	Files map[string]string
}

Tree is one side of a drift comparison: a name for the messages and the entries it holds. A key is a slash-separated relative path (or any other identifier, such as a module path); a value is the content to compare.

func ReadDir

func ReadDir(t *testing.T, name, dir string, skip func(rel string) bool) Tree

ReadDir reads every file under dir into a Tree keyed by slash path relative to dir. skip, when set, drops the paths it returns true for.

func ReadFS

func ReadFS(t *testing.T, name string, fsys fs.FS, skip func(rel string) bool) Tree

ReadFS reads every file under fsys into a Tree keyed by slash path. skip, when set, drops the paths it returns true for.

Jump to

Keyboard shortcuts

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