Skip to content

Commit

Permalink
fix: process coverage merging
Browse files Browse the repository at this point in the history
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
  • Loading branch information
demurgos committed Oct 9, 2018
1 parent b0d1b7b commit 7fd58cc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 41 deletions.
77 changes: 54 additions & 23 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const libReport = require('istanbul-lib-report')
const reports = require('istanbul-reports')
const { readdirSync, readFileSync } = require('fs')
const { resolve, isAbsolute } = require('path')
const v8CoverageMerge = require('v8-coverage-merge')
const { mergeProcessCovs } = require('@c88/v8-coverage')
const v8toIstanbul = require('v8-to-istanbul')

class Report {
Expand Down Expand Up @@ -41,38 +41,47 @@ class Report {
})
}
_getCoverageMapFromAllCoverageFiles () {
const v8ProcessCov = this._getMergedProcessCov()

const map = libCoverage.createCoverageMap({})
const mergedResults = {}
this._loadReports().forEach((report) => {
report.result.forEach((result) => {
if (this.exclude.shouldInstrument(result.url) &&
(!this.omitRelative || isAbsolute(result.url))) {
if (mergedResults[result.url]) {
mergedResults[result.url] = v8CoverageMerge(
mergedResults[result.url],
result
)
} else {
mergedResults[result.url] = result
}
}
})
})

Object.keys(mergedResults).forEach((url) => {
for (const v8ScriptCov of v8ProcessCov.result) {
try {
const result = mergedResults[url]
const path = resolve(this.resolve, result.url)
const path = resolve(this.resolve, v8ScriptCov.url)
const script = v8toIstanbul(path)
script.applyCoverage(result.functions)
script.applyCoverage(v8ScriptCov.functions)
map.merge(script.toIstanbul())
} catch (err) {
console.warn(`file: ${url} error: ${err.stack}`)
console.warn(`file: ${v8ScriptCov.url} error: ${err.stack}`)
}
})
}

return map
}

/**
* Returns the merged V8 process coverage.
*
* The result is computed from the individual process coverages generated
* by Node. It represents the sum of their counts.
*
* @return {ProcessCov} Merged V8 process coverage.
* @private
*/
_getMergedProcessCov () {
const v8ProcessCovs = []
for (const v8ProcessCov of this._loadReports()) {
v8ProcessCovs.push(this._filterProcessCov(v8ProcessCov))
}
return mergeProcessCovs(v8ProcessCovs)
}

/**
* Returns the list of V8 process coverages generated by Node.
*
* @return {ProcessCov[]} Process coverages generated by Node.
* @private
*/
_loadReports () {
const files = readdirSync(this.tempDirectory)

Expand All @@ -87,6 +96,28 @@ class Report {
}
})
}

/**
* Returns a filtered process coverage.
*
* The result is a copy of the input, with script coverages filtered based
* on their `url` and the current inclusion rules.
* There is no deep cloning.
*
* @param v8ProcessCov V8 process coverage to filter.
* @return {v8ProcessCov} Filtered V8 process coverage.
* @private
*/
_filterProcessCov (v8ProcessCov) {
const result = []
for (const v8ScriptCov of v8ProcessCov.result) {
if (this.exclude.shouldInstrument(v8ScriptCov.url) &&
(!this.omitRelative || isAbsolute(v8ScriptCov.url))) {
result.push(v8ScriptCov)
}
}
return { result }
}
}

module.exports = function (opts) {
Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"author": "Ben Coe <[email protected]>",
"license": "ISC",
"dependencies": {
"@c88/v8-coverage": "^0.1.0",
"find-up": "^3.0.0",
"foreground-child": "^1.5.6",
"istanbul-lib-coverage": "^2.0.1",
Expand All @@ -41,7 +42,6 @@
"rimraf": "^2.6.2",
"test-exclude": "^5.0.0",
"uuid": "^3.3.2",
"v8-coverage-merge": "^1.1.2",
"v8-to-istanbul": "^1.2.0",
"yargs": "^12.0.2",
"yargs-parser": "^10.1.0"
Expand Down
10 changes: 5 additions & 5 deletions test/integration.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ second
--------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------------|----------|----------|----------|----------|-------------------|
All files | 92.5 | 69.23 | 0 | 92.5 | |
All files | 94.53 | 71.7 | 0 | 94.53 | |
bin | 83.72 | 57.14 | 100 | 83.72 | |
c8.js | 83.72 | 57.14 | 100 | 83.72 |... 22,40,41,42,43 |
lib | 93.71 | 62.96 | 100 | 93.71 | |
lib | 96.6 | 62.07 | 100 | 96.6 | |
parse-args.js | 97.47 | 44.44 | 100 | 97.47 | 55,56 |
report.js | 90.63 | 72.22 | 100 | 90.63 |... 70,71,85,86,87 |
test/fixtures | 95.16 | 83.33 | 0 | 95.16 | |
report.js | 96.06 | 70 | 100 | 96.06 | 55,56,94,95,96 |
test/fixtures | 95.16 | 94.12 | 0 | 95.16 | |
async.js | 100 | 100 | 100 | 100 | |
multiple-spawn.js | 100 | 100 | 100 | 100 | |
normal.js | 85.71 | 75 | 0 | 85.71 | 14,15,16 |
subprocess.js | 100 | 71.43 | 100 | 100 | 9,13 |
subprocess.js | 100 | 100 | 100 | 100 | |
--------------------|----------|----------|----------|----------|-------------------|
,"
`;
Expand Down

0 comments on commit 7fd58cc

Please sign in to comment.