coverage

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildFlags

func BuildFlags(testFlags []string) []string

BuildFlags returns the build flags among testFlags, read as `go test` reads them: up to -args or `--`, with a value given as the next field. `go test -c` can't take the rest: it rejects any flag it doesn't know, such as a property framework's -rapid.checks=100, where `go test` would hand it to the test binary. Test flags reach the binary through testArgs instead.

func GOROOT

func GOROOT(ctx context.Context, projectDir string) (string, error)

GOROOT returns the GOROOT of the toolchain `go` builds projectDir's tests with. It is asked from projectDir, so a go.mod toolchain line or GOTOOLCHAIN that switches toolchains names the one switched to.

func LineKey

func LineKey(file string, line int) string

LineKey returns the coverage map's key for line in file, a coverage profile path such as a mutant's CoverageFile.

func RunPattern

func RunPattern(tests []string) string

RunPattern returns a -run regex pattern that matches exactly the given tests.

func TestBinaryArgs

func TestBinaryArgs(ctx context.Context, projectDir, tags, pkg string, flags []string) ([]string, error)

TestBinaryArgs returns the arguments `go test` passes a test binary for the given flags. It reads them from `go test -n`, which prints the binary's command line without building or running anything, so build flags, test flags (rewritten to -test.X) and -args are split exactly as `go test` splits them for the mutant runs.

func TestBinaryEnv

func TestBinaryEnv(goroot, dir string) []string

TestBinaryEnv returns the environment `go test` runs a test binary in from dir: gomutants' own, with goroot's bin directory first on PATH, so a test that runs `go` gets the toolchain that built it rather than whichever is first on PATH, and with PWD set to dir, which exec sets only for a command without an Env of its own. An empty goroot leaves PATH alone, as `go test` does.

Types

type Block

type Block struct {
	File      string
	StartLine int
	StartCol  int
	EndLine   int
	EndCol    int
	NumStmt   int
	Count     int
}

Block represents a single coverage block from a Go coverage profile.

type BuildOptions

type BuildOptions struct {
	// CoverPkg is the -coverpkg value the test binaries are built with;
	// empty means each package records coverage of itself only.
	CoverPkg string
	// Tags is the -tags value for resolving and compiling packages.
	Tags string
	// TmpDir holds the compiled test binaries and per-worker profiles.
	TmpDir string
	// Workers is the number of tests run in parallel.
	Workers int
	// TestTimeout bounds every run of a compiled test binary (listing and
	// each per-test run); zero means no bound. A test that hangs when run
	// alone would otherwise block the coverage phase forever: unlike
	// `go test`, a bare test binary has no default timeout.
	TestTimeout time.Duration
	// TestFlags are the flags the mutant runs pass to `go test` after the
	// package (the user's --test-flags, plus -short when the runner adds
	// it), so the coverage runs build and run tests the same way.
	TestFlags []string
	// Lines holds the positions (see LineKey) of the mutants to be
	// tested: only the groups of tests covering them are checked to pass
	// together (see checkGroups). Nil checks every covered line's.
	Lines map[string]bool
	// GOROOT is the toolchain's (see GOROOT), whose bin directory every
	// run of a test binary gets first on PATH, as under `go test` (see
	// TestBinaryEnv); empty leaves PATH alone.
	GOROOT string
}

BuildOptions configures BuildTestMap.

type Package

type Package struct {
	ImportPath string
	Dir        string
}

Package is a package in the coverage map's scope that has tests.

type Profile

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

Profile holds parsed coverage data indexed for fast lookup.

func ParseBytes

func ParseBytes(data []byte) (*Profile, error)

ParseBytes parses a Go coverage profile already in memory. Lets callers reusing a cached profile from disk JSON skip the temp-file round-trip ParseFile requires.

func ParseFile

func ParseFile(path string) (*Profile, error)

ParseFile parses a Go coverage profile file. Format: mode line, then "file:startLine.startCol,endLine.endCol numStmt count"

func (*Profile) HasCoveredBlock

func (p *Profile) HasCoveredBlock(file string) bool

HasCoveredBlock reports whether the file has at least one block with count > 0. Used to distinguish files that tests exercise (where gaps in instrumentation should be treated as covered) from files no test touches at all.

func (*Profile) IsCovered

func (p *Profile) IsCovered(file string, line, col int) bool

IsCovered returns true if the given position (file, line, col) falls within a coverage block with count > 0.

func (*Profile) IsInAnyBlock

func (p *Profile) IsInAnyBlock(file string, line, col int) bool

IsInAnyBlock reports whether the position falls within any block — covered or not. Useful for distinguishing positions the coverage tool instrumented but recorded as Count==0 (real coverage gap) from positions it did not instrument at all (package-level const/var initializers, select-case receive expressions, gaps between adjacent blocks on the same line, etc.).

type TestMap

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

TestMap maps (file, line) positions to the test functions that cover them.

Concurrency: TestMap is built sequentially in BuildTestMap (the receive loop is single-goroutine even though the producers are parallel) and is treated as immutable thereafter. The runner's per-mutant Worker.Test reads it concurrently across N workers without locking — that's safe only because the build phase completes before any worker reads. Don't expose new methods that mutate TestMap state after BuildTestMap returns, or the lock-free read assumption breaks.

func BuildTestMap

func BuildTestMap(ctx context.Context, projectDir string, packages []string, opts BuildOptions) (*TestMap, error)

BuildTestMap compiles each package's test binary once, lists the tests each binary contains, then runs every test function alone against its binary with coverage. Uses parallel workers.

func NewTestMapForTesting

func NewTestMapForTesting(perTest map[[2]string]time.Duration, coverIndex map[string][]TestRef) *TestMap

NewTestMapForTesting constructs a TestMap directly from raw timing data and a "file:line" → tests cover index. Exposed only because the runner-package timeout selector needs to be exercised against hand-built fixtures without spinning up `go test` to populate a real map. Production code must use BuildTestMap.

perTest: keys are [pkg, name] pairs; the helper aggregates them into the internal (pkg, name)→duration map.

coverIndex: keys are "file:line"; values are the covering tests as (pkg, name) references, mirroring the package-aware index BuildTestMap produces.

func (*TestMap) SuiteDir

func (tm *TestMap) SuiteDir(pkg string) (string, bool)

SuiteDir returns the directory of pkg, one of the map's suites, and whether the map holds it. Suites are sorted by import path, so the lookup copies nothing.

func (*TestMap) SuitePkgs

func (tm *TestMap) SuitePkgs(pkg string) []Package

SuitePkgs returns the packages whose whole test suites decide whether a mutant in pkg lives, sorted by import path: those in the map's scope whose tests can run code in pkg. Without -coverpkg a test covers only its own package, so that is pkg itself when it has tests; with it, every package whose test binary links pkg.

The map's covering tests are only the likeliest killers, run first because they are cheap. A test can cover a line in package order but not alone — it fails, skips or hangs without an earlier test's setup, or its name was lost from the listing — so a mutant they don't kill lives only once these suites pass with it in place.

func (*TestMap) Suites

func (tm *TestMap) Suites() []Package

Suites returns every package in the map's scope that has tests, sorted by import path: each one SuitePkgs can return.

func (*TestMap) SumDurationsFor

func (tm *TestMap) SumDurationsFor(pkg string, tests []string) (time.Duration, bool)

SumDurationsFor returns the total observed wall-time of `tests` within `pkg` (sum of per-test timings recorded during the coverage build), and reports whether every requested test had a recorded timing.

Callers use the returned `complete` flag to decide whether the sum is trustworthy: if any test was missing (e.g. it failed alone during the coverage build and was left out, or the test was added after the build) the caller should fall back to a coarser deadline.

On a nil TestMap, returns (0, false). Empty tests slice short-circuits to (0, false) so the fallback is reached without a misread of "all 0 of 0 tests had data → complete=true → use 0 timeout".

func (*TestMap) SumDurationsForRefs

func (tm *TestMap) SumDurationsForRefs(refs []TestRef) (time.Duration, bool)

SumDurationsForRefs is the cross-package analogue of SumDurationsFor: it sums the recorded wall-time of the given (pkg, name) references and reports whether every reference had a recorded timing. Empty input or any missing timing yields (0, false) so the caller degrades to the global ceiling rather than trusting a partial or zero sum.

func (*TestMap) TestRefsFor

func (tm *TestMap) TestRefsFor(file string, line int) []TestRef

TestRefsFor returns the (pkg, name) references of the tests covering the given position. Returns nil if no mapping exists (caller should run all tests). The runner uses this to preserve package context across packages.

func (*TestMap) TestsFor

func (tm *TestMap) TestsFor(file string, line int) []string

TestsFor returns the test function names that cover the given position, deduplicated across packages. Returns nil if no mapping exists (caller should run all tests). Used by the package-agnostic cache resolver; the runner uses TestRefsFor to keep package context.

func (*TestMap) Warnings

func (tm *TestMap) Warnings() []string

Warnings returns what went wrong building the map that didn't fail it, in the order it happened: each costs the run speed, never a kill, but the user should know why it is slower than the map promises.

func (*TestMap) WithSuitesForTesting

func (tm *TestMap) WithSuitesForTesting(crossPkg bool, deps map[string]map[string]bool, suites ...Package) *TestMap

WithSuitesForTesting returns a copy of tm whose scope is `suites`, with crossPkg as whether their tests may cover mutants outside themselves and deps as what their test binaries link (see SuitePkgs). Exposed for the runner's routing tests, like NewTestMapForTesting. tm itself is left as it is: a built TestMap is read without locks.

func (*TestMap) WithWarningsForTesting

func (tm *TestMap) WithWarningsForTesting(warnings ...string) *TestMap

WithWarningsForTesting returns a copy of tm whose Warnings are exactly `warnings`. Exposed for main's tests, like NewTestMapForTesting.

type TestRef

type TestRef struct {
	Pkg  string
	Name string
}

TestRef identifies a single covering test by its package import path and test function name. Cross-package routing needs the package so a covering test can be dispatched to a `go test` invocation against its own package.

Jump to

Keyboard shortcuts

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