Skip to content

Commit

Permalink
feat: Pass extension to test-exclude, use TestExclude.globSync. (#1026
Browse files Browse the repository at this point in the history
)

BREAKING CHANGE: The logic involving include/exclude processing has
changed.  Results should be verified to ensure all desired sources have
coverage data.
  • Loading branch information
coreyfarrell authored Mar 18, 2019
1 parent 64571d3 commit aae5a59
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
29 changes: 10 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const cachingTransform = require('caching-transform')
const util = require('util')
const findCacheDir = require('find-cache-dir')
const fs = require('fs')
const glob = require('glob')
const Hash = require('./lib/hash')
const libCoverage = require('istanbul-lib-coverage')
const libHook = require('istanbul-lib-hook')
Expand Down Expand Up @@ -50,10 +49,16 @@ function NYC (config) {
this.cacheDirectory = (config.cacheDir && path.resolve(config.cacheDir)) || findCacheDir({ name: 'nyc', cwd: this.cwd })
this.cache = Boolean(this.cacheDirectory && config.cache)

this.extensions = arrify(config.extension)
.concat('.js')
.map(ext => ext.toLowerCase())
.filter((item, pos, arr) => arr.indexOf(item) === pos)

this.exclude = testExclude({
cwd: this.cwd,
include: config.include,
exclude: config.exclude
exclude: config.exclude,
extension: this.extensions
})

this.sourceMaps = new SourceMaps({
Expand All @@ -64,17 +69,10 @@ function NYC (config) {
// require extensions can be provided as config in package.json.
this.require = arrify(config.require)

this.extensions = arrify(config.extension).concat('.js').map(function (ext) {
return ext.toLowerCase()
}).filter(function (item, pos, arr) {
// avoid duplicate extensions
return arr.indexOf(item) === pos
})

this.transforms = this.extensions.reduce(function (transforms, ext) {
this.transforms = this.extensions.reduce((transforms, ext) => {
transforms[ext] = this._createTransform(ext)
return transforms
}.bind(this), {})
}, {})

this.hookRequire = config.hookRequire
this.hookRunInContext = config.hookRunInContext
Expand Down Expand Up @@ -227,14 +225,7 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
}

NYC.prototype.walkAllFiles = function (dir, visitor) {
var pattern = null
if (this.extensions.length === 1) {
pattern = '**/*' + this.extensions[0]
} else {
pattern = '**/*{' + this.extensions.join() + '}'
}

glob.sync(pattern, { cwd: dir, nodir: true, ignore: this.exclude.exclude }).forEach(function (filename) {
this.exclude.globSync(dir).forEach(filename => {
visitor(filename)
})
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"find-cache-dir": "^2.0.0",
"find-up": "^3.0.0",
"foreground-child": "^1.5.6",
"glob": "^7.1.3",
"istanbul-lib-coverage": "^2.0.4",
"istanbul-lib-hook": "^2.0.4",
"istanbul-lib-instrument": "^3.1.1",
Expand All @@ -96,6 +95,7 @@
"any-path": "^1.3.0",
"chai": "^4.2.0",
"coveralls": "^3.0.3",
"glob": "^7.1.3",
"is-windows": "^1.0.2",
"lodash": "^4.17.11",
"newline-regex": "^0.2.1",
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
},
"nyc": {
"exclude": [
"**/blarg",
"**/blerg"
"**/blarg.js",
"**/blerg.js"
]
}
}
20 changes: 10 additions & 10 deletions test/nyc-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ describe('nyc', function () {
]))

// nyc always excludes "node_modules/**"
nyc.exclude.shouldInstrument('/cwd/foo', 'foo').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/node_modules/bar', 'node_modules/bar').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/foo/node_modules/bar', 'foo/node_modules/bar').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/foo.js', 'foo.js').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/node_modules/bar.js', 'node_modules/bar.js').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/foo/node_modules/bar.js', 'foo/node_modules/bar.js').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/test.js', 'test.js').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/testfoo.js', 'testfoo.js').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/test-foo.js', 'test-foo.js').should.equal(false)
Expand All @@ -115,22 +115,22 @@ describe('nyc', function () {
var nyc = new NYC(configUtil.buildYargs(fixtures).parse())

// fixtures/package.json configures excludes: "blarg", "blerg"
nyc.exclude.shouldInstrument('blarg', 'blarg').should.equal(false)
nyc.exclude.shouldInstrument('blarg.js', 'blarg.js').should.equal(false)
nyc.exclude.shouldInstrument('blarg/foo.js', 'blarg/foo.js').should.equal(false)
nyc.exclude.shouldInstrument('blerg', 'blerg').should.equal(false)
nyc.exclude.shouldInstrument('./blerg', './blerg').should.equal(false)
nyc.exclude.shouldInstrument('./blerg', '.\\blerg').should.equal(false)
nyc.exclude.shouldInstrument('blerg.js', 'blerg.js').should.equal(false)
nyc.exclude.shouldInstrument('./blerg.js', './blerg.js').should.equal(false)
nyc.exclude.shouldInstrument('./blerg.js', '.\\blerg.js').should.equal(false)
})

it('should exclude outside of the current working directory', function () {
var nyc = new NYC(configUtil.buildYargs('/cwd/foo/').parse())
nyc.exclude.shouldInstrument('/cwd/bar', '../bar').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/bar.js', '../bar.js').should.equal(false)
})

it('should not exclude if the current working directory is inside node_modules', function () {
var nyc = new NYC(configUtil.buildYargs('/cwd/node_modules/foo/').parse())
nyc.exclude.shouldInstrument('/cwd/node_modules/foo/bar', './bar').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/node_modules/foo/bar', '.\\bar').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/node_modules/foo/bar.js', './bar.js').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/node_modules/foo/bar.js', '.\\bar.js').should.equal(true)
})

it('allows files to be explicitly included, rather than excluded', function () {
Expand Down

0 comments on commit aae5a59

Please sign in to comment.