Skip to content

Commit

Permalink
fix: Exclude negated not working with '--all' switch (#977)
Browse files Browse the repository at this point in the history
* Add tests for nyc --all negated excludes
* Update file walker to allow negated excludes

Previously the call to glob.sync was knocking out all files in the exclude patterns,
this would also knock out any files that were intended to be restored by the exclude negated patterns.
This would prevent node_modules exclude negated files from being covered when run with --all.
  • Loading branch information
AndrewFinlay authored and coreyfarrell committed Feb 22, 2019
1 parent 509c6aa commit 91de23c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ test/build/
*.covered.js
*.swp
needs-transpile.js

!*test/fixtures/cli/include-exclude/node_modules/
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/* global __coverage__ */

const arrayUniq = require('array-uniq')
const arrify = require('arrify')
const cachingTransform = require('caching-transform')
const util = require('util')
Expand Down Expand Up @@ -247,16 +248,20 @@ 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() + '}'
}
const pattern = (this.extensions.length === 1)
? `**/*${this.extensions[0]}`
: `**/*{${this.extensions.join()}}`

glob.sync(pattern, { cwd: dir, nodir: true, ignore: this.exclude.exclude }).forEach(function (filename) {
visitor(filename)
let filesToWalk = glob.sync(pattern, { cwd: dir, nodir: true, ignore: this.exclude.exclude })

// package node-glob no longer observes negated excludes, so we need to restore these files ourselves
const excludeNegatedPaths = this.exclude.excludeNegated
excludeNegatedPaths.forEach(pattern => {
filesToWalk = filesToWalk.concat(glob.sync(pattern, { cwd: dir, nodir: true }))
})
filesToWalk = arrayUniq(filesToWalk)

filesToWalk.forEach(visitor)
}

NYC.prototype._maybeInstrumentSource = function (code, filename, relFile) {
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"license": "ISC",
"dependencies": {
"archy": "^1.0.0",
"array-uniq": "^2.0.0",
"arrify": "^1.0.1",
"caching-transform": "^3.0.1",
"convert-source-map": "^1.6.0",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/cli/include-exclude/exclude-negated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
console.log('Hello, World!')
2 changes: 2 additions & 0 deletions test/fixtures/cli/include-exclude/excluded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
console.log('Hello, World!')
2 changes: 2 additions & 0 deletions test/fixtures/cli/include-exclude/node_modules/cover-me.js

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

42 changes: 42 additions & 0 deletions test/nyc-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,48 @@ describe('the nyc cli', function () {
done()
})
})

it('should allow negated exclude patterns', function (done) {
const args = [bin, '--all', '--exclude', '**/include-exclude/**', '--exclude', '!**/exclude-negated.js', process.execPath, './half-covered.js']

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

let stdout = ''
proc.stdout.on('data', chunk => {
stdout += chunk
})

proc.on('close', code => {
code.should.equal(0)
stdout.should.not.match(/excluded\.js/)
stdout.should.match(/exclude-negated\.js/)
done()
})
})

it('should include \'node_modules\' using exclude patterns', function (done) {
const args = [bin, '--all', '--exclude', '!**/node_modules/**', process.execPath, './half-covered.js']

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

let stdout = ''
proc.stdout.on('data', chunk => {
stdout += chunk
})

proc.on('close', code => {
code.should.equal(0)
stdout.should.match(/include-exclude\/node_modules/)
stdout.should.match(/cover-me\.js/)
done()
})
})
})

describe('--ignore-class-method', function () {
Expand Down

0 comments on commit 91de23c

Please sign in to comment.