index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. "use strict"
  2. var test = require('tape')
  3. var path = require('path')
  4. var join = path.join
  5. var npmWhich = require('../')
  6. var level0 = join(__dirname, 'fixtures', 'level0')
  7. var level1 = join(level0, 'node_modules', 'level1')
  8. var level2 = join(level1, 'node_modules', 'level2')
  9. var LEVEL = [level0, level1, level2]
  10. var BINPATH_FOR_LEVEL = LEVEL.map(function(levelPath) {
  11. return join(levelPath, "node_modules", ".bin")
  12. })
  13. var before = process.env.PATH
  14. test('includes all .bin dirs in all parent node_modules folders', function(t) {
  15. t.test('no nesting', function(t) {
  16. var level1Bin = npmWhich(LEVEL[0]).sync('level1')
  17. t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
  18. t.end()
  19. })
  20. t.test('nesting', function(t) {
  21. var which = npmWhich(LEVEL[1])
  22. var level1Bin = which.sync('level1')
  23. t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
  24. var level2Bin = which.sync('level2')
  25. t.equal(level2Bin, join(BINPATH_FOR_LEVEL[1], 'level2'), 'got level2 path')
  26. t.end()
  27. })
  28. t.test('more nesting', function(t) {
  29. var which = npmWhich(LEVEL[1])
  30. var level1Bin = which.sync('level1')
  31. t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
  32. var level2Bin = which.sync('level2')
  33. t.equal(level2Bin, join(BINPATH_FOR_LEVEL[1], 'level2'), 'got level2 path')
  34. t.end()
  35. })
  36. t.end()
  37. })
  38. test('which.sync requires a cwd', function(t) {
  39. t.throws(function() {
  40. npmWhich().sync('level1')
  41. })
  42. t.end()
  43. })
  44. test('which.sync requires a cwd, can be supplied in options', function(t) {
  45. var level1Bin = npmWhich().sync('level1', {cwd: LEVEL[0]})
  46. t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
  47. t.end()
  48. })
  49. test('which requires a cwd', function(t) {
  50. npmWhich()('level1', function(err) {
  51. t.ok(err)
  52. t.end()
  53. })
  54. })
  55. test('which can be curried', function(t) {
  56. var which = npmWhich(LEVEL[0])('level1')
  57. which(function(err, level1Bin) {
  58. t.ifError(err)
  59. t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
  60. t.end()
  61. })
  62. })
  63. test('which can be curried, options overridden', function(t) {
  64. var which = npmWhich(LEVEL[1])('level1')
  65. which({cwd: LEVEL[0]}, function(err, level1Bin) {
  66. t.ifError(err)
  67. t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
  68. })
  69. t.end()
  70. })
  71. test('which can be curried with sync', function(t) {
  72. var which = npmWhich(LEVEL[0])('level1')
  73. var level1Bin = which.sync()
  74. t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
  75. t.end()
  76. })
  77. test('which can be curried with sync, options overridden', function(t) {
  78. var which = npmWhich(LEVEL[1])('level1')
  79. var level1Bin = which.sync({cwd: LEVEL[0]})
  80. t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
  81. t.end()
  82. })
  83. test('which requires a cwd, can be supplied in options', function(t) {
  84. npmWhich()('level1', {cwd: LEVEL[0]}, function(err, level1Bin) {
  85. t.ifError(err)
  86. t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
  87. t.end()
  88. })
  89. })
  90. test('cwd can be overridden in options', function(t) {
  91. npmWhich(LEVEL[0])('level2', {cwd: LEVEL[1]}, function(err, level2Bin) {
  92. t.ifError(err)
  93. t.equal(level2Bin, join(BINPATH_FOR_LEVEL[1], 'level2'), 'got level2 path')
  94. t.end()
  95. })
  96. })
  97. test('which.sync does not mutate PATH', function(t) {
  98. npmWhich(__dirname).sync('level1', {env: {PATH: BINPATH_FOR_LEVEL[0]}})
  99. var after = process.env.PATH
  100. t.deepEqual(after, before, 'PATH unmodified')
  101. t.end()
  102. })
  103. test('which.sync does not mutate PATH after failed find', function(t) {
  104. t.throws(function() {
  105. npmWhich(__dirname).sync('asdasd', {env: {PATH: BINPATH_FOR_LEVEL[0]}})
  106. })
  107. var after = process.env.PATH
  108. t.deepEqual(after, before, 'PATH unmodified')
  109. t.end()
  110. })
  111. test('which does not mutate PATH', function(t) {
  112. var level1Bin = npmWhich(__dirname)('level1', {env: {PATH: BINPATH_FOR_LEVEL[0]}}, function(err) {
  113. t.ifError(err)
  114. var after = process.env.PATH
  115. t.deepEqual(after, before, 'PATH unmodified')
  116. t.end()
  117. })
  118. })
  119. test('which does not mutate PATH after failed find', function(t) {
  120. npmWhich('asdasdb/jhbhj')('asdasd', function(err) {
  121. t.ok(err)
  122. var after = process.env.PATH
  123. t.deepEqual(after, before, 'PATH unmodified')
  124. t.end()
  125. })
  126. })
  127. test('can find path with bad cwd', function(t) {
  128. npmWhich('/asdasdb/jhbhj')('node', function(err, path) {
  129. t.ifError(err)
  130. t.ok(path)
  131. t.equal(path.split('/').pop(), 'node')
  132. var after = process.env.PATH
  133. t.deepEqual(after, before, 'PATH unmodified')
  134. t.end()
  135. })
  136. })
  137. test('which does not mutate PATH with bad cmd & cwd', function(t) {
  138. npmWhich('asdasdb/jhbhj')('asdasd', function(err) {
  139. t.ok(err)
  140. var after = process.env.PATH
  141. t.deepEqual(after, before, 'PATH unmodified')
  142. t.end()
  143. })
  144. })
  145. test('which.sync on default export will error without cwd', function(t) {
  146. t.throws(function() {
  147. npmWhich.sync('level1')
  148. })
  149. t.end()
  150. })
  151. if (process.version.indexOf('v0.10') !== -1) {
  152. // can't test this on 0.11 as process.platform is (rightfully) read-only
  153. test('which does not mutate PATH with bad cwd/cmd on "windows"', function(t) {
  154. var actualPlatform = process.platform
  155. process.platform = "win32"
  156. npmWhich('asdasdb/jhbhj')('asdasd', function(err) {
  157. process.platform = actualPlatform
  158. t.ok(err)
  159. var after = process.env.PATH
  160. t.deepEqual(after, before, 'PATH unmodified')
  161. t.end()
  162. })
  163. })
  164. }
  165. // Ensure old 1.0.0 tests function, except for the breakages.
  166. require('./1.0.0-interface')