Skip to content

Commit 8c9fe8e

Browse files
patrick-petrovicljharb
authored andcommitted
[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
1 parent e9c9aba commit 8c9fe8e

File tree

15 files changed

+141
-6
lines changed

15 files changed

+141
-6
lines changed

Diff for: .eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"allowReserved": false,
1111
},
1212
"rules": {
13+
"array-bracket-newline": "off",
1314
"array-bracket-spacing": "off",
1415
"complexity": "off",
1516
"func-style": ["error", "declaration"],

Diff for: bin/tape

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ var objectKeys = require('object-keys');
77

88
var opts = parseOpts(process.argv.slice(2), {
99
alias: { r: 'require', i: 'ignore' },
10-
string: ['require', 'ignore'],
10+
string: ['require', 'ignore', 'ignore-pattern'],
1111
boolean: ['only'],
12-
default: { r: [], i: null, only: null }
12+
default: { r: [], i: null, 'ignore-pattern': null, only: null }
1313
});
1414

1515
if (typeof opts.only === 'boolean') {
@@ -35,15 +35,23 @@ opts.require.forEach(function (module) {
3535
var resolvePath = require('path').resolve;
3636
var requireResolve = require.resolve;
3737

38-
var matcher;
38+
var ignoreStr = '';
3939
if (typeof opts.ignore === 'string') {
4040
var readFileSync = require('fs').readFileSync;
4141
try {
42-
var ignoreStr = readFileSync(resolvePath(cwd, opts.ignore || '.gitignore'), 'utf-8');
42+
ignoreStr = readFileSync(resolvePath(cwd, opts.ignore || '.gitignore'), 'utf-8');
4343
} catch (e) {
4444
console.error(e.message);
4545
process.exit(2);
4646
}
47+
}
48+
49+
if (typeof opts['ignore-pattern'] === 'string') {
50+
ignoreStr += '\n' + opts['ignore-pattern'];
51+
}
52+
53+
var matcher;
54+
if (ignoreStr) {
4755
var ignore = require('dotignore');
4856
matcher = ignore.createMatcher(ignoreStr);
4957
}

Diff for: readme.markdown

+13-2
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,25 @@ This is used to load modules before running tests and is explained extensively i
158158

159159
**Alias**: `-i`
160160

161-
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.
161+
This flag is used when tests from certain folders and/or files are not intended to be run.
162+
The argument is a path to a file that contains the patterns to be ignored.
163+
It defaults to `.gitignore` when passed with no argument.
162164

163165
```sh
164-
tape -i .ignore **/*.js
166+
tape -i .ignore '**/*.js'
165167
```
166168

167169
An error is thrown if the specified file passed as argument does not exist.
168170

171+
## --ignore-pattern
172+
173+
Same functionality as `--ignore`, but passing the pattern directly instead of an ignore file.
174+
If both `--ignore` and `--ignore-pattern` are given, the `--ignore-pattern` argument is appended to the content of the ignore file.
175+
176+
```sh
177+
tape --ignore-pattern 'integration_tests/**/*.js' '**/*.js'
178+
```
179+
169180
## --no-only
170181
This is particularly useful in a CI environment where an [only test](#testonlyname-opts-cb) is not supposed to go unnoticed.
171182

Diff for: test/ignore-pattern.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
var tap = require('tap');
4+
var path = require('path');
5+
var execFile = require('child_process').execFile;
6+
7+
var tapeBin = path.join(process.cwd(), 'bin/tape');
8+
9+
tap.test('should allow ignore file together with --ignore-pattern', function (tt) {
10+
tt.plan(1);
11+
var proc = execFile(tapeBin, ['--ignore', '.ignore', '--ignore-pattern', 'fake_other_ignored_dir', '**/*.js'], { cwd: path.join(__dirname, 'ignore-pattern') });
12+
13+
proc.on('exit', function (code) {
14+
tt.equals(code, 0);
15+
});
16+
});
17+
18+
tap.test('should allow --ignore-pattern without ignore file', function (tt) {
19+
tt.plan(1);
20+
var proc = execFile(tapeBin, ['--ignore-pattern', 'fake_*', '**/*.js'], { cwd: path.join(__dirname, 'ignore-pattern') });
21+
22+
proc.on('exit', function (code) {
23+
tt.equals(code, 0);
24+
});
25+
});
26+
27+
tap.test('should fail if not ignoring', function (tt) {
28+
tt.plan(1);
29+
var proc = execFile(tapeBin, ['**/*.js'], { cwd: path.join(__dirname, 'ignore-pattern') });
30+
31+
proc.on('exit', function (code) {
32+
tt.equals(code, 1);
33+
});
34+
});

Diff for: test/ignore-pattern/.ignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fake_node_modules

Diff for: test/ignore-pattern/fake_node_modules/stub1.js

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: test/ignore-pattern/fake_node_modules/stub2.js

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: test/ignore-pattern/fake_other_ignored_dir/stub1.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
var tape = require('../../../');
4+
5+
tape.test(function (t) {
6+
t.plan(1);
7+
t.fail('Should not print');
8+
});

Diff for: test/ignore-pattern/fake_other_ignored_dir/stub2.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
var tape = require('../../../');
4+
5+
tape.test(function (t) {
6+
t.fail('Should not print');
7+
t.end();
8+
});

Diff for: test/ignore-pattern/test.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
var tape = require('../../');
4+
5+
tape.test(function (t) {
6+
t.pass('Should print');
7+
t.end();
8+
});

Diff for: test/ignore-pattern/test/stub1.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
var tape = require('../../../');
4+
5+
tape.test(function (t) {
6+
t.plan(1);
7+
t.pass('test/stub1');
8+
});

Diff for: test/ignore-pattern/test/stub2.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
var tape = require('../../../');
4+
5+
tape.test(function (t) {
6+
t.pass('test/stub2');
7+
t.end();
8+
});

Diff for: test/ignore-pattern/test/sub/sub.stub1.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
var tape = require('../../../../');
4+
5+
tape.test(function (t) {
6+
t.plan(1);
7+
t.pass('test/sub/stub1');
8+
});

Diff for: test/ignore-pattern/test/sub/sub.stub2.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
var tape = require('../../../../');
4+
5+
tape.test(function (t) {
6+
t.pass('test/sub/stub2');
7+
t.end();
8+
});

Diff for: test/ignore-pattern/test2.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
var tape = require('../../');
4+
5+
tape.test(function (t) {
6+
t.pass('Should print');
7+
t.end();
8+
});

0 commit comments

Comments
 (0)