From e7f8cf29440ba867df0119922348cebf8bb7026b Mon Sep 17 00:00:00 2001 From: Charles Samborski Date: Sun, 23 Dec 2018 06:21:23 +0100 Subject: [PATCH] fix: file URL to system path conversion (#46) This commit fixes the conversion from `file://` urls to system-dependent paths. It ensures that it works on Windows and with special characters. --- lib/report.js | 20 +++++++++++++------- package-lock.json | 19 +++++++++++++++++++ package.json | 1 + 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/report.js b/lib/report.js index 3cb2d119..8851a601 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') @@ -74,7 +75,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) } @@ -101,21 +102,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 01223b1f..e1d4c85e 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 259ae8f0..bf6ab5fc 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",