Skip to content

Commit 9dbb702

Browse files
author
Code Clawd
committed
test_runner: exclude BRDA entries for ignored lines in LCOV reporter
When a line is marked with /* node:coverage ignore next */, the DA entry for that line is correctly excluded from the LCOV output. However, the corresponding BRDA entry was still being emitted, causing branch coverage to report incorrect percentages when using the lcov reporter. This commit adds: - A new test fixture (brda_ignore_output.js) with a branch that has /* node:coverage ignore next */ to test the BRDA exclusion behavior - A new test runner (lcov_reporter_brda_ignore.js) for the fixture - A separate snapshot test case for this specific scenario - Updates to lcov.js to filter BRDA entries for ignored lines PR-URL: #62740
1 parent d0fa608 commit 9dbb702

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

lib/internal/test_runner/reporter/lcov.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const { relative } = require('path');
44
const Transform = require('internal/streams/transform');
5+
const { SafeSet } = require('internal/primordials');
56

67
// This reporter is based on the LCOV format, as described here:
78
// https://ltp.sourceforge.net/coverage/lcov/geninfo.1.php
@@ -68,7 +69,20 @@ class LcovReporter extends Transform {
6869
// Taken is either '-' if the basic block containing the branch was
6970
// never executed or a number indicating how often that branch was
7071
// taken.
72+
// Build set of ignored line numbers from coverage data.
73+
// A line is ignored if it appears with ignore=true in the file.lines array.
74+
const ignoredLineSet = new SafeSet();
75+
for (let k = 0; k < file.lines.length; k++) {
76+
if (file.lines[k].ignore) {
77+
ignoredLineSet.add(file.lines[k].line);
78+
}
79+
}
80+
7181
for (let j = 0; j < file.branches.length; j++) {
82+
// Skip branches that point to ignored lines.
83+
if (ignoredLineSet.has(file.branches[j].line)) {
84+
continue;
85+
}
7286
lcov += `BRDA:${file.branches[j].line},${j},0,${file.branches[j].count}\n`;
7387
}
7488

@@ -104,4 +118,4 @@ class LcovReporter extends Transform {
104118
}
105119
}
106120

107-
module.exports = LcovReporter;
121+
module.exports = LcovReporter;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
require('../../../common');
3+
const test = require('node:test');
4+
5+
function funcWithIgnoredBranch(value) {
6+
/* node:coverage ignore next */
7+
if (value > 0) { // This branch should be ignored
8+
return 'positive';
9+
} else {
10+
return 'negative';
11+
}
12+
}
13+
14+
test('should not report BRDA for ignored branch', () => {
15+
funcWithIgnoredBranch(1); // Only call one path of the branch
16+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
require('../../../common');
3+
const fixtures = require('../../../common/fixtures');
4+
const spawn = require('node:child_process').spawn;
5+
6+
spawn(
7+
process.execPath,
8+
[
9+
'--no-warnings',
10+
'--experimental-test-coverage',
11+
'--test-coverage-exclude=!test/**',
12+
'--test-reporter', 'lcov',
13+
fixtures.path('test-runner/output/brda_ignore_output.js'),
14+
],
15+
{ stdio: 'inherit' },
16+
);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
TN:
2+
SF:test/fixtures/test-runner/output/brda_ignore_output.js
3+
FN:5,funcWithIgnoredBranch
4+
FN:14,anonymous_1
5+
FNDA:1,funcWithIgnoredBranch
6+
FNDA:1,anonymous_1
7+
FNF:2
8+
FNH:2
9+
BRDA:1,0,0,1
10+
BRDA:5,1,0,1
11+
BRDA:9,2,0,0
12+
BRDA:14,3,0,1
13+
BRF:4
14+
BRH:3
15+
DA:1,1
16+
DA:2,1
17+
DA:3,1
18+
DA:4,1
19+
DA:5,1
20+
DA:6,1
21+
DA:8,1
22+
DA:9,1
23+
DA:10,0
24+
DA:11,0
25+
DA:12,1
26+
DA:13,1
27+
DA:14,1
28+
DA:15,1
29+
DA:16,1
30+
LH:14
31+
LF:16
32+
end_of_record
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as common from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import { spawnAndAssert, lcovTransform, ensureCwdIsProjectRoot } from '../common/assertSnapshot.js';
4+
5+
if (!process.features.inspector) {
6+
common.skip('inspector support required');
7+
}
8+
9+
ensureCwdIsProjectRoot();
10+
await spawnAndAssert(
11+
fixtures.path('test-runner/output/lcov_reporter_brda_ignore.js'),
12+
lcovTransform,
13+
);

0 commit comments

Comments
 (0)