From 349f7463634f8b3ffbc059985a275df50420da4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20M=C3=BCller?= Date: Sun, 21 Aug 2016 17:36:16 +0200 Subject: [PATCH] Report non-matching patterns to STDERR and exit with non-zero status if no files are matched at all. Fixes #2194 --- bin/_mocha | 18 +++++++++++++++++- test/acceptance/glob/glob.sh | 15 +++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/bin/_mocha b/bin/_mocha index 98cabdbc47..949a00d3fe 100755 --- a/bin/_mocha +++ b/bin/_mocha @@ -358,9 +358,25 @@ 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; + } else { + throw err; + } + } + + files = files.concat(newFiles); }); +if (files.length === 0) { + 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 }