Skip to content

Commit 253a1d6

Browse files
committed
fix(coverage-changed): refuse stale or foreign reports via the coverage stamp
The changed-line gate scored whatever lcov file it found, so a stale report or one written in another checkout read as a verdict — a foreign report even passed 0/0. It now refuses before scoring when the stamp beside the report is absent, names another commit or working tree, or records a narrowed run, and when lcov records files outside this checkout (the stamp cannot catch two checkouts of one commit). The refusal fires before the coverage-waiver label is consulted. Instrumentation now comes from the verified stamp's include/exclude, shared with the CRAP gate, and the duplicated SOURCE_ROOT policy is removed. Closes #4379
1 parent 5a836c0 commit 253a1d6

5 files changed

Lines changed: 279 additions & 48 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,15 @@ itself out of scope — the config's `exclude`, or a whole-file ignore comment.
298298
against 30 too, so a below-bar guess about new code is not reported at all; the stamp is what stops that
299299
guess from standing in for a measurement of code you changed.
300300

301+
The changed-line gate (`check:coverage-changed`) reads the same stamp: with no stamp, a stamp from
302+
another commit or working tree, a narrowed run, or an lcov report whose records name files outside this
303+
checkout, it prints `no trustworthy verdict` and names the reason instead of a PASS/FAIL table. The fix
304+
is the same — re-run `pnpm test:coverage` on this commit — and the refusal fires before the
305+
`coverage-waiver` label is consulted, because a waiver skips a failure rather than blessing a report
306+
with no numbers to print. Which files owe coverage comes from the stamp's include/exclude, so a file the
307+
config excludes is out of scope by declaration, while a file the stamp instruments that lcov omits is
308+
still tallied as excluded-path lines.
309+
301310
## Test app and Maestro compatibility
302311

303312
The Expo fixture app owns its setup, simulator/device, Metro, replay, and Maestro instructions in

‎scripts/coverage-changed/model.test.ts‎

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import {
55
CHANGED_LINE_COVERAGE_THRESHOLD,
66
computeChangedCoverage,
77
ignoredLineSet,
8-
isIncludableSource,
9-
isTestFile,
108
parseLcov,
119
} from './model.ts';
10+
import { isInstrumentedPath, type CoverageConfig } from '../coverage-crap/stamp.ts';
1211

1312
test('parseLcov reads DA hits and BRDA taken, treating "-" as not taken', () => {
1413
const lcov = [
@@ -34,23 +33,39 @@ test('parseLcov reads DA hits and BRDA taken, treating "-" as not taken', () =>
3433
assert.equal(index.get('src/b.ts')?.lineHits.get(10), 1);
3534
});
3635

37-
test('source/test classification matches the coverage include glob', () => {
38-
assert.equal(isIncludableSource('src/core/x.ts'), true);
39-
assert.equal(isIncludableSource('src/core/x.test.ts'), false);
40-
assert.equal(isIncludableSource('src/core/__tests__/x.ts'), false);
41-
assert.equal(isIncludableSource('scripts/y.ts'), false);
42-
assert.equal(isIncludableSource('src/core/x.tsx'), false);
36+
// The stamp's include/exclude, mirroring `test.coverage` in `vitest.config.ts`
37+
// for the paths these tests exercise: both coverage roots, test files and the
38+
// config's named exclusions out.
39+
const COVERAGE_CONFIG: CoverageConfig = {
40+
include: ['src/**/*.ts', 'packages/*/src/**/*.ts'],
41+
exclude: [
42+
'**/*.test.ts',
43+
'**/__tests__/**',
44+
'**/*-types.ts',
45+
'**/types.ts',
46+
'src/sdk/**',
47+
'src/bin.ts',
48+
],
49+
};
50+
51+
test('source/test classification matches the verified stamp selection', () => {
52+
assert.equal(isInstrumentedPath('src/core/x.ts', COVERAGE_CONFIG), true);
53+
assert.equal(isInstrumentedPath('src/core/x.test.ts', COVERAGE_CONFIG), false);
54+
assert.equal(isInstrumentedPath('src/core/__tests__/x.ts', COVERAGE_CONFIG), false);
55+
assert.equal(isInstrumentedPath('scripts/y.ts', COVERAGE_CONFIG), false);
56+
assert.equal(isInstrumentedPath('src/core/x.tsx', COVERAGE_CONFIG), false);
4357
// Workspace packages are the second coverage root.
44-
assert.equal(isIncludableSource('packages/contracts/src/x.ts'), true);
45-
assert.equal(isIncludableSource('packages/contracts/src/deep/x.ts'), true);
46-
assert.equal(isIncludableSource('packages/contracts/src/x.test.ts'), false);
47-
assert.equal(isIncludableSource('packages/contracts/src/__tests__/x.ts'), false);
48-
assert.equal(isIncludableSource('packages/contracts/src/x.tsx'), false);
49-
assert.equal(isIncludableSource('packages/contracts/test/x.ts'), false);
50-
assert.equal(isIncludableSource('packages/contracts/x.ts'), false);
51-
assert.equal(isTestFile('src/a.test.ts'), true);
52-
assert.equal(isTestFile('src/__tests__/a.ts'), true);
53-
assert.equal(isTestFile('src/a.ts'), false);
58+
assert.equal(isInstrumentedPath('packages/contracts/src/x.ts', COVERAGE_CONFIG), true);
59+
assert.equal(isInstrumentedPath('packages/contracts/src/deep/x.ts', COVERAGE_CONFIG), true);
60+
assert.equal(isInstrumentedPath('packages/contracts/src/x.test.ts', COVERAGE_CONFIG), false);
61+
assert.equal(isInstrumentedPath('packages/contracts/src/__tests__/x.ts', COVERAGE_CONFIG), false);
62+
assert.equal(isInstrumentedPath('packages/contracts/src/x.tsx', COVERAGE_CONFIG), false);
63+
assert.equal(isInstrumentedPath('packages/contracts/test/x.ts', COVERAGE_CONFIG), false);
64+
assert.equal(isInstrumentedPath('packages/contracts/x.ts', COVERAGE_CONFIG), false);
65+
// What the stamp excludes is out of scope by declaration, not merely unscored.
66+
assert.equal(isInstrumentedPath('src/sdk/thing.ts', COVERAGE_CONFIG), false);
67+
assert.equal(isInstrumentedPath('src/core/x-types.ts', COVERAGE_CONFIG), false);
68+
assert.equal(isInstrumentedPath('src/bin.ts', COVERAGE_CONFIG), false);
5469
});
5570

5671
test('ignoredLineSet handles next, next N and start/stop ranges', () => {
@@ -98,6 +113,7 @@ test('gate fails when changed-line coverage is below threshold and lists offende
98113
const result = computeChangedCoverage({
99114
diffs: [diff('src/covered.ts', [1, 2, 3])],
100115
coverage,
116+
stamp: COVERAGE_CONFIG,
101117
fileLines: () => null,
102118
});
103119
// lines 1 & 3 covered, line 2 uncovered -> 2/3 = 66.67% < 70.
@@ -118,6 +134,7 @@ test('workspace package source counts toward the gate like root source', () => {
118134
const result = computeChangedCoverage({
119135
diffs: [diff('packages/contracts/src/feature.ts', [1, 2])],
120136
coverage: packageCoverage,
137+
stamp: COVERAGE_CONFIG,
121138
fileLines: () => null,
122139
});
123140
// 1/2 = 50% < 70: a package change can now fail the gate on its own.
@@ -132,6 +149,7 @@ test('non-executable added lines (absent from DA) never enter the denominator',
132149
const result = computeChangedCoverage({
133150
diffs: [diff('src/covered.ts', [1, 3, 99])],
134151
coverage,
152+
stamp: COVERAGE_CONFIG,
135153
fileLines: () => null,
136154
});
137155
assert.equal(result.totalLines, 2);
@@ -144,6 +162,7 @@ test('deleted files and docs-only changes are a trivial pass with null pct', ()
144162
const result = computeChangedCoverage({
145163
diffs: [diff('src/covered.ts', [1], true), diff('README.md', [1, 2])],
146164
coverage,
165+
stamp: COVERAGE_CONFIG,
147166
fileLines: () => null,
148167
});
149168
assert.equal(result.pct, null);
@@ -155,6 +174,7 @@ test('waiver forces a pass but still reports the numbers', () => {
155174
const result = computeChangedCoverage({
156175
diffs: [diff('src/covered.ts', [1, 2, 3])],
157176
coverage,
177+
stamp: COVERAGE_CONFIG,
158178
waived: true,
159179
fileLines: () => null,
160180
});
@@ -163,7 +183,7 @@ test('waiver forces a pass but still reports the numbers', () => {
163183
assert.equal(result.pct, 66.67);
164184
});
165185

166-
test('an includable path whose new side is a symlink or gitlink is skipped, not read', () => {
186+
test('an instrumented path whose new side is a symlink or gitlink is skipped, not read', () => {
167187
// A committed symlink like `tools/helper-tests/Sources -> ../helper/Sources` shows in the
168188
// diff as added text (its target), so it reaches this loop by path shape. Its working-tree
169189
// entry resolves to a directory, so a content read dies on EISDIR; reading the
@@ -176,6 +196,7 @@ test('an includable path whose new side is a symlink or gitlink is skipped, not
176196
diff('src/covered.ts', [1, 2, 3]),
177197
],
178198
coverage,
199+
stamp: COVERAGE_CONFIG,
179200
fileLines: (path) => {
180201
readPaths.push(path);
181202
return null;
@@ -188,10 +209,11 @@ test('an includable path whose new side is a symlink or gitlink is skipped, not
188209
assert.deepEqual(result.excluded.files, []);
189210
});
190211

191-
test('includable source absent from lcov counts as excluded-path code lines', () => {
212+
test('instrumented source absent from lcov counts as excluded-path code lines', () => {
192213
const result = computeChangedCoverage({
193-
diffs: [diff('src/sdk/thing.ts', [1, 2, 3])],
214+
diffs: [diff('src/missing.ts', [1, 2, 3])],
194215
coverage,
216+
stamp: COVERAGE_CONFIG,
195217
fileLines: () => ['const kept = 1;', '', '// a comment'],
196218
});
197219
assert.equal(result.totalLines, 0);
@@ -200,11 +222,25 @@ test('includable source absent from lcov counts as excluded-path code lines', ()
200222
assert.deepEqual(result.excluded.files[0]?.lines, [1]);
201223
});
202224

225+
test('a path the stamp excludes is out of scope, not tallied', () => {
226+
const result = computeChangedCoverage({
227+
diffs: [diff('src/sdk/thing.ts', [1, 2, 3])],
228+
coverage,
229+
stamp: COVERAGE_CONFIG,
230+
fileLines: () => ['const kept = 1;', '', '// a comment'],
231+
});
232+
assert.equal(result.totalLines, 0);
233+
assert.equal(result.excluded.totalLines, 0);
234+
assert.deepEqual(result.excluded.files, []);
235+
assert.equal(result.passed, true);
236+
});
237+
203238
test('changed line suppressed by an ignore directive is tallied, not gated', () => {
204239
const cov = parseLcov(['SF:src/x.ts', 'DA:1,1', 'end_of_record'].join('\n'));
205240
const result = computeChangedCoverage({
206241
diffs: [diff('src/x.ts', [1, 3])],
207242
coverage: cov,
243+
stamp: COVERAGE_CONFIG,
208244
fileLines: () => ['const a = 1;', '/* v8 ignore next */', 'const suppressed = 3;'],
209245
});
210246
// Line 3 is code-like, dropped from DA by the directive on line 2, and inside
@@ -279,6 +315,7 @@ test('rename-only hunks owe no changed-line coverage; edited moves owe their edi
279315
uncoveredRecord('src/moved-pure.ts', 30),
280316
].join('\n'),
281317
),
318+
stamp: COVERAGE_CONFIG,
282319
fileLines: () => null,
283320
});
284321
// 2 ordinary edits + 3 edited move lines; the 30-line pure move owes nothing.

‎scripts/coverage-changed/model.ts‎

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
// LINES ONLY. Only added (new-side) lines count — deleted lines never do — so
44
// the gate is deterministic against renames and deletes. run.ts owns all I/O.
55
//
6-
// The coverable universe is the lcov report itself, not a second copy of
7-
// vitest's exclude globs: coverage runs with `all` on, so every includable file
8-
// under either coverage root (`src/**/*.ts` and `packages/*/src/**/*.ts`)
9-
// appears in lcov even when untested. A changed includable file ABSENT from
10-
// lcov was dropped by an exclude glob — reported (non-gating) so exclusions
11-
// cannot silently absorb new logic.
6+
// The coverable universe is the verified stamp's include/exclude, not a second
7+
// copy of the coverage config: `isInstrumentedPath` against the stamp is what
8+
// decides whether a changed file owes coverage, the same selection the CRAP
9+
// gate reads from the same stamp. A changed instrumented file ABSENT from
10+
// lcov was dropped by the run — reported (non-gating) so the gap cannot
11+
// silently absorb new logic. A changed file the stamp excludes is out of scope
12+
// by declaration and owes nothing, not even a tally entry.
1213

1314
import type { ChangedFileDiff } from '../lib/repo-git.ts';
1415
import { normalizePath } from '../lib/repo-path.ts';
16+
import { isInstrumentedPath, type CoverageConfig } from '../coverage-crap/stamp.ts';
1517

1618
// Single source of truth for the gate. Changed-line coverage below this
1719
// percentage fails the Coverage CI job (unless waived).
@@ -73,22 +75,6 @@ export function parseLcov(text: string, rootDir?: string): CoverageIndex {
7375
return index;
7476
}
7577

76-
// Vitest coverage `include` spans two roots: `src/**/*.ts` and
77-
// `packages/*/src/**/*.ts`. Test files are excluded there and carry no product
78-
// logic, so they never count toward the gate or the excluded tally (which
79-
// exists to surface hidden logic, not test code).
80-
export function isTestFile(path: string): boolean {
81-
return /\.test\.ts$/.test(path) || /(^|\/)__tests__\//.test(path);
82-
}
83-
84-
// Both coverage roots, package-generic so a new workspace needs no edit here.
85-
// Anything outside them — scripts, package-level `test/`, `.tsx` — stays out.
86-
const SOURCE_ROOT = /^(?:packages\/[^/]+\/)?src\/.*\.ts$/;
87-
88-
export function isIncludableSource(path: string): boolean {
89-
return SOURCE_ROOT.test(path) && !isTestFile(path);
90-
}
91-
9278
// A changed line looks like product code (not blank, not a comment-only line).
9379
// Used only for the non-gating excluded tally, where no lcov signal exists.
9480
function isCodeLike(text: string | undefined): boolean {
@@ -205,6 +191,10 @@ function ignoredChangedLines(
205191
export type ComputeInput = {
206192
readonly diffs: readonly ChangedFileDiff[];
207193
readonly coverage: CoverageIndex;
194+
// The verified stamp's include/exclude: what the coverage run was told to
195+
// instrument. run.ts verifies the stamp before calling; a model test passes
196+
// the config the case is about.
197+
readonly stamp: CoverageConfig;
208198
// New-side content of a changed file (working tree at HEAD), or null when the
209199
// path cannot be read. Only consulted for the non-gating excluded tally.
210200
readonly fileLines: (path: string) => readonly string[] | null;
@@ -226,14 +216,14 @@ export function computeChangedCoverage(input: ComputeInput): ChangedCoverageResu
226216
diff.deleted ||
227217
!diff.regularFile ||
228218
diff.added.length === 0 ||
229-
!isIncludableSource(diff.path)
219+
!isInstrumentedPath(diff.path, input.stamp)
230220
)
231221
continue;
232222

233223
const cov = input.coverage.get(diff.path);
234224
if (!cov) {
235-
// Includable source absent from the all-files lcov report: an exclude glob
236-
// dropped it. Count its code-like added lines so the exclusion is visible.
225+
// Instrumented source absent from the lcov report: the run recorded no
226+
// counters for it. Count its code-like added lines so the gap is visible.
237227
const lines = input.fileLines(diff.path);
238228
const excluded = diff.added.filter((n) => isCodeLike(lines?.[n - 1]));
239229
if (excluded.length > 0) {

0 commit comments

Comments
 (0)