Skip to content

Commit f2e74d2

Browse files
authored
test(coverage): fix what the deletion pins claimed (#3501)
* test(coverage): fix what the deletion pins claimed Two review threads survived #3486 and #3492 because both were merged while the checks were still queued. The second deletion case was named for a function whose own lines did not change, and its own fixture deletes a line inside that function. Renamed to the scenario it covers: additions below the span, alongside a removal inside it. The third case pinned its fixture rather than any scoping rule, since a function absent from the map is unscored in either scope. Measured that first: a stale map which still records the deleted function yields nothing from `--all` either, so no scoping keeps it uncharged — the removal takes the score with it. The test now asserts that in both scopes and says which of the two mechanisms it is evidence for. CONTRIBUTING said removing code retires debt. It does not always: dropping lines that used to be covered lowers the coverage side of the ratio and can raise the score, and that debt lands in the `--all` backlog rather than the PR. The sentence now says deletions are uncharged, which is the accurate claim, and names where the debt it can create goes. Part of #3414. * test(coverage): score the deleted-function pin against the map it claims Both review points on the previous commit were right. The third case's `--all` assertion proved nothing about a stale map, because its fixture recorded no functions at all — there was no deleted-function score for `--all` to omit. It now keeps the `score` record at CRAP 42 while the source loses the function, which is the shape a forgotten `pnpm test:coverage` really leaves behind, and asserts `--all` omits it anyway: scoring walks the source, so neither scope reports a function that is no longer there. Measured through run() before being written. CONTRIBUTING said a deletion's raised score reaches `--all` rather than the PR, which holds only when no added line lands inside that function — a charged function is scored as it stands after the deletions, so the rise blocks with it. Said conditionally now. The rewrap also brought two over-long lines this paragraph had inherited back under the house width.
1 parent ecf597b commit f2e74d2

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,13 @@ fails when it cannot score them. It prints `no trustworthy verdict` and names th
252252
or when a touched function postdates the map and would be scored from complexity alone. Every one of
253253
those is a stale coverage run, not a debt you owe: re-run `pnpm test:coverage` on the commit and
254254
re-check. What does count as debt is editing a function that already scores 30 or more — the score
255-
does not have to be new. Moving a file without editing its lines touches none of its functions, and
256-
neither does deleting lines from one: removing code retires debt instead of adding it, which is also
257-
why deleting a whole function passes. A deletion that leaves an untested complex function standing is
258-
not charged, and that function keeps showing in the `--all` backlog. Unlike the changed-line gate,
259-
this one has no `coverage-waiver` label: the only way past a score is to test or simplify the
260-
function, or to exempt the file in `fallow-crap.json`.
255+
does not have to be new. Adding lines is what charges a function; deleting is not, and removing a
256+
whole function takes its score with it. The two still interact: a charged function is scored as it
257+
stands after your deletions, and dropping lines that used to be covered can raise that number. Where
258+
no added line lands inside the function, the deletion stays out of the PR entirely and whatever the
259+
score rose to waits in the `--all` backlog. Moving a file without editing its lines touches none of
260+
its functions. Unlike the changed-line gate, this one has no `coverage-waiver` label: the only way
261+
past a score is to test or simplify the function, or to exempt the file in `fallow-crap.json`.
261262

262263
Two things are listed rather than blocked, so a green run does not mean they were checked. A touched
263264
function in a file coverage never instrumented — one `coverage.exclude` drops, or one a stale local

‎scripts/coverage-crap/run.test.ts‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ test('a rename that also edits the body owes the edited lines', () => {
291291
test('deleting lines from a debt function does not charge the function that survives them', () => {
292292
// Measured, not assumed: fallow's `--diff-file` index holds added lines only, so a hunk that is
293293
// nothing but removals contributes no line to intersect a function's span with, and this run
294-
// agrees rather than overriding it. Removing code retires debt instead of adding it, which is the
295-
// rule the whole-file and pure-rename exemptions already follow — the residual is deliberate: a
296-
// deletion that leaves an untested complex function standing is not charged here, and `--all`
297-
// still lists it on main. Pinned so a change to either layer's scoping has to say so.
294+
// agrees rather than overriding it — the same reason the whole-file and pure-rename exemptions
295+
// exist. The residual is deliberate and not free: a deletion can even raise a surviving
296+
// function's CRAP by removing lines that used to be covered, and that new debt shows up only in
297+
// `--all`. Pinned so a change to either layer's scoping has to say so.
298298
writeCoverage({ 'src/debt.ts': { span: [1, 13] }, 'src/clean.ts': { hits: 4 } });
299299
write('src/debt.ts', DEBT.replace(' total += 1;\n', ''));
300300
commit('delete a line inside the debt function');
@@ -309,11 +309,10 @@ test('deleting lines from a debt function does not charge the function that surv
309309
assert.match(backlog.out, /\| `src\/debt\.ts:1` score \| 42\.0 \| 6 \| 0% \|/);
310310
});
311311

312-
test('lines added elsewhere in a file do not charge a function whose own lines did not change', () => {
313-
// Scoping is per function span, not per file, so a one-line edit in a large module does not
314-
// inherit every debt score inside it. Here the file has added lines — `helper` is rewritten and
315-
// another export appears below the function — and the function lost one line, yet none of the
316-
// added lines fall in its span, so it is not this change's verdict.
312+
test('lines added below a debt function do not charge it, though the diff also deleted one inside', () => {
313+
// The per-span half of the boundary: scoping is per function span, not per file, so a module is
314+
// not charged for the debt it merely contains. `score` loses one line here and the file gains two
315+
// below it, and because none of the added lines land inside the span, none of them charge it.
317316
writeCoverage({ 'src/debt.ts': { span: [1, 13] }, 'src/clean.ts': { hits: 4 } });
318317
write(
319318
'src/debt.ts',
@@ -329,15 +328,23 @@ test('lines added elsewhere in a file do not charge a function whose own lines d
329328
assert.match(backlog.out, /\| `src\/debt\.ts:1` score \| 42\.0 \| 6 \| 0% \|/);
330329
});
331330

332-
test('deleting the debt function altogether is retiring it, not a change to it', () => {
333-
writeCoverage({ 'src/debt.ts': { noFunctions: true }, 'src/clean.ts': { hits: 4 } });
331+
test('a deleted function takes its score with it, out of both scopes', () => {
332+
// The stronger fixture, and the reason the exemption is structural rather than the diff filter
333+
// again: the map still records `score` at 42 from a coverage run that predates the deletion, yet
334+
// neither scope reports it, because scoring walks the source and the source no longer holds the
335+
// function. The two cases above pin debt that IS scored and is held back by scoping; this one
336+
// pins debt that no longer exists in either scope even while the map claims it.
337+
writeCoverage({ 'src/debt.ts': { span: [1, 14] }, 'src/clean.ts': { hits: 4 } });
334338
write('src/debt.ts', TRIVIAL);
335-
commit('replace the debt function with a constant');
339+
commit('delete the debt function, leave the map recording it');
336340

337341
const { code, out } = check(['--base', HEADS[0]!]);
338342

339343
assert.equal(code, 0);
340344
assert.match(out, /no touched function\(s\) at or above 30/);
345+
const backlog = check(['--all']);
346+
assert.equal(backlog.code, 0);
347+
assert.match(backlog.out, /no function\(s\) at or above 30/);
341348
});
342349

343350
test('a touched file the sanitizer dropped as corrupt yields no verdict', () => {

0 commit comments

Comments
 (0)