Skip to content

Commit c5bb0a2

Browse files
authored
test(coverage): pin what a deletion-only edit is charged (#3492)
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 87a803c commit c5bb0a2

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
}
@@ -284,6 +288,58 @@ test('a rename that also edits the body owes the edited lines', () => {
284288
assert.match(out, /\| `src\/moved\.ts:1` score \| 56\.0 \| 7 \| 0% \|/);
285289
});
286290

291+
test('deleting lines from a debt function does not charge the function that survives them', () => {
292+
// Measured, not assumed: fallow's `--diff-file` index holds added lines only, so a hunk that is
293+
// 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.
298+
writeCoverage({ 'src/debt.ts': { span: [1, 13] }, 'src/clean.ts': { hits: 4 } });
299+
write('src/debt.ts', DEBT.replace(' total += 1;\n', ''));
300+
commit('delete a line inside the debt function');
301+
302+
const { code, out } = check(['--base', HEADS[0]!]);
303+
304+
assert.equal(code, 0);
305+
assert.match(out, /no touched function\(s\) at or above 30/);
306+
// Not a vacuous pass: the same map, scored without the diff, does hold this function at 42.
307+
const backlog = check(['--all']);
308+
assert.equal(backlog.code, 0);
309+
assert.match(backlog.out, /\| `src\/debt\.ts:1` score \| 42\.0 \| 6 \| 0% \|/);
310+
});
311+
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.
317+
writeCoverage({ 'src/debt.ts': { span: [1, 13] }, 'src/clean.ts': { hits: 4 } });
318+
write(
319+
'src/debt.ts',
320+
`${DEBT.replace(' total += 1;\n', '')}\nexport const helper = 2;\nexport const extra = 3;\n`,
321+
);
322+
commit('drop a line inside the function, add two below it');
323+
324+
const { code, out } = check(['--base', HEADS[0]!]);
325+
326+
assert.equal(code, 0);
327+
assert.doesNotMatch(out, /`src\/debt\.ts:1` score/);
328+
const backlog = check(['--all']);
329+
assert.match(backlog.out, /\| `src\/debt\.ts:1` score \| 42\.0 \| 6 \| 0% \|/);
330+
});
331+
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 } });
334+
write('src/debt.ts', TRIVIAL);
335+
commit('replace the debt function with a constant');
336+
337+
const { code, out } = check(['--base', HEADS[0]!]);
338+
339+
assert.equal(code, 0);
340+
assert.match(out, /no touched function\(s\) at or above 30/);
341+
});
342+
287343
test('a touched file the sanitizer dropped as corrupt yields no verdict', () => {
288344
writeCoverage({ 'src/debt.ts': {}, 'src/clean.ts': { corrupt: true } });
289345
write('src/clean.ts', `${TRIVIAL}export const also_trivial = 2;\n`);

0 commit comments

Comments
 (0)