Skip to content

Commit

Permalink
feat: add support for 1:1 source-maps (bcoe#85)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: c8 will now load source-maps if possible and remap coverage accordingly
  • Loading branch information
bcoe authored May 2, 2019
1 parent fde596e commit 6ca4345
Show file tree
Hide file tree
Showing 22 changed files with 388 additions and 26 deletions.
4 changes: 2 additions & 2 deletions bin/c8.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ let argv = buildYargs().parse(instrumenterArgs)
}

process.env.NODE_V8_COVERAGE = argv.tempDirectory
foreground(hideInstrumenterArgs(argv), done => {
outputReport(argv)
foreground(hideInstrumenterArgs(argv), async (done) => {
await outputReport(argv)
done()
})
}
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/check-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ exports.handler = function (argv) {
exports.checkCoverages(argv, report)
}

exports.checkCoverages = function (argv, report) {
exports.checkCoverages = async function (argv, report) {
const thresholds = {
lines: argv.lines,
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
}
const map = report.getCoverageMapFromAllCoverageFiles()
const map = await report.getCoverageMapFromAllCoverageFiles()
if (argv.perFile) {
map.files().forEach(file => {
checkCoverage(map.fileCoverageFor(file).toSummary(), thresholds, file)
Expand Down
8 changes: 4 additions & 4 deletions lib/commands/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ exports.command = 'report'

exports.describe = 'read V8 coverage data from temp and output report'

exports.handler = function (argv) {
exports.outputReport(argv)
exports.handler = async function (argv) {
await exports.outputReport(argv)
}

exports.outputReport = function (argv) {
exports.outputReport = async function (argv) {
const report = Report({
include: argv.include,
exclude: argv.exclude,
Expand All @@ -21,6 +21,6 @@ exports.outputReport = function (argv) {
omitRelative: argv.omitRelative,
wrapperLength: argv.wrapperLength
})
report.run()
await report.run()
if (argv.checkCoverage) checkCoverages(argv, report)
}
13 changes: 7 additions & 6 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Report {
this.omitRelative = omitRelative
this.wrapperLength = wrapperLength
}
run () {
const map = this.getCoverageMapFromAllCoverageFiles()
async run () {
const map = await this.getCoverageMapFromAllCoverageFiles()
var context = libReport.createContext({
dir: this.reportsDirectory,
watermarks: this.watermarks
Expand All @@ -47,7 +47,7 @@ class Report {
})
}

getCoverageMapFromAllCoverageFiles () {
async getCoverageMapFromAllCoverageFiles () {
// the merge process can be very expensive, and it's often the case that
// check-coverage is called immediately after a report. We memoize the
// result from getCoverageMapFromAllCoverageFiles() to address this
Expand All @@ -61,9 +61,10 @@ class Report {
for (const v8ScriptCov of v8ProcessCov.result) {
try {
const path = resolve(this.resolve, v8ScriptCov.url)
const script = v8toIstanbul(path, this.wrapperLength)
script.applyCoverage(v8ScriptCov.functions)
map.merge(script.toIstanbul())
const converter = v8toIstanbul(path, this.wrapperLength)
await converter.load()
converter.applyCoverage(v8ScriptCov.functions)
map.merge(converter.toIstanbul())
} catch (err) {
console.warn(`file: ${v8ScriptCov.url} error: ${err.stack}`)
}
Expand Down
25 changes: 15 additions & 10 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
"istanbul-reports": "^2.0.0",
"rimraf": "^2.6.2",
"test-exclude": "^5.0.0",
"uuid": "^3.3.2",
"v8-to-istanbul": "^2.0.4",
"v8-to-istanbul": "^3.0.1",
"yargs": "^13.1.0",
"yargs-parser": "^10.1.0"
},
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/source-maps/branches/branches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
if (false) {
console.info('unreachable')
} else if (true) {
console.info('reachable')
} else {
console.info('unreachable')
}

function branch (a) {
if (a) {
console.info('a = true')
} else if (undefined) {
console.info('unreachable')
} else {
console.info('a = false')
}
}

branch(true)
branch(false)
2 changes: 2 additions & 0 deletions test/fixtures/source-maps/branches/branches.nyc.js

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

28 changes: 28 additions & 0 deletions test/fixtures/source-maps/branches/branches.typescript.js

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

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

25 changes: 25 additions & 0 deletions test/fixtures/source-maps/branches/branches.typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
enum ATrue {
IsTrue = 1,
IsFalse = 0
}

if (false) {
console.info('unreachable')
} else if (true) {
console.info('reachable')
} else {
console.info('unreachable')
}

function branch (a: boolean) {
if (a) {
console.info('a = true')
} else if (undefined) {
console.info('unreachable')
} else {
console.info('a = false')
}
}

branch(!!ATrue.IsTrue)
branch(!!ATrue.IsFalse)
2 changes: 2 additions & 0 deletions test/fixtures/source-maps/branches/branches.uglify.js

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

1 change: 1 addition & 0 deletions test/fixtures/source-maps/branches/branches.uglify.js.map

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

27 changes: 27 additions & 0 deletions test/fixtures/source-maps/classes/classes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Foo {
constructor (x=33) {
this.x = x ? x : 99
if (this.x) {
console.info('covered')
} else {
console.info('uncovered')
}
this.methodC()
}
methodA () {
console.info('covered')
}
methodB () {
console.info('uncovered')
}
methodC () {
console.info('covered')
}
methodD () {
console.info('uncovered')
}
}

const a = new Foo(0)
const b = new Foo(33)
a.methodA()
2 changes: 2 additions & 0 deletions test/fixtures/source-maps/classes/classes.nyc.js

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

32 changes: 32 additions & 0 deletions test/fixtures/source-maps/classes/classes.typescript.js

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

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

Loading

0 comments on commit 6ca4345

Please sign in to comment.