Skip to content

Commit 571e3e3

Browse files
committed
test_runner: match coverage globs against cwd-relative paths
Avoid applying coverage include/exclude glob checks to absolute paths so projects under directories named like test/ are not accidentally excluded. Add a regression test for cwd paths containing test. Made-with: Cursor
1 parent 69fdff9 commit 571e3e3

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

lib/internal/test_runner/coverage.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -480,20 +480,18 @@ class TestCoverage {
480480
// This check filters out files that match the exclude globs.
481481
if (excludeGlobs?.length > 0) {
482482
for (let i = 0; i < excludeGlobs.length; ++i) {
483-
if (
484-
matchGlobPattern(relativePath, excludeGlobs[i]) ||
485-
matchGlobPattern(absolutePath, excludeGlobs[i])
486-
) return true;
483+
if (matchGlobPattern(relativePath, excludeGlobs[i])) {
484+
return true;
485+
}
487486
}
488487
}
489488

490489
// This check filters out files that do not match the include globs.
491490
if (includeGlobs?.length > 0) {
492491
for (let i = 0; i < includeGlobs.length; ++i) {
493-
if (
494-
matchGlobPattern(relativePath, includeGlobs[i]) ||
495-
matchGlobPattern(absolutePath, includeGlobs[i])
496-
) return false;
492+
if (matchGlobPattern(relativePath, includeGlobs[i])) {
493+
return false;
494+
}
497495
}
498496
return true;
499497
}

test/parallel/test-runner-coverage-default-exclusion.mjs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { before, describe, it } from 'node:test';
33
import assert from 'node:assert';
44
import { spawnSync } from 'node:child_process';
55
import { cp } from 'node:fs/promises';
6+
import { join } from 'node:path';
67
import tmpdir from '../common/tmpdir.js';
78
import fixtures from '../common/fixtures.js';
89
const skipIfNoInspector = {
@@ -14,6 +15,7 @@ tmpdir.refresh();
1415
async function setupFixtures() {
1516
const fixtureDir = fixtures.path('test-runner', 'coverage-default-exclusion');
1617
await cp(fixtureDir, tmpdir.path, { recursive: true });
18+
await cp(fixtureDir, join(tmpdir.path, 'test'), { recursive: true });
1719
}
1820

1921
describe('test runner coverage default exclusion', skipIfNoInspector, () => {
@@ -114,4 +116,33 @@ describe('test runner coverage default exclusion', skipIfNoInspector, () => {
114116
assert(result.stdout.toString().includes(report));
115117
assert.strictEqual(result.status, 0);
116118
});
119+
120+
it('should use cwd-relative matching for default exclusion globs', async () => {
121+
const report = [
122+
'# start of coverage report',
123+
'# --------------------------------------------------------------',
124+
'# file | line % | branch % | funcs % | uncovered lines',
125+
'# --------------------------------------------------------------',
126+
'# logic-file.js | 66.67 | 100.00 | 50.00 | 5-7',
127+
'# --------------------------------------------------------------',
128+
'# all files | 66.67 | 100.00 | 50.00 | ',
129+
'# --------------------------------------------------------------',
130+
'# end of coverage report',
131+
].join('\n');
132+
133+
const args = [
134+
'--no-experimental-strip-types',
135+
'--test',
136+
'--experimental-test-coverage',
137+
'--test-reporter=tap',
138+
];
139+
const result = spawnSync(process.execPath, args, {
140+
env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path },
141+
cwd: join(tmpdir.path, 'test'),
142+
});
143+
144+
assert.strictEqual(result.stderr.toString(), '');
145+
assert(result.stdout.toString().includes(report));
146+
assert.strictEqual(result.status, 0);
147+
});
117148
});

0 commit comments

Comments
 (0)