Skip to content

Commit 59254b7

Browse files
sotojuannovemberborn
authored andcommitted
Use current working directory if no package.json (#1044)
1 parent 3c3a6d8 commit 59254b7

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

api.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,13 @@ Api.prototype._setupPrecompiler = function (files) {
118118
var cacheDir = uniqueTempDir();
119119

120120
if (isCacheEnabled) {
121-
cacheDir = findCacheDir({
121+
var foundDir = findCacheDir({
122122
name: 'ava',
123123
files: files
124124
});
125+
if (foundDir !== null) {
126+
cacheDir = foundDir;
127+
}
125128
}
126129

127130
this.options.cacheDir = cacheDir;

lib/cli.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ Promise.longStackTraces();
2222

2323
exports.run = function () {
2424
var conf = pkgConf.sync('ava');
25-
var pkgDir = path.dirname(pkgConf.filepath(conf));
25+
26+
var filepath = pkgConf.filepath(conf);
27+
var pkgDir = filepath === null ? process.cwd() : path.dirname(filepath);
2628

2729
var cli = meow([
2830
'Usage',

test/cli.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var mkdirp = require('mkdirp');
1212
var touch = require('touch');
1313
var proxyquire = require('proxyquire');
1414
var sinon = require('sinon');
15+
var uniqueTempDir = require('unique-temp-dir');
16+
var execa = require('execa');
1517

1618
var cliPath = path.join(__dirname, '../cli.js');
1719

@@ -371,6 +373,17 @@ test('prefers local version of ava', function (t) {
371373
t.end();
372374
});
373375

376+
test('use current working directory if `package.json` is not found', function () {
377+
var cwd = uniqueTempDir({create: true});
378+
var testFilePath = path.join(cwd, 'test.js');
379+
var cliPath = require.resolve('../cli.js');
380+
var avaPath = require.resolve('../');
381+
382+
fs.writeFileSync(testFilePath, 'import test from ' + JSON.stringify(avaPath) + ';\ntest(t => { t.pass(); });');
383+
384+
return execa(process.execPath, [cliPath], {cwd: cwd});
385+
});
386+
374387
test('workers ensure test files load the same version of ava', function (t) {
375388
var target = path.join(__dirname, 'fixture', 'ava-paths', 'target');
376389

0 commit comments

Comments
 (0)