Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add more general support for negated exclude rules #58

Merged
merged 2 commits into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 32 additions & 12 deletions packages/test-exclude/index.js
Expand Up @@ -32,27 +32,43 @@ function TestExclude (opts) {
this.include = false
}

if (!this.removeNegatedModuleExclude() && this.exclude.indexOf('**/node_modules/**') === -1) {
if (this.exclude.indexOf('**/node_modules/**') === -1) {
this.exclude.push('**/node_modules/**')
}

this.exclude = prepGlobPatterns(
[].concat(arrify(this.exclude))
)

this.handleNegation()
}

// if a glob has been provided that explicitly negates
// the **/node_modules/** default exclude rule, remove it from
// excludes but don't add the default exclude rule.
TestExclude.prototype.removeNegatedModuleExclude = function () {
var moduleExcludeNegated = false
// handle the special case of negative globs
// (!**foo/bar); we create a new this.excludeNegated set
// of rules, which is applied after excludes and we
// move excluded include rules into this.excludes.
TestExclude.prototype.handleNegation = function () {
if (Array.isArray(this.include)) {
const includeNegated = this.include.filter(function (e) {
return e.charAt(0) === '!'
}).map(function (e) {
return e.slice(1)
})
this.exclude.push.apply(this.exclude, prepGlobPatterns(includeNegated))
this.include = this.include.filter(function (e) {
return e.charAt(0) !== '!'
})
}

this.excludeNegated = this.exclude.filter(function (e) {
return e.charAt(0) === '!'
}).map(function (e) {
return e.slice(1)
})
this.exclude = this.exclude.filter(function (e) {
var negated = !!micromatch('./node_modules/foo.js', e, {nonegate: false}).length !==
!!micromatch('./node_modules/foo.js', e, {nonegate: true}).length
if (negated) moduleExcludeNegated = true
return !negated
return e.charAt(0) !== '!'
})
return moduleExcludeNegated
this.excludeNegated = prepGlobPatterns(this.excludeNegated)
}

TestExclude.prototype.shouldInstrument = function (filename, relFile) {
Expand All @@ -67,7 +83,11 @@ TestExclude.prototype.shouldInstrument = function (filename, relFile) {
pathToCheck = relFile.replace(/^\.[\\/]/, '') // remove leading './' or '.\'.
}

return (!this.include || micromatch.any(pathToCheck, this.include, {dotfiles: true})) && !micromatch.any(pathToCheck, this.exclude, {dotfiles: true})
return (
!this.include ||
micromatch.any(pathToCheck, this.include, {dotfiles: true})) &&
(!micromatch.any(pathToCheck, this.exclude, {dotfiles: true}) ||
micromatch.any(pathToCheck, this.excludeNegated, {dotfiles: true}))
}

TestExclude.prototype.pkgConf = function (key, path) {
Expand Down
26 changes: 26 additions & 0 deletions packages/test-exclude/test/test-exclude.js
Expand Up @@ -86,6 +86,32 @@ describe('testExclude', function () {
e.shouldInstrument('src/foo.js').should.equal(true)
})

it('allows negated exclude patterns', function () {
const e = exclude({
exclude: ['foo/**', '!foo/bar.js']
})

e.shouldInstrument('./foo/fizz.js').should.equal(false)
e.shouldInstrument('./foo/bar.js').should.equal(true)
})

it('allows negated include patterns', function () {
const e = exclude({
include: ['batman/**', '!batman/robin.js']
})

e.shouldInstrument('./batman/joker.js').should.equal(true)
e.shouldInstrument('./batman/robin.js').should.equal(false)
})

it('negated exclude patterns unrelated to node_modules do not affect default node_modules exclude behavior', function () {
const e = exclude({
exclude: ['!foo/**']
})

e.shouldInstrument('node_modules/cat.js').should.equal(false)
})

it('exports defaultExclude', function () {
exclude.defaultExclude.should.deep.equal([
'coverage/**',
Expand Down