coverage-gate

command
v0.20.0 Latest Latest
Warning

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

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

Documentation

Overview

coverage-gate is a diff-aware NEW-CODE coverage gate.

It reads a Go cover profile and a unified diff, then reports any newly-added or modified prod (non-test) Go source lines that have zero coverage. Exits 0 if all new prod lines are covered; exits 1 with a per-file report otherwise.

Usage:

coverage-gate [-rename-threshold N] <cover.out> <diff.patch>

Inputs:

  • cover.out: produced by `go test -coverprofile=cover.out ./...`
  • diff.patch: unified diff, e.g. `git diff base..HEAD > diff.patch` or `gh pr diff <num> > diff.patch`

IMPORTANT — generating the diff:

For best results, generate the diff with `git diff -B5% -M5% -C5%` which surfaces both rename (similarity index) and break-rewrite (dissimilarity index) hunks. Plain `git diff` or `gh pr diff` will not produce these headers, so renames look like delete+create and heavy in-place rewrites look like a full new file — which means the -rename-threshold flag and the dissimilarity-index handling below are effectively dead unless you pass `-B` / `-M` / `-C` when capturing the diff. The `task coverage-gate` target in Taskfile.yml uses the recommended invocation; copy from there if running by hand.

Flags:

  • -rename-threshold N (0..100, default 50): when a `diff --git` block carries a `similarity index N%` header (emitted by `git diff -M` or `git diff -C`), and N is >= threshold, the block's `+` lines are treated as a pure move and excluded from the new-lines set. A `dissimilarity index N%` header (emitted by `git diff -B` for in-place rewrites) is mapped to similarity (100 - N)% and uses the same threshold. 0 = treat any detected rename/copy/rewrite as not-new; 100 = require an exact-content move.

Behavior:

  • Parses cover profile into file → []{startLine, endLine, hits} ranges.
  • Parses diff for added lines in .go files (NOT _test.go, NOT testdata).
  • Skips blank lines and comment-only lines (// or /*-prefixed after trim).
  • For each added prod line that intersects a cover range with hits==0, records it as uncovered.
  • Lines not in any cover range are not statements (decl, blank, comment) and are silently skipped.
  • A `// coverage:ignore` suffix on an added line suppresses the warning for that single line.
  • Pure-move refactors detected via `similarity index` headers (see -rename-threshold) are excluded.
  • In-place rewrites flagged via `dissimilarity index N%` are mapped to similarity (100 - N)% and excluded when that value is at or above -rename-threshold.

Jump to

Keyboard shortcuts

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