Skip to content

Commit ea543d9

Browse files
cjihrigMoLow
authored andcommitted
test_runner: omit inaccessible files from coverage
If V8 generates code coverage for a file that is later inaccessible to the test runner, then omit that file from the coverage report. PR-URL: #47850 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Beth Griggs <bethanyngriggs@gmail.com>
1 parent 4bad757 commit ea543d9

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lib/internal/test_runner/coverage.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,16 @@ class TestCoverage {
9999
// original line endings because those characters are necessary for
100100
// determining offsets in the file.
101101
const filePath = fileURLToPath(url);
102-
const source = readFileSync(filePath, 'utf8');
102+
let source;
103+
104+
try {
105+
source = readFileSync(filePath, 'utf8');
106+
} catch {
107+
// The file can no longer be read. It may have been deleted among
108+
// other possibilities. Leave it out of the coverage report.
109+
continue;
110+
}
111+
103112
const linesWithBreaks =
104113
RegExpPrototypeSymbolSplit(kLineSplitRegex, source);
105114
let ignoreCount = 0;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
'use strict';
2+
const assert = require('node:assert');
3+
const { unlinkSync, writeFileSync } = require('node:fs')
4+
const { join } = require('node:path');
25
const { test } = require('node:test');
36
const common = require('./common');
47

58
test('third 1', () => {
69
common.fnC(1, 4);
710
common.fnD(99);
811
});
12+
13+
assert(process.env.NODE_TEST_TMPDIR);
14+
const tmpFilePath = join(process.env.NODE_TEST_TMPDIR, 'temp-module.js');
15+
writeFileSync(tmpFilePath, `
16+
module.exports = {
17+
fn() {
18+
return 42;
19+
}
20+
};
21+
`);
22+
const tempModule = require(tmpFilePath);
23+
assert.strictEqual(tempModule.fn(), 42);
24+
// Deleted files should not be part of the coverage report.
25+
unlinkSync(tmpFilePath);

test/parallel/test-runner-coverage.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ test('coverage is combined for multiple processes', skipIfNoInspector, () => {
156156
'| 100.00 | 100.00 | ',
157157
'# test/fixtures/v8-coverage/combined_coverage/third.test.js | 100.00 | ' +
158158
'100.00 | 100.00 | ',
159-
'# all files | 90.72 | 72.73 | 88.89 |',
159+
'# all files | 92.11 | 72.73 | 88.89 |',
160160
'# end of coverage report',
161161
].join('\n');
162162

@@ -168,7 +168,9 @@ test('coverage is combined for multiple processes', skipIfNoInspector, () => {
168168
const args = [
169169
'--test', '--experimental-test-coverage', '--test-reporter', 'tap', fixture,
170170
];
171-
const result = spawnSync(process.execPath, args);
171+
const result = spawnSync(process.execPath, args, {
172+
env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path }
173+
});
172174

173175
assert.strictEqual(result.stderr.toString(), '');
174176
assert(result.stdout.toString().includes(report));

0 commit comments

Comments
 (0)