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

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎lib/internal/test_runner/coverage.js‎

Lines changed: 10 additions & 1 deletion
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
constfilePath=fileURLToPath(url);
102-
constsource=readFileSync(filePath,'utf8');
102+
letsource;
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
constlinesWithBreaks=
104113
RegExpPrototypeSymbolSplit(kLineSplitRegex,source);
105114
letignoreCount=0;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
'use strict';
2+
constassert=require('node:assert');
3+
const{ unlinkSync, writeFileSync }=require('node:fs')
4+
const{ join }=require('node:path');
25
const{ test }=require('node:test');
36
constcommon=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+
consttmpFilePath=join(process.env.NODE_TEST_TMPDIR,'temp-module.js');
15+
writeFileSync(tmpFilePath,`
16+
module.exports = {
17+
fn() {
18+
return 42;
19+
}
20+
};
21+
`);
22+
consttempModule=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‎

Lines changed: 4 additions & 2 deletions
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
constargs=[
169169
'--test','--experimental-test-coverage','--test-reporter','tap',fixture,
170170
];
171-
constresult=spawnSync(process.execPath,args);
171+
constresult=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)