Skip to content

Commit

Permalink
test_runner: omit inaccessible files from coverage
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cjihrig authored and MoLow committed Jul 6, 2023
1 parent 4bad757 commit ea543d9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,16 @@ class TestCoverage {
// original line endings because those characters are necessary for
// determining offsets in the file.
const filePath = fileURLToPath(url);
const source = readFileSync(filePath, 'utf8');
let source;

try {
source = readFileSync(filePath, 'utf8');
} catch {
// The file can no longer be read. It may have been deleted among
// other possibilities. Leave it out of the coverage report.
continue;
}

const linesWithBreaks =
RegExpPrototypeSymbolSplit(kLineSplitRegex, source);
let ignoreCount = 0;
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/v8-coverage/combined_coverage/third.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
'use strict';
const assert = require('node:assert');
const { unlinkSync, writeFileSync } = require('node:fs')
const { join } = require('node:path');
const { test } = require('node:test');
const common = require('./common');

test('third 1', () => {
common.fnC(1, 4);
common.fnD(99);
});

assert(process.env.NODE_TEST_TMPDIR);
const tmpFilePath = join(process.env.NODE_TEST_TMPDIR, 'temp-module.js');
writeFileSync(tmpFilePath, `
module.exports = {
fn() {
return 42;
}
};
`);
const tempModule = require(tmpFilePath);
assert.strictEqual(tempModule.fn(), 42);
// Deleted files should not be part of the coverage report.
unlinkSync(tmpFilePath);
6 changes: 4 additions & 2 deletions test/parallel/test-runner-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ test('coverage is combined for multiple processes', skipIfNoInspector, () => {
'| 100.00 | 100.00 | ',
'# test/fixtures/v8-coverage/combined_coverage/third.test.js | 100.00 | ' +
'100.00 | 100.00 | ',
'# all files | 90.72 | 72.73 | 88.89 |',
'# all files | 92.11 | 72.73 | 88.89 |',
'# end of coverage report',
].join('\n');

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

assert.strictEqual(result.stderr.toString(), '');
assert(result.stdout.toString().includes(report));
Expand Down

0 comments on commit ea543d9

Please sign in to comment.