From c61c492fc744fba15199312411c58d2e7ba0cd10 Mon Sep 17 00:00:00 2001 From: Alex Bainter Date: Sat, 31 Mar 2018 21:42:14 -0700 Subject: [PATCH] #1577 Add "--exclude" Option (#3210) * Add --exclude as option. * Add tests for --exclude option. * Implement --exclude. * Update --exclude description. * Remove old exclude fixture. * Add new exclude fixture. * Fix tests for --exclude option. * Add name to package.contributors. * Filter new files before they're added. * Allow multiple --exclude flags rather than using a comma-delimited list. * Use .fixture extension for exclude fixtures. * Use runMochaJSON (run) instead of of directInvoke. --- bin/_mocha | 11 +++- package.json | 2 + .../fixtures/options/exclude/fail.fixture.js | 7 +++ .../options/exclude/nested/fail.fixture.js | 7 +++ .../options/exclude/nested/pass.fixture.js | 5 ++ .../fixtures/options/exclude/pass.fixture.js | 5 ++ test/integration/options.spec.js | 53 +++++++++++++++++++ 7 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 test/integration/fixtures/options/exclude/fail.fixture.js create mode 100644 test/integration/fixtures/options/exclude/nested/fail.fixture.js create mode 100644 test/integration/fixtures/options/exclude/nested/pass.fixture.js create mode 100644 test/integration/fixtures/options/exclude/pass.fixture.js diff --git a/bin/_mocha b/bin/_mocha index ba250e97ba..c7d9c7f3d0 100755 --- a/bin/_mocha +++ b/bin/_mocha @@ -10,6 +10,7 @@ const program = require('commander'); const path = require('path'); const fs = require('fs'); +const minimatch = require('minimatch'); const resolve = path.resolve; const exists = fs.existsSync; const Mocha = require('../'); @@ -205,7 +206,8 @@ program .option('--allow-uncaught', 'enable uncaught errors to propagate') .option('--forbid-only', 'causes test marked with only to fail the suite') .option('--forbid-pending', 'causes pending tests and test marked with skip to fail the suite') - .option('--file ', 'include a file to be ran during the suite', collect, []); + .option('--file ', 'include a file to be ran during the suite', collect, []) + .option('--exclude ', 'a file or glob pattern to ignore', collect, []); program._name = 'mocha'; @@ -494,6 +496,13 @@ args.forEach(arg => { throw err; } + if (typeof newFiles !== 'undefined') { + if (typeof newFiles === 'string') { + newFiles = [newFiles]; + } + newFiles = newFiles.filter(fileName => program.exclude.every(pattern => !minimatch(fileName, pattern))); + } + files = files.concat(newFiles); }); diff --git a/package.json b/package.json index 34f3c79e7f..e1ff16052e 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "Adrian Ludwig (https://github.com/adrian-ludwig)", "Ainthe Kitchen (https://github.com/ainthek)", "ajaykodali (https://github.com/ajaykodali)", + "Alex Bainter (https://github.com/metalex9)", "Alex Early (https://github.com/aearly)", "Alex Pham (https://github.com/thedark1337)", "amsul (https://github.com/amsul)", @@ -314,6 +315,7 @@ "glob": "7.1.2", "growl": "1.10.3", "he": "1.1.1", + "minimatch": "^3.0.4", "mkdirp": "0.5.1", "supports-color": "4.4.0" }, diff --git a/test/integration/fixtures/options/exclude/fail.fixture.js b/test/integration/fixtures/options/exclude/fail.fixture.js new file mode 100644 index 0000000000..2b58528bfc --- /dev/null +++ b/test/integration/fixtures/options/exclude/fail.fixture.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('exclude test fail', function () { + it('should not run this test', function () { + throw new Error('should not run'); + }); +}); diff --git a/test/integration/fixtures/options/exclude/nested/fail.fixture.js b/test/integration/fixtures/options/exclude/nested/fail.fixture.js new file mode 100644 index 0000000000..5264ce7bff --- /dev/null +++ b/test/integration/fixtures/options/exclude/nested/fail.fixture.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('exclude test nested fail', function () { + it('should not run this test', function () { + throw new Error('should not run'); + }); +}); diff --git a/test/integration/fixtures/options/exclude/nested/pass.fixture.js b/test/integration/fixtures/options/exclude/nested/pass.fixture.js new file mode 100644 index 0000000000..36f55d138c --- /dev/null +++ b/test/integration/fixtures/options/exclude/nested/pass.fixture.js @@ -0,0 +1,5 @@ +'use strict'; + +describe('exclude test nested pass', function () { + it('should find this test', function () {}); +}); diff --git a/test/integration/fixtures/options/exclude/pass.fixture.js b/test/integration/fixtures/options/exclude/pass.fixture.js new file mode 100644 index 0000000000..ada7e68678 --- /dev/null +++ b/test/integration/fixtures/options/exclude/pass.fixture.js @@ -0,0 +1,5 @@ +'use strict'; + +describe('exclude test pass', function () { + it('should find this test', function () {}); +}); diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js index a010572821..5cc63b42d5 100644 --- a/test/integration/options.spec.js +++ b/test/integration/options.spec.js @@ -450,4 +450,57 @@ describe('options', function () { }, path.join(__dirname, 'fixtures', 'options', 'help')); }); }); + + describe('--exclude', function () { + /* + * Runs mocha in {path} with the given args. + * Calls handleResult with the result. + */ + function runMochaTest (fixture, args, handleResult, done) { + run(fixture, args, function (err, res) { + if (err) { + done(err); + return; + } + handleResult(res); + done(); + }); + } + + it('should exclude specific files', function (done) { + runMochaTest('options/exclude/*.fixture.js', [ + '--exclude', + 'test/integration/fixtures/options/exclude/fail.fixture.js' + ], function (res) { + assert.equal(res.stats.pending, 0); + assert.equal(res.stats.passes, 1); + assert.equal(res.stats.failures, 0); + assert.equal(res.passes[0].title, 'should find this test'); + assert.equal(res.code, 0); + }, done); + }); + + it('should exclude globbed files', function (done) { + runMochaTest('options/exclude/**/*.fixture.js', ['--exclude', '**/fail.fixture.js'], function (res) { + assert.equal(res.stats.pending, 0); + assert.equal(res.stats.passes, 2); + assert.equal(res.stats.failures, 0); + assert.equal(res.code, 0); + }, done); + }); + + it('should exclude multiple patterns', function (done) { + runMochaTest('options/exclude/**/*.fixture.js', [ + '--exclude', + 'test/integration/fixtures/options/exclude/fail.fixture.js', + '--exclude', + 'test/integration/fixtures/options/exclude/nested/fail.fixture.js' + ], function (res) { + assert.equal(res.stats.pending, 0); + assert.equal(res.stats.passes, 2); + assert.equal(res.stats.failures, 0); + assert.equal(res.code, 0); + }, done); + }); + }); });