From ea543d95f63c57c535e6e39f5b1d71f5ca5b4d66 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 10 May 2023 10:08:00 -0400 Subject: [PATCH] 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 Reviewed-By: Beth Griggs --- lib/internal/test_runner/coverage.js | 11 ++++++++++- .../v8-coverage/combined_coverage/third.test.js | 17 +++++++++++++++++ test/parallel/test-runner-coverage.js | 6 ++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/internal/test_runner/coverage.js b/lib/internal/test_runner/coverage.js index 1f5aa919a43707..5a098f71af633b 100644 --- a/lib/internal/test_runner/coverage.js +++ b/lib/internal/test_runner/coverage.js @@ -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; diff --git a/test/fixtures/v8-coverage/combined_coverage/third.test.js b/test/fixtures/v8-coverage/combined_coverage/third.test.js index 5416f440223907..78d7de60ee4cff 100644 --- a/test/fixtures/v8-coverage/combined_coverage/third.test.js +++ b/test/fixtures/v8-coverage/combined_coverage/third.test.js @@ -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); diff --git a/test/parallel/test-runner-coverage.js b/test/parallel/test-runner-coverage.js index 125fb3228d7a3d..0eba610288261b 100644 --- a/test/parallel/test-runner-coverage.js +++ b/test/parallel/test-runner-coverage.js @@ -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));