Skip to content

Commit f01d05f

Browse files
nblthreenovemberborn
authored andcommittedJul 30, 2019
Ensure CLI receives file paths
Fixes #2158.
1 parent 2fc7d56 commit f01d05f

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed
 

‎lib/cli.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const path = require('path');
3+
const fs = require('fs');
34
const del = require('del');
45
const updateNotifier = require('update-notifier');
56
const figures = require('figures');
@@ -213,6 +214,24 @@ exports.run = async () => { // eslint-disable-line complexity
213214

214215
const match = arrify(conf.match);
215216
const resolveTestsFrom = cli.input.length === 0 ? projectDir : process.cwd();
217+
218+
const files = cli.input.map(file => path.relative(resolveTestsFrom, path.resolve(process.cwd(), file)));
219+
220+
for (const file of cli.input) {
221+
try {
222+
const stats = fs.statSync(file);
223+
if (!stats.isFile()) {
224+
exit(`${file} is not a test file.`);
225+
}
226+
} catch (error) {
227+
if (error.code === 'ENOENT') {
228+
exit(`${file} does not exist.`);
229+
} else {
230+
exit(`Error accessing ${file}\n\n${chalk.gray((error && error.stack) || error)}`);
231+
}
232+
}
233+
}
234+
216235
const api = new Api({
217236
babelConfig,
218237
cacheEnabled: conf.cache !== false,
@@ -268,8 +287,6 @@ exports.run = async () => { // eslint-disable-line complexity
268287
});
269288
});
270289

271-
const files = cli.input.map(file => path.relative(resolveTestsFrom, path.resolve(process.cwd(), file)));
272-
273290
if (conf.watch) {
274291
const watcher = new Watcher({
275292
api,

‎test/integration/assorted.js

+16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ test('timeout', t => {
2727
// }, 2000);
2828
// });
2929

30+
test('Should throw error if passed file does not exist', t => {
31+
execCli('no-such-file.js', (err, e, stdout) => {
32+
t.ok(err);
33+
t.match(stdout, /no-such-file\.js does not exist\./);
34+
t.end();
35+
});
36+
});
37+
38+
test('Should throw error if passed file is a directory', t => {
39+
execCli('ava-paths', (err, e, stdout) => {
40+
t.ok(err);
41+
t.match(stdout, /ava-paths is not a test file\./);
42+
t.end();
43+
});
44+
});
45+
3046
test('include anonymous functions in error reports', t => {
3147
execCli('error-in-anonymous-function.js', (err, stdout) => {
3248
t.ok(err);

0 commit comments

Comments
 (0)
Please sign in to comment.