From 8c9fe8e678ea43be75f787eaab4037d13ad5d2a1 Mon Sep 17 00:00:00 2001 From: Patrick Petrovic Date: Thu, 24 Nov 2022 11:25:24 +0100 Subject: [PATCH] [New] `bin/tape`: 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 | 16 ++++++--- readme.markdown | 15 ++++++-- test/ignore-pattern.js | 34 +++++++++++++++++++ test/ignore-pattern/.ignore | 1 + .../ignore-pattern/fake_node_modules/stub1.js | 8 +++++ .../ignore-pattern/fake_node_modules/stub2.js | 8 +++++ .../fake_other_ignored_dir/stub1.js | 8 +++++ .../fake_other_ignored_dir/stub2.js | 8 +++++ test/ignore-pattern/test.js | 8 +++++ test/ignore-pattern/test/stub1.js | 8 +++++ test/ignore-pattern/test/stub2.js | 8 +++++ test/ignore-pattern/test/sub/sub.stub1.js | 8 +++++ test/ignore-pattern/test/sub/sub.stub2.js | 8 +++++ test/ignore-pattern/test2.js | 8 +++++ 15 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 test/ignore-pattern.js create mode 100644 test/ignore-pattern/.ignore create mode 100644 test/ignore-pattern/fake_node_modules/stub1.js create mode 100644 test/ignore-pattern/fake_node_modules/stub2.js create mode 100644 test/ignore-pattern/fake_other_ignored_dir/stub1.js create mode 100644 test/ignore-pattern/fake_other_ignored_dir/stub2.js create mode 100644 test/ignore-pattern/test.js create mode 100644 test/ignore-pattern/test/stub1.js create mode 100644 test/ignore-pattern/test/stub2.js create mode 100644 test/ignore-pattern/test/sub/sub.stub1.js create mode 100644 test/ignore-pattern/test/sub/sub.stub2.js create mode 100644 test/ignore-pattern/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..3b41be76 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') { @@ -35,15 +35,23 @@ opts.require.forEach(function (module) { 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']; +} + +var matcher; +if (ignoreStr) { var ignore = require('dotignore'); matcher = ignore.createMatcher(ignoreStr); } diff --git a/readme.markdown b/readme.markdown index 5150c379..05c5e986 100644 --- a/readme.markdown +++ b/readme.markdown @@ -158,14 +158,25 @@ This is used to load modules before running tests and is explained extensively i **Alias**: `-i` -This flag is used when tests from certain folders and/or files are not intended to be run. It defaults to `.gitignore` file when passed with no argument. +This flag is used when tests from certain folders and/or files are not intended to be run. +The argument is a path to a file that contains the patterns to be ignored. +It defaults to `.gitignore` when passed with no argument. ```sh -tape -i .ignore **/*.js +tape -i .ignore '**/*.js' ``` An error is thrown if the specified file passed as argument does not exist. +## --ignore-pattern + +Same functionality as `--ignore`, but passing the pattern directly instead of an ignore file. +If both `--ignore` and `--ignore-pattern` are given, the `--ignore-pattern` argument is appended to the content of the ignore file. + +```sh +tape --ignore-pattern 'integration_tests/**/*.js' '**/*.js' +``` + ## --no-only This is particularly useful in a CI environment where an [only test](#testonlyname-opts-cb) is not supposed to go unnoticed. 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/ignore-pattern/.ignore b/test/ignore-pattern/.ignore new file mode 100644 index 00000000..ec1cc296 --- /dev/null +++ b/test/ignore-pattern/.ignore @@ -0,0 +1 @@ +fake_node_modules diff --git a/test/ignore-pattern/fake_node_modules/stub1.js b/test/ignore-pattern/fake_node_modules/stub1.js new file mode 100644 index 00000000..d8d58e15 --- /dev/null +++ b/test/ignore-pattern/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/ignore-pattern/fake_node_modules/stub2.js b/test/ignore-pattern/fake_node_modules/stub2.js new file mode 100644 index 00000000..213ee99e --- /dev/null +++ b/test/ignore-pattern/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/ignore-pattern/fake_other_ignored_dir/stub1.js b/test/ignore-pattern/fake_other_ignored_dir/stub1.js new file mode 100644 index 00000000..d8d58e15 --- /dev/null +++ b/test/ignore-pattern/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/ignore-pattern/fake_other_ignored_dir/stub2.js b/test/ignore-pattern/fake_other_ignored_dir/stub2.js new file mode 100644 index 00000000..213ee99e --- /dev/null +++ b/test/ignore-pattern/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/ignore-pattern/test.js b/test/ignore-pattern/test.js new file mode 100644 index 00000000..1364e64d --- /dev/null +++ b/test/ignore-pattern/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/ignore-pattern/test/stub1.js b/test/ignore-pattern/test/stub1.js new file mode 100644 index 00000000..0ea00c46 --- /dev/null +++ b/test/ignore-pattern/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/ignore-pattern/test/stub2.js b/test/ignore-pattern/test/stub2.js new file mode 100644 index 00000000..7f061890 --- /dev/null +++ b/test/ignore-pattern/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/ignore-pattern/test/sub/sub.stub1.js b/test/ignore-pattern/test/sub/sub.stub1.js new file mode 100644 index 00000000..96a23cde --- /dev/null +++ b/test/ignore-pattern/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/ignore-pattern/test/sub/sub.stub2.js b/test/ignore-pattern/test/sub/sub.stub2.js new file mode 100644 index 00000000..daa2c84e --- /dev/null +++ b/test/ignore-pattern/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/ignore-pattern/test2.js b/test/ignore-pattern/test2.js new file mode 100644 index 00000000..1364e64d --- /dev/null +++ b/test/ignore-pattern/test2.js @@ -0,0 +1,8 @@ +'use strict'; + +var tape = require('../../'); + +tape.test(function (t) { + t.pass('Should print'); + t.end(); +});