From bf50220cfd01a8aa811aef8f75e36b1ba8e76b2b Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Fri, 18 Jan 2019 17:33:32 -0500 Subject: [PATCH] Fix messages for file lookup issues - error scenario (#3654); see #3646 Scenario where multiple files (all) are not found only show a single message. I feel its useful for debugging to keep the message if only 1 file is not found. Happy to remove if others don't agree. No changes where at least 1 is found (still prints msg). See PR https://github.com/mochajs/mocha/pull/3650 for full details ## Scenarios ### Nothing given Command: `mocha` Output: ``` Error: No test files found: "test/" ``` ### Nothing found Command: `mocha -R json-stream --no-config "test/integration/fixtures/glob/**/*-none.js"` Output: ``` Error: No test files found: "test/integration/fixtures/glob/**/*-none.js" ``` ### Multiple not found Command: `mocha -R json-stream --no-config "test/integration/fixtures/glob/**/*-none.js" "test/integration/fixtures/glob/**/*-none-again.js"` New output ``` Error: No test files found ``` Previous output ``` Error: Cannot find any files matching pattern "test/integration/fixtures/glob/**/*-none.js" Error: Cannot find any files matching pattern "test/integration/fixtures/glob/**/*-none-again.js" ``` ## Applicable issues (semver-patch) --- lib/cli/run-helpers.js | 18 ++++++++++-------- test/integration/glob.spec.js | 27 ++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/lib/cli/run-helpers.js b/lib/cli/run-helpers.js index 95d1c2fc08..df53117fec 100644 --- a/lib/cli/run-helpers.js +++ b/lib/cli/run-helpers.js @@ -137,14 +137,14 @@ exports.handleFiles = ({ spec = [] } = {}) => { let files = []; - const errors = []; + const unmatched = []; spec.forEach(arg => { let newFiles; try { newFiles = utils.lookupFiles(arg, extension, recursive); } catch (err) { if (err.code === 'ERR_MOCHA_NO_FILES_MATCH_PATTERN') { - errors.push(err.message); + unmatched.push({message: err.message, pattern: err.pattern}); return; } @@ -164,15 +164,17 @@ exports.handleFiles = ({ }); if (!files.length) { - // print messages as an error - errors.forEach(message => { - console.error(ansi.red(`Error: ${message}`)); - }); + // give full message details when only 1 file is missing + const noneFoundMsg = + unmatched.length === 1 + ? `Error: No test files found: ${JSON.stringify(unmatched[0].pattern)}` // stringify to print escaped characters raw + : 'Error: No test files found'; + console.error(ansi.red(noneFoundMsg)); process.exit(1); } else { // print messages as an warning - errors.forEach(message => { - console.warn(ansi.yellow(`Warning: ${message}`)); + unmatched.forEach(warning => { + console.warn(ansi.yellow(`Warning: ${warning.message}`)); }); } diff --git a/test/integration/glob.spec.js b/test/integration/glob.spec.js index da1f5f35c6..4284320aa7 100644 --- a/test/integration/glob.spec.js +++ b/test/integration/glob.spec.js @@ -28,13 +28,24 @@ describe('globbing', function() { expect( results.stderr, 'to contain', - 'Error: Cannot find any files matching pattern "./*-none.js"' + 'Error: No test files found: "./*-none.js"' ); }, done ); }); + it('should handle multiple non-matching patterns', function(done) { + testGlob.shouldFail( + './*-none.js ./*-none-twice.js', + function(results) { + expect(results.stderr, 'to contain', 'Error: No test files found'); + expect(results.stderr, 'not to contain', '*-none'); + }, + done + ); + }); + it('should handle both matching and non-matching patterns in the same command', function(done) { testGlob.shouldSucceed( './*.js ./*-none.js', @@ -77,13 +88,23 @@ describe('globbing', function() { expect( results.stderr, 'to contain', - 'Error: Cannot find any files matching pattern "./*-none.js"' + 'Error: No test files found: "./*-none.js"' ); }, done ); }); + it('should handle multiple non-matching patterns', function(done) { + testGlob.shouldFail( + '"./*-none.js" "./*-none-twice.js"', + function(results) { + expect(results.stderr, 'to contain', 'Error: No test files found'); + }, + done + ); + }); + it('should handle both matching and non-matching patterns in the same command', function(done) { testGlob.shouldSucceed( '"./*.js" "./*-none.js"', @@ -125,7 +146,7 @@ describe('globbing', function() { expect( results.stderr, 'to contain', - 'Error: Cannot find any files matching pattern "./**/*-none.js"' + 'Error: No test files found: "./**/*-none.js"' ); }, done