Skip to content

Commit 0675d34

Browse files
ingrosindresorhus
authored andcommitted
Close #531 PR: Support NODE_PATH in forked process. Fixes #515
1 parent e50f3de commit 0675d34

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

lib/fork.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ var debug = require('debug')('ava');
77
var AvaError = require('./ava-error');
88
var send = require('./send');
99

10+
var env = process.env;
11+
12+
if (process.env.NODE_PATH) {
13+
var nodePath = process.env.NODE_PATH
14+
.split(path.delimiter)
15+
.map(function (p) {
16+
return path.resolve(p);
17+
})
18+
.join(path.delimiter);
19+
20+
env = objectAssign({
21+
NODE_PATH: nodePath
22+
}, env);
23+
}
24+
1025
module.exports = function (file, opts) {
1126
opts = objectAssign({
1227
file: file,
@@ -18,7 +33,8 @@ module.exports = function (file, opts) {
1833

1934
var ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), [JSON.stringify(opts)], {
2035
cwd: path.dirname(file),
21-
silent: true
36+
silent: true,
37+
env: env
2238
});
2339

2440
var relFile = path.relative('.', file);

test/cli.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ var arrify = require('arrify');
88
var touch = require('touch');
99
var cliPath = path.join(__dirname, '../cli.js');
1010

11-
function execCli(args, dirname, cb) {
12-
if (typeof dirname === 'function') {
13-
cb = dirname;
11+
function execCli(args, opts, cb) {
12+
var dirname;
13+
var env;
14+
15+
if (typeof opts === 'function') {
16+
cb = opts;
1417
dirname = __dirname;
18+
env = {};
1519
} else {
16-
dirname = path.join(__dirname, dirname);
20+
dirname = path.join(__dirname, opts.dirname ? opts.dirname : '');
21+
env = opts.env || {};
1722
}
1823

19-
var env = {};
20-
2124
if (process.env.AVA_APPVEYOR) {
2225
env.AVA_APPVEYOR = 1;
2326
}
@@ -95,21 +98,21 @@ test('log failed tests', function (t) {
9598
});
9699

97100
test('pkg-conf: defaults', function (t) {
98-
execCli([], 'fixture/pkg-conf/defaults', function (err) {
101+
execCli([], {dirname: 'fixture/pkg-conf/defaults'}, function (err) {
99102
t.ifError(err);
100103
t.end();
101104
});
102105
});
103106

104107
test('pkg-conf: pkg-overrides', function (t) {
105-
execCli([], 'fixture/pkg-conf/pkg-overrides', function (err) {
108+
execCli([], {dirname: 'fixture/pkg-conf/pkg-overrides'}, function (err) {
106109
t.ifError(err);
107110
t.end();
108111
});
109112
});
110113

111114
test('pkg-conf: cli takes precedence', function (t) {
112-
execCli(['--no-serial', '--cache', '--no-fail-fast', '--require=./required.js', 'c.js'], 'fixture/pkg-conf/precedence', function (err) {
115+
execCli(['--no-serial', '--cache', '--no-fail-fast', '--require=./required.js', 'c.js'], {dirname: 'fixture/pkg-conf/precedence'}, function (err) {
113116
t.ifError(err);
114117
t.end();
115118
});
@@ -124,7 +127,7 @@ test('watcher works', function (t) {
124127
hasChokidar = true;
125128
} catch (err) {}
126129

127-
var child = execCli(['--verbose', '--watch', 'test.js'], 'fixture/watcher', function (err, stdout) {
130+
var child = execCli(['--verbose', '--watch', 'test.js'], {dirname: 'fixture/watcher'}, function (err, stdout) {
128131
if (err && err.code === 1 && !hasChokidar) {
129132
t.comment('chokidar dependency is missing, cannot test watcher');
130133
t.match(stdout, 'The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.');
@@ -155,3 +158,11 @@ test('watcher works', function (t) {
155158
}
156159
});
157160
});
161+
162+
test('handles NODE_PATH', function (t) {
163+
var nodePaths = 'node-paths/modules' + path.delimiter + 'node-paths/deep/nested';
164+
execCli('fixture/node-paths.js', {env: {NODE_PATH: nodePaths}}, function (err) {
165+
t.notOk(err);
166+
t.end();
167+
});
168+
});

test/fixture/node-paths.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import test from '../../';
2+
3+
import foo from 'nested/foo';
4+
import bar from 'path/bar';
5+
6+
test('relative require', t => {
7+
t.is(foo(), 'bar');
8+
t.is(bar(), 'baz');
9+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = function () {
2+
return 'baz';
3+
};
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = function () {
2+
return 'bar';
3+
};
4+

0 commit comments

Comments
 (0)