Skip to content

Commit

Permalink
fix: reverts _maybeInstrumentSource logic, so that exclude is still a…
Browse files Browse the repository at this point in the history
…pplied (#429)
  • Loading branch information
bcoe committed Oct 30, 2016
1 parent 63a8758 commit b90d26f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 41 deletions.
4 changes: 4 additions & 0 deletions bin/nyc.js
@@ -1,4 +1,8 @@
#!/usr/bin/env node

// the babel cache does not play nicely with nyc.
process.env.BABEL_DISABLE_CACHE = '1'

var configUtil = require('../lib/config-util')
var foreground = require('foreground-child')
var NYC
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -238,7 +238,7 @@ NYC.prototype.walkAllFiles = function (dir, visitor) {
}

NYC.prototype._maybeInstrumentSource = function (code, filename, relFile) {
var instrument = (!this.instrument && this.all) || this.exclude.shouldInstrument(filename, relFile)
var instrument = this.exclude.shouldInstrument(filename, relFile)
if (!instrument) {
return null
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cli/external-instrumenter.js

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

131 changes: 91 additions & 40 deletions test/src/nyc-bin.js
@@ -1,14 +1,16 @@
/* global describe, it */

var _ = require('lodash')
var path = require('path')
var fs = require('fs')
var spawn = require('child_process').spawn
var isWindows = require('is-windows')()
var bin = path.resolve(__dirname, '../../bin/nyc')
var fixturesCLI = path.resolve(__dirname, '../fixtures/cli')
var fixturesHooks = path.resolve(__dirname, '../fixtures/hooks')
var fakebin = path.resolve(fixturesCLI, 'fakebin')
var bin = path.resolve(__dirname, '../../bin/nyc')
var fixturesHooks = path.resolve(__dirname, '../fixtures/hooks')
var fs = require('fs')
var glob = require('glob')
var isWindows = require('is-windows')()
var rimraf = require('rimraf')
var spawn = require('child_process').spawn

require('chai').should()
require('tap').mochaGlobals()
Expand Down Expand Up @@ -332,41 +334,6 @@ describe('the nyc cli', function () {
})
})

it('setting instrument to "false" sets noop instrumenter', function (done) {
var args = [
bin,
'--silent',
'--no-instrument',
'--no-source-map',
process.execPath,
'./env.js'
]
var expected = {
silent: true,
instrument: false,
sourceMap: false,
instrumenter: './lib/instrumenters/noop'
}

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

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', function (code) {
code.should.equal(0)
var env = JSON.parse(stdout)
var config = JSON.parse(env.NYC_CONFIG, null, 2)
config.should.include(expected)
done()
})
})

describe('coverage', function () {
if (parseInt(process.versions.node.split('.')[0]) < 4) return
it('reports appropriate coverage information for es6 source files', function (done) {
Expand Down Expand Up @@ -619,4 +586,88 @@ describe('the nyc cli', function () {
})
})
})

describe('noop instrumenter', function () {
it('setting instrument to "false" configures noop instrumenter', function (done) {
var args = [
bin,
'--silent',
'--no-instrument',
'--no-source-map',
process.execPath,
'./env.js'
]
var expected = {
silent: true,
instrument: false,
sourceMap: false,
instrumenter: './lib/instrumenters/noop'
}

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

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', function (code) {
code.should.equal(0)
var env = JSON.parse(stdout)
var config = JSON.parse(env.NYC_CONFIG, null, 2)
config.should.include(expected)
done()
})
})

describe('--all', function () {
it('extracts coverage headers from unexecuted files', function (done) {
var nycOutput = path.resolve(fixturesCLI, '.nyc_output')
rimraf.sync(nycOutput)

var args = [
bin,
'--all',
'--silent',
'--no-instrument',
'--no-source-map',
process.execPath,
// any file other than external-instrument.js, which we
// want to ensure has its header loaded.
'./env.js'
]

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

proc.on('close', function (code) {
code.should.equal(0)
glob(nycOutput + '/*.json', function (_err, files) {
// we should have extracted the coverage header from external-instrumenter.js.
var coverage = {}
files.forEach(function (file) {
_.assign(coverage, JSON.parse(
fs.readFileSync(file, 'utf-8')
))
})
Object.keys(coverage)[0].should.equal('./external-instrumenter.js')

// we should not have executed file, so all counts sould be 0.
var sum = 0
;Object.keys(coverage['./external-instrumenter.js'].s).forEach(function (k) {
sum += coverage['./external-instrumenter.js'].s[k]
})
sum.should.equal(0)

return done()
})
})
})
})
})
})

0 comments on commit b90d26f

Please sign in to comment.