Skip to content

Commit

Permalink
Improve code quality by increasing code coverage: lib/source-map-from…
Browse files Browse the repository at this point in the history
…-file.js (bcoe#453)

refactor:  lib/source-map-from-file.js to improve code coverage (bcoe#453)
refactor: exposed source-map-from-file.js function to write test cases (bcoe#453)
test: Added two test case cases to cover error handling for improper formatting (bcoe#453)
  • Loading branch information
mcknasty committed Jan 4, 2024
1 parent 5e18365 commit e967873
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3,383 deletions.
2 changes: 1 addition & 1 deletion lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ try {
const { readdirSync, readFileSync, statSync } = require('fs')
const { isAbsolute, resolve, extname } = require('path')
const { pathToFileURL, fileURLToPath } = require('url')
const getSourceMapFromFile = require('./source-map-from-file')
const { getSourceMapFromFile } = require('./source-map-from-file')
// TODO: switch back to @c88/v8-coverage once patch is landed.
const v8toIstanbul = require('v8-to-istanbul')
const util = require('util')
Expand Down
12 changes: 11 additions & 1 deletion lib/source-map-from-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ function sourceMapFromDataUrl (url) {
const { 0: format, 1: data } = url.split(',')
const splitFormat = format.split(';')
const contentType = splitFormat[0]
// always evaluates to true for our use cases. Could possibly be false.
// Per https://github.com/bcoe/c8/pull/453#discussion_r1106306167
const base64 = splitFormat[splitFormat.length - 1] === 'base64'
if (contentType === 'application/json') {
// the data variable is never a json string in the test cases
// the upstream branch in node.js still has this logic
// https://github.com/nodejs/node/blob/v19.x/lib/internal/source_map/source_map_cache.js#L241
const decodedData = base64 ? Buffer.from(data, 'base64').toString('utf8') : data
try {
return JSON.parse(decodedData)
Expand All @@ -97,4 +102,9 @@ function sourceMapFromDataUrl (url) {
}
}

module.exports = getSourceMapFromFile
module.exports = {
getSourceMapFromFile,
dataFromUrl,
sourceMapFromFile,
sourceMapFromDataUrl
}
Loading

0 comments on commit e967873

Please sign in to comment.