From 1330d57acf14b57ee2d6bc6b13891051de757d7c Mon Sep 17 00:00:00 2001 From: Charles Samborski Date: Wed, 21 Nov 2018 18:46:44 +0100 Subject: [PATCH] fix: file URL to system path conversion This commit fixes the conversion from `file://` urls to system-dependent paths. It ensures that it works on Windows and with special characters. - Closes bcoe/c8#41 - Closes bcoe/c8#45 --- lib/report.js | 20 +++++++++++++------- package-lock.json | 19 +++++++++++++++++++ package.json | 1 + test/integration.js.snap | 6 +++--- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/report.js b/lib/report.js index eff01778..3bde822f 100644 --- a/lib/report.js +++ b/lib/report.js @@ -1,4 +1,5 @@ const Exclude = require('test-exclude') +const furi = require('furi') const libCoverage = require('istanbul-lib-coverage') const libReport = require('istanbul-lib-report') const reports = require('istanbul-reports') @@ -72,7 +73,7 @@ class Report { _getMergedProcessCov () { const v8ProcessCovs = [] for (const v8ProcessCov of this._loadReports()) { - v8ProcessCovs.push(this._filterProcessCov(v8ProcessCov)) + v8ProcessCovs.push(this._normalizeProcessCov(v8ProcessCov)) } return mergeProcessCovs(v8ProcessCovs) } @@ -99,21 +100,26 @@ class Report { } /** - * Returns a filtered process coverage. + * Normalizes a process coverage. + * + * This function replaces file URLs (`url` property) by their corresponding + * system-dependent path and applies the current inclusion rules to filter out + * the excluded script coverages. * * 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. + * @param v8ProcessCov V8 process coverage to normalize. + * @return {v8ProcessCov} Normalized V8 process coverage. * @private */ - _filterProcessCov (v8ProcessCov) { + _normalizeProcessCov (v8ProcessCov) { const result = [] for (const v8ScriptCov of v8ProcessCov.result) { - // TODO: implement real fix from https://github.com/nodejs/node/issues/23783 - v8ScriptCov.url = v8ScriptCov.url.replace(/^file:\/\//, '') + if (/^file:\/\//.test(v8ScriptCov.url)) { + v8ScriptCov.url = furi.toSysPath(v8ScriptCov.url) + } if (this.exclude.shouldInstrument(v8ScriptCov.url) && (!this.omitRelative || isAbsolute(v8ScriptCov.url))) { result.push(v8ScriptCov) diff --git a/package-lock.json b/package-lock.json index 76c18b51..7d11de87 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,11 @@ "resolved": "https://registry.npmjs.org/@c88/v8-coverage/-/v8-coverage-0.1.0.tgz", "integrity": "sha512-Y3NzP6KPpYlHx0FDOY9E7f1TgtorGuSKwUslgIgOOIsS4WP5UsJOckCW5WtAXB5GP8VLyJoXwJhdIXQVAfY1ew==" }, + "@types/is-windows": { + "version": "0.2.0", + "resolved": "http://registry.npmjs.org/@types/is-windows/-/is-windows-0.2.0.tgz", + "integrity": "sha1-byTuSHMdMRaOpRBhDW3RXl/Jxv8=" + }, "JSONStream": { "version": "1.3.4", "resolved": "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.4.tgz", @@ -1680,6 +1685,15 @@ "integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=", "dev": true }, + "furi": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/furi/-/furi-1.0.0.tgz", + "integrity": "sha512-jwY6hz7dEXS+c0FAiPFJ20W6QnwZO0uthR0xgWoM+giKN3RLKWDDOiG17VUD+Vmw8A3Kj327cxAKUyNbWahnsQ==", + "requires": { + "@types/is-windows": "^0.2.0", + "is-windows": "^1.0.2" + } + }, "get-caller-file": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.2.tgz", @@ -2310,6 +2324,11 @@ "integrity": "sha1-Sw2hRCEE0bM2NA6AeX6GXPOffXI=", "dev": true }, + "is-windows": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz", + "integrity": "sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==" + }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", diff --git a/package.json b/package.json index 3708d329..38c9916c 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "@c88/v8-coverage": "^0.1.0", "find-up": "^3.0.0", "foreground-child": "^1.5.6", + "furi": "^1.0.0", "istanbul-lib-coverage": "^2.0.1", "istanbul-lib-report": "^2.0.1", "istanbul-reports": "^2.0.0", diff --git a/test/integration.js.snap b/test/integration.js.snap index ebf19de2..ebae89c7 100644 --- a/test/integration.js.snap +++ b/test/integration.js.snap @@ -8,12 +8,12 @@ second --------------------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | --------------------|----------|----------|----------|----------|-------------------| -All files | 95 | 72.22 | 0 | 95 | | +All files | 95.09 | 72.73 | 0 | 95.09 | | bin | 87.76 | 62.5 | 100 | 87.76 | | c8.js | 87.76 | 62.5 | 100 | 87.76 | 35,39,46,47,48,49 | - lib | 96.65 | 62.07 | 100 | 96.65 | | + lib | 96.74 | 63.33 | 100 | 96.74 | | parse-args.js | 97.47 | 44.44 | 100 | 97.47 | 55,56 | - report.js | 96.15 | 70 | 100 | 96.15 | 56,57,95,96,97 | + report.js | 96.32 | 71.43 | 100 | 96.32 | 57,58,96,97,98 | test/fixtures | 95.16 | 94.12 | 0 | 95.16 | | async.js | 100 | 100 | 100 | 100 | | multiple-spawn.js | 100 | 100 | 100 | 100 | |