From b3c49403042533b6e80df0ab6b3edc0ca67c98f5 Mon Sep 17 00:00:00 2001 From: Patrick Petrovic Date: Thu, 24 Nov 2022 11:25:24 +0100 Subject: [PATCH] [New] Add `--ignore-pattern` flag This PR introduces the --ignore-pattern flag as a shorthand for ignoring test files. The flag may be used together with --ignore; the input will be concatenated in that case. I figured this behavior would make the most sense, since users may want to ignore everything in .gitignore and some other files on top. Fixes #586 --- .eslintrc | 1 + bin/tape | 14 ++++++-- test/ignore-pattern.js | 34 +++++++++++++++++++ test/ignorePattern/.ignore | 1 + test/ignorePattern/fake_node_modules/stub1.js | 8 +++++ test/ignorePattern/fake_node_modules/stub2.js | 8 +++++ .../fake_other_ignored_dir/stub1.js | 8 +++++ .../fake_other_ignored_dir/stub2.js | 8 +++++ test/ignorePattern/test.js | 8 +++++ test/ignorePattern/test/stub1.js | 8 +++++ test/ignorePattern/test/stub2.js | 8 +++++ test/ignorePattern/test/sub/sub.stub1.js | 8 +++++ test/ignorePattern/test/sub/sub.stub2.js | 8 +++++ test/ignorePattern/test2.js | 8 +++++ 14 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 test/ignore-pattern.js create mode 100644 test/ignorePattern/.ignore create mode 100644 test/ignorePattern/fake_node_modules/stub1.js create mode 100644 test/ignorePattern/fake_node_modules/stub2.js create mode 100644 test/ignorePattern/fake_other_ignored_dir/stub1.js create mode 100644 test/ignorePattern/fake_other_ignored_dir/stub2.js create mode 100644 test/ignorePattern/test.js create mode 100644 test/ignorePattern/test/stub1.js create mode 100644 test/ignorePattern/test/stub2.js create mode 100644 test/ignorePattern/test/sub/sub.stub1.js create mode 100644 test/ignorePattern/test/sub/sub.stub2.js create mode 100644 test/ignorePattern/test2.js diff --git a/.eslintrc b/.eslintrc index 4c288e12..a9102333 100644 --- a/.eslintrc +++ b/.eslintrc @@ -10,6 +10,7 @@ "allowReserved": false, }, "rules": { + "array-bracket-newline": "off", "array-bracket-spacing": "off", "complexity": "off", "func-style": ["error", "declaration"], diff --git a/bin/tape b/bin/tape index 0525f238..38b246b7 100755 --- a/bin/tape +++ b/bin/tape @@ -7,9 +7,9 @@ var objectKeys = require('object-keys'); var opts = parseOpts(process.argv.slice(2), { alias: { r: 'require', i: 'ignore' }, - string: ['require', 'ignore'], + string: ['require', 'ignore', 'ignore-pattern'], boolean: ['only'], - default: { r: [], i: null, only: null } + default: { r: [], i: null, 'ignore-pattern': null, only: null } }); if (typeof opts.only === 'boolean') { @@ -36,14 +36,22 @@ var resolvePath = require('path').resolve; var requireResolve = require.resolve; var matcher; +var ignoreStr = ''; if (typeof opts.ignore === 'string') { var readFileSync = require('fs').readFileSync; try { - var ignoreStr = readFileSync(resolvePath(cwd, opts.ignore || '.gitignore'), 'utf-8'); + ignoreStr = readFileSync(resolvePath(cwd, opts.ignore || '.gitignore'), 'utf-8'); } catch (e) { console.error(e.message); process.exit(2); } +} + +if (typeof opts['ignore-pattern'] === 'string') { + ignoreStr += '\n' + opts['ignore-pattern']; +} + +if (ignoreStr) { var ignore = require('dotignore'); matcher = ignore.createMatcher(ignoreStr); } diff --git a/test/ignore-pattern.js b/test/ignore-pattern.js new file mode 100644 index 00000000..9ad0952f --- /dev/null +++ b/test/ignore-pattern.js @@ -0,0 +1,34 @@ +'use strict'; + +var tap = require('tap'); +var path = require('path'); +var execFile = require('child_process').execFile; + +var tapeBin = path.join(process.cwd(), 'bin/tape'); + +tap.test('should allow ignore file together with --ignore-pattern', function (tt) { + tt.plan(1); + var proc = execFile(tapeBin, ['--ignore', '.ignore', '--ignore-pattern', 'fake_other_ignored_dir', '**/*.js'], { cwd: path.join(__dirname, 'ignore-pattern') }); + + proc.on('exit', function (code) { + tt.equals(code, 0); + }); +}); + +tap.test('should allow --ignore-pattern without ignore file', function (tt) { + tt.plan(1); + var proc = execFile(tapeBin, ['--ignore-pattern', 'fake_*', '**/*.js'], { cwd: path.join(__dirname, 'ignore-pattern') }); + + proc.on('exit', function (code) { + tt.equals(code, 0); + }); +}); + +tap.test('should fail if not ignoring', function (tt) { + tt.plan(1); + var proc = execFile(tapeBin, ['**/*.js'], { cwd: path.join(__dirname, 'ignore-pattern') }); + + proc.on('exit', function (code) { + tt.equals(code, 1); + }); +}); diff --git a/test/ignorePattern/.ignore b/test/ignorePattern/.ignore new file mode 100644 index 00000000..ec1cc296 --- /dev/null +++ b/test/ignorePattern/.ignore @@ -0,0 +1 @@ +fake_node_modules diff --git a/test/ignorePattern/fake_node_modules/stub1.js b/test/ignorePattern/fake_node_modules/stub1.js new file mode 100644 index 00000000..d8d58e15 --- /dev/null +++ b/test/ignorePattern/fake_node_modules/stub1.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../'); + +tape.test(function (t) { + t.plan(1); + t.fail('Should not print'); +}); diff --git a/test/ignorePattern/fake_node_modules/stub2.js b/test/ignorePattern/fake_node_modules/stub2.js new file mode 100644 index 00000000..213ee99e --- /dev/null +++ b/test/ignorePattern/fake_node_modules/stub2.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../'); + +tape.test(function (t) { + t.fail('Should not print'); + t.end(); +}); diff --git a/test/ignorePattern/fake_other_ignored_dir/stub1.js b/test/ignorePattern/fake_other_ignored_dir/stub1.js new file mode 100644 index 00000000..d8d58e15 --- /dev/null +++ b/test/ignorePattern/fake_other_ignored_dir/stub1.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../'); + +tape.test(function (t) { + t.plan(1); + t.fail('Should not print'); +}); diff --git a/test/ignorePattern/fake_other_ignored_dir/stub2.js b/test/ignorePattern/fake_other_ignored_dir/stub2.js new file mode 100644 index 00000000..213ee99e --- /dev/null +++ b/test/ignorePattern/fake_other_ignored_dir/stub2.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../'); + +tape.test(function (t) { + t.fail('Should not print'); + t.end(); +}); diff --git a/test/ignorePattern/test.js b/test/ignorePattern/test.js new file mode 100644 index 00000000..1364e64d --- /dev/null +++ b/test/ignorePattern/test.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../'); + +tape.test(function (t) { + t.pass('Should print'); + t.end(); +}); diff --git a/test/ignorePattern/test/stub1.js b/test/ignorePattern/test/stub1.js new file mode 100644 index 00000000..0ea00c46 --- /dev/null +++ b/test/ignorePattern/test/stub1.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../'); + +tape.test(function (t) { + t.plan(1); + t.pass('test/stub1'); +}); diff --git a/test/ignorePattern/test/stub2.js b/test/ignorePattern/test/stub2.js new file mode 100644 index 00000000..7f061890 --- /dev/null +++ b/test/ignorePattern/test/stub2.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../'); + +tape.test(function (t) { + t.pass('test/stub2'); + t.end(); +}); diff --git a/test/ignorePattern/test/sub/sub.stub1.js b/test/ignorePattern/test/sub/sub.stub1.js new file mode 100644 index 00000000..96a23cde --- /dev/null +++ b/test/ignorePattern/test/sub/sub.stub1.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../../'); + +tape.test(function (t) { + t.plan(1); + t.pass('test/sub/stub1'); +}); diff --git a/test/ignorePattern/test/sub/sub.stub2.js b/test/ignorePattern/test/sub/sub.stub2.js new file mode 100644 index 00000000..daa2c84e --- /dev/null +++ b/test/ignorePattern/test/sub/sub.stub2.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../../../'); + +tape.test(function (t) { + t.pass('test/sub/stub2'); + t.end(); +}); diff --git a/test/ignorePattern/test2.js b/test/ignorePattern/test2.js new file mode 100644 index 00000000..1364e64d --- /dev/null +++ b/test/ignorePattern/test2.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../'); + +tape.test(function (t) { + t.pass('Should print'); + t.end(); +});