3 var test = require('tape')
5 var path = require('path')
9 var which = require('which')
11 var npmPath = require('../')
13 var SEP = npmPath.SEPARATOR
14 var PATH = npmPath.PATH
16 var level0 = path.join(__dirname, 'fixtures', 'level0')
17 var level1 = path.join(level0, 'node_modules', 'level1')
18 var level2 = path.join(level1, 'node_modules', 'level2')
20 var level = [level0, level1, level2]
21 var binPath = level.map(function(levelPath) {
22 return path.join(levelPath, "node_modules", ".bin")
25 test('exports separator', function(t) {
26 t.ok(npmPath.SEPARATOR)
30 test('exports $PATH key', function(t) {
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)
41 test('async version works', function(t) {
43 npmPath.get({cwd: level0}, function(err, level0Path) {
46 t.ok(level0Path.indexOf(path.dirname(process.execPath) + SEP) != -1)
49 isAsync = true // can only be set if above callback not synchronous
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)
58 test('sync options is optional', function(t) {
59 var newPath = npmPath.get()
60 t.ok(newPath.indexOf(path.dirname(process.execPath) + SEP) != -1)
64 test('async options is optional', function(t) {
66 npmPath.get(function(err, newPath) {
68 t.ok(newPath.indexOf(path.dirname(process.execPath) + SEP) != -1)
72 isAsync = true // can only be set if above callback not synchronous
75 test('includes all .bin dirs in all parent node_modules folders', function(t) {
76 t.test('no nesting', function(t) {
77 var level0Path = npmPath.getSync({cwd: level[0]})
78 t.ok(level0Path.indexOf(binPath[0] + SEP) != -1, 'should include level 0 .bin')
79 t.ok(level0Path.indexOf(binPath[1] + SEP) === -1, 'should not include child paths')
80 t.ok(level0Path.indexOf(binPath[2] + SEP) === -1, 'should not include child paths')
84 t.test('1 level of nesting', function(t) {
85 var level1Path = npmPath.getSync({cwd: level[1]})
86 t.ok(level1Path.indexOf(binPath[0] + SEP) != -1, 'should include level 0 .bin')
87 t.ok(level1Path.indexOf(binPath[1] + SEP) != -1, 'should include level 1 .bin')
88 t.ok(level1Path.indexOf(binPath[2] + SEP) === -1, 'should not include child paths')
92 t.test('2 levels of nesting', function(t) {
93 var level1Path = npmPath.getSync({cwd: level[2]})
94 t.ok(level1Path.indexOf(binPath[0] + SEP) != -1, 'should include level 0 .bin')
95 t.ok(level1Path.indexOf(binPath[1] + SEP) != -1, 'should include level 1 .bin')
96 t.ok(level1Path.indexOf(binPath[2] + SEP) != -1, 'should include level 2 .bin')
104 test('can set path', function(t) {
105 var oldPath = process.env[PATH]
107 var newPath = process.env[PATH]
108 t.notDeepEqual(oldPath, newPath)
109 process.env[PATH] = oldPath
113 test('includes node-gyp bundled with current npm', function(t) {
114 var oldPath = process.env[PATH]
115 var oldGypPath = which.sync('node-gyp')
117 var newGypPath = which.sync('node-gyp')
119 t.notEqual(newGypPath, oldGypPath)
120 t.ok(fs.existsSync(newGypPath))
121 t.ok(newGypPath.indexOf(path.join('npm', 'bin', 'node-gyp-bin') + SEP !== -1))
122 process.env[PATH] = oldPath
126 test('can set path to npm root to use for node-gyp lookup', function(t) {
127 var oldPath = process.env[PATH]
128 var pathToNpm = path.resolve(
129 fs.realpathSync(which.sync('npm')),
133 var tmpFile = path.join(os.tmpdir(), 'npm-path-custom-npm')
134 try {fs.unlinkSync(tmpFile)}catch(e){}
135 fs.linkSync(pathToNpm, tmpFile)
136 var newPath = npmPath.get({
139 t.ok(newPath.indexOf(
140 path.join(tmpFile, 'bin', 'node-gyp-bin') + SEP
142 process.env[PATH] = oldPath
143 fs.unlinkSync(tmpFile)