Help us shape the future of Go! Take the 2026 Go Developer Survey

testutil

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package testutil provides shared test fixtures and harness helpers:

  • Materialize copies testdata/fixture-repo into a temp dir and turns it into a git repository with tracked, untracked, ignored, vendored and oversized files.
  • Synth generates deterministic synthetic repositories for benchmarks.
  • CanonicalDump renders graph.db as sorted text keyed by stable identities (path, qname, kind) instead of row ids, so an incremental build can be compared with a from-scratch build.

Index

Constants

View Source
const (
	// BigFilePath is a >1 MiB source file created by Materialize and
	// committed in the second commit (so it is tracked but oversized).
	BigFilePath = "python/shop/catalog_big.py"
	// GeneratedPath carries a "Code generated ... DO NOT EDIT." header.
	GeneratedPath = "go/internal/gen/status_string.go"
	// MinifiedPath is a minified JavaScript bundle. The default *.min.*
	// config exclude drops it before the content filters run.
	MinifiedPath = "js/public/widgets.min.js"
)

Well-known paths inside a materialized fixture (repo-relative, slash form).

Variables

This section is empty.

Functions

func CanonicalDump

func CanonicalDump(db *sql.DB) string

CanonicalDump renders the graph stored in db as sorted text that does not depend on row ids or stat data, so two databases describing the same graph produce identical dumps. It panics on query errors; use CanonicalDumpE to handle them.

func CanonicalDumpE

func CanonicalDumpE(db *sql.DB) (string, error)

CanonicalDumpE is CanonicalDump returning errors instead of panicking. Missing tables are reported as "(absent)" sections rather than errors.

func ExpectedEdgesPath

func ExpectedEdgesPath() string

ExpectedEdgesPath is the location of the fixture's edge expectations.

func FixtureDir

func FixtureDir() string

FixtureDir returns the absolute path of testdata/fixture-repo.

func FixturePaths

func FixturePaths() ([]string, error)

FixturePaths returns the sorted repo-relative paths of the committed fixture files, i.e. what Materialize puts in the first commit.

func Git

func Git(t testing.TB, dir string, args ...string) string

Git runs git in dir with an isolated environment (no global or system config) and returns stdout with trailing newlines trimmed. It fails the test on error.

func IgnoredPaths

func IgnoredPaths() []string

IgnoredPaths returns the sorted files ignored via .gitignore rules.

func Materialize

func Materialize(t testing.TB) string

Materialize builds a git repository from the fixture in t.TempDir() and returns its root. The resulting layout is:

  • commit 1: every file of testdata/fixture-repo
  • commit 2: VendorPaths() and BigFilePath
  • untracked (not ignored): UntrackedPaths()
  • ignored: IgnoredPaths()

Commits use a fixed identity and date, so HEAD is identical across runs.

func MaterializeSynth

func MaterializeSynth(t testing.TB, seed int64, files, locPerFile int, langs []model.Lang) string

MaterializeSynth writes Synth(seed, files, locPerFile, langs) into t.TempDir(), commits everything to a fresh git repository and returns its root.

func MaterializeTo

func MaterializeTo(root string) error

MaterializeTo builds the same repository as Materialize in root, which must be empty or missing (benchmarks and manual end-to-end runs).

func UntrackedPaths

func UntrackedPaths() []string

UntrackedPaths returns the sorted untracked, non-ignored files.

func VendorPaths

func VendorPaths() []string

VendorPaths returns the sorted vendored files tracked by Materialize.

Types

type ExpectedEdge

type ExpectedEdge struct {
	Section  string // "[lang]" header the line belongs to
	Line     int    // 1-based line number in the file
	Optional bool   // "?" prefix: desirable, logged when missing
	Negative bool   // "!" prefix: must not exist
	Kind     model.EdgeKind
	Src, Dst string
	MinConf  int
}

ExpectedEdge is one line of testdata/fixture-repo/expected_edges.txt:

[prefix]<kind> <src> -> <dst> [conf>=N]

Src and Dst are "path::Chain" (container chain joined with "."), a bare path (imports/includes) or "external:<qual.name>".

func LoadExpectedEdges

func LoadExpectedEdges(section string) ([]ExpectedEdge, error)

LoadExpectedEdges parses the expectations of one section, or of every section when section is "".

func ParseExpectedEdge

func ParseExpectedEdge(line string) (ExpectedEdge, error)

ParseExpectedEdge parses one expectation line (without section or line number).

func (ExpectedEdge) External

func (e ExpectedEdge) External() (name string, ok bool)

External reports whether Dst names a target outside the indexed code and returns its simple name (the part after the last "." or ":").

func (ExpectedEdge) String

func (e ExpectedEdge) String() string

type SynthFile

type SynthFile struct {
	Path    string // repo-relative, slash-separated
	Content []byte
}

SynthFile is one generated file.

type SynthRepo

type SynthRepo struct {
	// contains filtered or unexported fields
}

SynthRepo describes a deterministic synthetic repository. Content is generated on demand: file contents depend only on (seed, parameters, language, file index), never on generation order.

func Synth

func Synth(seed int64, files, locPerFile int, langs []model.Lang) *SynthRepo

Synth describes a synthetic repository of `files` source files spread round-robin over langs, each about locPerFile lines long (never shorter; longer only when locPerFile is below the size of one function plus header). Every function calls a function in another file of its package, one in a lower-numbered package (or its own package for package 0) and one in its own file, using each language's real import mechanism.

Manifests and package-init files (go.mod, __init__.py, mod.rs, C headers, ...) are emitted in addition to the source files. Synth panics on invalid arguments, since it is a test helper.

func (*SynthRepo) Files

func (r *SynthRepo) Files() []SynthFile

Files returns sources plus support files (manifests, headers, package init files), sorted by path.

func (*SynthRepo) SourceCount

func (r *SynthRepo) SourceCount() int

SourceCount returns the number of generated source files.

func (*SynthRepo) Sources

func (r *SynthRepo) Sources() []SynthFile

Sources generates every source file, sorted by path.

func (*SynthRepo) WriteDir

func (r *SynthRepo) WriteDir(dir string) error

WriteDir writes all files under dir, in parallel.

Jump to

Keyboard shortcuts

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