coverage

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package coverage is the single home for Go line-coverage attribution in codegrapher: it turns a `go test -coverprofile` profile into per-file and per-function coverage records, and defines the INGR recordset format those records are exported/uploaded/served in.

One library, several callers:

  • the `codegrapher coverage` CLI runs Ingest locally on the same checkout the tests ran on (guaranteed matching commit) and emits the recordsets;
  • the server validates uploaded recordsets via Validate and stores them next to graph data;
  • any other consumer may call Ingest directly against an open *store.Store.

Scope (see SPEC.md): Go only, line coverage only. Per-function counts are attributed to the INNERMOST enclosing node (non-overlapping); inclusive roll-ups (a function plus its nested closures) are computed by consumers via the graph's `contains` edges, not stored here.

Index

Constants

View Source
const (
	RecordsetCoverage     = "coverage"
	RecordsetNodeCoverage = "node_coverage"
)

Recordset names. These are the INGR collection identifiers used on disk ({name}.ingr), in the snapshot manifest counts, and on the wire when the CLI uploads to the server's collect endpoint.

View Source
const (
	KindHit  = "hit"
	KindMiss = "miss"
)

Variables

This section is empty.

Functions

func EncodeFileCoverage

func EncodeFileCoverage(w io.Writer, recs []FileCoverage) error

EncodeFileCoverage writes recs as the "coverage" INGR recordset, sorted by file path for byte-determinism.

func EncodeNodeCoverage

func EncodeNodeCoverage(w io.Writer, recs []NodeCoverage) error

EncodeNodeCoverage writes recs as the "node_coverage" INGR recordset, sorted by node id for byte-determinism.

func Pct

func Pct(covered, uncovered int) float64

Pct returns the covered percentage for the given line counts, or 0 when there are no measured lines.

func Validate

func Validate(recordset string, data []byte) error

Validate checks that data is a well-formed INGR recordset for the named collection ("coverage" or "node_coverage"): decodable, every record has a non-empty $ID and content_hash, non-negative counts, and (for coverage) parseable ranges with valid kinds. The server collect endpoint calls this on uploaded bytes before storing them.

Types

type FileCoverage

type FileCoverage struct {
	FilePath       string  `json:"filePath"`
	ContentHash    string  `json:"contentHash"`
	Mode           string  `json:"mode"` // go profile mode: set | count | atomic
	Ranges         []Range `json:"ranges"`
	LinesCovered   int     `json:"linesCovered"`
	LinesUncovered int     `json:"linesUncovered"`
	PctCovered     float64 `json:"pctCovered"`
	RunAt          int64   `json:"runAt"` // unix ms
}

FileCoverage is per-file line coverage from one ingest run. ContentHash is the file's hash at ingest time; a later mismatch against the live file marks this record stale (consumers keep + flag it, never drop). PctCovered is derived from the line counts, not stored in the recordset.

func DecodeFileCoverage

func DecodeFileCoverage(r io.Reader) ([]FileCoverage, error)

DecodeFileCoverage reads a "coverage" INGR recordset. PctCovered is recomputed from the line counts.

func FileCoverageFromStore

func FileCoverageFromStore(st *store.Store) ([]FileCoverage, error)

FileCoverageFromStore reads every per-file coverage row from st and converts it to []FileCoverage (decoding the stored RLE JSON). Used by the CLI and the snapshot exporter to emit the "coverage" recordset.

type Ingestor

type Ingestor interface {
	Ingest(ctx context.Context, st *store.Store, profile io.Reader, opts Options) (Summary, error)
}

Ingestor parses a Go coverage profile and writes coverage + node_coverage records into st, attributing lines to the innermost enclosing node.

func NewIngestor

func NewIngestor() Ingestor

NewIngestor returns the default Ingestor (the real attribution implementation).

type NodeCoverage

type NodeCoverage struct {
	NodeID         string  `json:"nodeId"`
	ContentHash    string  `json:"contentHash"`
	LinesCovered   int     `json:"linesCovered"`
	LinesUncovered int     `json:"linesUncovered"`
	PctCovered     float64 `json:"pctCovered"`
	RunAt          int64   `json:"runAt"` // unix ms
}

NodeCoverage is innermost-attributed line counts for one function/method node. Lines inside a nested closure count toward that closure, not its parent (non-overlapping). PctCovered is derived, not stored in the recordset.

func DecodeNodeCoverage

func DecodeNodeCoverage(r io.Reader) ([]NodeCoverage, error)

DecodeNodeCoverage reads a "node_coverage" INGR recordset. PctCovered is recomputed from the line counts.

func NodeCoverageFromStore

func NodeCoverageFromStore(st *store.Store) ([]NodeCoverage, error)

NodeCoverageFromStore reads every per-node coverage row from st and converts it to []NodeCoverage. Used by the CLI and snapshot exporter to emit the "node_coverage" recordset.

type Options

type Options struct {
	// Ref is the git ref/branch the profile was produced on (recorded for the
	// snapshot manifest; informational here).
	Ref string
	// Root is the repository root used to resolve profile (module) paths to the
	// repo-relative file paths stored in the graph.
	Root string
	// Now overrides the RunAt clock (unix ms). nil uses the real clock.
	Now func() int64
}

Options configure an Ingest run.

type Range

type Range struct {
	Start int    `json:"start"`
	End   int    `json:"end"`
	Kind  string `json:"kind"`
}

Range is a run-length-encoded span of consecutive lines sharing one coverage state. Start and End are 1-indexed and inclusive. Kind is "hit" or "miss".

type Summary

type Summary struct {
	FilesMatched   int
	FilesSkipped   int // profile files with no matching indexed file
	LinesCovered   int
	LinesUncovered int
	PctCovered     float64
}

Summary reports the outcome of an Ingest run.

Jump to

Keyboard shortcuts

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