diff --git a/lib/report.js b/lib/report.js index 9fb53294..18f46cd0 100644 --- a/lib/report.js +++ b/lib/report.js @@ -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 { @@ -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) @@ -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) { diff --git a/package-lock.json b/package-lock.json index 23895723..4a2f0758 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,11 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@c88/v8-coverage": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/@c88/v8-coverage/-/v8-coverage-0.1.0.tgz", + "integrity": "sha512-Y3NzP6KPpYlHx0FDOY9E7f1TgtorGuSKwUslgIgOOIsS4WP5UsJOckCW5WtAXB5GP8VLyJoXwJhdIXQVAfY1ew==" + }, "JSONStream": { "version": "1.3.4", "resolved": "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.4.tgz", @@ -187,7 +192,7 @@ "dependencies": { "chalk": { "version": "1.1.3", - "resolved": "http://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", "dev": true, "requires": { @@ -438,7 +443,7 @@ }, "commander": { "version": "2.15.1", - "resolved": "http://registry.npmjs.org/commander/-/commander-2.15.1.tgz", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz", "integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==", "dev": true }, @@ -825,7 +830,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, @@ -1545,7 +1550,7 @@ }, "external-editor": { "version": "2.2.0", - "resolved": "http://registry.npmjs.org/external-editor/-/external-editor-2.2.0.tgz", + "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-2.2.0.tgz", "integrity": "sha512-bSn6gvGxKt+b7+6TKEv1ZycHleA7aHhRHyAqJyp5pbUFuYYNIzpZnQDk7AsYckyWdEnTeAnay0aCy2aV6iTk9A==", "dev": true, "requires": { @@ -1779,7 +1784,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, @@ -2812,7 +2817,7 @@ }, "mkdirp": { "version": "0.5.1", - "resolved": "http://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", "dev": true, "requires": { @@ -3741,7 +3746,7 @@ }, "chalk": { "version": "1.1.3", - "resolved": "http://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", "dev": true, "requires": { @@ -4158,11 +4163,6 @@ "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" }, - "v8-coverage-merge": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/v8-coverage-merge/-/v8-coverage-merge-1.1.2.tgz", - "integrity": "sha512-ZgRnN6rS7F5KL0y3UWte70J9JL7yRSiz5B2SG1EYu4lg+pNgSDP7+aIIkzWc6469ucmVlT6HLxeZs3iTrEebaw==" - }, "v8-to-istanbul": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-1.2.0.tgz", diff --git a/package.json b/package.json index adf91896..036306c6 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "author": "Ben Coe ", "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", @@ -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" diff --git a/test/integration.js.snap b/test/integration.js.snap index c4ad80af..c147625a 100644 --- a/test/integration.js.snap +++ b/test/integration.js.snap @@ -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 | | --------------------|----------|----------|----------|----------|-------------------| ," `;