From b9411fdec6054db3b391b1bb9faa23b6f5d579fd Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 23 Sep 2026 19:47:41 +0200 Subject: [PATCH] The 100% gate rounds, and this module is over the line where that matters `go tool cover -func` prints its total with one decimal, so it says "100.0%" for anything from 99.95% upward. `%.1f` crosses that line at exactly 2000 statements: 1999 statements, 1 uncovered -> 99.949975% -> prints 99.9% -> refused 2000 statements, 1 uncovered -> 99.950000% -> prints 100.0% -> PASSED This module instruments 2770, so it is over it. Demonstrated here rather than argued: zeroing one block's counts in a real profile from this workflow's own command leaves `go tool cover` saying 100.0% while the statement is plainly uncovered. go tool cover says: 100.0% the gate says: uncovered (1 statements): parser.go:23.36,23.52 2769 of 2770 statements covered # It is exact today, which is why now Measured with this workflow's command, -race and the /ast exclusion included: 2770 of 2770. Nothing is hidden. Changing the instrument while the code is clean is cheaper than discovering later that it was not. # What the rounding hides is not a random statement It hides the ones hardest to reach from a test, and that population is mostly GUARDS. In go-crdt, where this was first found, the two statements the rounded gate had been passing were both allocation bounds against hostile input -- one of them a ceiling no test could reach from data at all. # The gate It counts statements in the profile, where the numbers are exact. Two details it carries from having been got wrong before: - A profile from `-coverpkg` with `./...` holds the same block once per test binary, so the counts are summed PER BLOCK before anything is called uncovered. Reading each line on its own reported thousands of covered statements as uncovered. - A pattern that matches nothing is a FAILURE rather than an empty pass, so a rename cannot quietly empty the gate. Verified: exit 1 on a profile with one block zeroed, exit 0 on the real one, and the empty-match guard fires. Not touched: paren_singleton_def_test.go is listed by gofmt here and not by CI, which is a toolchain difference and none of this change's business. Co-Authored-By: Claude Opus 5 --- .github/coverage-gate.sh | 82 ++++++++++++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 5 +-- 2 files changed, 83 insertions(+), 4 deletions(-) create mode 100755 .github/coverage-gate.sh diff --git a/.github/coverage-gate.sh b/.github/coverage-gate.sh new file mode 100755 index 0000000..8a98d58 --- /dev/null +++ b/.github/coverage-gate.sh @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +# A 100% coverage gate that means 100%. +# +# The gate this replaces read the total `go tool cover -func` prints. That total is +# rounded to one decimal, so it says "100.0%" for anything from 99.95% upward -- and +# `%.1f` crosses that line at exactly 2000 statements: +# +# 1999 statements, 1 uncovered -> 99.949975% -> prints 99.9% -> refused +# 2000 statements, 1 uncovered -> 99.950000% -> prints 100.0% -> PASSED +# +# This module instruments 2770 statements, so it is over that line: one uncovered +# statement would have printed as 100.0% and passed a gate named for 100%. It is +# exact today (2770 of 2770, measured with this workflow's own command), which is +# what makes this the right moment to change the instrument rather than the code. +# +# What the rounding hides is not a random statement. It hides the ones hardest to +# reach from a test, and that population is mostly GUARDS -- in go-crdt, where this +# was first found, the two hidden statements were both allocation bounds against +# hostile input. +# +# So this counts statements in the profile, where the numbers are exact. Each line is +# file.go:fromLine.col,toLine.col numberOfStatements timesExecuted +# and a block executed zero times is the thing a 100% gate exists to refuse. +# +# Usage: coverage-gate.sh [--report] [awk-regexp over the file:line field] +# +# The regexp narrows the gate to part of a profile; matching NOTHING is a failure +# rather than an empty pass, so a rename cannot quietly empty it. --report prints the +# count without failing on uncovered statements. +set -euo pipefail + +report=0 +if [ "${1:-}" = "--report" ]; then + report=1 + shift +fi +profile=${1:?usage: coverage-gate.sh [--report] [pattern]} +pattern=${2:-} + +# Two passes over the profile, because it may carry the SAME block more than +# once. `go test -coverpkg=X ./...` writes one section per test binary, each +# listing every block of X with that binary's counts -- so a block the root +# package's tests cover and another package's tests do not appears twice, once +# with a count and once with a zero. `go tool cover` merges those by block; a +# script that reads each line on its own does not, and reports thousands of +# uncovered statements that are covered. Measured: 4646 of them, on a profile the +# rounded gate called 100.0%. +# +# So the first pass sums the counts per block and the second prints the ones that +# are still zero, in file order. +awk -v pat="$pattern" -v report="$report" ' + FNR == 1 { next } # the "mode:" header, in each pass + pat != "" && $1 !~ pat { next } + NR == FNR { + if (!($1 in stmts)) { stmts[$1] = $2; order[++n] = $1 } + hits[$1] += $3 + next + } + # Second pass: nothing to do per line, the work is in END. + { } + END { + for (i = 1; i <= n; i++) { + key = order[i] + total += stmts[key] + if (hits[key] + 0 == 0 && stmts[key] + 0 > 0) { + uncovered += stmts[key] + if (!report) print "uncovered (" stmts[key] " statements): " key + } + } + if (total == 0) { + print "::error::the coverage gate matched no statements" \ + (pat == "" ? "" : " for /" pat "/") ", which is not a pass" + exit 1 + } + printf "%d of %d statements covered\n", total - uncovered, total + if (report) exit 0 + if (uncovered > 0) { + print "::error::" uncovered " statement(s) uncovered; the gate is 100%" + exit 1 + } + } +' "$profile" "$profile" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9e40300..c289e56 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,10 +34,7 @@ jobs: COVERPKG=$(go list ./... | grep -v /ast | paste -sd, -) go test -race -coverpkg="$COVERPKG" -coverprofile=cover.out ./... go tool cover -func=cover.out | tail -n 25 - cov=$(go tool cover -func=cover.out | tail -1 | awk '{print $3}' | tr -d '%') - echo "coverage: ${cov}%" - awk -v t="$cov" 'BEGIN { exit !(t+0 >= 100.0) }' || { - echo "::error::coverage ${cov}% < 100%"; exit 1; } + "$GITHUB_WORKSPACE/.github/coverage-gate.sh" cover.out # The parser is architecture-independent Go; the org still validates it on all # six supported 64-bit targets. amd64/arm64 run natively, the rest under qemu.