Skip to content

Commit 195d2ae

Browse files
committed
test_runner: merge call counts of functions without block coverage
When merging the coverage reports of several processes, an entry without block coverage was treated as carrying no data, and was replaced outright by an entry with block coverage. That holds for a function that was never compiled, which V8 reports with a count of 0. It does not hold for a function that V8 could not instrument, such as one deserialized from a code cache or compiled before coverage started: V8 reports those with their real call count. Depending on the order in which the reports were read, a function that ran could be reported as never run. Add up the call counts whenever either entry lacks block coverage, and keep the block level ranges if one of them has them. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
1 parent 502c117 commit 195d2ae

4 files changed

Lines changed: 55 additions & 10 deletions

File tree

‎lib/internal/test_runner/coverage.js‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -754,19 +754,17 @@ function mergeCoverageScripts(oldScript, newScript) {
754754
// These are the same functions.
755755
found = true;
756756

757-
// If newFn is block level coverage, then it will:
758-
// - Replace oldFn if oldFn is not block level coverage.
759-
// - Merge with oldFn if it is also block level coverage.
760-
// If newFn is not block level coverage, then it has no new data.
761-
if (newFn.isBlockCoverage) {
762-
if (oldFn.isBlockCoverage) {
763-
// Merge the oldFn ranges with the newFn ranges.
764-
mergeCoverageRanges(oldFn, newFn);
765-
} else {
766-
// Replace oldFn with newFn.
757+
if (newFn.isBlockCoverage && oldFn.isBlockCoverage) {
758+
mergeCoverageRanges(oldFn, newFn);
759+
} else {
760+
// A function without block level coverage only has a call count.
761+
// Keep the block level ranges if either side has them.
762+
const count = oldFn.ranges[0].count + newFn.ranges[0].count;
763+
if (newFn.isBlockCoverage) {
767764
oldFn.isBlockCoverage = true;
768765
oldFn.ranges = newFn.ranges;
769766
}
767+
oldFn.ranges[0].count = count;
770768
}
771769

772770
break;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function a() {}
2+
function b() {}
3+
function c() {}
4+
function d() {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
const { writeFileSync } = require('node:fs');
3+
const { join } = require('node:path');
4+
const { pathToFileURL } = require('node:url');
5+
6+
function fn(functionName, startOffset, count, isBlockCoverage = false) {
7+
const endOffset = functionName ? startOffset + 15 : 64;
8+
return { functionName, ranges: [{ startOffset, endOffset, count }], isBlockCoverage };
9+
}
10+
11+
function writeCoverage(id, functions) {
12+
writeFileSync(
13+
join(process.env.NODE_V8_COVERAGE, `coverage-${id}-0000000000000-0.json`),
14+
JSON.stringify({
15+
result: [{
16+
scriptId: '1',
17+
url: pathToFileURL(join(__dirname, 'lib.js')).href,
18+
functions: [fn('', 0, 1), ...functions],
19+
}],
20+
}),
21+
);
22+
}
23+
24+
writeCoverage(1, [fn('a', 0, 1), fn('b', 16, 0), fn('c', 32, 1), fn('d', 48, 0, true)]);
25+
writeCoverage(2, [fn('a', 0, 0), fn('b', 16, 1), fn('c', 32, 0, true), fn('d', 48, 1)]);

‎test/parallel/test-runner-coverage.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,21 @@ test('coverage with directory and file named "file"', skipIfNoInspector, () => {
657657
assert.strictEqual(result.status, 0);
658658
assertIncludesReport(result, 'start of coverage report');
659659
});
660+
661+
test('merges call counts of functions without block coverage', skipIfNoInspector, () => {
662+
const args = [
663+
'--test',
664+
'--experimental-test-coverage',
665+
'--test-coverage-exclude=!test/**',
666+
'--test-reporter',
667+
'tap',
668+
'test.js',
669+
];
670+
const result = spawnSync(process.execPath, args, {
671+
cwd: fixtures.path('test-runner', 'coverage-function-counts'),
672+
});
673+
674+
assert.strictEqual(result.stderr.toString(), '');
675+
assert.strictEqual(result.status, 0);
676+
assert.match(result.stdout.toString(), /# lib\.js +\| 100\.00 \| +100\.00 \| +100\.00 \|/);
677+
});

0 commit comments

Comments
 (0)