Skip to content

Commit

Permalink
[New] 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 tape-testing#586
  • Loading branch information
ppati000 authored and ljharb committed Nov 24, 2022
1 parent e9c9aba commit 100f313
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 3 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
14 changes: 11 additions & 3 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 @@ -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);
}
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 --ignorePattern', function (tt) {
tt.plan(1);
var proc = execFile(tapeBin, ['--ignore', '.ignore', '--ignorePattern', 'fake_other_ignored_dir', '**/*.js'], { cwd: path.join(__dirname, 'ignorePattern') });

proc.on('exit', function (code) {
tt.equals(code, 0);
});
});

tap.test('should allow --ignorePattern without ignore file', function (tt) {
tt.plan(1);
var proc = execFile(tapeBin, ['--ignorePattern', 'fake_*', '**/*.js'], { cwd: path.join(__dirname, 'ignorePattern') });

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, 'ignorePattern') });

proc.on('exit', function (code) {
tt.equals(code, 1);
});
});
1 change: 1 addition & 0 deletions test/ignorePattern/.ignore
@@ -0,0 +1 @@
fake_node_modules
8 changes: 8 additions & 0 deletions test/ignorePattern/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/ignorePattern/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/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');
});
8 changes: 8 additions & 0 deletions 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();
});
8 changes: 8 additions & 0 deletions test/ignorePattern/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/ignorePattern/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/ignorePattern/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/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');
});
8 changes: 8 additions & 0 deletions 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();
});
8 changes: 8 additions & 0 deletions test/ignorePattern/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 100f313

Please sign in to comment.