Expand description
Go -coverprofile parser (line coverage only).
go test -coverprofile writes a mode: set|count|atomic header followed by
one record per instrumented block:
mode: set
example.com/m/pkg/a.go:12.34,15.2 3 1That is file:startLine.startCol,endLine.endCol numStmts count. A block is a
source range, not a line, so each block is expanded to every line it spans and
the line takes the block’s count. Where blocks overlap the larger count wins,
which is the model’s rule for any line several regions cover
(FileCoverage::record). A line shared by two blocks (} else {) therefore
reads covered when either ran: the blocks are sub-line spans and the model is
per line.
A block with no statements (an empty function body) holds nothing to execute,
so it is not an executable line. The total is the number of distinct lines
some block spans, not the statement-weighted figure go tool cover -func
reports.
The file names are Go import paths (example.com/m/pkg/a.go), not paths on
disk; module_path() reads the module an import path is rooted at so the
caller can make them repo-relative.
Reference: https://go.dev/blog/cover and cmd/cover’s profile reader.
Functions§
- module_
path - Returns the module path declared by a
go.mod, orNonewhen it has nomoduledirective. - parse
- Parses a Go coverprofile into a
CoverageReportkeyed by import path.