Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw well formatted error if file passed via argument cannot be found (closes #4047) #4050

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/cli/collect-files.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const fs = require('fs');
const ansi = require('ansi-colors');
const debug = require('debug')('mocha:cli:run:helpers');
const minimatch = require('minimatch');
Expand Down Expand Up @@ -66,6 +67,14 @@ module.exports = ({ignore, extension, file, recursive, sort, spec} = {}) => {
files = fileArgs.concat(files);
debug('files (in order): ', files);

// Check if the given files are resolvable
files.forEach(fileArg => {
if (!fs.existsSync(fileArg)) {
console.error(ansi.red(`Error: Cannot find file '${fileArg}'`));
process.exit(1);
}
});

if (!files.length) {
// give full message details when only 1 file is missing
const noneFoundMsg =
Expand Down
18 changes: 18 additions & 0 deletions test/integration/options/file.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var path = require('path').posix;
var helpers = require('../helpers');
var invokeMocha = helpers.invokeMocha;
var runMochaJSON = helpers.runMochaJSON;
var resolvePath = helpers.resolveFixturePath;

Expand Down Expand Up @@ -64,4 +65,21 @@ describe('--file', function() {
done();
});
});

it('should fail if the file can not be found', function(done) {
args = ['--file', 'non-existent'];

invokeMocha(
args,
function(err, result) {
if (err) {
return done(err);
}
expect(result, 'to have failed');
expect(result.output, 'to match', /Cannot find file/i);
done();
},
{stdio: 'pipe'}
);
});
});