Skip to content

Commit

Permalink
feat: add option that allows instrument to exit on error (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed May 31, 2018
1 parent bc9ffe5 commit 1329a3b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
24 changes: 14 additions & 10 deletions index.js
@@ -1,3 +1,5 @@
'use strict'

/* global __coverage__ */

const arrify = require('arrify')
Expand Down Expand Up @@ -268,25 +270,27 @@ NYC.prototype._maybeInstrumentSource = function (code, filename, relFile) {
}

NYC.prototype._transformFactory = function (cacheDir) {
var _this = this
var instrumenter = this.instrumenter()
var instrumented
const instrumenter = this.instrumenter()
let instrumented

return function (code, metadata, hash) {
var filename = metadata.filename
var sourceMap = null
return (code, metadata, hash) => {
const filename = metadata.filename
let sourceMap = null

if (_this._sourceMap) sourceMap = _this.sourceMaps.extractAndRegister(code, filename, hash)
if (this._sourceMap) sourceMap = this.sourceMaps.extractAndRegister(code, filename, hash)

try {
instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
} catch (e) {
// don't fail external tests due to instrumentation bugs.
debugLog('failed to instrument ' + filename + 'with error: ' + e.stack)
instrumented = code
if (this.config.exitOnError) {
process.exit(1)
} else {
instrumented = code
}
}

if (_this.fakeRequire) {
if (this.fakeRequire) {
return 'function x () {}'
} else {
return instrumented
Expand Down
8 changes: 7 additions & 1 deletion lib/commands/instrument.js
Expand Up @@ -46,6 +46,11 @@ exports.builder = function (yargs) {
type: 'boolean',
description: 'should nyc handle instrumentation?'
})
.option('exit-on-error', {
default: false,
type: 'boolean',
description: 'should nyc exit when an instrumentation failure occurs?'
})
.example('$0 instrument ./lib ./output', 'instrument all .js files in ./lib with coverage and output in ./output')
}

Expand All @@ -62,7 +67,8 @@ exports.handler = function (argv) {
extension: argv.extension,
require: argv.require,
compact: argv.compact,
preserveComments: argv.preserveComments
preserveComments: argv.preserveComments,
exitOnError: argv.exitOnError
})

nyc.instrumentAllFiles(argv.input, argv.output, function (err) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cli/subdir/input-dir/bad.js
@@ -0,0 +1 @@
generate async futurelet console.log('Hello, World!') // this isn't real JS.
27 changes: 26 additions & 1 deletion test/nyc-bin.js
@@ -1,4 +1,4 @@
/* global describe, it */
/* global describe, it, beforeEach */

const _ = require('lodash')
const path = require('path')
Expand Down Expand Up @@ -425,6 +425,10 @@ describe('the nyc cli', function () {
})

describe('instrument', function () {
beforeEach(() => {
rimraf.sync(path.resolve(fixturesCLI, 'subdir', 'output-dir'))
})

describe('no output folder', function () {
it('allows a single file to be instrumented', function (done) {
var args = [bin, 'instrument', './half-covered.js']
Expand Down Expand Up @@ -485,6 +489,27 @@ describe('the nyc cli', function () {
done()
})
})

it('can be configured to exit on error', function (done) {
var args = [
bin,
'instrument',
'--exit-on-error',
'./input-dir',
'./output-dir'
]

var subdir = path.resolve(fixturesCLI, 'subdir')
var proc = spawn(process.execPath, args, {
cwd: subdir,
env: env
})

proc.on('exit', function (code) {
code.should.equal(1)
done()
})
})
})

describe('output folder specified', function () {
Expand Down

0 comments on commit 1329a3b

Please sign in to comment.