Skip to content

Commit 1329a3b

Browse files
authored
feat: add option that allows instrument to exit on error (#850)
1 parent bc9ffe5 commit 1329a3b

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

index.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
/* global __coverage__ */
24

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

270272
NYC.prototype._transformFactory = function (cacheDir) {
271-
var _this = this
272-
var instrumenter = this.instrumenter()
273-
var instrumented
273+
const instrumenter = this.instrumenter()
274+
let instrumented
274275

275-
return function (code, metadata, hash) {
276-
var filename = metadata.filename
277-
var sourceMap = null
276+
return (code, metadata, hash) => {
277+
const filename = metadata.filename
278+
let sourceMap = null
278279

279-
if (_this._sourceMap) sourceMap = _this.sourceMaps.extractAndRegister(code, filename, hash)
280+
if (this._sourceMap) sourceMap = this.sourceMaps.extractAndRegister(code, filename, hash)
280281

281282
try {
282283
instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
283284
} catch (e) {
284-
// don't fail external tests due to instrumentation bugs.
285285
debugLog('failed to instrument ' + filename + 'with error: ' + e.stack)
286-
instrumented = code
286+
if (this.config.exitOnError) {
287+
process.exit(1)
288+
} else {
289+
instrumented = code
290+
}
287291
}
288292

289-
if (_this.fakeRequire) {
293+
if (this.fakeRequire) {
290294
return 'function x () {}'
291295
} else {
292296
return instrumented

lib/commands/instrument.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ exports.builder = function (yargs) {
4646
type: 'boolean',
4747
description: 'should nyc handle instrumentation?'
4848
})
49+
.option('exit-on-error', {
50+
default: false,
51+
type: 'boolean',
52+
description: 'should nyc exit when an instrumentation failure occurs?'
53+
})
4954
.example('$0 instrument ./lib ./output', 'instrument all .js files in ./lib with coverage and output in ./output')
5055
}
5156

@@ -62,7 +67,8 @@ exports.handler = function (argv) {
6267
extension: argv.extension,
6368
require: argv.require,
6469
compact: argv.compact,
65-
preserveComments: argv.preserveComments
70+
preserveComments: argv.preserveComments,
71+
exitOnError: argv.exitOnError
6672
})
6773

6874
nyc.instrumentAllFiles(argv.input, argv.output, function (err) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
generate async futurelet console.log('Hello, World!') // this isn't real JS.

test/nyc-bin.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global describe, it */
1+
/* global describe, it, beforeEach */
22

33
const _ = require('lodash')
44
const path = require('path')
@@ -425,6 +425,10 @@ describe('the nyc cli', function () {
425425
})
426426

427427
describe('instrument', function () {
428+
beforeEach(() => {
429+
rimraf.sync(path.resolve(fixturesCLI, 'subdir', 'output-dir'))
430+
})
431+
428432
describe('no output folder', function () {
429433
it('allows a single file to be instrumented', function (done) {
430434
var args = [bin, 'instrument', './half-covered.js']
@@ -485,6 +489,27 @@ describe('the nyc cli', function () {
485489
done()
486490
})
487491
})
492+
493+
it('can be configured to exit on error', function (done) {
494+
var args = [
495+
bin,
496+
'instrument',
497+
'--exit-on-error',
498+
'./input-dir',
499+
'./output-dir'
500+
]
501+
502+
var subdir = path.resolve(fixturesCLI, 'subdir')
503+
var proc = spawn(process.execPath, args, {
504+
cwd: subdir,
505+
env: env
506+
})
507+
508+
proc.on('exit', function (code) {
509+
code.should.equal(1)
510+
done()
511+
})
512+
})
488513
})
489514

490515
describe('output folder specified', function () {

0 commit comments

Comments
 (0)