From 4c97f544939d949f18a1f53211f5c35c6902d52b Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 14 Sep 2024 15:42:59 -0700 Subject: [PATCH] [New] `bin/tape`: add `--strict` When --strict is passed and zero files are found, tape will exit nonzero --- bin/tape | 9 ++++-- readme.markdown | 6 ++++ test/strict.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 test/strict.js diff --git a/bin/tape b/bin/tape index e45a8ee9..17ec3d47 100755 --- a/bin/tape +++ b/bin/tape @@ -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') { @@ -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('../'); diff --git a/readme.markdown b/readme.markdown index f37aa7e5..43867cca 100644 --- a/readme.markdown +++ b/readme.markdown @@ -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: diff --git a/test/strict.js b/test/strict.js new file mode 100644 index 00000000..5683c404 --- /dev/null +++ b/test/strict.js @@ -0,0 +1,74 @@ +'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 + +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 + }); + } +);