-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.js
94 lines (80 loc) · 4.09 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import process from 'node:process';
import path from 'node:path';
import {fileURLToPath, pathToFileURL} from 'node:url';
import test from 'ava';
import {npmRunPath, npmRunPathEnv} from './index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const testLocalDirectory = (t, addExecPath, preferLocal, expectedResult) => {
t.is(
npmRunPath({path: '', addExecPath, preferLocal}).split(path.delimiter)[0] === path.join(__dirname, 'node_modules/.bin'),
expectedResult,
);
};
test('Adds node_modules/.bin - npmRunPath()', testLocalDirectory, undefined, undefined, true);
test('"addExecPath: false" still adds node_modules/.bin - npmRunPath()', testLocalDirectory, false, undefined, true);
test('"preferLocal: false" does not add node_modules/.bin - npmRunPath()', testLocalDirectory, undefined, false, false);
test('"preferLocal: false", "addExecPath: false" does not add node_modules/.bin - npmRunPath()', testLocalDirectory, false, false, false);
const testLocalDirectoryEnv = (t, addExecPath, preferLocal, expectedResult) => {
t.is(
npmRunPathEnv({env: {PATH: 'foo'}, addExecPath, preferLocal}).PATH.split(path.delimiter)[0] === path.join(__dirname, 'node_modules/.bin'),
expectedResult,
);
};
test('Adds node_modules/.bin - npmRunPathEnv()', testLocalDirectoryEnv, undefined, undefined, true);
test('"addExecPath: false" still adds node_modules/.bin - npmRunPathEnv()', testLocalDirectoryEnv, false, undefined, true);
test('"preferLocal: false" does not add node_modules/.bin - npmRunPathEnv()', testLocalDirectoryEnv, undefined, false, false);
test('"preferLocal: false", "addExecPath: false" does not add node_modules/.bin - npmRunPathEnv()', testLocalDirectoryEnv, false, false, false);
test('the `cwd` option changes the current directory', t => {
t.is(
npmRunPath({path: '', cwd: '/dir'}).split(path.delimiter)[0],
path.normalize('/dir/node_modules/.bin'),
);
});
test('the `cwd` option can be a file URL', t => {
t.is(
npmRunPath({path: '', cwd: new URL('file:///dir')}).split(path.delimiter)[0],
path.normalize('/dir/node_modules/.bin'),
);
});
test('push `execPath` later in the PATH', t => {
const pathEnv = npmRunPath({path: ''}).split(path.delimiter);
t.is(pathEnv.at(-2), path.dirname(process.execPath));
});
const testExecPath = (t, preferLocal, addExecPath, expectedResult) => {
const pathEnv = npmRunPath({
path: '',
execPath: 'test/test',
preferLocal,
addExecPath,
}).split(path.delimiter);
t.is(pathEnv.at(-2) === path.resolve('test'), expectedResult);
};
test('can change `execPath` with the `execPath` option - npmRunPath()', testExecPath, undefined, undefined, true);
test('"preferLocal: false" still adds execPath - npmRunPath()', testExecPath, false, undefined, true);
test('"addExecPath: false" does not add execPath - npmRunPath()', testExecPath, undefined, false, false);
test('"addExecPath: false", "preferLocal: false" does not add execPath - npmRunPath()', testExecPath, false, false, false);
const testExecPathEnv = (t, preferLocal, addExecPath, expectedResult) => {
const pathEnv = npmRunPathEnv({
env: {PATH: 'foo'},
execPath: 'test/test',
preferLocal,
addExecPath,
}).PATH.split(path.delimiter);
t.is(pathEnv.at(-2) === path.resolve('test'), expectedResult);
};
test('can change `execPath` with the `execPath` option - npmRunPathEnv()', testExecPathEnv, undefined, undefined, true);
test('"preferLocal: false" still adds execPath - npmRunPathEnv()', testExecPathEnv, false, undefined, true);
test('"addExecPath: false" does not add execPath - npmRunPathEnv()', testExecPathEnv, undefined, false, false);
test('"addExecPath: false", "preferLocal: false" does not add execPath - npmRunPathEnv()', testExecPathEnv, false, false, false);
test('the `execPath` option can be a file URL', t => {
const pathEnv = npmRunPath({path: '', execPath: pathToFileURL('test/test')}).split(path.delimiter);
t.is(pathEnv.at(-2), path.resolve('test'));
});
test('the `execPath` option is relative to the `cwd` option', t => {
const pathEnv = npmRunPath({
path: '',
execPath: 'test/test',
cwd: '/dir',
}).split(path.delimiter);
t.is(pathEnv.at(-2), path.normalize('/dir/test'));
});