1 | "use strict"
|
2 |
|
3 | var test = require('tape')
|
4 |
|
5 | var path = require('path')
|
6 | var join = path.join
|
7 |
|
8 | var npmWhich = require('../')
|
9 |
|
10 | var level0 = join(__dirname, 'fixtures', 'level0')
|
11 | var level1 = join(level0, 'node_modules', 'level1')
|
12 | var level2 = join(level1, 'node_modules', 'level2')
|
13 |
|
14 | var LEVEL = [level0, level1, level2]
|
15 |
|
16 | var BINPATH_FOR_LEVEL = LEVEL.map(function(levelPath) {
|
17 | return join(levelPath, "node_modules", ".bin")
|
18 | })
|
19 |
|
20 | var before = process.env.PATH
|
21 |
|
22 | test('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 |
|
50 | test('which.sync requires a cwd', function(t) {
|
51 | t.throws(function() {
|
52 | npmWhich().sync('level1')
|
53 | })
|
54 | t.end()
|
55 | })
|
56 |
|
57 | test('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 |
|
63 | test('which requires a cwd', function(t) {
|
64 | npmWhich()('level1', function(err) {
|
65 | t.ok(err)
|
66 | t.end()
|
67 | })
|
68 | })
|
69 |
|
70 | test('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 |
|
79 | test('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 |
|
88 | test('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 |
|
95 | test('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 |
|
102 | test('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 |
|
110 | test('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 |
|
118 | test('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 |
|
125 | test('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 |
|
134 | test('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 |
|
143 | test('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 |
|
152 | test('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 |
|
163 | test('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 |
|
172 | test('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 |
|
179 | if (process.version.indexOf('v0.10') !== -1) {
|
180 |
|
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 |
|
197 | require('./1.0.0-interface')
|