Skip to content

Commit

Permalink
fix: address bugs with source remapping on Windows (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
clemyan committed May 2, 2021
1 parent 86bd364 commit c817902
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 43 deletions.
29 changes: 23 additions & 6 deletions lib/report.js
@@ -1,10 +1,10 @@
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')
const { readdirSync, readFileSync, statSync } = require('fs')
const { isAbsolute, resolve, extname } = require('path')
const { pathToFileURL, fileURLToPath } = require('url')
const getSourceMapFromFile = require('./source-map-from-file')
// TODO: switch back to @c88/v8-coverage once patch is landed.
const v8toIstanbul = require('v8-to-istanbul')
Expand Down Expand Up @@ -139,8 +139,8 @@ class Report {
*/
_getSourceMap (v8ScriptCov) {
const sources = {}
if (this.sourceMapCache[`file://${v8ScriptCov.url}`]) {
const sourceMapAndLineLengths = this.sourceMapCache[`file://${v8ScriptCov.url}`]
const sourceMapAndLineLengths = this.sourceMapCache[pathToFileURL(v8ScriptCov.url).href]
if (sourceMapAndLineLengths) {
// See: https://github.com/nodejs/node/pull/34305
if (!sourceMapAndLineLengths.data) return
sources.sourceMap = {
Expand Down Expand Up @@ -173,7 +173,7 @@ class Report {
for (const v8ProcessCov of this._loadReports()) {
if (this._isCoverageObject(v8ProcessCov)) {
if (v8ProcessCov['source-map-cache']) {
Object.assign(this.sourceMapCache, v8ProcessCov['source-map-cache'])
Object.assign(this.sourceMapCache, this._normalizeSourceMapCache(v8ProcessCov['source-map-cache']))
}
v8ProcessCovs.push(this._normalizeProcessCov(v8ProcessCov, fileIndex))
}
Expand All @@ -194,7 +194,7 @@ class Report {
const stat = statSync(fullPath)
const sourceMap = getSourceMapFromFile(fullPath)
if (sourceMap) {
this.sourceMapCache[`file://${fullPath}`] = { data: sourceMap }
this.sourceMapCache[pathToFileURL(fullPath)] = { data: sourceMap }
}
emptyReports.push({
scriptId: 0,
Expand Down Expand Up @@ -275,7 +275,7 @@ class Report {
}
if (/^file:\/\//.test(v8ScriptCov.url)) {
try {
v8ScriptCov.url = furi.toSysPath(v8ScriptCov.url)
v8ScriptCov.url = fileURLToPath(v8ScriptCov.url)
fileIndex.add(v8ScriptCov.url)
} catch (err) {
debuglog(`${err.stack}`)
Expand All @@ -290,6 +290,23 @@ class Report {
}
return { result }
}

/**
* Normalizes a V8 source map cache.
*
* This function normalizes file URLs to a system-independent format.
*
* @param v8SourceMapCache V8 source map cache to normalize.
* @return {v8SourceMapCache} Normalized V8 source map cache.
* @private
*/
_normalizeSourceMapCache (v8SourceMapCache) {
const cache = {}
for (const fileURL of Object.keys(v8SourceMapCache)) {
cache[pathToFileURL(fileURLToPath(fileURL)).href] = v8SourceMapCache[fileURL]
}
return cache
}
}

module.exports = function (opts) {
Expand Down
19 changes: 0 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -37,7 +37,6 @@
"@istanbuljs/schema": "^0.1.2",
"find-up": "^5.0.0",
"foreground-child": "^2.0.0",
"furi": "^2.0.0",
"istanbul-lib-coverage": "^3.0.0",
"istanbul-lib-report": "^3.0.0",
"istanbul-reports": "^3.0.2",
Expand Down
34 changes: 17 additions & 17 deletions test/integration.js_10.snap
Expand Up @@ -152,13 +152,13 @@ hey
--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 73.17 | 58.02 | 62.16 | 73.17 |
All files | 73.12 | 58.02 | 60.53 | 73.12 |
bin | 78.85 | 60 | 66.67 | 78.85 |
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
lib | 76.44 | 51.79 | 72.73 | 76.44 |
lib | 76.27 | 51.79 | 69.57 | 76.27 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 96.39 | 45.45 | 100 | 96.39 | 120-121,129-130,143-144
report.js | 75.42 | 58.33 | 84.62 | 75.42 | ...216,246-247,274-275,281-283
report.js | 75.16 | 58.33 | 78.57 | 75.16 | ...247,274-275,281-283,304-309
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 45.65 | 75 | 16.67 | 45.65 |
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
Expand All @@ -167,9 +167,9 @@ All files | 73.17 | 58.02 | 62.16 | 73.17 |
async.js | 100 | 100 | 100 | 100 |
normal.js | 75 | 66.67 | 33.33 | 75 | 14-16,18-20
--------------------------|---------|----------|---------|---------|--------------------------------
,ERROR: Coverage for lines (73.17%) does not meet global threshold (101%)
,ERROR: Coverage for lines (73.12%) does not meet global threshold (101%)
ERROR: Coverage for branches (58.02%) does not meet global threshold (82%)
ERROR: Coverage for statements (73.17%) does not meet global threshold (95%)
ERROR: Coverage for statements (73.12%) does not meet global threshold (95%)
"
`;

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

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

Expand Down Expand Up @@ -287,13 +287,13 @@ exports[`c8 report generates report from existing temporary files 1`] = `
",--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 73.17 | 58.02 | 62.16 | 73.17 |
All files | 73.12 | 58.02 | 60.53 | 73.12 |
bin | 78.85 | 60 | 66.67 | 78.85 |
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
lib | 76.44 | 51.79 | 72.73 | 76.44 |
lib | 76.27 | 51.79 | 69.57 | 76.27 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 96.39 | 45.45 | 100 | 96.39 | 120-121,129-130,143-144
report.js | 75.42 | 58.33 | 84.62 | 75.42 | ...216,246-247,274-275,281-283
report.js | 75.16 | 58.33 | 78.57 | 75.16 | ...247,274-275,281-283,304-309
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 45.65 | 75 | 16.67 | 45.65 |
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
Expand All @@ -309,13 +309,13 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = `
",--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 73.17 | 58.02 | 62.16 | 73.17 |
All files | 73.12 | 58.02 | 60.53 | 73.12 |
bin | 78.85 | 60 | 66.67 | 78.85 |
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
lib | 76.44 | 51.79 | 72.73 | 76.44 |
lib | 76.27 | 51.79 | 69.57 | 76.27 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 96.39 | 45.45 | 100 | 96.39 | 120-121,129-130,143-144
report.js | 75.42 | 58.33 | 84.62 | 75.42 | ...216,246-247,274-275,281-283
report.js | 75.16 | 58.33 | 78.57 | 75.16 | ...247,274-275,281-283,304-309
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
lib/commands | 45.65 | 75 | 16.67 | 45.65 |
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
Expand All @@ -324,9 +324,9 @@ All files | 73.17 | 58.02 | 62.16 | 73.17 |
async.js | 100 | 100 | 100 | 100 |
normal.js | 75 | 66.67 | 33.33 | 75 | 14-16,18-20
--------------------------|---------|----------|---------|---------|--------------------------------
,ERROR: Coverage for lines (73.17%) does not meet global threshold (101%)
,ERROR: Coverage for lines (73.12%) does not meet global threshold (101%)
ERROR: Coverage for branches (58.02%) does not meet global threshold (82%)
ERROR: Coverage for statements (73.17%) does not meet global threshold (95%)
ERROR: Coverage for statements (73.12%) does not meet global threshold (95%)
"
`;
Expand Down

0 comments on commit c817902

Please sign in to comment.