Skip to content

Commit 38f9e6d

Browse files
cliffhallclaude
andcommitted
fix(coverage-py): refuse a report measured without branch coverage
coverage.py writes num_branches: 0 for every file when branch measurement is off, which the per-file check counted as 100% on branches, so removing `branch = true` would have let every file pass that dimension. Require meta.branch_coverage === true and fail with a message pointing at [tool.coverage.run] branch = true. Regression tests for false, a missing flag and a missing meta. Signed-off-by: cliffhall <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f9a66a7 commit 38f9e6d

4 files changed

Lines changed: 36 additions & 5 deletions

File tree

‎.claude/skills/pre-push-gate/SKILL.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ branches (`120->124` is the jump from line 120 to 124 never taken).
229229
| `pytest --cov` | A test failed, so no verdict was reached; fix it as for `validate:py`'s `pytest`. `unrecognized arguments: --cov` means `pytest-cov` is missing from the server's dev dependencies |
230230
| `per-file check`, "below 90%" | Write tests for the lines and branches the report lists. Drive the server in-process through a `ClientSession` where you can (`/testing`) |
231231
| `per-file check`, "was not written" or "measured no files" | The run produced no usable `coverage.json`: check the server's `[tool.coverage.run]` (`branch = true`, `source` naming its package) |
232-
| `per-file check`, "no covered_branches/num_branches" | `branch = true` is missing from `[tool.coverage.run]` |
232+
| `per-file check`, "not measured with branch coverage" or "no covered_branches/num_branches" | `branch = true` is missing from `[tool.coverage.run]`. The check refuses such a report rather than counting every file's zero branches as 100% |
233233

234234
**Never lower the gate**, and never drop a file from `source` to clear it.
235235
Code that genuinely cannot run (a guard the types already rule out, a branch

‎scripts/coverage-py.test.mjs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ const entry = (cl, st, cb, br) => ({
1919
},
2020
});
2121

22-
const passing = { files: { "src/pkg/server.py": entry(10, 10, 4, 4) } };
22+
const meta = { branch_coverage: true };
23+
const passing = { meta, files: { "src/pkg/server.py": entry(10, 10, 4, 4) } };
2324
const failing = {
25+
meta,
2426
files: {
2527
"src/pkg/server.py": entry(5, 10, 4, 4),
2628
"src/pkg/__main__.py": entry(0, 3, 1, 2),
@@ -125,7 +127,7 @@ describe("coverServers", () => {
125127
});
126128

127129
it("fails a server whose report measured nothing", () => {
128-
const { results } = harness({ reports: { fetch: { files: {} } } });
130+
const { results } = harness({ reports: { fetch: { meta, files: {} } } });
129131
assert.equal(results[0].failedStep, "per-file check");
130132
assert.match(results[0].detail, /measured no files/);
131133
});

‎scripts/lib/py-coverage.mjs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
* compared exactly in integers, so 89.96% fails rather than rounding up.
2323
* - A dimension with nothing to measure (no statements, or no branches)
2424
* counts as 100%: there is nothing in it left untested.
25-
* - A report that is missing, unreadable, or measured no files at all is a
26-
* failure, never a vacuous pass.
25+
* - A report that is missing, unreadable, measured no files at all, or was
26+
* measured without branch coverage (`meta.branch_coverage` not true, where
27+
* every file's branch count is 0) is a failure, never a vacuous pass.
2728
*
2829
* Pure apart from `readCoverageReport`, so `node --test` covers it without
2930
* `uv` or Python.
@@ -86,6 +87,16 @@ export function checkReport(report, threshold = THRESHOLD) {
8687
if (!files || typeof files !== "object" || Array.isArray(files)) {
8788
return { error: "not a coverage.py JSON report (no `files` object)" };
8889
}
90+
// Without branch measurement coverage.py still writes `num_branches: 0` for
91+
// every file, which would read as 100% below. Refuse the report instead.
92+
const meta = /** @type {{ meta?: { branch_coverage?: unknown } }} */ (report)
93+
.meta;
94+
if (meta?.branch_coverage !== true) {
95+
return {
96+
error:
97+
"the report was not measured with branch coverage (meta.branch_coverage is not true); set [tool.coverage.run] branch = true in pyproject.toml",
98+
};
99+
}
89100
const names = Object.keys(files).sort();
90101
if (names.length === 0) {
91102
return {

‎scripts/lib/py-coverage.test.mjs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ describe("checkReport", () => {
146146
assert.ok(result.verdicts.every((v) => v.ok));
147147
});
148148

149+
for (const [name, meta] of [
150+
["branch_coverage: false", { branch_coverage: false }],
151+
["no branch_coverage flag", {}],
152+
["no meta at all", undefined],
153+
]) {
154+
it(`rejects a report measured without branches (${name})`, () => {
155+
// coverage.py writes num_branches: 0 for every file when branch = true
156+
// is missing, which would otherwise count as 100% on branches.
157+
const result = checkReport({
158+
...(meta === undefined ? {} : { meta }),
159+
files: { "src/pkg/server.py": entry(100, 100, 0, 0) },
160+
});
161+
assert.ok("error" in result);
162+
assert.match(result.error, /not measured with branch coverage/);
163+
assert.match(result.error, /branch = true/);
164+
});
165+
}
166+
149167
it("rejects a report that measured no files, rather than passing it", () => {
150168
const result = checkReport(report({}));
151169
assert.ok("error" in result);

0 commit comments

Comments
 (0)