1 | "use strict"
|
2 |
|
3 | var test = require('tape')
|
4 |
|
5 | var path = require('path')
|
6 | var fs = require('fs')
|
7 | var os = require('os')
|
8 |
|
9 | var which = require('which')
|
10 |
|
11 | var npmPath = require('../')
|
12 |
|
13 | var SEP = npmPath.SEPARATOR
|
14 | var PATH = npmPath.PATH
|
15 |
|
16 | var level0 = path.join(__dirname, 'fixture', 'level0')
|
17 | var level1 = path.join(level0, 'node_modules', 'level1')
|
18 | var level2 = path.join(level1, 'node_modules', 'level2')
|
19 |
|
20 | var level = [level0, level1, level2]
|
21 | var binPath = level.map(function(levelPath) {
|
22 | return path.join(levelPath, "node_modules", ".bin")
|
23 | })
|
24 |
|
25 | test('exports separator', function(t) {
|
26 | t.ok(npmPath.SEPARATOR)
|
27 | t.end()
|
28 | })
|
29 |
|
30 | test('exports $PATH key', function(t) {
|
31 | t.ok(npmPath.PATH)
|
32 | t.end()
|
33 | })
|
34 |
|
35 | test('includes current node executable dir', function(t) {
|
36 | var level0Path = npmPath.getSync({cwd: level0})
|
37 | t.ok(level0Path.indexOf(path.dirname(process.execPath) + SEP) != -1)
|
38 | t.end()
|
39 | })
|
40 |
|
41 | test('async version works', function(t) {
|
42 | var isAsync = false
|
43 | npmPath.get({cwd: level0}, function(err, level0Path) {
|
44 | t.ifError(err)
|
45 | t.ok(isAsync)
|
46 | t.ok(level0Path.indexOf(path.dirname(process.execPath) + SEP) != -1)
|
47 | t.end()
|
48 | })
|
49 | isAsync = true
|
50 | })
|
51 |
|
52 | test('no fn == sync', function(t) {
|
53 | var level0Path = npmPath.get({cwd: level0})
|
54 | t.ok(level0Path.indexOf(path.dirname(process.execPath) + SEP) != -1)
|
55 | t.end()
|
56 | })
|
57 |
|
58 | test('sync options is optional', function(t) {
|
59 | var newPath = npmPath.get()
|
60 | t.ok(newPath.indexOf(path.dirname(process.execPath) + SEP) != -1)
|
61 | t.end()
|
62 | })
|
63 |
|
64 | test('async options is optional', function(t) {
|
65 | var isAsync = false
|
66 | npmPath.get(function(err, newPath) {
|
67 | t.ifError(err)
|
68 | t.ok(newPath.indexOf(path.dirname(process.execPath) + SEP) != -1)
|
69 | t.ok(isAsync)
|
70 | t.end()
|
71 | })
|
72 | isAsync = true
|
73 | })
|
74 |
|
75 | test('includes bin from sibling dirs', function(t) {
|
76 | t.test('from existing sibling directory', function(t) {
|
77 | var level1Path = npmPath.getSync({cwd: path.join(level[0], 'test')})
|
78 | t.ok(level1Path.indexOf(binPath[0] + SEP) != -1, 'should include level 0 .bin')
|
79 | t.ok(level1Path.indexOf(binPath[2] + SEP) === -1, 'should not include child paths')
|
80 | t.end()
|
81 | })
|
82 |
|
83 | t.test('from existing sibling directory async', function(t) {
|
84 | npmPath({cwd: path.join(level[0], 'test')}, function(err, level1Path) {
|
85 | t.ifError(err)
|
86 | t.ok(level1Path.indexOf(binPath[0] + SEP) != -1, 'should include level 0 .bin')
|
87 | t.ok(level1Path.indexOf(binPath[2] + SEP) === -1, 'should not include child paths')
|
88 | t.end()
|
89 | })
|
90 | })
|
91 | })
|
92 |
|
93 | test('includes all .bin dirs in all parent node_modules folders', function(t) {
|
94 | t.test('no nesting', function(t) {
|
95 | var level0Path = npmPath.getSync({cwd: level[0]})
|
96 | t.ok(level0Path.indexOf(binPath[0] + SEP) != -1, 'should include level 0 .bin')
|
97 | t.ok(level0Path.indexOf(binPath[1] + SEP) === -1, 'should not include child paths')
|
98 | t.ok(level0Path.indexOf(binPath[2] + SEP) === -1, 'should not include child paths')
|
99 | t.end()
|
100 | })
|
101 |
|
102 | t.test('1 level of nesting', function(t) {
|
103 | var level1Path = npmPath.getSync({cwd: level[1]})
|
104 | t.ok(level1Path.indexOf(binPath[0] + SEP) != -1, 'should include level 0 .bin')
|
105 | t.ok(level1Path.indexOf(binPath[1] + SEP) != -1, 'should include level 1 .bin')
|
106 | t.ok(level1Path.indexOf(binPath[2] + SEP) === -1, 'should not include child paths')
|
107 | t.end()
|
108 | })
|
109 |
|
110 | t.test('2 levels of nesting', function(t) {
|
111 | var level1Path = npmPath.getSync({cwd: level[2]})
|
112 | t.ok(level1Path.indexOf(binPath[0] + SEP) != -1, 'should include level 0 .bin')
|
113 | t.ok(level1Path.indexOf(binPath[1] + SEP) != -1, 'should include level 1 .bin')
|
114 | t.ok(level1Path.indexOf(binPath[2] + SEP) != -1, 'should include level 2 .bin')
|
115 | t.end()
|
116 | })
|
117 |
|
118 | t.end()
|
119 | })
|
120 |
|
121 | test('handles directories with node_modules in the name', function(t) {
|
122 | var trickyL0 = level[0].replace('level0', 'level0_node_modules')
|
123 | var trickyL1 = level[1].replace('level0', 'level0_node_modules')
|
124 | var trickyL2 = level[2].replace('level0', 'level0_node_modules')
|
125 |
|
126 | t.test('no nesting', function(t) {
|
127 | var level0Path = npmPath.getSync({cwd: trickyL0})
|
128 | t.ok(level0Path.indexOf(path.join(trickyL0, 'node_modules', '.bin') + SEP) != -1, 'should include level 0 .bin')
|
129 | t.end()
|
130 | })
|
131 |
|
132 | t.test('1 level of nesting', function(t) {
|
133 | var level1Path = npmPath.getSync({cwd: trickyL1})
|
134 |
|
135 | t.ok(level1Path.indexOf(path.join(trickyL0, 'node_modules', '.bin') + SEP) != -1, 'should include level 0 .bin')
|
136 | t.ok(level1Path.indexOf(path.join(trickyL1, 'node_modules', '.bin') + SEP) != -1, 'should include level 1 .bin')
|
137 | t.end()
|
138 | })
|
139 |
|
140 | t.test('2 levels of nesting', function(t) {
|
141 | var level2Path = npmPath.getSync({cwd: trickyL2})
|
142 |
|
143 | t.ok(level2Path.indexOf(path.join(trickyL0, 'node_modules', '.bin') + SEP) != -1, 'should include level 0 .bin')
|
144 | t.ok(level2Path.indexOf(path.join(trickyL1, 'node_modules', '.bin') + SEP) != -1, 'should include level 1 .bin')
|
145 | t.ok(level2Path.indexOf(path.join(trickyL2, 'node_modules', '.bin') + SEP) != -1, 'should include level 1 .bin')
|
146 | t.end()
|
147 | })
|
148 |
|
149 | t.end()
|
150 | })
|
151 |
|
152 | test('can set path', function(t) {
|
153 | var oldPath = process.env[PATH]
|
154 | npmPath.set.sync()
|
155 | var newPath = process.env[PATH]
|
156 | t.notDeepEqual(oldPath, newPath)
|
157 | process.env[PATH] = oldPath
|
158 | t.end()
|
159 | })
|
160 |
|
161 | test('includes node-gyp bundled with current npm', function(t) {
|
162 | var oldPath = process.env[PATH]
|
163 | var oldGypPath = which.sync('node-gyp')
|
164 | npmPath()
|
165 | var newGypPath = which.sync('node-gyp')
|
166 | t.ok(newGypPath)
|
167 | t.ok(fs.existsSync(newGypPath))
|
168 | t.ok(newGypPath.indexOf(path.join('npm', 'bin', 'node-gyp-bin') + SEP !== -1))
|
169 | process.env[PATH] = oldPath
|
170 | t.end()
|
171 | })
|
172 |
|
173 | test('can set path to npm root to use for node-gyp lookup', function(t) {
|
174 | var oldPath = process.env[PATH]
|
175 | var pathToNpm = path.resolve(
|
176 | fs.realpathSync(which.sync('npm')),
|
177 | '..',
|
178 | '..'
|
179 | )
|
180 |
|
181 | var tmpFile = path.join(os.tmpdir(), 'npm-path-custom-npm')
|
182 | try {fs.unlinkSync(tmpFile)}catch(e){}
|
183 | fs.linkSync(pathToNpm, tmpFile)
|
184 | var newPath = npmPath.get({
|
185 | npm: tmpFile
|
186 | })
|
187 | t.ok(newPath.indexOf(
|
188 | path.join(tmpFile, 'bin', 'node-gyp-bin') + SEP
|
189 | ) !== -1)
|
190 | process.env[PATH] = oldPath
|
191 | fs.unlinkSync(tmpFile)
|
192 | t.end()
|
193 | })
|
194 |
|