index.js 7.2 KB

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