Skip to content

Commit 4c97f54

Browse files
committed
[New] bin/tape: add --strict
When --strict is passed and zero files are found, tape will exit nonzero
1 parent 2d5c8dc commit 4c97f54

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

bin/tape

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ var objectKeys = require('object-keys');
99
var opts = parseOpts(process.argv.slice(2), {
1010
alias: { r: 'require', i: 'ignore' },
1111
string: ['require', 'ignore', 'ignore-pattern'],
12-
boolean: ['only'],
13-
default: { r: [], i: null, 'ignore-pattern': null, only: null }
12+
boolean: ['only', 'strict'],
13+
default: { r: [], i: null, 'ignore-pattern': null, only: null, strict: false }
1414
});
1515

1616
if (typeof opts.only === 'boolean') {
@@ -91,6 +91,11 @@ var files = opts._.reduce(function (result, arg) {
9191
return requireResolve(resolvePath(cwd, file));
9292
});
9393

94+
if (opts.strict && files.length === 0) {
95+
console.error('No test files found!');
96+
process.exit(127);
97+
}
98+
9499
var hasImport = require('has-dynamic-import');
95100

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

readme.markdown

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ $ tape 'tests/**/*.js'
6868
$ tape "tests/**/*.js"
6969
```
7070

71+
If you want `tape` to error when no files are found, pass `--strict`:
72+
73+
```sh
74+
$ tape --strict 'tests/**/*.js'
75+
```
76+
7177
## Preloading modules
7278

7379
Additionally, it is possible to make `tape` load one or more modules before running any tests, by using the `-r` or `--require` flag. Here's an example that loads [babel-register](https://babeljs.io/docs/usage/require/) before running any tests, to allow for JIT compilation:

test/strict.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
var tap = require('tap');
4+
var path = require('path');
5+
var exec = require('child_process').exec;
6+
7+
var stripFullStack = require('./common').stripFullStack;
8+
var stripDeprecations = require('./common').stripDeprecations;
9+
10+
var tapeBin = 'node ' + path.join(__dirname, '../bin/tape');
11+
12+
var expectedStackTraceBug = (/^3\.[012]\.\d+$/).test(process.versions.node); // https://github.com/nodejs/node/issues/2581
13+
14+
tap.test(
15+
'should throw error when --strict is passed via cli and no files are found',
16+
{ todo: expectedStackTraceBug ? 'Fails on these node versions' : false },
17+
function (tt) {
18+
tt.plan(3);
19+
20+
exec(tapeBin + ' --strict "no*files*found"', { cwd: path.join(__dirname) }, function (err, stdout, stderr) {
21+
tt.same(stdout.toString('utf8'), '');
22+
tt.match(stripFullStack(stderr.toString('utf8')).join('\n'), /^No test files found!\n$/);
23+
tt.equal(err.code, 127);
24+
});
25+
}
26+
);
27+
28+
tap.test(
29+
'should not throw error when --no-strict is passed via cli and no files are found',
30+
{ todo: expectedStackTraceBug ? 'Fails on these node versions' : false },
31+
function (tt) {
32+
tt.plan(3);
33+
34+
exec(tapeBin + ' --no-strict "no*files*found"', { cwd: path.join(__dirname) }, function (err, stdout, stderr) {
35+
tt.equal(stripDeprecations(stderr.toString('utf8')), '');
36+
tt.same(stripFullStack(stdout.toString('utf8')), [
37+
'TAP version 13',
38+
'',
39+
'1..0',
40+
'# tests 0',
41+
'# pass 0',
42+
'',
43+
'# ok',
44+
'',
45+
''
46+
]);
47+
tt.equal(err, null); // code 0
48+
});
49+
}
50+
);
51+
52+
tap.test(
53+
'should not throw error when no files are found',
54+
{ todo: expectedStackTraceBug ? 'Fails on these node versions' : false },
55+
function (tt) {
56+
tt.plan(3);
57+
58+
exec(tapeBin + ' "no*files*found"', { cwd: path.join(__dirname) }, function (err, stdout, stderr) {
59+
tt.equal(stripDeprecations(stderr.toString('utf8')), '');
60+
tt.same(stripFullStack(stdout.toString('utf8')), [
61+
'TAP version 13',
62+
'',
63+
'1..0',
64+
'# tests 0',
65+
'# pass 0',
66+
'',
67+
'# ok',
68+
'',
69+
''
70+
]);
71+
tt.equal(err, null); // code 0
72+
});
73+
}
74+
);

0 commit comments

Comments
 (0)