covreport

package
v0.69.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package covreport turns Go coverage profiles into a per-file Markdown table for the PR coverage comment (see .github/workflows/coverage.yml).

Go's `go tool cover -func` reports per-function statement coverage and an aggregate total, but not a per-file covered/total breakdown. This package parses the raw profile itself — summing statement counts per file — so the coverage comment can show which files have the most untested code (sorted by uncovered statement count, most first, the most actionable) with a delta measured against the PR base.

Coverage in Go is statement-based, not line-based, so the counts here are statements. The rendered table labels them accordingly rather than claiming "lines".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ModulePath

func ModulePath(gomod string) string

ModulePath reads the module path from a go.mod file (the token after the leading `module` directive). It is best-effort: on any read error or a file without a module line it returns an empty string, which makes Parse leave import paths unstripped rather than fail the whole report.

func Parse

func Parse(r io.Reader, module string) (map[string]*FileCov, error)

Parse reads a Go coverage profile and returns per-file statement counts keyed by repo-relative path. The module prefix (from go.mod) is stripped from the profile's import-path-qualified file names so the table shows repo-relative paths. The leading "mode:" line and blank lines are skipped.

Profile block lines have the form:

<import-path>/file.go:startLine.startCol,endLine.endCol numStatements count

A block counts toward Covered when its execution count is non-zero.

func RenderTable

func RenderTable(rows []Row, baseAvailable bool) string

RenderTable renders rows as a GitHub-flavoured Markdown table. When baseAvailable is false (the base commit failed to build/test) the delta column is filled with "—" rather than misleading per-file deltas; when it is true, a file absent from the base shows "new". With no rows it returns a short italic placeholder so the caller can still embed something meaningful.

Types

type FileCov

type FileCov struct {
	Path    string // repo-relative path, e.g. internal/daemon/handler.go
	Covered int
	Total   int
}

FileCov accumulates covered and total statement counts for one file.

func (*FileCov) Pct

func (f *FileCov) Pct() float64

Pct is the statement-coverage percentage. A file with no statements reports 0 rather than dividing by zero.

type Row

type Row struct {
	Path    string
	Pct     float64
	Covered int
	Total   int
	Delta   float64 // Pct(head) - Pct(base); meaningful only when InBase
	InBase  bool    // false => new file (no base measurement)
}

Row is one file's line in the rendered table, including its delta against the base branch.

func BuildRows

func BuildRows(head, base map[string]*FileCov) []Row

BuildRows joins head and base per-file coverage into table rows, sorted by most uncovered statements first (ties broken by path for stable output). Ordering by absolute uncovered statement count — not coverage percentage — surfaces the files where writing tests buys the most: a 90%-covered 500-statement file has more untested code (and more risk) than a 50%-covered 4-statement file, yet a percentage sort would bury it. Only files present in head are listed — a file that existed at base but was deleted in the PR no longer has coverage to report.

Jump to

Keyboard shortcuts

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