From 19b9384add1e44961394b910d0468ab40585c0ba Mon Sep 17 00:00:00 2001 From: Andrew Finlay Date: Fri, 25 Jan 2019 14:19:11 +1100 Subject: [PATCH 1/6] Add tests for nyc --all negated excludes Test that we can include excluded files using negated excludes Test that we can include files under 'node_modules' using negated excludes All tests currently fail --- .gitignore | 2 + .../cli/include-exclude/exclude-negated.js | 2 + test/fixtures/cli/include-exclude/excluded.js | 2 + .../include-exclude/node_modules/cover-me.js | 2 + test/nyc-bin.js | 42 +++++++++++++++++++ 5 files changed, 50 insertions(+) create mode 100644 test/fixtures/cli/include-exclude/exclude-negated.js create mode 100644 test/fixtures/cli/include-exclude/excluded.js create mode 100644 test/fixtures/cli/include-exclude/node_modules/cover-me.js diff --git a/.gitignore b/.gitignore index 397b8d1ea..f47f288c0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ test/build/ *.covered.js *.swp needs-transpile.js + +!*test/fixtures/cli/include-exclude/node_modules/ diff --git a/test/fixtures/cli/include-exclude/exclude-negated.js b/test/fixtures/cli/include-exclude/exclude-negated.js new file mode 100644 index 000000000..239a04e74 --- /dev/null +++ b/test/fixtures/cli/include-exclude/exclude-negated.js @@ -0,0 +1,2 @@ +'use strict'; +console.log('Hello, World!') diff --git a/test/fixtures/cli/include-exclude/excluded.js b/test/fixtures/cli/include-exclude/excluded.js new file mode 100644 index 000000000..239a04e74 --- /dev/null +++ b/test/fixtures/cli/include-exclude/excluded.js @@ -0,0 +1,2 @@ +'use strict'; +console.log('Hello, World!') diff --git a/test/fixtures/cli/include-exclude/node_modules/cover-me.js b/test/fixtures/cli/include-exclude/node_modules/cover-me.js new file mode 100644 index 000000000..239a04e74 --- /dev/null +++ b/test/fixtures/cli/include-exclude/node_modules/cover-me.js @@ -0,0 +1,2 @@ +'use strict'; +console.log('Hello, World!') diff --git a/test/nyc-bin.js b/test/nyc-bin.js index a22ea4c09..d76b8ded4 100644 --- a/test/nyc-bin.js +++ b/test/nyc-bin.js @@ -69,6 +69,48 @@ describe('the nyc cli', function () { done() }) }) + + it('should allow negated exclude patterns', function (done) { + var args = [bin, '--all', '--exclude', '**/include-exclude/**', '--exclude', '!**/exclude-negated.js', process.execPath, './half-covered.js'] + + var proc = spawn(process.execPath, args, { + cwd: fixturesCLI, + env: env + }) + + var stdout = '' + proc.stdout.on('data', function (chunk) { + stdout += chunk + }) + + proc.on('close', function (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) { + var args = [bin, '--all', '--exclude', '!**/node_modules/**', process.execPath, './half-covered.js'] + + var proc = spawn(process.execPath, args, { + cwd: fixturesCLI, + env: env + }) + + var stdout = '' + proc.stdout.on('data', function (chunk) { + stdout += chunk + }) + + proc.on('close', function (code) { + code.should.equal(0) + stdout.should.match(/include-exclude\/node_modules/) + stdout.should.match(/cover-me\.js/) + done() + }) + }) }) describe('--ignore-class-method', function () { From 89b64c1fee834d18b117f02401fcc97d713b7121 Mon Sep 17 00:00:00 2001 From: Andrew Finlay Date: Fri, 25 Jan 2019 14:35:28 +1100 Subject: [PATCH 2/6] 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. Notes: I've promoted 'array-uniq' to a top level dependency, I figured I'd point out that it uses es6 under the hood as I don't see if used often in this project --- index.js | 13 ++++++++++++- package.json | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ffa1b3f17..690f580e4 100755 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ /* global __coverage__ */ +const arrayUniq = require('array-uniq') const arrify = require('arrify') const cachingTransform = require('caching-transform') const debugLog = require('debug-log')('nyc') @@ -246,7 +247,17 @@ NYC.prototype.walkAllFiles = function (dir, visitor) { pattern = '**/*{' + this.extensions.join() + '}' } - glob.sync(pattern, {cwd: dir, nodir: true, ignore: this.exclude.exclude}).forEach(function (filename) { + var filesToWalk = glob.sync(pattern, {cwd: dir, nodir: true, ignore: this.exclude.exclude}) + + var excludeNegatedPatterns = this.exclude.excludeNegated + if (excludeNegatedPatterns.length > 0) { + excludeNegatedPatterns.forEach(function (pattern) { + filesToWalk = filesToWalk.concat(glob.sync(pattern, {cwd: dir, nodir: true})) + }) + } + filesToWalk = arrayUniq(filesToWalk) + + filesToWalk.forEach(function (filename) { visitor(filename) }) } diff --git a/package.json b/package.json index 50638a021..44e670e3f 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "license": "ISC", "dependencies": { "archy": "^1.0.0", + "array-uniq": "^2.0.0", "arrify": "^1.0.1", "caching-transform": "^2.0.0", "convert-source-map": "^1.6.0", From ece8aa3204482903c261b32c1099e701e1f0869d Mon Sep 17 00:00:00 2001 From: Andrew Finlay Date: Fri, 25 Jan 2019 15:22:22 +1100 Subject: [PATCH 3/6] Simplify changes I'm putting this one down to being a 43C day and the air conditioning being broken. --- index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 690f580e4..b6de349f0 100755 --- a/index.js +++ b/index.js @@ -250,11 +250,9 @@ NYC.prototype.walkAllFiles = function (dir, visitor) { var filesToWalk = glob.sync(pattern, {cwd: dir, nodir: true, ignore: this.exclude.exclude}) var excludeNegatedPatterns = this.exclude.excludeNegated - if (excludeNegatedPatterns.length > 0) { - excludeNegatedPatterns.forEach(function (pattern) { - filesToWalk = filesToWalk.concat(glob.sync(pattern, {cwd: dir, nodir: true})) - }) - } + excludeNegatedPatterns.forEach(function (pattern) { + filesToWalk = filesToWalk.concat(glob.sync(pattern, {cwd: dir, nodir: true})) + }) filesToWalk = arrayUniq(filesToWalk) filesToWalk.forEach(function (filename) { From 569b529332bd996a19b35396fe4fb7b806e3bc03 Mon Sep 17 00:00:00 2001 From: Andrew Finlay Date: Tue, 19 Feb 2019 10:17:05 +1100 Subject: [PATCH 4/6] Update package-lock file --- package-lock.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/package-lock.json b/package-lock.json index f35957e52..9f59fa7fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -216,6 +216,11 @@ "es-abstract": "^1.7.0" } }, + "array-uniq": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/array-uniq/-/array-uniq-2.0.0.tgz", + "integrity": "sha512-O3QZEr+3wDj7otzF7PjNGs6CA3qmYMLvt5xGkjY/V0VxS+ovvqVo/5wKM/OVOAyuX4DTh9H31zE/yKtO66hTkg==" + }, "arrify": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/arrify/-/arrify-1.0.1.tgz", From ed4ccd86a9ab8c771e9a280c50be931be2be2193 Mon Sep 17 00:00:00 2001 From: Andrew Finlay Date: Tue, 19 Feb 2019 10:29:09 +1100 Subject: [PATCH 5/6] Fix style issues --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 88f14ccf2..94ae28669 100755 --- a/index.js +++ b/index.js @@ -250,11 +250,11 @@ NYC.prototype.walkAllFiles = function (dir, visitor) { pattern = '**/*{' + this.extensions.join() + '}' } - var filesToWalk = glob.sync(pattern, {cwd: dir, nodir: true, ignore: this.exclude.exclude}) + var filesToWalk = glob.sync(pattern, { cwd: dir, nodir: true, ignore: this.exclude.exclude }) var excludeNegatedPatterns = this.exclude.excludeNegated excludeNegatedPatterns.forEach(function (pattern) { - filesToWalk = filesToWalk.concat(glob.sync(pattern, {cwd: dir, nodir: true})) + filesToWalk = filesToWalk.concat(glob.sync(pattern, { cwd: dir, nodir: true })) }) filesToWalk = arrayUniq(filesToWalk) From d5d9b64fced4075e111ab060d7c2ad1815220b6c Mon Sep 17 00:00:00 2001 From: Andrew Finlay Date: Wed, 20 Feb 2019 15:35:06 +1100 Subject: [PATCH 6/6] Modernise code changes --- index.js | 20 ++++++++------------ test/nyc-bin.js | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 94ae28669..122ed8a12 100755 --- a/index.js +++ b/index.js @@ -243,24 +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()}}` - var filesToWalk = glob.sync(pattern, { cwd: dir, nodir: true, ignore: this.exclude.exclude }) + let filesToWalk = glob.sync(pattern, { cwd: dir, nodir: true, ignore: this.exclude.exclude }) - var excludeNegatedPatterns = this.exclude.excludeNegated - excludeNegatedPatterns.forEach(function (pattern) { + // 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(function (filename) { - visitor(filename) - }) + filesToWalk.forEach(visitor) } NYC.prototype._maybeInstrumentSource = function (code, filename, relFile) { diff --git a/test/nyc-bin.js b/test/nyc-bin.js index 03e73a892..3529ee9ba 100644 --- a/test/nyc-bin.js +++ b/test/nyc-bin.js @@ -103,19 +103,19 @@ describe('the nyc cli', function () { }) it('should allow negated exclude patterns', function (done) { - var args = [bin, '--all', '--exclude', '**/include-exclude/**', '--exclude', '!**/exclude-negated.js', process.execPath, './half-covered.js'] + const args = [bin, '--all', '--exclude', '**/include-exclude/**', '--exclude', '!**/exclude-negated.js', process.execPath, './half-covered.js'] - var proc = spawn(process.execPath, args, { + const proc = spawn(process.execPath, args, { cwd: fixturesCLI, env: env }) - var stdout = '' - proc.stdout.on('data', function (chunk) { + let stdout = '' + proc.stdout.on('data', chunk => { stdout += chunk }) - proc.on('close', function (code) { + proc.on('close', code => { code.should.equal(0) stdout.should.not.match(/excluded\.js/) stdout.should.match(/exclude-negated\.js/) @@ -124,19 +124,19 @@ describe('the nyc cli', function () { }) it('should include \'node_modules\' using exclude patterns', function (done) { - var args = [bin, '--all', '--exclude', '!**/node_modules/**', process.execPath, './half-covered.js'] + const args = [bin, '--all', '--exclude', '!**/node_modules/**', process.execPath, './half-covered.js'] - var proc = spawn(process.execPath, args, { + const proc = spawn(process.execPath, args, { cwd: fixturesCLI, env: env }) - var stdout = '' - proc.stdout.on('data', function (chunk) { + let stdout = '' + proc.stdout.on('data', chunk => { stdout += chunk }) - proc.on('close', function (code) { + proc.on('close', code => { code.should.equal(0) stdout.should.match(/include-exclude\/node_modules/) stdout.should.match(/cover-me\.js/)