Skip to content

ci: compute coverage gate from profile, not 'go tool cover -func' total#219

Merged
shinagawa-web merged 2 commits into
mainfrom
ci/218-coverage-metric
Jul 2, 2026
Merged

ci: compute coverage gate from profile, not 'go tool cover -func' total#219
shinagawa-web merged 2 commits into
mainfrom
ci/218-coverage-metric

Conversation

@shinagawa-web

Copy link
Copy Markdown
Owner

Closes #218

Problem

make check-coverage derives the enforced total from go tool cover -func <profile> | grep ^total. That total under-counts uncovered blocks inside function literals (e.g. the cobra RunE: closure, main.go closures), so it reports a higher percentage than the real statement coverage.

On a deliberately gappy cmd/colref profile:

Metric Value
go test -cover (truth) 97.3%
go tool cover -func total (old gate) 98.3%
profile-direct covered_stmts / total_stmts (new gate) 97.3%

Because the gate read the lenient number, a func-literal gap could pass the 100% threshold unnoticed — which is exactly how the RunE validateFormat error branch stayed uncovered on main (real coverage was 99.9%, gate said 100%).

Fix

  • Compute the total directly from the coverage profile (executed statement counts / total statement counts), matching go test -cover. Guards an empty profile as 0.0. Threshold stays 100%.
  • The honest metric immediately exposed the pre-existing uncovered validateFormat RunE branch, so add TestCheckCmd_RunE_InvalidFormat to close it and keep the gate green at a real 100%.

Verification

  • make check-coverageTotal coverage: 100.0% / Coverage OK.
  • Empty profile → 0.0 (no divide-by-zero)
  • golangci-lint 0 issues, go vet clean, unit + e2e pass

Relationship to other PRs

@shinagawa-web shinagawa-web added the ci CI/CD changes label Jul 2, 2026
Copilot AI review requested due to automatic review settings July 2, 2026 05:57
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Benchmark Comparison

Comparing PR branch against main branch:

Legend: ✅ OK (no change or faster) | ⚠️ Caution (+10~50% slower) | ❌ Regression (+50%+ slower)

goos: linux
goarch: amd64
pkg: github.com/shinagawa-web/colref/internal/refs
cpu: AMD EPYC 9V74 80-Core Processor                
Scan     766.4m 766.7m  +0.04% ✅ [time/op]
Scan    139.8Mi 139.8Mi  +0.00% ✅ [memory/op]
Scan     1.371M 1.371M  +0.00% ✅ [allocs/op]
pkg: github.com/shinagawa-web/colref/internal/schema
ParseModels     159.4m 163.6m  +2.63% ✅ [time/op]
ParseModels    18.27Mi 18.27Mi  +0.00% ✅ [memory/op]
ParseModels     163.5k 163.5k  +0.00% ✅ [allocs/op]

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the CI coverage gate to compute total statement coverage directly from the Go coverage profile (matching go test -cover), addressing under-reporting from go tool cover -func totals for function-literal blocks. Adds a regression test to cover the check command’s invalid --format path that was previously missed by the (now-honest) 100% gate.

Changes:

  • Replace go tool cover -func | grep ^total with profile-based covered_stmts / total_stmts computation in make check-coverage.
  • Add TestCheckCmd_RunE_InvalidFormat to exercise the validateFormat error branch in the Cobra RunE closure.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
Makefile Computes coverage threshold from the raw coverprofile to match go test -cover behavior.
cmd/colref/check_test.go Adds a test to cover the invalid --format error path in the check command.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Makefile Outdated
Comment thread cmd/colref/check_test.go Outdated
shinagawa-web added a commit that referenced this pull request Jul 2, 2026
Address Copilot review on #219:
- The check-coverage recipe is one ;-joined shell block ending in
  'echo Coverage OK' (exit 0), so a go test *failure* (which still yields a
  full-coverage profile) was masked. Add 'set -e' so a non-zero go test
  aborts the target. Verified end-to-end: an injected failing test makes
  'make check-coverage' exit non-zero without printing 'Coverage OK'.
- TestCheckCmd_RunE_InvalidFormat now asserts the error message contains
  the --format validation failure, verifying that path specifically rather
  than accepting any error.
'go tool cover -func | grep ^total' under-counts uncovered blocks inside
function literals (e.g. the cobra RunE: closure), reporting a higher
percentage than real statement coverage. On a gappy cmd/colref profile the
old metric read 98.3% while go test -cover reported the true 97.3%, so a
func-literal gap could pass the 100% gate unnoticed — which is exactly how
the RunE validateFormat error branch stayed uncovered.

Compute the total directly from the profile (executed statements / total
statements), matching go test -cover. The honest metric then exposed that
uncovered validateFormat branch, so add TestCheckCmd_RunE_InvalidFormat to
close it and keep the gate green at a real 100%.

Closes #218
Address Copilot review on #219:
- The check-coverage recipe is one ;-joined shell block ending in
  'echo Coverage OK' (exit 0), so a go test *failure* (which still yields a
  full-coverage profile) was masked. Add 'set -e' so a non-zero go test
  aborts the target. Verified end-to-end: an injected failing test makes
  'make check-coverage' exit non-zero without printing 'Coverage OK'.
- TestCheckCmd_RunE_InvalidFormat now asserts the error message contains
  the --format validation failure, verifying that path specifically rather
  than accepting any error.
@shinagawa-web
shinagawa-web force-pushed the ci/218-coverage-metric branch from cb8c4e0 to e40213e Compare July 2, 2026 08:18
@shinagawa-web
shinagawa-web merged commit 047f2bb into main Jul 2, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci CI/CD changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ci: coverage gate under-reports — 'go tool cover -func' total misses func-literal blocks

3 participants