Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: omit inaccessible files from coverage #47850

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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