Skip to content

Commit 6d26b06

Browse files
committed
test_runner: exclude ignored branches from lcov output
1 parent c52fad3 commit 6d26b06

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

lib/internal/test_runner/reporter/lcov.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,38 @@ class LcovReporter extends Transform {
6868
// Taken is either '-' if the basic block containing the branch was
6969
// never executed or a number indicating how often that branch was
7070
// taken.
71+
let branchCount = 0;
72+
let coveredBranchCount = 0;
73+
7174
for (let j = 0; j < file.branches.length; j++) {
72-
lcov += `BRDA:${file.branches[j].line},${j},0,${file.branches[j].count}\n`;
75+
const branch = file.branches[j];
76+
let lineReported = false;
77+
78+
for (let k = 0; k < file.lines.length; k++) {
79+
if (file.lines[k].line === branch.line) {
80+
lineReported = true;
81+
break;
82+
}
83+
}
84+
85+
if (!lineReported) {
86+
continue;
87+
}
88+
89+
lcov += `BRDA:${branch.line},${branchCount},0,${branch.count}\n`;
90+
91+
branchCount++;
92+
93+
if (branch.count !== 0) {
94+
coveredBranchCount++;
95+
}
7396
}
7497

7598
// Branch coverage summaries are stored in two lines:
7699
// ## BRF:\<number of branches found\>
77100
// ## BRH:\<number of branches hit\>
78-
lcov += `BRF:${file.totalBranchCount}\n`;
79-
lcov += `BRH:${file.coveredBranchCount}\n`;
101+
lcov += `BRF:${branchCount}\n`;
102+
lcov += `BRH:${coveredBranchCount}\n`;
80103

81104
// Then there is a list of execution counts for each instrumented line
82105
// (i.e. a line which resulted in executable code):

test/parallel/test-runner-coverage.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const common = require('../common');
33
const assert = require('node:assert');
44
const { spawnSync } = require('node:child_process');
5-
const { readdirSync } = require('node:fs');
5+
const { readdirSync, writeFileSync } = require('node:fs');
66
const { test } = require('node:test');
77
const fixtures = require('../common/fixtures');
88
const tmpdir = require('../common/tmpdir');
@@ -77,6 +77,43 @@ function getSpecCoverageFixtureReport() {
7777
return report;
7878
}
7979

80+
test('lcov reporter excludes BRDA entries for ignored lines', skipIfNoInspector, () => {
81+
const fixture = tmpdir.resolve('lcov-ignore-branch.test.js');
82+
83+
writeFileSync(fixture, `
84+
'use strict';
85+
86+
const test = require('node:test');
87+
88+
test('ignored branch', () => {
89+
// node:coverage ignore next
90+
if (false) {
91+
throw new Error('ignored');
92+
}
93+
94+
if (true) {
95+
// Covered branch to ensure LCOV still reports branch data.
96+
}
97+
});
98+
`);
99+
100+
const child = spawnSync(process.execPath, [
101+
'--test',
102+
'--experimental-test-coverage',
103+
'--test-reporter',
104+
'lcov',
105+
fixture,
106+
]);
107+
108+
assert.strictEqual(child.stderr.toString(), '');
109+
assert.strictEqual(child.status, 0);
110+
111+
const stdout = child.stdout.toString();
112+
113+
assert(!stdout.includes('BRDA:8,'));
114+
assert.match(stdout, /BRDA:/);
115+
});
116+
80117
test('test coverage report', async (t) => {
81118
await t.test('handles the inspector not being available', (t) => {
82119
if (process.features.inspector) {

0 commit comments

Comments
 (0)