Skip to content

Commit

Permalink
[New] bin/tape: add --strict
Browse files Browse the repository at this point in the history
When --strict is passed and zero files are found, tape will exit nonzero
  • Loading branch information
ljharb committed Sep 14, 2024
1 parent 11c5e95 commit 982f07b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
9 changes: 7 additions & 2 deletions bin/tape
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var objectKeys = require('object-keys');
var opts = parseOpts(process.argv.slice(2), {
alias: { r: 'require', i: 'ignore' },
string: ['require', 'ignore', 'ignore-pattern'],
boolean: ['only'],
default: { r: [], i: null, 'ignore-pattern': null, only: null }
boolean: ['only', 'strict'],
default: { r: [], i: null, 'ignore-pattern': null, only: null, strict: false }
});

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

if (opts.strict && files.length === 0) {
console.error('No test files found!');
process.exit(127);
}

var hasImport = require('has-dynamic-import');

var tape = require('../');
Expand Down
6 changes: 6 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ $ tape 'tests/**/*.js'
$ tape "tests/**/*.js"
```

If you want `tape` to error when no files are found, pass `--strict`:

```sh
$ tape --strict 'tests/**/*.js'
```

## Preloading modules

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:
Expand Down
75 changes: 75 additions & 0 deletions test/strict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

var tap = require('tap');
var path = require('path');
var exec = require('child_process').exec;

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

var tapeBin = 'node ' + path.join(__dirname, '../bin/tape');

var expectedStackTraceBug = (/^3\.[012]\.\d+$/).test(process.versions.node); // https://github.com/nodejs/node/issues/2581
var expectedExitCodeOnError = (/^0\.(?:9|10)/).test(process.versions.node) ? 8 : 127; // node v0.9 sets this exit code to 8, for some reason

tap.test(
'should throw error when --strict is passed via cli and no files are found',
{ todo: expectedStackTraceBug ? 'Fails on these node versions' : false },
function (tt) {
tt.plan(3);

exec(tapeBin + ' --strict "no*files*found"', { cwd: path.join(__dirname) }, function (err, stdout, stderr) {
tt.same(stdout.toString('utf8'), '');
tt.match(stripFullStack(stderr.toString('utf8')).join('\n'), /^No test files found!\n$/);
tt.equal(err.code, 127);
});
}
);

tap.test(
'should not throw error when --no-strict is passed via cli and no files are found',
{ todo: expectedStackTraceBug ? 'Fails on these node versions' : false },
function (tt) {
tt.plan(3);

exec(tapeBin + ' --no-strict "no*files*found"', { cwd: path.join(__dirname) }, function (err, stdout, stderr) {
tt.equal(stripDeprecations(stderr.toString('utf8')), '');
tt.same(stripFullStack(stdout.toString('utf8')), [
'TAP version 13',
'',
'1..0',
'# tests 0',
'# pass 0',
'',
'# ok',
'',
''
]);
tt.equal(err, null); // code 0
});
}
);

tap.test(
'should not throw error when no files are found',
{ todo: expectedStackTraceBug ? 'Fails on these node versions' : false },
function (tt) {
tt.plan(3);

exec(tapeBin + ' "no*files*found"', { cwd: path.join(__dirname) }, function (err, stdout, stderr) {
tt.equal(stripDeprecations(stderr.toString('utf8')), '');
tt.same(stripFullStack(stdout.toString('utf8')), [
'TAP version 13',
'',
'1..0',
'# tests 0',
'# pass 0',
'',
'# ok',
'',
''
]);
tt.equal(err, null); // code 0
});
}
);

0 comments on commit 982f07b

Please sign in to comment.