From e5f8f73294785d51fe25e1deb0715e95c79a6fcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20M=C3=BCller?= Date: Mon, 19 Sep 2016 00:06:13 +0200 Subject: [PATCH] Report non-match to STDERR and exit if no files are matched (#2450) * Report non-matching patterns to STDERR and exit with non-zero status if no files are matched at all. Fixes #2194 * Code review changes --- bin/_mocha | 19 ++++++++++++++++++- test/acceptance/glob/glob.sh | 15 +++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/bin/_mocha b/bin/_mocha index fe3f67861a..b355e28352 100755 --- a/bin/_mocha +++ b/bin/_mocha @@ -358,9 +358,26 @@ if (!args.length) { } args.forEach(function(arg) { - files = files.concat(utils.lookupFiles(arg, extensions, program.recursive)); + var newFiles; + try { + newFiles = utils.lookupFiles(arg, extensions, program.recursive); + } catch (err) { + if (err.message.indexOf('cannot resolve path') === 0) { + console.error('Warning: Could not find any test files matching pattern: ' + arg); + return; + } + + throw err; + } + + files = files.concat(newFiles); }); +if (!files.length) { + console.error('No test files found'); + process.exit(1); +} + // resolve files = files.map(function(path) { diff --git a/test/acceptance/glob/glob.sh b/test/acceptance/glob/glob.sh index a2b8c6fea2..823ba07fb9 100755 --- a/test/acceptance/glob/glob.sh +++ b/test/acceptance/glob/glob.sh @@ -22,11 +22,22 @@ cat /tmp/mocha-glob.txt | grep -q -F '["end",{"suites":1,"tests":1,"passes":1,"p exit 1 } -cat /tmp/mocha-glob.txt | grep -q -F 'cannot resolve path' || { +cat /tmp/mocha-glob.txt | grep -q -F 'Could not find any test files matching pattern' || { echo Globbing './*-none.js' in `pwd` should match no files and run no tests. exit 1 } +../../../bin/mocha -R json-stream ./*.js ./*-none.js >& /tmp/mocha-glob.txt || { + echo Globbing ./*.js ./*-none.js in `pwd` failed. + exit 1 +} + +cat /tmp/mocha-glob.txt | grep -q -F '["end",{"suites":1,"tests":1,"passes":1,"pending":0,"failures":0,' && +cat /tmp/mocha-glob.txt | grep -q -F 'Could not find any test files matching pattern' || { + echo Globbing ./*.js ./*-none.js in `pwd` should match glob.js with one test inside and display one warning for the non-existing file. + exit 1 +} + # Globbing in windows command-shell differs completely from unix-style globbing. # In bash, the shell expands globs and passes the result to executables. # In windows, the shell passes globs unexpanded, executables do expansion if they support it. @@ -47,7 +58,7 @@ cat /tmp/mocha-glob.txt | grep -q -F '["end",{"suites":1,"tests":1,"passes":1,"p exit 1 } -cat /tmp/mocha-glob.txt | grep -q -F 'cannot resolve path' || { +cat /tmp/mocha-glob.txt | grep -q -F 'Could not find any test files matching pattern' || { echo Globbing './*-none.js' in `pwd` should match no files and run no tests. exit 1 }