Skip to content

Commit

Permalink
[Ignore] Introduce --ignorePattern flag
Browse files Browse the repository at this point in the history
This PR introduces the --ignorePattern 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.

Let me know if this looks good, then I'll also add some documentation :)

Fixes tape-testing#586
  • Loading branch information
ppati000 committed Nov 24, 2022
1 parent e9c9aba commit 67de7f7
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 9 deletions.
28 changes: 19 additions & 9 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', 'ignorePattern'],
boolean: ['only'],
default: { r: [], i: null, only: null }
default: { r: [], i: null, ignorePattern: null, only: null }
});

if (typeof opts.only === 'boolean') {
Expand All @@ -36,14 +36,24 @@ var resolvePath = require('path').resolve;
var requireResolve = require.resolve;

var matcher;
if (typeof opts.ignore === 'string') {
var readFileSync = require('fs').readFileSync;
try {
var ignoreStr = readFileSync(resolvePath(cwd, opts.ignore || '.gitignore'), 'utf-8');
} catch (e) {
console.error(e.message);
process.exit(2);
var isIgnoreFlag = typeof opts.ignore === 'string'
var isIgnorePatternFlag = typeof opts.ignorePattern === 'string'

if (isIgnoreFlag || isIgnorePatternFlag) {
var ignoreStr = ''
if (isIgnoreFlag) {
var readFileSync = require('fs').readFileSync;
try {
ignoreStr = readFileSync(resolvePath(cwd, opts.ignore || '.gitignore'), 'utf-8');
} catch (e) {
console.error(e.message);
process.exit(2);
}
}
if (isIgnorePatternFlag) {
ignoreStr = ignoreStr.concat("\n" + opts.ignorePattern)
}

var ignore = require('dotignore');
matcher = ignore.createMatcher(ignoreStr);
}
Expand Down
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();
});
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, ['**/*.js', '--ignore', '.ignore', '--ignorePattern', 'fake_other_ignored_dir'], { 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, ['**/*.js', '--ignorePattern', 'fake_*'], { 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);
});
});

0 comments on commit 67de7f7

Please sign in to comment.