cb446d894bd9a3621555f06854f8d000c7df41f7
[platform/framework/web/crosswalk-tizen.git] /
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, 'fixtures', '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 // can only be set if above callback not synchronous
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 // can only be set if above callback not synchronous
73 })
74
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')
81     t.end()
82   })
83
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')
89     t.end()
90   })
91
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')
97     t.end()
98   })
99
100   t.end()
101 })
102
103
104 test('can set path', function(t) {
105   var oldPath = process.env[PATH]
106   npmPath.set.sync()
107   var newPath = process.env[PATH]
108   t.notDeepEqual(oldPath, newPath)
109   process.env[PATH] = oldPath
110   t.end()
111 })
112
113 test('includes node-gyp bundled with current npm', function(t) {
114   var oldPath = process.env[PATH]
115   var oldGypPath = which.sync('node-gyp')
116   npmPath()
117   var newGypPath = which.sync('node-gyp')
118
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
123   t.end()
124 })
125
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')),
130     '..',
131     '..'
132   )
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({
137     npm: tmpFile
138   })
139   t.ok(newPath.indexOf(
140     path.join(tmpFile, 'bin', 'node-gyp-bin') + SEP
141   ) !== -1)
142   process.env[PATH] = oldPath
143   fs.unlinkSync(tmpFile)
144   t.end()
145 })