Skip to content

Commit 462826f

Browse files
committed
test(coverage): pin what a deletion-only edit is charged
The question was whether an edit that only deletes lines inside a surviving function scoring >= 30 goes uncharged. Reproduced against real fallow with `--diff-file`: it goes uncharged, and it is not a gap in this gate's own attribution. Fallow's diff index holds added lines only — it reports so itself when handed a deletion-only diff — so no line exists to intersect a function's span with, and `readTouchedFiles` agrees with the filter it is priced against rather than overriding it. So the fix is a pin, not a rule change: charging deletions would need fallow's own scoping loosened, which would block every function in a file any line of which changed. Three cases hold the boundary down, each proving with `--all` that the debt really is in the map rather than absent from it: a deletion inside the function, lines added elsewhere in the same file, and deleting the function outright. The first and third pass because removing code retires debt — the same reason the whole-file and pure-rename exemptions exist. A deletion that leaves an untested complex function standing is the residual, and `--all` still lists it. Part of #3414.
1 parent e155fbf commit 462826f

1 file changed

Lines changed: 65 additions & 9 deletions

File tree

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

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type CoverageOptions = Readonly<{
4848
hits?: number;
4949
/** Emit a count no execution can produce, which the sanitizer drops with the whole record. */
5050
corrupt?: boolean;
51+
/** Record the file with no functions in it, as a run over a deleted function's file reports. */
52+
noFunctions?: boolean;
5153
}>;
5254

5355
function istanbulRecord(file: string, options: CoverageOptions): Record<string, unknown> {
@@ -56,17 +58,19 @@ function istanbulRecord(file: string, options: CoverageOptions): Record<string,
5658
return {
5759
path: path.join(repo, file),
5860
statementMap: { 0: { start: line(from), end: line(to) } },
59-
fnMap: {
60-
0: {
61-
name: 'score',
62-
decl: { start: line(from), end: line(from) },
63-
loc: { start: line(from), end: line(to) },
64-
line: from,
65-
},
66-
},
61+
fnMap: options.noFunctions
62+
? {}
63+
: {
64+
0: {
65+
name: 'score',
66+
decl: { start: line(from), end: line(from) },
67+
loc: { start: line(from), end: line(to) },
68+
line: from,
69+
},
70+
},
6771
branchMap: {},
6872
s: { 0: options.corrupt ? -1 : (options.hits ?? 0) },
69-
f: { 0: options.hits ?? 0 },
73+
f: options.noFunctions ? {} : { 0: options.hits ?? 0 },
7074
b: {},
7175
};
7276
}
@@ -257,6 +261,58 @@ test('a rename that also edits the body owes the edited lines', () => {
257261
assert.match(out, /\| `src\/moved\.ts:1` score \| 56\.0 \| 7 \| 0% \|/);
258262
});
259263

264+
test('deleting lines from a debt function does not charge the function that survives them', () => {
265+
// Measured, not assumed: fallow's `--diff-file` index holds added lines only, so a hunk that is
266+
// nothing but removals contributes no line to intersect a function's span with, and this run
267+
// agrees rather than overriding it. Removing code retires debt instead of adding it, which is the
268+
// rule the whole-file and pure-rename exemptions already follow — the residual is deliberate: a
269+
// deletion that leaves an untested complex function standing is not charged here, and `--all`
270+
// still lists it on main. Pinned so a change to either layer's scoping has to say so.
271+
writeCoverage({ 'src/debt.ts': { span: [1, 13] }, 'src/clean.ts': { hits: 4 } });
272+
write('src/debt.ts', DEBT.replace(' total += 1;\n', ''));
273+
commit('delete a line inside the debt function');
274+
275+
const { code, out } = check(['--base', HEADS[0]!]);
276+
277+
assert.equal(code, 0);
278+
assert.match(out, /no touched function\(s\) at or above 30/);
279+
// Not a vacuous pass: the same map, scored without the diff, does hold this function at 42.
280+
const backlog = check(['--all']);
281+
assert.equal(backlog.code, 0);
282+
assert.match(backlog.out, /\| `src\/debt\.ts:1` score \| 42\.0 \| 6 \| 0% \|/);
283+
});
284+
285+
test('lines added elsewhere in a file do not charge a function whose own lines did not change', () => {
286+
// Scoping is per function span, not per file, so a one-line edit in a large module does not
287+
// inherit every debt score inside it. Here the file has added lines — `helper` is rewritten and
288+
// another export appears below the function — and the function lost one line, yet none of the
289+
// added lines fall in its span, so it is not this change's verdict.
290+
writeCoverage({ 'src/debt.ts': { span: [1, 13] }, 'src/clean.ts': { hits: 4 } });
291+
write(
292+
'src/debt.ts',
293+
`${DEBT.replace(' total += 1;\n', '')}\nexport const helper = 2;\nexport const extra = 3;\n`,
294+
);
295+
commit('drop a line inside the function, add two below it');
296+
297+
const { code, out } = check(['--base', HEADS[0]!]);
298+
299+
assert.equal(code, 0);
300+
assert.doesNotMatch(out, /`src\/debt\.ts:1` score/);
301+
const backlog = check(['--all']);
302+
assert.match(backlog.out, /\| `src\/debt\.ts:1` score \| 42\.0 \| 6 \| 0% \|/);
303+
});
304+
305+
test('deleting the debt function altogether is retiring it, not a change to it', () => {
306+
writeCoverage({ 'src/debt.ts': { noFunctions: true }, 'src/clean.ts': { hits: 4 } });
307+
write('src/debt.ts', TRIVIAL);
308+
commit('replace the debt function with a constant');
309+
310+
const { code, out } = check(['--base', HEADS[0]!]);
311+
312+
assert.equal(code, 0);
313+
assert.match(out, /no touched function\(s\) at or above 30/);
314+
});
315+
260316
test('a touched file the sanitizer dropped as corrupt yields no verdict', () => {
261317
writeCoverage({ 'src/debt.ts': {}, 'src/clean.ts': { corrupt: true } });
262318
write('src/clean.ts', `${TRIVIAL}export const also_trivial = 2;\n`);

0 commit comments

Comments
 (0)