Skip to content

Commit a69963e

Browse files
committed
fix(coverage-changed): widen test-material tally exclusion and share include matching
The excluded tally exists to surface hidden product logic, not test code, but isTestFile missed test-kit helpers and fixtures files the stamp excludes, so their lines were tallied as hidden product logic. Widen the predicate to cover them, with a regression test that fails without the fix. Extract one include-match helper in stamp.ts so isInstrumentedPath and isIncludedPath cannot drift, and qualify the CONTRIBUTING sentence about the tally to code-like, non-test additions.
1 parent 833e56a commit a69963e

5 files changed

Lines changed: 59 additions & 9 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ coverage run first. The fix is the same — re-run `pnpm test:coverage` on this
306306
fires before the `coverage-waiver` label is consulted, because a waiver skips a failure rather than
307307
blessing a report with no numbers to print. Which files owe coverage comes from the stamp's
308308
include/exclude: a file the config excludes is out of scope by declaration, but when an include root
309-
covers it its added lines are still tallied as excluded-path lines, as is a file the stamp instruments
309+
covers it, its code-like, non-test added lines are still tallied as excluded-path lines, as is a file the stamp instruments
310310
that lcov omits.
311311

312312
## Test app and Maestro compatibility

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ignoredLineSet,
88
parseLcov,
99
} from './model.ts';
10-
import { isInstrumentedPath } from '../coverage-crap/stamp.ts';
10+
import { isInstrumentedPath, type CoverageConfig } from '../coverage-crap/stamp.ts';
1111
import { COVERAGE_CONFIG } from './fixtures.ts';
1212

1313
test('parseLcov reads DA hits and BRDA taken, treating "-" as not taken', () => {
@@ -235,6 +235,28 @@ test('a stamp-excluded test file or a path outside every include root is not tal
235235
assert.equal(result.passed, true);
236236
});
237237

238+
test('stamp-excluded test-kit helpers and fixtures files are not tallied', () => {
239+
// The real stamp excludes these alongside test files (vitest.config.ts), so
240+
// they are test material, not product logic hiding under an exclude entry.
241+
const stamp: CoverageConfig = {
242+
...COVERAGE_CONFIG,
243+
exclude: [...COVERAGE_CONFIG.exclude, 'packages/test-kit/**', '**/*.fixtures.ts'],
244+
};
245+
const result = computeChangedCoverage({
246+
diffs: [
247+
diff('packages/test-kit/src/helper.ts', [1, 2, 3]),
248+
diff('src/data.fixtures.ts', [1, 2]),
249+
],
250+
coverage,
251+
stamp,
252+
fileLines: () => ['const kept = 1;', '', '// a comment'],
253+
});
254+
assert.equal(result.totalLines, 0);
255+
assert.equal(result.excluded.totalLines, 0);
256+
assert.deepEqual(result.excluded.files, []);
257+
assert.equal(result.passed, true);
258+
});
259+
238260
test('changed line suppressed by an ignore directive is tallied, not gated', () => {
239261
const cov = parseLcov(['SF:src/x.ts', 'DA:1,1', 'end_of_record'].join('\n'));
240262
const result = computeChangedCoverage({

‎scripts/coverage-changed/model.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,16 @@ function sum<T>(items: readonly T[], pick: (item: T) => number): number {
208208
}
209209

210210
// Test code never counts toward the gate or the excluded tally, which exists
211-
// to surface hidden product logic, not test code.
211+
// to surface hidden product logic, not test code. Test-kit helpers and
212+
// fixtures files are test material too — the coverage config excludes them
213+
// alongside test files — so their added lines stay out of the tally as well.
212214
function isTestFile(path: string): boolean {
213-
return /\.test\.ts$/.test(path) || /(^|\/)__tests__\//.test(path);
215+
return (
216+
/\.test\.ts$/.test(path) ||
217+
/(^|\/)__tests__\//.test(path) ||
218+
/\.fixtures\.ts$/.test(path) ||
219+
path.startsWith('packages/test-kit/')
220+
);
214221
}
215222

216223
// Code-like added lines of a file the run did not measure, as the non-gating

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
beginCoverageRun,
1010
describeStampMismatch,
1111
finishCoverageRun,
12+
isIncludedPath,
1213
isInstrumentedPath,
1314
verifyCoverageStamp,
1415
type CoverageConfig,
@@ -427,3 +428,12 @@ test('eligibility reaches dot-prefixed segments, the way the provider matches th
427428
assert.equal(isInstrumentedPath('src/.hidden.ts', CONFIG), true);
428429
assert.equal(isInstrumentedPath('src/.generated/deep.ts', CONFIG), true);
429430
});
431+
432+
test('include matching is the same check both eligibility predicates use', () => {
433+
// `isIncludedPath` is the include half of `isInstrumentedPath`: an excluded
434+
// file still counts as include-covered, a path outside every include root
435+
// counts as neither.
436+
assert.equal(isIncludedPath('src/generated/table.ts', CONFIG), true);
437+
assert.equal(isIncludedPath('scripts/tool.ts', CONFIG), false);
438+
assert.equal(isIncludedPath('src/.hidden.ts', CONFIG), true);
439+
});

‎scripts/coverage-crap/stamp.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,27 @@ function withHiddenVisible(text: string): string {
169169
.join('/');
170170
}
171171

172+
/**
173+
* Whether any pattern in `patterns` matches `candidate`, after the same
174+
* hidden-segment normalization the provider applies. One check, so the
175+
* include half of eligibility cannot drift between its two callers.
176+
*/
177+
function matchesAnyPattern(candidate: string, patterns: readonly string[]): boolean {
178+
return patterns.some((pattern) => path.matchesGlob(candidate, withHiddenVisible(pattern)));
179+
}
180+
181+
function normalizedCandidate(relativePath: string): string {
182+
return withHiddenVisible(relativePath.split('\\').join('/'));
183+
}
184+
172185
/**
173186
* Whether the resolved config instruments `relativePath`. Vitest matches these patterns against the
174187
* path relative to the root, so `src/**` never reaches `packages/x/src/**`.
175188
*/
176189
export function isInstrumentedPath(relativePath: string, config: CoverageConfig): boolean {
177-
const candidate = withHiddenVisible(relativePath.split('\\').join('/'));
190+
const candidate = normalizedCandidate(relativePath);
178191
return (
179-
config.include.some((pattern) => path.matchesGlob(candidate, withHiddenVisible(pattern))) &&
180-
!config.exclude.some((pattern) => path.matchesGlob(candidate, withHiddenVisible(pattern)))
192+
matchesAnyPattern(candidate, config.include) && !matchesAnyPattern(candidate, config.exclude)
181193
);
182194
}
183195

@@ -187,8 +199,7 @@ export function isInstrumentedPath(relativePath: string, config: CoverageConfig)
187199
* then took it back out.
188200
*/
189201
export function isIncludedPath(relativePath: string, config: CoverageConfig): boolean {
190-
const candidate = withHiddenVisible(relativePath.split('\\').join('/'));
191-
return config.include.some((pattern) => path.matchesGlob(candidate, withHiddenVisible(pattern)));
202+
return matchesAnyPattern(normalizedCandidate(relativePath), config.include);
192203
}
193204

194205
/**

0 commit comments

Comments
 (0)