coverage

package
v0.0.0-...-3167200 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateAnnotations

func GenerateAnnotations(result *AnalysisResult) []*github.Annotation

GenerateAnnotations converts analysis result to GitHub Check Run annotations. Uses github.GroupIntoRanges to merge consecutive lines into ranges. Returns annotations with "notice" level as per GitHub Check Run API format.

func GetAddedLinesByFile

func GetAddedLinesByFile(fileDiffs []*FileDiff) map[string][]int

GetAddedLinesByFile returns a map of filename to added line numbers. The filename is normalized to use the new name (after any renames). Binary files, deleted files, and non-Go files are excluded.

func NormalizeFilename

func NormalizeFilename(filename string) string

NormalizeFilename removes the a/ or b/ prefix from diff filenames.

func SerializeProfiles

func SerializeProfiles(profiles []*Profile) ([]byte, error)

SerializeProfiles converts profiles back to standard Go coverage format. This is used when saving merged coverage data.

func ValidateProfile

func ValidateProfile(p *Profile) error

ValidateProfile checks if a coverage profile is well-formed. It returns an error if the profile has invalid block data.

Types

type AnalysisResult

type AnalysisResult struct {
	// UncoveredByFile maps filenames to their uncovered line numbers
	UncoveredByFile map[string][]int
	// TotalLines is the total number of instrumented lines across all profiles
	TotalLines int
	// TotalCovered is the total number of covered lines across all profiles
	TotalCovered int
	// DiffAddedLines is the total number of lines added in the diff
	DiffAddedLines int
	// DiffAddedCovered is the total number of covered lines among added lines
	DiffAddedCovered int
}

AnalysisResult contains the results of coverage analysis.

func AnalyzeCoverage

func AnalyzeCoverage(profiles []*Profile, addedLinesByFile map[string][]int) *AnalysisResult

AnalyzeCoverage cross-references coverage profiles with diff to find uncovered added lines. Coverage profiles are the primary source - we extract uncovered lines from them, then filter by the diff to only report lines that were added. It takes coverage profiles and a map of added lines by file (from GetAddedLinesByFile). Returns an AnalysisResult with uncovered lines grouped by file.

func (*AnalysisResult) GetSortedFiles

func (r *AnalysisResult) GetSortedFiles() []string

GetSortedFiles returns a sorted list of files with uncovered lines. Useful for consistent output ordering.

func (*AnalysisResult) HasUncoveredLines

func (r *AnalysisResult) HasUncoveredLines() bool

HasUncoveredLines returns true if there are any uncovered lines in the result.

type CoverageComparison

type CoverageComparison struct {
	BaseCoverage float64
	HeadCoverage float64
	Delta        float64 // positive = improvement, negative = regression
	Decreased    bool
}

CoverageComparison holds the result of comparing two coverage reports.

func CompareCoverage

func CompareCoverage(base, head *CoverageStats) *CoverageComparison

CompareCoverage compares base and head coverage stats. Returns a comparison showing the delta between base and head. If base is nil, it's treated as 0% coverage (first coverage report).

type CoverageStats

type CoverageStats struct {
	TotalStatements   int
	CoveredStatements int
	Percentage        float64
	ByFile            map[string]*FileCoverage
}

CoverageStats holds coverage statistics.

func CalculateCoverageStats

func CalculateCoverageStats(profiles []*Profile) *CoverageStats

CalculateCoverageStats calculates coverage statistics from profiles. It computes overall coverage percentage and per-file breakdown. Coverage is calculated as: (covered statements / total statements) * 100

type FileCoverage

type FileCoverage struct {
	FileName          string
	TotalStatements   int
	CoveredStatements int
	Percentage        float64
}

FileCoverage holds coverage statistics for a single file.

type FileDiff

type FileDiff struct {
	// OldName is the original filename (a/path/to/file)
	OldName string
	// NewName is the new filename (b/path/to/file)
	NewName string
	// AddedLines contains the line numbers that were added in this diff
	AddedLines []int
	// IsBinary indicates if this is a binary file
	IsBinary bool
	// IsRenamed indicates if the file was renamed
	IsRenamed bool
	// IsDeleted indicates if the file was deleted
	IsDeleted bool
}

FileDiff represents the changes to a single file in a diff.

func ParseDiff

func ParseDiff(diffData []byte) ([]*FileDiff, error)

ParseDiff parses a unified diff format and returns the files and their added lines. The diff should be in the format produced by `git diff` or GitHub's diff output.

type Profile

type Profile struct {
	FileName string
	Mode     string
	Blocks   []ProfileBlock
}

Profile represents a single coverage profile for a file.

func MergeProfiles

func MergeProfiles(profiles []*Profile) ([]*Profile, error)

MergeProfiles merges multiple coverage profiles into a single profile. It implements the gocovmerge algorithm, which merges coverage blocks at the block level, handling overlapping coverage from multiple test runs.

The algorithm: 1. Groups profiles by file name 2. For each file, merges all blocks using additive merging 3. Uses the mode from the first profile (all profiles must use same mode) 4. Returns merged profiles sorted by file name

func ParseProfiles

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

ParseProfiles parses coverage data in standard Go coverage format. It returns a slice of Profile structs representing the coverage data.

func ParseProfilesFromFile

func ParseProfilesFromFile(path string, data []byte) ([]*Profile, error)

ParseProfilesFromFile parses coverage data from a file path. This is primarily used when reading coverage files directly.

func ParseProfilesFromZip

func ParseProfilesFromZip(zipData []byte) ([]*Profile, error)

ParseProfilesFromZip extracts and parses all coverage files from a zip archive. It looks for files matching common coverage patterns (*.out, *.cov, coverage.txt). Returns all parsed profiles from all coverage files found in the archive.

type ProfileBlock

type ProfileBlock struct {
	StartLine int
	StartCol  int
	EndLine   int
	EndCol    int
	NumStmt   int
	Count     int
}

ProfileBlock represents a single block of code coverage.

Jump to

Keyboard shortcuts

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