Skip to content

Commit c817902

Browse files
authored
fix: address bugs with source remapping on Windows (bcoe#301)
1 parent 86bd364 commit c817902

4 files changed

Lines changed: 40 additions & 43 deletions

File tree

lib/report.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const Exclude = require('test-exclude')
2-
const furi = require('furi')
32
const libCoverage = require('istanbul-lib-coverage')
43
const libReport = require('istanbul-lib-report')
54
const reports = require('istanbul-reports')
65
const { readdirSync, readFileSync, statSync } = require('fs')
76
const { isAbsolute, resolve, extname } = require('path')
7+
const { pathToFileURL, fileURLToPath } = require('url')
88
const getSourceMapFromFile = require('./source-map-from-file')
99
// TODO: switch back to @c88/v8-coverage once patch is landed.
1010
const v8toIstanbul = require('v8-to-istanbul')
@@ -139,8 +139,8 @@ class Report {
139139
*/
140140
_getSourceMap (v8ScriptCov) {
141141
const sources = {}
142-
if (this.sourceMapCache[`file://${v8ScriptCov.url}`]) {
143-
const sourceMapAndLineLengths = this.sourceMapCache[`file://${v8ScriptCov.url}`]
142+
const sourceMapAndLineLengths = this.sourceMapCache[pathToFileURL(v8ScriptCov.url).href]
143+
if (sourceMapAndLineLengths) {
144144
// See: https://github.com/nodejs/node/pull/34305
145145
if (!sourceMapAndLineLengths.data) return
146146
sources.sourceMap = {
@@ -173,7 +173,7 @@ class Report {
173173
for (const v8ProcessCov of this._loadReports()) {
174174
if (this._isCoverageObject(v8ProcessCov)) {
175175
if (v8ProcessCov['source-map-cache']) {
176-
Object.assign(this.sourceMapCache, v8ProcessCov['source-map-cache'])
176+
Object.assign(this.sourceMapCache, this._normalizeSourceMapCache(v8ProcessCov['source-map-cache']))
177177
}
178178
v8ProcessCovs.push(this._normalizeProcessCov(v8ProcessCov, fileIndex))
179179
}
@@ -194,7 +194,7 @@ class Report {
194194
const stat = statSync(fullPath)
195195
const sourceMap = getSourceMapFromFile(fullPath)
196196
if (sourceMap) {
197-
this.sourceMapCache[`file://${fullPath}`] = { data: sourceMap }
197+
this.sourceMapCache[pathToFileURL(fullPath)] = { data: sourceMap }
198198
}
199199
emptyReports.push({
200200
scriptId: 0,
@@ -275,7 +275,7 @@ class Report {
275275
}
276276
if (/^file:\/\//.test(v8ScriptCov.url)) {
277277
try {
278-
v8ScriptCov.url = furi.toSysPath(v8ScriptCov.url)
278+
v8ScriptCov.url = fileURLToPath(v8ScriptCov.url)
279279
fileIndex.add(v8ScriptCov.url)
280280
} catch (err) {
281281
debuglog(`${err.stack}`)
@@ -290,6 +290,23 @@ class Report {
290290
}
291291
return { result }
292292
}
293+
294+
/**
295+
* Normalizes a V8 source map cache.
296+
*
297+
* This function normalizes file URLs to a system-independent format.
298+
*
299+
* @param v8SourceMapCache V8 source map cache to normalize.
300+
* @return {v8SourceMapCache} Normalized V8 source map cache.
301+
* @private
302+
*/
303+
_normalizeSourceMapCache (v8SourceMapCache) {
304+
const cache = {}
305+
for (const fileURL of Object.keys(v8SourceMapCache)) {
306+
cache[pathToFileURL(fileURLToPath(fileURL)).href] = v8SourceMapCache[fileURL]
307+
}
308+
return cache
309+
}
293310
}
294311

295312
module.exports = function (opts) {

package-lock.json

Lines changed: 0 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"@istanbuljs/schema": "^0.1.2",
3838
"find-up": "^5.0.0",
3939
"foreground-child": "^2.0.0",
40-
"furi": "^2.0.0",
4140
"istanbul-lib-coverage": "^3.0.0",
4241
"istanbul-lib-report": "^3.0.0",
4342
"istanbul-reports": "^3.0.2",

test/integration.js_10.snap

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ hey
152152
--------------------------|---------|----------|---------|---------|--------------------------------
153153
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
154154
--------------------------|---------|----------|---------|---------|--------------------------------
155-
All files | 73.17 | 58.02 | 62.16 | 73.17 |
155+
All files | 73.12 | 58.02 | 60.53 | 73.12 |
156156
bin | 78.85 | 60 | 66.67 | 78.85 |
157157
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
158-
lib | 76.44 | 51.79 | 72.73 | 76.44 |
158+
lib | 76.27 | 51.79 | 69.57 | 76.27 |
159159
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
160160
parse-args.js | 96.39 | 45.45 | 100 | 96.39 | 120-121,129-130,143-144
161-
report.js | 75.42 | 58.33 | 84.62 | 75.42 | ...216,246-247,274-275,281-283
161+
report.js | 75.16 | 58.33 | 78.57 | 75.16 | ...247,274-275,281-283,304-309
162162
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
163163
lib/commands | 45.65 | 75 | 16.67 | 45.65 |
164164
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
@@ -167,9 +167,9 @@ All files | 73.17 | 58.02 | 62.16 | 73.17 |
167167
async.js | 100 | 100 | 100 | 100 |
168168
normal.js | 75 | 66.67 | 33.33 | 75 | 14-16,18-20
169169
--------------------------|---------|----------|---------|---------|--------------------------------
170-
,ERROR: Coverage for lines (73.17%) does not meet global threshold (101%)
170+
,ERROR: Coverage for lines (73.12%) does not meet global threshold (101%)
171171
ERROR: Coverage for branches (58.02%) does not meet global threshold (82%)
172-
ERROR: Coverage for statements (73.17%) does not meet global threshold (95%)
172+
ERROR: Coverage for statements (73.12%) does not meet global threshold (95%)
173173
"
174174
`;
175175

@@ -187,9 +187,9 @@ ERROR: Coverage for branches (25%) does not meet threshold (82%) for lib/is-cjs-
187187
ERROR: Coverage for statements (90%) does not meet threshold (95%) for lib/is-cjs-esm-bridge.js
188188
ERROR: Coverage for lines (96.39%) does not meet threshold (101%) for lib/parse-args.js
189189
ERROR: Coverage for branches (45.45%) does not meet threshold (82%) for lib/parse-args.js
190-
ERROR: Coverage for lines (75.42%) does not meet threshold (101%) for lib/report.js
190+
ERROR: Coverage for lines (75.16%) does not meet threshold (101%) for lib/report.js
191191
ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/report.js
192-
ERROR: Coverage for statements (75.42%) does not meet threshold (95%) for lib/report.js
192+
ERROR: Coverage for statements (75.16%) does not meet threshold (95%) for lib/report.js
193193
ERROR: Coverage for lines (45%) does not meet threshold (101%) for lib/source-map-from-file.js
194194
ERROR: Coverage for statements (45%) does not meet threshold (95%) for lib/source-map-from-file.js
195195
ERROR: Coverage for lines (100%) does not meet threshold (101%) for test/fixtures/async.js
@@ -202,9 +202,9 @@ ERROR: Coverage for statements (75%) does not meet threshold (95%) for test/fixt
202202
exports[`c8 check-coverage exits with 0 if coverage within threshold 1`] = `",,"`;
203203

204204
exports[`c8 check-coverage exits with 1 if coverage is below threshold 1`] = `
205-
",,ERROR: Coverage for lines (73.17%) does not meet global threshold (101%)
205+
",,ERROR: Coverage for lines (73.12%) does not meet global threshold (101%)
206206
ERROR: Coverage for branches (58.02%) does not meet global threshold (82%)
207-
ERROR: Coverage for statements (73.17%) does not meet global threshold (95%)
207+
ERROR: Coverage for statements (73.12%) does not meet global threshold (95%)
208208
"
209209
`;
210210

@@ -287,13 +287,13 @@ exports[`c8 report generates report from existing temporary files 1`] = `
287287
",--------------------------|---------|----------|---------|---------|--------------------------------
288288
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
289289
--------------------------|---------|----------|---------|---------|--------------------------------
290-
All files | 73.17 | 58.02 | 62.16 | 73.17 |
290+
All files | 73.12 | 58.02 | 60.53 | 73.12 |
291291
bin | 78.85 | 60 | 66.67 | 78.85 |
292292
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
293-
lib | 76.44 | 51.79 | 72.73 | 76.44 |
293+
lib | 76.27 | 51.79 | 69.57 | 76.27 |
294294
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
295295
parse-args.js | 96.39 | 45.45 | 100 | 96.39 | 120-121,129-130,143-144
296-
report.js | 75.42 | 58.33 | 84.62 | 75.42 | ...216,246-247,274-275,281-283
296+
report.js | 75.16 | 58.33 | 78.57 | 75.16 | ...247,274-275,281-283,304-309
297297
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
298298
lib/commands | 45.65 | 75 | 16.67 | 45.65 |
299299
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
@@ -309,13 +309,13 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = `
309309
",--------------------------|---------|----------|---------|---------|--------------------------------
310310
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
311311
--------------------------|---------|----------|---------|---------|--------------------------------
312-
All files | 73.17 | 58.02 | 62.16 | 73.17 |
312+
All files | 73.12 | 58.02 | 60.53 | 73.12 |
313313
bin | 78.85 | 60 | 66.67 | 78.85 |
314314
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
315-
lib | 76.44 | 51.79 | 72.73 | 76.44 |
315+
lib | 76.27 | 51.79 | 69.57 | 76.27 |
316316
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
317317
parse-args.js | 96.39 | 45.45 | 100 | 96.39 | 120-121,129-130,143-144
318-
report.js | 75.42 | 58.33 | 84.62 | 75.42 | ...216,246-247,274-275,281-283
318+
report.js | 75.16 | 58.33 | 78.57 | 75.16 | ...247,274-275,281-283,304-309
319319
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
320320
lib/commands | 45.65 | 75 | 16.67 | 45.65 |
321321
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
@@ -324,9 +324,9 @@ All files | 73.17 | 58.02 | 62.16 | 73.17 |
324324
async.js | 100 | 100 | 100 | 100 |
325325
normal.js | 75 | 66.67 | 33.33 | 75 | 14-16,18-20
326326
--------------------------|---------|----------|---------|---------|--------------------------------
327-
,ERROR: Coverage for lines (73.17%) does not meet global threshold (101%)
327+
,ERROR: Coverage for lines (73.12%) does not meet global threshold (101%)
328328
ERROR: Coverage for branches (58.02%) does not meet global threshold (82%)
329-
ERROR: Coverage for statements (73.17%) does not meet global threshold (95%)
329+
ERROR: Coverage for statements (73.12%) does not meet global threshold (95%)
330330
"
331331
`;
332332

0 commit comments

Comments
 (0)