Skip to content

Commit 91de23c

Browse files
AndrewFinlaycoreyfarrell
authored andcommitted
fix: Exclude negated not working with '--all' switch (#977)
* 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.
1 parent 509c6aa commit 91de23c

File tree

8 files changed

+69
-8
lines changed

8 files changed

+69
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ test/build/
66
*.covered.js
77
*.swp
88
needs-transpile.js
9+
10+
!*test/fixtures/cli/include-exclude/node_modules/

index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/* global __coverage__ */
44

5+
const arrayUniq = require('array-uniq')
56
const arrify = require('arrify')
67
const cachingTransform = require('caching-transform')
78
const util = require('util')
@@ -247,16 +248,20 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
247248
}
248249

249250
NYC.prototype.walkAllFiles = function (dir, visitor) {
250-
var pattern = null
251-
if (this.extensions.length === 1) {
252-
pattern = '**/*' + this.extensions[0]
253-
} else {
254-
pattern = '**/*{' + this.extensions.join() + '}'
255-
}
251+
const pattern = (this.extensions.length === 1)
252+
? `**/*${this.extensions[0]}`
253+
: `**/*{${this.extensions.join()}}`
256254

257-
glob.sync(pattern, { cwd: dir, nodir: true, ignore: this.exclude.exclude }).forEach(function (filename) {
258-
visitor(filename)
255+
let filesToWalk = glob.sync(pattern, { cwd: dir, nodir: true, ignore: this.exclude.exclude })
256+
257+
// package node-glob no longer observes negated excludes, so we need to restore these files ourselves
258+
const excludeNegatedPaths = this.exclude.excludeNegated
259+
excludeNegatedPaths.forEach(pattern => {
260+
filesToWalk = filesToWalk.concat(glob.sync(pattern, { cwd: dir, nodir: true }))
259261
})
262+
filesToWalk = arrayUniq(filesToWalk)
263+
264+
filesToWalk.forEach(visitor)
260265
}
261266

262267
NYC.prototype._maybeInstrumentSource = function (code, filename, relFile) {

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"license": "ISC",
7575
"dependencies": {
7676
"archy": "^1.0.0",
77+
"array-uniq": "^2.0.0",
7778
"arrify": "^1.0.1",
7879
"caching-transform": "^3.0.1",
7980
"convert-source-map": "^1.6.0",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict';
2+
console.log('Hello, World!')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict';
2+
console.log('Hello, World!')

test/fixtures/cli/include-exclude/node_modules/cover-me.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/nyc-bin.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,48 @@ describe('the nyc cli', function () {
101101
done()
102102
})
103103
})
104+
105+
it('should allow negated exclude patterns', function (done) {
106+
const args = [bin, '--all', '--exclude', '**/include-exclude/**', '--exclude', '!**/exclude-negated.js', process.execPath, './half-covered.js']
107+
108+
const proc = spawn(process.execPath, args, {
109+
cwd: fixturesCLI,
110+
env: env
111+
})
112+
113+
let stdout = ''
114+
proc.stdout.on('data', chunk => {
115+
stdout += chunk
116+
})
117+
118+
proc.on('close', code => {
119+
code.should.equal(0)
120+
stdout.should.not.match(/excluded\.js/)
121+
stdout.should.match(/exclude-negated\.js/)
122+
done()
123+
})
124+
})
125+
126+
it('should include \'node_modules\' using exclude patterns', function (done) {
127+
const args = [bin, '--all', '--exclude', '!**/node_modules/**', process.execPath, './half-covered.js']
128+
129+
const proc = spawn(process.execPath, args, {
130+
cwd: fixturesCLI,
131+
env: env
132+
})
133+
134+
let stdout = ''
135+
proc.stdout.on('data', chunk => {
136+
stdout += chunk
137+
})
138+
139+
proc.on('close', code => {
140+
code.should.equal(0)
141+
stdout.should.match(/include-exclude\/node_modules/)
142+
stdout.should.match(/cover-me\.js/)
143+
done()
144+
})
145+
})
104146
})
105147

106148
describe('--ignore-class-method', function () {

0 commit comments

Comments
 (0)