Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: process coverage merging #37

Merged
merged 1 commit into from
Oct 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 55 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 @@ -40,39 +40,49 @@ class Report {
tree.visit(reports.create(_reporter), context)
})
}

_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 +97,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 <ben@npmjs.com>",
"license": "ISC",
"dependencies": {
"@c88/v8-coverage": "^0.1.0",
demurgos marked this conversation as resolved.
Show resolved Hide resolved
"find-up": "^3.0.0",
"foreground-child": "^1.5.6",
"istanbul-lib-coverage": "^2.0.1",
Expand All @@ -40,7 +41,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 | 93.01 | 69.81 | 0 | 93.01 | |
All files | 94.97 | 72.22 | 0 | 94.97 | |
bin | 87.76 | 62.5 | 100 | 87.76 | |
c8.js | 87.76 | 62.5 | 100 | 87.76 | 35,39,46,47,48,49 |
lib | 93.71 | 62.96 | 100 | 93.71 | |
lib | 96.62 | 62.07 | 100 | 96.62 | |
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.09 | 70 | 100 | 96.09 | 56,57,95,96,97 |
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