Skip to content

Commit e2814ca

Browse files
sindresorhuskevva
authored andcommitted
Use the import-local module (#1376)
1 parent 0faf87c commit e2814ca

File tree

6 files changed

+39
-40
lines changed

6 files changed

+39
-40
lines changed

cli.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
#!/usr/bin/env node
22
'use strict';
3-
const path = require('path');
43
const debug = require('debug')('ava');
4+
const importLocal = require('import-local');
55

6-
// Prefer the local installation of AVA.
7-
const resolveCwd = require('resolve-cwd');
8-
9-
const localCLI = resolveCwd('ava/cli');
10-
11-
// Use `path.relative()` to detect local AVA installation,
12-
// because __filename's case is inconsistent on Windows
13-
// see https://github.com/nodejs/node/issues/6624
14-
if (localCLI && path.relative(localCLI, __filename) !== '') {
6+
// Prefer the local installation of AVA
7+
if (importLocal(__filename)) {
158
debug('Using local install of AVA');
16-
require(localCLI);
179
} else {
1810
if (debug.enabled) {
1911
require('time-require'); // eslint-disable-line import/no-unassigned-import

package-lock.json

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
"has-flag": "^2.0.0",
134134
"hullabaloo-config-manager": "^1.1.0",
135135
"ignore-by-default": "^1.0.0",
136+
"import-local": "^0.1.1",
136137
"indent-string": "^3.0.0",
137138
"is-ci": "^1.0.7",
138139
"is-generator-fn": "^1.0.0",

test/cli.js

+10-29
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ const getStream = require('get-stream');
77
const figures = require('figures');
88
const makeDir = require('make-dir');
99
const touch = require('touch');
10-
const proxyquire = require('proxyquire');
11-
const sinon = require('sinon');
1210
const uniqueTempDir = require('unique-temp-dir');
1311
const execa = require('execa');
1412
const stripAnsi = require('strip-ansi');
@@ -33,7 +31,7 @@ function execCli(args, opts, cb) {
3331
let stderr;
3432

3533
const processPromise = new Promise(resolve => {
36-
child = childProcess.spawn(process.execPath, [path.relative(dirname, cliPath)].concat(args), {
34+
child = childProcess.spawn(process.execPath, [cliPath].concat(args), {
3735
cwd: dirname,
3836
env,
3937
stdio: [null, 'pipe', 'pipe']
@@ -421,33 +419,16 @@ test('should warn ava is required without the cli', t => {
421419
});
422420

423421
test('prefers local version of ava', t => {
424-
t.plan(1);
425-
426-
const stubModulePath = path.join(__dirname, '/fixture/empty');
427-
const debugSpy = sinon.spy();
428-
const resolveCwdStub = () => stubModulePath;
429-
430-
function debugStub() {
431-
return message => {
432-
let result = {
433-
enabled: false
434-
};
435-
436-
if (message) {
437-
result = debugSpy(message);
438-
}
439-
440-
return result;
441-
};
442-
}
443-
444-
proxyquire('../cli', {
445-
debug: debugStub,
446-
'resolve-cwd': resolveCwdStub
422+
execCli('', {
423+
dirname: 'fixture/local-bin',
424+
env: {
425+
DEBUG: 'ava'
426+
}
427+
}, (err, stdout, stderr) => {
428+
t.ifError(err);
429+
t.match(stderr, 'Using local install of AVA');
430+
t.end();
447431
});
448-
449-
t.ok(debugSpy.calledWith('Using local install of AVA'));
450-
t.end();
451432
});
452433

453434
test('use current working directory if `package.json` is not found', () => {

test/fixture/local-bin/node_modules/ava/cli.js

Whitespace-only changes.

test/fixture/local-bin/node_modules/ava/package.json

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)