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: https://github.com/nodejs/node/pull/47850
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Beth Griggs <bethanyngriggs@gmail.com>
pull/47953/head
cjihrig 2023-05-10 10:08:00 -04:00 committed by GitHub
parent 19afcba9d4
commit 4360389d67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 3 deletions

View File

@ -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;

View File

@ -1,4 +1,7 @@
'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');
@ -6,3 +9,17 @@ 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);

View File

@ -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');
@ -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));