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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Exclude negated not working with '--all' switch #977

Merged
merged 13 commits into from
Feb 22, 2019
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 @@ -242,16 +243,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)
coreyfarrell marked this conversation as resolved.
Show resolved Hide resolved

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add another test (in a follow-up) to do nearly the same thing as this, just without the --all argument and with a test script that actually has require('./include-exclude/node_modules/cover-me.js');? This will prove that --exclude '!**/node_modules/**' works again. I've already done this basic test at the command-line and it did what I expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I'll put something together after lunch

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