Skip to content

Commit

Permalink
[New] bin/tape: add --ignore-pattern flag
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ppati000 authored and ljharb committed Nov 24, 2022
1 parent e9c9aba commit 8c9fe8e
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 6 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Expand Up @@ -10,6 +10,7 @@
"allowReserved": false,
},
"rules": {
"array-bracket-newline": "off",
"array-bracket-spacing": "off",
"complexity": "off",
"func-style": ["error", "declaration"],
Expand Down
16 changes: 12 additions & 4 deletions bin/tape
Expand Up @@ -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') {
Expand All @@ -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);
}
Expand Down
15 changes: 13 additions & 2 deletions readme.markdown
Expand Up @@ -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.

Expand Down
34 changes: 34 additions & 0 deletions 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);
});
});
1 change: 1 addition & 0 deletions test/ignore-pattern/.ignore
@@ -0,0 +1 @@
fake_node_modules
8 changes: 8 additions & 0 deletions test/ignore-pattern/fake_node_modules/stub1.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/ignore-pattern/fake_node_modules/stub2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions 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');
});
8 changes: 8 additions & 0 deletions 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();
});
8 changes: 8 additions & 0 deletions test/ignore-pattern/test.js
@@ -0,0 +1,8 @@
'use strict';

var tape = require('../../');

tape.test(function (t) {
t.pass('Should print');
t.end();
});
8 changes: 8 additions & 0 deletions 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');
});
8 changes: 8 additions & 0 deletions 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();
});
8 changes: 8 additions & 0 deletions 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');
});
8 changes: 8 additions & 0 deletions 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();
});
8 changes: 8 additions & 0 deletions test/ignore-pattern/test2.js
@@ -0,0 +1,8 @@
'use strict';

var tape = require('../../');

tape.test(function (t) {
t.pass('Should print');
t.end();
});

0 comments on commit 8c9fe8e

Please sign in to comment.