Skip to content

Commit

Permalink
Ensure CLI receives file paths
Browse files Browse the repository at this point in the history
Fixes #2158.
  • Loading branch information
nblthree authored and novemberborn committed Jul 30, 2019
1 parent 2fc7d56 commit f01d05f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/cli.js
@@ -1,5 +1,6 @@
'use strict';
const path = require('path');
const fs = require('fs');
const del = require('del');
const updateNotifier = require('update-notifier');
const figures = require('figures');
Expand Down Expand Up @@ -213,6 +214,24 @@ exports.run = async () => { // eslint-disable-line complexity

const match = arrify(conf.match);
const resolveTestsFrom = cli.input.length === 0 ? projectDir : process.cwd();

const files = cli.input.map(file => path.relative(resolveTestsFrom, path.resolve(process.cwd(), file)));

for (const file of cli.input) {
try {
const stats = fs.statSync(file);
if (!stats.isFile()) {
exit(`${file} is not a test file.`);
}
} catch (error) {
if (error.code === 'ENOENT') {
exit(`${file} does not exist.`);
} else {
exit(`Error accessing ${file}\n\n${chalk.gray((error && error.stack) || error)}`);
}
}
}

const api = new Api({
babelConfig,
cacheEnabled: conf.cache !== false,
Expand Down Expand Up @@ -268,8 +287,6 @@ exports.run = async () => { // eslint-disable-line complexity
});
});

const files = cli.input.map(file => path.relative(resolveTestsFrom, path.resolve(process.cwd(), file)));

if (conf.watch) {
const watcher = new Watcher({
api,
Expand Down
16 changes: 16 additions & 0 deletions test/integration/assorted.js
Expand Up @@ -27,6 +27,22 @@ test('timeout', t => {
// }, 2000);
// });

test('Should throw error if passed file does not exist', t => {
execCli('no-such-file.js', (err, e, stdout) => {
t.ok(err);
t.match(stdout, /no-such-file\.js does not exist\./);
t.end();
});
});

test('Should throw error if passed file is a directory', t => {
execCli('ava-paths', (err, e, stdout) => {
t.ok(err);
t.match(stdout, /ava-paths is not a test file\./);
t.end();
});
});

test('include anonymous functions in error reports', t => {
execCli('error-in-anonymous-function.js', (err, stdout) => {
t.ok(err);
Expand Down

0 comments on commit f01d05f

Please sign in to comment.