gotest

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package gotest runs `go test -json` and folds its event stream into the aggregated model in core (core.Aggregator / core.PackageResult).

Two things it buys over capturing `go test -v` console text:

  • Attribution. Every JSON event carries its own Package/Test, so results stay correct when several packages run in parallel and their output interleaves. Nothing is guessed after the fact.
  • Unambiguous re-runs. A run is always package-qualified: RerunTest takes the package the test actually lives in and an anchored -run pattern, so two packages holding a test of the same name no longer collide the way a bare name across `./...` does.

The package is UI-agnostic: it starts a process and calls back with parsed events, nothing else.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Args

func Args(o Options) []string

Args returns the argv (minus the go binary itself) for o. Pure — it starts nothing — so the exact command line can be echoed into a build log and asserted in tests.

func RunRegexp

func RunRegexp(test string) string

RunRegexp builds an anchored -run pattern for one test or subtest path.

`go test -run` splits its pattern on "/" and matches each element against the corresponding level of the test name, so a subtest path has to be anchored element by element: "TestA/sub" becomes `^TestA$/^sub$`. Each element is quoted, so a table-driven subtest named "a+b" matches literally instead of as a regexp.

Types

type Options

type Options struct {
	// Packages are the package arguments: import paths ("github.com/uk0/silk/gui")
	// or relative patterns ("./gui/"). Empty means "./..." — the whole module.
	Packages []string
	// Run is the -run regexp. Empty omits the flag. Build it with RunRegexp so
	// the anchoring matches go test's per-segment matching.
	Run string
	// Count1 adds -count=1. A re-run needs it: without it the test cache
	// replays the previous result instead of executing the test again.
	Count1 bool
}

Options describes one `go test -json` invocation.

func RerunFailed

func RerunFailed(agg *core.Aggregator) []Options

RerunFailed returns one Options per package that needs re-running after a run, in the packages' first-seen order. Each entry is package-qualified and carries -count=1 so cached passes cannot mask the re-run.

Failing subtests are folded into their top-level test: -run's element-wise matching cannot express "these particular subtests under those particular parents" in a single pattern, and a subtest failure always fails its parent too, so collapsing to the root name loses no failure. A package that failed without any failing test — a build failure, or a panic that took the binary down before any result — is re-run whole, with no -run filter.

func RerunTest

func RerunTest(pkg, test string) Options

RerunTest returns Options that re-run exactly one test in exactly one package — the package-qualified form. An empty pkg falls back to the whole module, which is the ambiguous case this exists to avoid; pass the Package from the test's own core.PackageResult.

type Runner

type Runner struct {
	Dir string   // module directory; "" uses the process working directory
	Go  string   // go binary; "" uses "go" from PATH
	Env []string // full child environment; nil inherits the parent's
}

Runner runs `go test -json` in one module directory.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, o Options, onUpdate func(Update)) (*core.Aggregator, error)

Run executes `go test -json` for o, streaming an Update per line, and returns the aggregator holding the folded results. It blocks until the toolchain exits (or ctx is cancelled) and never returns a nil aggregator, so partial results from a cancelled or crashed run are still inspectable.

Failing tests are NOT an error: `go test` exits non-zero for them, and that exit status is swallowed here. Judge the outcome from the aggregator — Counts(), a package Status, or a non-empty BuildOutput(). A non-nil error means the run itself did not complete: the binary could not start, ctx was cancelled (errors.Is(err, context.Canceled) holds), or the stream could not be read.

type Update

type Update struct {
	Event core.TestEvent
	Raw   string
	Agg   *core.Aggregator
}

Update is one streaming notification, delivered once per line of the event stream after that line has been folded into Agg.

Event.Action is empty when the line was not a JSON event (a build error from an older toolchain, or a diagnostic the go command wrote to stderr); such lines land in Agg.BuildOutput().

Agg is the live aggregator, not a copy. Callbacks are serialized and run on the runner's reader goroutine, so reading it inside the callback is safe; handing state to another thread (a GUI thread, say) requires Agg.Clone().

Jump to

Keyboard shortcuts

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