UNPKG

5.64 kBJavaScriptView Raw
1"use strict"
2
3var test = require('tape')
4
5var path = require('path')
6var join = path.join
7
8var npmWhich = require('../')
9
10var level0 = join(__dirname, 'fixtures', 'level0')
11var level1 = join(level0, 'node_modules', 'level1')
12var level2 = join(level1, 'node_modules', 'level2')
13
14var LEVEL = [level0, level1, level2]
15
16var BINPATH_FOR_LEVEL = LEVEL.map(function(levelPath) {
17 return join(levelPath, "node_modules", ".bin")
18})
19
20var before = process.env.PATH
21
22test('includes all .bin dirs in all parent node_modules folders', function(t) {
23 t.test('no nesting', function(t) {
24 var level1Bin = npmWhich(LEVEL[0]).sync('level1')
25 t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
26 t.end()
27 })
28
29 t.test('nesting', function(t) {
30 var which = npmWhich(LEVEL[1])
31 var level1Bin = which.sync('level1')
32 t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
33 var level2Bin = which.sync('level2')
34 t.equal(level2Bin, join(BINPATH_FOR_LEVEL[1], 'level2'), 'got level2 path')
35 t.end()
36 })
37
38 t.test('more nesting', function(t) {
39 var which = npmWhich(LEVEL[1])
40 var level1Bin = which.sync('level1')
41 t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
42 var level2Bin = which.sync('level2')
43 t.equal(level2Bin, join(BINPATH_FOR_LEVEL[1], 'level2'), 'got level2 path')
44 t.end()
45 })
46
47 t.end()
48})
49
50test('which.sync requires a cwd', function(t) {
51 t.throws(function() {
52 npmWhich().sync('level1')
53 })
54 t.end()
55})
56
57test('which.sync requires a cwd, can be supplied in options', function(t) {
58 var level1Bin = npmWhich().sync('level1', {cwd: LEVEL[0]})
59 t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
60 t.end()
61})
62
63test('which requires a cwd', function(t) {
64 npmWhich()('level1', function(err) {
65 t.ok(err)
66 t.end()
67 })
68})
69
70test('which can be curried', function(t) {
71 var which = npmWhich(LEVEL[0])('level1')
72 which(function(err, level1Bin) {
73 t.ifError(err)
74 t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
75 t.end()
76 })
77})
78
79test('which can be curried, options overridden', function(t) {
80 var which = npmWhich(LEVEL[1])('level1')
81 which({cwd: LEVEL[0]}, function(err, level1Bin) {
82 t.ifError(err)
83 t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
84 })
85 t.end()
86})
87
88test('which can be curried with sync', function(t) {
89 var which = npmWhich(LEVEL[0])('level1')
90 var level1Bin = which.sync()
91 t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
92 t.end()
93})
94
95test('which can be curried with sync, options overridden', function(t) {
96 var which = npmWhich(LEVEL[1])('level1')
97 var level1Bin = which.sync({cwd: LEVEL[0]})
98 t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
99 t.end()
100})
101
102test('which requires a cwd, can be supplied in options', function(t) {
103 npmWhich()('level1', {cwd: LEVEL[0]}, function(err, level1Bin) {
104 t.ifError(err)
105 t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
106 t.end()
107 })
108})
109
110test('cwd can be overridden in options', function(t) {
111 npmWhich(LEVEL[0])('level2', {cwd: LEVEL[1]}, function(err, level2Bin) {
112 t.ifError(err)
113 t.equal(level2Bin, join(BINPATH_FOR_LEVEL[1], 'level2'), 'got level2 path')
114 t.end()
115 })
116})
117
118test('which.sync does not mutate PATH', function(t) {
119 npmWhich(__dirname).sync('level1', {env: {PATH: BINPATH_FOR_LEVEL[0]}})
120 var after = process.env.PATH
121 t.deepEqual(after, before, 'PATH unmodified')
122 t.end()
123})
124
125test('which.sync does not mutate PATH after failed find', function(t) {
126 t.throws(function() {
127 npmWhich(__dirname).sync('asdasd', {env: {PATH: BINPATH_FOR_LEVEL[0]}})
128 })
129 var after = process.env.PATH
130 t.deepEqual(after, before, 'PATH unmodified')
131 t.end()
132})
133
134test('which does not mutate PATH', function(t) {
135 var level1Bin = npmWhich(__dirname)('level1', {env: {PATH: BINPATH_FOR_LEVEL[0]}}, function(err) {
136 t.ifError(err)
137 var after = process.env.PATH
138 t.deepEqual(after, before, 'PATH unmodified')
139 t.end()
140 })
141})
142
143test('which does not mutate PATH after failed find', function(t) {
144 npmWhich('asdasdb/jhbhj')('asdasd', function(err) {
145 t.ok(err)
146 var after = process.env.PATH
147 t.deepEqual(after, before, 'PATH unmodified')
148 t.end()
149 })
150})
151
152test('can find path with bad cwd', function(t) {
153 npmWhich('/asdasdb/jhbhj')('node', function(err, path) {
154 t.ifError(err)
155 t.ok(path)
156 t.equal(path.split('/').pop(), 'node')
157 var after = process.env.PATH
158 t.deepEqual(after, before, 'PATH unmodified')
159 t.end()
160 })
161})
162
163test('which does not mutate PATH with bad cmd & cwd', function(t) {
164 npmWhich('asdasdb/jhbhj')('asdasd', function(err) {
165 t.ok(err)
166 var after = process.env.PATH
167 t.deepEqual(after, before, 'PATH unmodified')
168 t.end()
169 })
170})
171
172test('which.sync on default export will error without cwd', function(t) {
173 t.throws(function() {
174 npmWhich.sync('level1')
175 })
176 t.end()
177})
178
179if (process.version.indexOf('v0.10') !== -1) {
180 // can't test this on 0.11 as process.platform is (rightfully) read-only
181 test('which does not mutate PATH with bad cwd/cmd on "windows"', function(t) {
182 var actualPlatform = process.platform
183 process.platform = "win32"
184 npmWhich('asdasdb/jhbhj')('asdasd', function(err) {
185 process.platform = actualPlatform
186 t.ok(err)
187 var after = process.env.PATH
188 t.deepEqual(after, before, 'PATH unmodified')
189 t.end()
190 })
191 })
192}
193
194
195
196// Ensure old 1.0.0 tests function, except for the breakages.
197require('./1.0.0-interface')