Skip to content

Commit cf35b03

Browse files
heathduttonjuanarbol
authored andcommitted
test_runner: fix coverage report when a directory is named file
The coverage tree traversal checked `tree[key].file` to detect file entries. When a directory named "file" contained a file also named "file", this check incorrectly matched the child entry instead of file metadata, causing a TypeError when accessing `.path`. Check for `.file?.path` instead to correctly identify file metadata. Fixes: #61080 PR-URL: #61169 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 03f45a5 commit cf35b03

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

lib/internal/test_runner/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ function getCoverageReport(pad, summary, symbol, color, table) {
520520

521521
function printCoverageBodyTree(tree, depth = 0) {
522522
for (const key in tree) {
523-
if (tree[key].file) {
523+
if (tree[key].file?.path) {
524524
const file = tree[key].file;
525525
const fileName = ArrayPrototypePop(StringPrototypeSplit(file.path, sep));
526526

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
module.exports.fn = function() {
3+
return 1;
4+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
const { fn } = require('./file/file');
3+
const test = require('node:test');
4+
5+
test('coverage with file/file directory structure', () => {
6+
fn();
7+
});

test/parallel/test-runner-coverage.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,19 @@ test('correctly prints the coverage report of files contained in parent director
489489
assert(result.stdout.toString().includes(report));
490490
assert.strictEqual(result.status, 0);
491491
});
492+
493+
// Regression test for https://github.com/nodejs/node/issues/61080
494+
test('coverage with directory and file named "file"', skipIfNoInspector, () => {
495+
const fixture = fixtures.path('test-runner', 'coverage-file-name', 'test.js');
496+
const args = [
497+
'--experimental-test-coverage',
498+
'--test-reporter',
499+
'tap',
500+
fixture,
501+
];
502+
const result = spawnSync(process.execPath, args);
503+
504+
assert.strictEqual(result.stderr.toString(), '');
505+
assert.strictEqual(result.status, 0);
506+
assert(result.stdout.toString().includes('start of coverage report'));
507+
});

0 commit comments

Comments
 (0)