index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. 'use strict';
  2. var test = require('tape');
  3. var path = require('path');
  4. var fs = require('fs');
  5. var os = require('os');
  6. var which = require('which');
  7. var npmPath = require('../');
  8. var SEP = npmPath.SEPARATOR;
  9. var PATH = npmPath.PATH;
  10. var level0 = path.join(__dirname, 'fixture', 'level0');
  11. var level1 = path.join(level0, 'node_modules', 'level1');
  12. var level2 = path.join(level1, 'node_modules', 'level2');
  13. var level = [level0, level1, level2];
  14. var 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. var 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. var 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. var 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. var 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. var 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. var 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. var 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. var 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. var 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. var trickyL0 = level[0].replace('level0', 'level0_node_modules');
  102. var trickyL1 = level[1].replace('level0', 'level0_node_modules');
  103. var trickyL2 = level[2].replace('level0', 'level0_node_modules');
  104. t.test('no nesting', function (t) {
  105. var 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. var 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. var 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. var oldPath = process.env[PATH];
  126. npmPath.set.sync();
  127. var 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. var oldPath = process.env[PATH];
  134. npmPath();
  135. var 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. var pathWithDuplicates = '/bin:/sbin:/bin';
  144. var 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. var oldPath = process.env[PATH];
  151. var pathToNpm = path.resolve(fs.realpathSync(which.sync('npm')), '..', '..');
  152. var tmpFile = path.join(os.tmpdir(), 'npm-path-custom-npm');
  153. try {
  154. fs.unlinkSync(tmpFile);
  155. } catch (err) {
  156. err; // ignore
  157. }
  158. fs.linkSync(pathToNpm, tmpFile);
  159. var newPath = npmPath.get({
  160. npm: tmpFile
  161. });
  162. t.notEqual(newPath.indexOf(path.join(tmpFile, 'bin', 'node-gyp-bin') + SEP), -1);
  163. process.env[PATH] = oldPath;
  164. fs.unlinkSync(tmpFile);
  165. t.end();
  166. });
  167. test('error if passing bad path to npm root', { skip: true }, function (t) {
  168. var oldPath = process.env[PATH];
  169. var tmpFile = path.join(os.tmpdir(), 'npm-path-custom-npm');
  170. try {
  171. fs.unlinkSync(tmpFile);
  172. } catch (err) {
  173. err; // ignore
  174. }
  175. t.throws(function () {
  176. npmPath.get({
  177. npm: tmpFile
  178. });
  179. });
  180. process.env[PATH] = oldPath;
  181. t.end();
  182. });
  183. test('no error if no npm on existing path', function (t) {
  184. var oldPath = process.env[PATH];
  185. process.env[PATH] = '';
  186. var newPath = npmPath.get();
  187. t.equal(newPath.indexOf(path.join('bin', 'node-gyp-bin') + SEP), -1);
  188. process.env[PATH] = oldPath;
  189. t.end();
  190. });