Skip to content

Commit a0f9350

Browse files
r0mflipljharb
authored andcommitted
[New] tape binary: Add -i flag to ignore files from gitignore
1 parent 36a30eb commit a0f9350

File tree

12 files changed

+204
-4
lines changed

12 files changed

+204
-4
lines changed

bin/tape

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
var resolveModule = require('resolve').sync;
66
var resolvePath = require('path').resolve;
7+
var readFileSync = require('fs').readFileSync;
78
var parseOpts = require('minimist');
89
var glob = require('glob');
10+
var ignore = require('dotignore');
911

1012
var opts = parseOpts(process.argv.slice(2), {
11-
alias: { r: 'require' },
12-
string: 'require',
13-
default: { r: [] }
13+
alias: { r: 'require', i: 'ignore' },
14+
string: ['require', 'ignore'],
15+
default: { r: [], i: null }
1416
});
1517

1618
var cwd = process.cwd();
@@ -29,6 +31,16 @@ opts.require.forEach(function (module) {
2931
}
3032
});
3133

34+
if (typeof opts.ignore === 'string') {
35+
try {
36+
var ignoreStr = readFileSync(resolvePath(cwd, opts.ignore || '.gitignore'), 'utf-8');
37+
} catch (e) {
38+
console.error(e.message);
39+
process.exit(2);
40+
}
41+
var matcher = ignore.createMatcher(ignoreStr);
42+
}
43+
3244
opts._.forEach(function (arg) {
3345
// If glob does not match, `files` will be an empty array.
3446
// Note: `glob.sync` may throw an error and crash the node process.
@@ -38,7 +50,7 @@ opts._.forEach(function (arg) {
3850
throw new TypeError('unknown error: glob.sync did not return an array or throw. Please report this.');
3951
}
4052

41-
files.forEach(function (file) {
53+
files.filter(function (file) { return !matcher || !matcher.shouldIgnore(file); }).forEach(function (file) {
4254
require(resolvePath(cwd, file));
4355
});
4456
});

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"deep-equal": "^2.0.1",
2424
"defined": "^1.0.0",
25+
"dotignore": "^0.1.2",
2526
"for-each": "^0.3.3",
2627
"function-bind": "^1.1.1",
2728
"glob": "^7.1.6",

test/ignore/.ignore

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

test/ignore/fake_node_modules/stub1.js

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

test/ignore/fake_node_modules/stub2.js

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

test/ignore/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.plan(1);
7+
t.ok('Okay');
8+
});

test/ignore/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+
});

test/ignore/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+
});

test/ignore/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+
});

test/ignore/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+
});

test/ignore/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+
});

test/ignore_from_gitignore.js

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
'use strict';
2+
3+
var tap = require('tap');
4+
var path = require('path');
5+
var spawn = require('child_process').spawn;
6+
var concat = require('concat-stream');
7+
8+
var stripFullStack = require('./common').stripFullStack;
9+
10+
var tapeBin = path.join(process.cwd(), 'bin/tape');
11+
12+
tap.test('Should pass with ignoring', { skip: process.platform === 'win32' }, function (tt) {
13+
tt.plan(2);
14+
15+
var tc = function (rows) {
16+
tt.same(stripFullStack(rows.toString('utf8')), [
17+
'TAP version 13',
18+
'# (anonymous)',
19+
'ok 1 should be truthy',
20+
'# (anonymous)',
21+
'ok 2 test/stub1',
22+
'# (anonymous)',
23+
'ok 3 test/stub2',
24+
'# (anonymous)',
25+
'ok 4 test/sub/stub1',
26+
'# (anonymous)',
27+
'ok 5 test/sub/stub2',
28+
'# (anonymous)',
29+
'ok 6 Should print',
30+
'',
31+
'1..6',
32+
'# tests 6',
33+
'# pass 6',
34+
'',
35+
'# ok',
36+
'',
37+
''
38+
].join('\n'));
39+
};
40+
41+
var ps = spawn(tapeBin, ['**/*.js', '-i', '.ignore'], {cwd: path.join(__dirname, 'ignore')});
42+
ps.stdout.pipe(concat(tc));
43+
ps.on('exit', function (code) {
44+
tt.equal(code, 0); // code 0
45+
});
46+
});
47+
48+
tap.test('Should pass', { skip: process.platform === 'win32' }, function (tt) {
49+
tt.plan(2);
50+
51+
var tc = function (rows) {
52+
tt.same(stripFullStack(rows.toString('utf8')), [
53+
'TAP version 13',
54+
'# (anonymous)',
55+
'not ok 1 Should not print',
56+
' ---',
57+
' operator: fail',
58+
' at: Test.<anonymous> ($TEST/ignore/fake_node_modules/stub1.js:$LINE:$COL)',
59+
' stack: |-',
60+
' Error: Should not print',
61+
' [... stack stripped ...]',
62+
' at Test.<anonymous> ($TEST/ignore/fake_node_modules/stub1.js:$LINE:$COL)',
63+
' [... stack stripped ...]',
64+
' ...',
65+
'# (anonymous)',
66+
'not ok 2 Should not print',
67+
' ---',
68+
' operator: fail',
69+
' at: Test.<anonymous> ($TEST/ignore/fake_node_modules/stub2.js:$LINE:$COL)',
70+
' stack: |-',
71+
' Error: Should not print',
72+
' [... stack stripped ...]',
73+
' at Test.<anonymous> ($TEST/ignore/fake_node_modules/stub2.js:$LINE:$COL)',
74+
' [... stack stripped ...]',
75+
' ...',
76+
'# (anonymous)',
77+
'ok 3 should be truthy',
78+
'# (anonymous)',
79+
'ok 4 test/stub1',
80+
'# (anonymous)',
81+
'ok 5 test/stub2',
82+
'# (anonymous)',
83+
'ok 6 test/sub/stub1',
84+
'# (anonymous)',
85+
'ok 7 test/sub/stub2',
86+
'# (anonymous)',
87+
'ok 8 Should print',
88+
'',
89+
'1..8',
90+
'# tests 8',
91+
'# pass 6',
92+
'# fail 2',
93+
'',
94+
''
95+
].join('\n'));
96+
};
97+
98+
var ps = spawn(tapeBin, ['**/*.js'], {cwd: path.join(__dirname, 'ignore')});
99+
ps.stdout.pipe(concat(tc));
100+
ps.on('exit', function (code) {
101+
tt.equal(code, 1);
102+
});
103+
});
104+
105+
tap.test('Should fail when ignore file does not exist', { skip: process.platform === 'win32' }, function (tt) {
106+
tt.plan(3);
107+
108+
var testStdout = function (rows) {
109+
tt.same(rows.toString('utf8'), '');
110+
};
111+
112+
var testStderr = function (rows) {
113+
tt.ok(/^ENOENT[:,] no such file or directory,? (?:open )?'\$TEST\/ignore\/.gitignore'\n$/m.test(stripFullStack(rows.toString('utf8'))));
114+
};
115+
116+
var ps = spawn(tapeBin, ['**/*.js', '-i'], {cwd: path.join(__dirname, 'ignore')});
117+
ps.stdout.pipe(concat(testStdout));
118+
ps.stderr.pipe(concat(testStderr));
119+
ps.on('exit', function (code) {
120+
tt.equal(code, 2);
121+
});
122+
});

0 commit comments

Comments
 (0)