diff --git a/api.js b/api.js index 92ccfcebd..68b121102 100644 --- a/api.js +++ b/api.js @@ -118,10 +118,13 @@ Api.prototype._setupPrecompiler = function (files) { var cacheDir = uniqueTempDir(); if (isCacheEnabled) { - cacheDir = findCacheDir({ + var foundDir = findCacheDir({ name: 'ava', files: files }); + if (foundDir !== null) { + cacheDir = foundDir; + } } this.options.cacheDir = cacheDir; diff --git a/lib/cli.js b/lib/cli.js index 31e66ac0c..a5efea564 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -22,7 +22,9 @@ Promise.longStackTraces(); exports.run = function () { var conf = pkgConf.sync('ava'); - var pkgDir = path.dirname(pkgConf.filepath(conf)); + + var filepath = pkgConf.filepath(conf); + var pkgDir = filepath === null ? process.cwd() : path.dirname(filepath); var cli = meow([ 'Usage', diff --git a/test/cli.js b/test/cli.js index 1430823b0..fc9119fb0 100644 --- a/test/cli.js +++ b/test/cli.js @@ -12,6 +12,8 @@ var mkdirp = require('mkdirp'); var touch = require('touch'); var proxyquire = require('proxyquire'); var sinon = require('sinon'); +var uniqueTempDir = require('unique-temp-dir'); +var execa = require('execa'); var cliPath = path.join(__dirname, '../cli.js'); @@ -371,6 +373,17 @@ test('prefers local version of ava', function (t) { t.end(); }); +test('use current working directory if `package.json` is not found', function () { + var cwd = uniqueTempDir({create: true}); + var testFilePath = path.join(cwd, 'test.js'); + var cliPath = require.resolve('../cli.js'); + var avaPath = require.resolve('../'); + + fs.writeFileSync(testFilePath, 'import test from ' + JSON.stringify(avaPath) + ';\ntest(t => { t.pass(); });'); + + return execa(process.execPath, [cliPath], {cwd: cwd}); +}); + test('workers ensure test files load the same version of ava', function (t) { var target = path.join(__dirname, 'fixture', 'ava-paths', 'target');