Skip to content

Commit 63ddd61

Browse files
committed
fix: process coverage merging
This commit uses the [new merge algorithm][merge] to handle sub-processes. It fixes the issues reported in bcoe/v8-coverage-merge#7 and improves performance. [merge]: https://github.com/demurgos/v8-coverage/blob/master/docs/merge.md
1 parent b0d1b7b commit 63ddd61

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
lines changed

lib/report.js

+54-23
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const libReport = require('istanbul-lib-report')
44
const reports = require('istanbul-reports')
55
const { readdirSync, readFileSync } = require('fs')
66
const { resolve, isAbsolute } = require('path')
7-
const v8CoverageMerge = require('v8-coverage-merge')
7+
const { mergeProcessCovs } = require('@c88/v8-coverage')
88
const v8toIstanbul = require('v8-to-istanbul')
99

1010
class Report {
@@ -41,38 +41,47 @@ class Report {
4141
})
4242
}
4343
_getCoverageMapFromAllCoverageFiles () {
44+
const v8ProcessCov = this._getMergedProcessCov()
45+
4446
const map = libCoverage.createCoverageMap({})
45-
const mergedResults = {}
46-
this._loadReports().forEach((report) => {
47-
report.result.forEach((result) => {
48-
if (this.exclude.shouldInstrument(result.url) &&
49-
(!this.omitRelative || isAbsolute(result.url))) {
50-
if (mergedResults[result.url]) {
51-
mergedResults[result.url] = v8CoverageMerge(
52-
mergedResults[result.url],
53-
result
54-
)
55-
} else {
56-
mergedResults[result.url] = result
57-
}
58-
}
59-
})
60-
})
6147

62-
Object.keys(mergedResults).forEach((url) => {
48+
for (const v8ScriptCov of v8ProcessCov.result) {
6349
try {
64-
const result = mergedResults[url]
65-
const path = resolve(this.resolve, result.url)
50+
const path = resolve(this.resolve, v8ScriptCov.url)
6651
const script = v8toIstanbul(path)
67-
script.applyCoverage(result.functions)
52+
script.applyCoverage(v8ScriptCov.functions)
6853
map.merge(script.toIstanbul())
6954
} catch (err) {
70-
console.warn(`file: ${url} error: ${err.stack}`)
55+
console.warn(`file: ${v8ScriptCov.url} error: ${err.stack}`)
7156
}
72-
})
57+
}
7358

7459
return map
7560
}
61+
62+
/**
63+
* Returns the merged V8 process coverage.
64+
*
65+
* The result is computed from the individual process coverages generated
66+
* by Node. It represents the sum of their counts.
67+
*
68+
* @return {ProcessCov} Merged V8 process coverage.
69+
* @private
70+
*/
71+
_getMergedProcessCov () {
72+
const v8ProcessCovs = []
73+
for (const v8ProcessCov of this._loadReports()) {
74+
v8ProcessCovs.push(this._filterProcessCov(v8ProcessCov))
75+
}
76+
return mergeProcessCovs(v8ProcessCovs)
77+
}
78+
79+
/**
80+
* Returns the list of V8 process coverages generated by Node.
81+
*
82+
* @return {ProcessCov[]} Process coverages generated by Node.
83+
* @private
84+
*/
7685
_loadReports () {
7786
const files = readdirSync(this.tempDirectory)
7887

@@ -87,6 +96,28 @@ class Report {
8796
}
8897
})
8998
}
99+
100+
/**
101+
* Returns a filtered process coverage.
102+
*
103+
* The result is a copy of the input, with script coverages filtered based
104+
* on their `url` and the current inclusion rules.
105+
* There is no deep cloning.
106+
*
107+
* @param v8ProcessCov V8 process coverage to filter.
108+
* @return {v8ProcessCov} Filtered V8 process coverage.
109+
* @private
110+
*/
111+
_filterProcessCov (v8ProcessCov) {
112+
const result = []
113+
for (const v8ScriptCov of v8ProcessCov.result) {
114+
if (this.exclude.shouldInstrument(v8ScriptCov.url) &&
115+
(!this.omitRelative || isAbsolute(v8ScriptCov.url))) {
116+
result.push(v8ScriptCov)
117+
}
118+
}
119+
return { result }
120+
}
90121
}
91122

92123
module.exports = function (opts) {

package-lock.json

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"rimraf": "^2.6.2",
4242
"test-exclude": "^5.0.0",
4343
"uuid": "^3.3.2",
44-
"v8-coverage-merge": "^1.1.2",
44+
"@c88/v8-coverage": "^0.1.0",
4545
"v8-to-istanbul": "^1.2.0",
4646
"yargs": "^12.0.2",
4747
"yargs-parser": "^10.1.0"

test/integration.js.snap

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ second
88
--------------------|----------|----------|----------|----------|-------------------|
99
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
1010
--------------------|----------|----------|----------|----------|-------------------|
11-
All files | 92.5 | 69.23 | 0 | 92.5 | |
11+
All files | 94.53 | 71.7 | 0 | 94.53 | |
1212
bin | 83.72 | 57.14 | 100 | 83.72 | |
1313
c8.js | 83.72 | 57.14 | 100 | 83.72 |... 22,40,41,42,43 |
14-
lib | 93.71 | 62.96 | 100 | 93.71 | |
14+
lib | 96.6 | 62.07 | 100 | 96.6 | |
1515
parse-args.js | 97.47 | 44.44 | 100 | 97.47 | 55,56 |
16-
report.js | 90.63 | 72.22 | 100 | 90.63 |... 70,71,85,86,87 |
17-
test/fixtures | 95.16 | 83.33 | 0 | 95.16 | |
16+
report.js | 96.06 | 70 | 100 | 96.06 | 55,56,94,95,96 |
17+
test/fixtures | 95.16 | 94.12 | 0 | 95.16 | |
1818
async.js | 100 | 100 | 100 | 100 | |
1919
multiple-spawn.js | 100 | 100 | 100 | 100 | |
2020
normal.js | 85.71 | 75 | 0 | 85.71 | 14,15,16 |
21-
subprocess.js | 100 | 71.43 | 100 | 100 | 9,13 |
21+
subprocess.js | 100 | 100 | 100 | 100 | |
2222
--------------------|----------|----------|----------|----------|-------------------|
2323
,"
2424
`;

0 commit comments

Comments
 (0)