Skip to content

Commit b90d26f

Browse files
authored
fix: reverts _maybeInstrumentSource logic, so that exclude is still applied (#429)
1 parent 63a8758 commit b90d26f

File tree

4 files changed

+97
-41
lines changed

4 files changed

+97
-41
lines changed

bin/nyc.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/usr/bin/env node
2+
3+
// the babel cache does not play nicely with nyc.
4+
process.env.BABEL_DISABLE_CACHE = '1'
5+
26
var configUtil = require('../lib/config-util')
37
var foreground = require('foreground-child')
48
var NYC

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ NYC.prototype.walkAllFiles = function (dir, visitor) {
238238
}
239239

240240
NYC.prototype._maybeInstrumentSource = function (code, filename, relFile) {
241-
var instrument = (!this.instrument && this.all) || this.exclude.shouldInstrument(filename, relFile)
241+
var instrument = this.exclude.shouldInstrument(filename, relFile)
242242
if (!instrument) {
243243
return null
244244
}

test/fixtures/cli/external-instrumenter.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/src/nyc-bin.js

+91-40
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/* global describe, it */
22

3+
var _ = require('lodash')
34
var path = require('path')
4-
var fs = require('fs')
5-
var spawn = require('child_process').spawn
6-
var isWindows = require('is-windows')()
5+
var bin = path.resolve(__dirname, '../../bin/nyc')
76
var fixturesCLI = path.resolve(__dirname, '../fixtures/cli')
8-
var fixturesHooks = path.resolve(__dirname, '../fixtures/hooks')
97
var fakebin = path.resolve(fixturesCLI, 'fakebin')
10-
var bin = path.resolve(__dirname, '../../bin/nyc')
8+
var fixturesHooks = path.resolve(__dirname, '../fixtures/hooks')
9+
var fs = require('fs')
10+
var glob = require('glob')
11+
var isWindows = require('is-windows')()
1112
var rimraf = require('rimraf')
13+
var spawn = require('child_process').spawn
1214

1315
require('chai').should()
1416
require('tap').mochaGlobals()
@@ -332,41 +334,6 @@ describe('the nyc cli', function () {
332334
})
333335
})
334336

335-
it('setting instrument to "false" sets noop instrumenter', function (done) {
336-
var args = [
337-
bin,
338-
'--silent',
339-
'--no-instrument',
340-
'--no-source-map',
341-
process.execPath,
342-
'./env.js'
343-
]
344-
var expected = {
345-
silent: true,
346-
instrument: false,
347-
sourceMap: false,
348-
instrumenter: './lib/instrumenters/noop'
349-
}
350-
351-
var proc = spawn(process.execPath, args, {
352-
cwd: fixturesCLI,
353-
env: env
354-
})
355-
356-
var stdout = ''
357-
proc.stdout.on('data', function (chunk) {
358-
stdout += chunk
359-
})
360-
361-
proc.on('close', function (code) {
362-
code.should.equal(0)
363-
var env = JSON.parse(stdout)
364-
var config = JSON.parse(env.NYC_CONFIG, null, 2)
365-
config.should.include(expected)
366-
done()
367-
})
368-
})
369-
370337
describe('coverage', function () {
371338
if (parseInt(process.versions.node.split('.')[0]) < 4) return
372339
it('reports appropriate coverage information for es6 source files', function (done) {
@@ -619,4 +586,88 @@ describe('the nyc cli', function () {
619586
})
620587
})
621588
})
589+
590+
describe('noop instrumenter', function () {
591+
it('setting instrument to "false" configures noop instrumenter', function (done) {
592+
var args = [
593+
bin,
594+
'--silent',
595+
'--no-instrument',
596+
'--no-source-map',
597+
process.execPath,
598+
'./env.js'
599+
]
600+
var expected = {
601+
silent: true,
602+
instrument: false,
603+
sourceMap: false,
604+
instrumenter: './lib/instrumenters/noop'
605+
}
606+
607+
var proc = spawn(process.execPath, args, {
608+
cwd: fixturesCLI,
609+
env: env
610+
})
611+
612+
var stdout = ''
613+
proc.stdout.on('data', function (chunk) {
614+
stdout += chunk
615+
})
616+
617+
proc.on('close', function (code) {
618+
code.should.equal(0)
619+
var env = JSON.parse(stdout)
620+
var config = JSON.parse(env.NYC_CONFIG, null, 2)
621+
config.should.include(expected)
622+
done()
623+
})
624+
})
625+
626+
describe('--all', function () {
627+
it('extracts coverage headers from unexecuted files', function (done) {
628+
var nycOutput = path.resolve(fixturesCLI, '.nyc_output')
629+
rimraf.sync(nycOutput)
630+
631+
var args = [
632+
bin,
633+
'--all',
634+
'--silent',
635+
'--no-instrument',
636+
'--no-source-map',
637+
process.execPath,
638+
// any file other than external-instrument.js, which we
639+
// want to ensure has its header loaded.
640+
'./env.js'
641+
]
642+
643+
var proc = spawn(process.execPath, args, {
644+
cwd: fixturesCLI,
645+
env: env
646+
})
647+
648+
proc.on('close', function (code) {
649+
code.should.equal(0)
650+
glob(nycOutput + '/*.json', function (_err, files) {
651+
// we should have extracted the coverage header from external-instrumenter.js.
652+
var coverage = {}
653+
files.forEach(function (file) {
654+
_.assign(coverage, JSON.parse(
655+
fs.readFileSync(file, 'utf-8')
656+
))
657+
})
658+
Object.keys(coverage)[0].should.equal('./external-instrumenter.js')
659+
660+
// we should not have executed file, so all counts sould be 0.
661+
var sum = 0
662+
;Object.keys(coverage['./external-instrumenter.js'].s).forEach(function (k) {
663+
sum += coverage['./external-instrumenter.js'].s[k]
664+
})
665+
sum.should.equal(0)
666+
667+
return done()
668+
})
669+
})
670+
})
671+
})
672+
})
622673
})

0 commit comments

Comments
 (0)