Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add -i flag to specify ignore files #492

Merged
merged 1 commit into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions bin/tape
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

var resolveModule = require('resolve').sync;
var resolvePath = require('path').resolve;
var readFileSync = require('fs').readFileSync;
var parseOpts = require('minimist');
var glob = require('glob');
var ignore = require('dotignore');

var opts = parseOpts(process.argv.slice(2), {
alias: { r: 'require' },
string: 'require',
default: { r: [] }
alias: { r: 'require', i: 'ignore' },
string: ['require', 'ignore'],
default: { r: [], i: null }
});

var cwd = process.cwd();
Expand All @@ -29,6 +31,16 @@ opts.require.forEach(function (module) {
}
});

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

opts._.forEach(function (arg) {
// If glob does not match, `files` will be an empty array.
// Note: `glob.sync` may throw an error and crash the node process.
Expand All @@ -38,7 +50,7 @@ opts._.forEach(function (arg) {
throw new TypeError('unknown error: glob.sync did not return an array or throw. Please report this.');
}

files.forEach(function (file) {
files.filter(function (file) { return !matcher || !matcher.shouldIgnore(file); }).forEach(function (file) {
require(resolvePath(cwd, file));
});
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"deep-equal": "^2.0.1",
"defined": "^1.0.0",
"dotignore": "^0.1.2",
"for-each": "^0.3.3",
"function-bind": "^1.1.1",
"glob": "^7.1.6",
Expand Down
1 change: 1 addition & 0 deletions test/ignore/.ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fake_node_modules
8 changes: 8 additions & 0 deletions test/ignore/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/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/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

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

tape.test(function (t) {
t.plan(1);
t.ok('Okay');
});
8 changes: 8 additions & 0 deletions test/ignore/test/stub1.js
Original file line number Diff line number Diff line change
@@ -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/test/stub2.js
Original file line number Diff line number Diff line change
@@ -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/test/sub/sub.stub1.js
Original file line number Diff line number Diff line change
@@ -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/test/sub/sub.stub2.js
Original file line number Diff line number Diff line change
@@ -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/test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

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

tape.test(function (t) {
t.pass('Should print');
t.end();
});
122 changes: 122 additions & 0 deletions test/ignore_from_gitignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
'use strict';

var tap = require('tap');
var path = require('path');
var spawn = require('child_process').spawn;
var concat = require('concat-stream');

var stripFullStack = require('./common').stripFullStack;

var tapeBin = path.join(process.cwd(), 'bin/tape');

tap.test('Should pass with ignoring', { skip: process.platform === 'win32' }, function (tt) {
tt.plan(2);

var tc = function (rows) {
tt.same(stripFullStack(rows.toString('utf8')), [
'TAP version 13',
'# (anonymous)',
'ok 1 should be truthy',
'# (anonymous)',
'ok 2 test/stub1',
'# (anonymous)',
'ok 3 test/stub2',
'# (anonymous)',
'ok 4 test/sub/stub1',
'# (anonymous)',
'ok 5 test/sub/stub2',
'# (anonymous)',
'ok 6 Should print',
'',
'1..6',
'# tests 6',
'# pass 6',
'',
'# ok',
'',
''
].join('\n'));
};

var ps = spawn(tapeBin, ['**/*.js', '-i', '.ignore'], {cwd: path.join(__dirname, 'ignore')});
ps.stdout.pipe(concat(tc));
ps.on('exit', function (code) {
tt.equal(code, 0); // code 0
});
});

tap.test('Should pass', { skip: process.platform === 'win32' }, function (tt) {
tt.plan(2);

var tc = function (rows) {
tt.same(stripFullStack(rows.toString('utf8')), [
'TAP version 13',
'# (anonymous)',
'not ok 1 Should not print',
' ---',
' operator: fail',
' at: Test.<anonymous> ($TEST/ignore/fake_node_modules/stub1.js:$LINE:$COL)',
' stack: |-',
' Error: Should not print',
' [... stack stripped ...]',
' at Test.<anonymous> ($TEST/ignore/fake_node_modules/stub1.js:$LINE:$COL)',
' [... stack stripped ...]',
' ...',
'# (anonymous)',
'not ok 2 Should not print',
' ---',
' operator: fail',
' at: Test.<anonymous> ($TEST/ignore/fake_node_modules/stub2.js:$LINE:$COL)',
' stack: |-',
' Error: Should not print',
' [... stack stripped ...]',
' at Test.<anonymous> ($TEST/ignore/fake_node_modules/stub2.js:$LINE:$COL)',
' [... stack stripped ...]',
' ...',
'# (anonymous)',
'ok 3 should be truthy',
'# (anonymous)',
'ok 4 test/stub1',
'# (anonymous)',
'ok 5 test/stub2',
'# (anonymous)',
'ok 6 test/sub/stub1',
'# (anonymous)',
'ok 7 test/sub/stub2',
'# (anonymous)',
'ok 8 Should print',
'',
'1..8',
'# tests 8',
'# pass 6',
'# fail 2',
'',
''
].join('\n'));
};

var ps = spawn(tapeBin, ['**/*.js'], {cwd: path.join(__dirname, 'ignore')});
ps.stdout.pipe(concat(tc));
ps.on('exit', function (code) {
tt.equal(code, 1);
});
});

tap.test('Should fail when ignore file does not exist', { skip: process.platform === 'win32' }, function (tt) {
tt.plan(3);

var testStdout = function (rows) {
tt.same(rows.toString('utf8'), '');
};

var testStderr = function (rows) {
tt.ok(/^ENOENT[:,] no such file or directory,? (?:open )?'\$TEST\/ignore\/.gitignore'\n$/m.test(stripFullStack(rows.toString('utf8'))));
};

var ps = spawn(tapeBin, ['**/*.js', '-i'], {cwd: path.join(__dirname, 'ignore')});
ps.stdout.pipe(concat(testStdout));
ps.stderr.pipe(concat(testStderr));
ps.on('exit', function (code) {
tt.equal(code, 2);
});
});