diff --git a/cli.js b/cli.js index 1bcdf482a..80151397c 100755 --- a/cli.js +++ b/cli.js @@ -1,19 +1,11 @@ #!/usr/bin/env node 'use strict'; -const path = require('path'); const debug = require('debug')('ava'); +const importLocal = require('import-local'); -// Prefer the local installation of AVA. -const resolveCwd = require('resolve-cwd'); - -const localCLI = resolveCwd('ava/cli'); - -// Use `path.relative()` to detect local AVA installation, -// because __filename's case is inconsistent on Windows -// see https://github.com/nodejs/node/issues/6624 -if (localCLI && path.relative(localCLI, __filename) !== '') { +// Prefer the local installation of AVA +if (importLocal(__filename)) { debug('Using local install of AVA'); - require(localCLI); } else { if (debug.enabled) { require('time-require'); // eslint-disable-line import/no-unassigned-import diff --git a/package-lock.json b/package-lock.json index 511fb3321..833e6a094 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2117,6 +2117,28 @@ "resolved": "https://registry.npmjs.org/ignore-by-default/-/ignore-by-default-1.0.1.tgz", "integrity": "sha1-SMptcvbGo68Aqa1K5odr44ieKwk=" }, + "import-local": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/import-local/-/import-local-0.1.1.tgz", + "integrity": "sha1-sReVcqrNwRxqkQCftDDbyrX2aKg=", + "dependencies": { + "pkg-dir": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-2.0.0.tgz", + "integrity": "sha1-9tXREJ4Z1j7fQo4L1X4Sd3YVM0s=" + }, + "resolve-cwd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/resolve-cwd/-/resolve-cwd-2.0.0.tgz", + "integrity": "sha1-AKn3OHVW4nA46uIyyqNypqWbZlo=" + }, + "resolve-from": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-3.0.0.tgz", + "integrity": "sha1-six699nWiBvItuZTM17rywoYh0g=" + } + } + }, "imurmurhash": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", diff --git a/package.json b/package.json index be16766c4..6b6783adc 100644 --- a/package.json +++ b/package.json @@ -133,6 +133,7 @@ "has-flag": "^2.0.0", "hullabaloo-config-manager": "^1.1.0", "ignore-by-default": "^1.0.0", + "import-local": "^0.1.1", "indent-string": "^3.0.0", "is-ci": "^1.0.7", "is-generator-fn": "^1.0.0", diff --git a/test/cli.js b/test/cli.js index a2a65a33c..f762ba455 100644 --- a/test/cli.js +++ b/test/cli.js @@ -7,8 +7,6 @@ const getStream = require('get-stream'); const figures = require('figures'); const makeDir = require('make-dir'); const touch = require('touch'); -const proxyquire = require('proxyquire'); -const sinon = require('sinon'); const uniqueTempDir = require('unique-temp-dir'); const execa = require('execa'); const stripAnsi = require('strip-ansi'); @@ -33,7 +31,7 @@ function execCli(args, opts, cb) { let stderr; const processPromise = new Promise(resolve => { - child = childProcess.spawn(process.execPath, [path.relative(dirname, cliPath)].concat(args), { + child = childProcess.spawn(process.execPath, [cliPath].concat(args), { cwd: dirname, env, stdio: [null, 'pipe', 'pipe'] @@ -421,33 +419,16 @@ test('should warn ava is required without the cli', t => { }); test('prefers local version of ava', t => { - t.plan(1); - - const stubModulePath = path.join(__dirname, '/fixture/empty'); - const debugSpy = sinon.spy(); - const resolveCwdStub = () => stubModulePath; - - function debugStub() { - return message => { - let result = { - enabled: false - }; - - if (message) { - result = debugSpy(message); - } - - return result; - }; - } - - proxyquire('../cli', { - debug: debugStub, - 'resolve-cwd': resolveCwdStub + execCli('', { + dirname: 'fixture/local-bin', + env: { + DEBUG: 'ava' + } + }, (err, stdout, stderr) => { + t.ifError(err); + t.match(stderr, 'Using local install of AVA'); + t.end(); }); - - t.ok(debugSpy.calledWith('Using local install of AVA')); - t.end(); }); test('use current working directory if `package.json` is not found', () => { diff --git a/test/fixture/local-bin/node_modules/ava/cli.js b/test/fixture/local-bin/node_modules/ava/cli.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixture/local-bin/node_modules/ava/package.json b/test/fixture/local-bin/node_modules/ava/package.json new file mode 100644 index 000000000..7c83cc461 --- /dev/null +++ b/test/fixture/local-bin/node_modules/ava/package.json @@ -0,0 +1,3 @@ +{ + "name": "ava" +}