node-modules-paths.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. var test = require('tape');
  2. var path = require('path');
  3. var parse = path.parse || require('path-parse');
  4. var keys = require('object-keys');
  5. var nodeModulesPaths = require('../lib/node-modules-paths');
  6. var verifyDirs = function verifyDirs(t, start, dirs, moduleDirectories, paths) {
  7. var moduleDirs = [].concat(moduleDirectories || 'node_modules');
  8. if (paths) {
  9. for (var k = 0; k < paths.length; ++k) {
  10. moduleDirs.push(path.basename(paths[k]));
  11. }
  12. }
  13. var foundModuleDirs = {};
  14. var uniqueDirs = {};
  15. var parsedDirs = {};
  16. for (var i = 0; i < dirs.length; ++i) {
  17. var parsed = parse(dirs[i]);
  18. if (!foundModuleDirs[parsed.base]) { foundModuleDirs[parsed.base] = 0; }
  19. foundModuleDirs[parsed.base] += 1;
  20. parsedDirs[parsed.dir] = true;
  21. uniqueDirs[dirs[i]] = true;
  22. }
  23. t.equal(keys(parsedDirs).length >= start.split(path.sep).length, true, 'there are >= dirs than "start" has');
  24. var foundModuleDirNames = keys(foundModuleDirs);
  25. t.deepEqual(foundModuleDirNames, moduleDirs, 'all desired module dirs were found');
  26. t.equal(keys(uniqueDirs).length, dirs.length, 'all dirs provided were unique');
  27. var counts = {};
  28. for (var j = 0; j < foundModuleDirNames.length; ++j) {
  29. counts[foundModuleDirs[j]] = true;
  30. }
  31. t.equal(keys(counts).length, 1, 'all found module directories had the same count');
  32. };
  33. test('node-modules-paths', function (t) {
  34. t.test('no options', function (t) {
  35. var start = path.join(__dirname, 'resolver');
  36. var dirs = nodeModulesPaths(start);
  37. verifyDirs(t, start, dirs);
  38. t.end();
  39. });
  40. t.test('empty options', function (t) {
  41. var start = path.join(__dirname, 'resolver');
  42. var dirs = nodeModulesPaths(start, {});
  43. verifyDirs(t, start, dirs);
  44. t.end();
  45. });
  46. t.test('with paths=array option', function (t) {
  47. var start = path.join(__dirname, 'resolver');
  48. var paths = ['a', 'b'];
  49. var dirs = nodeModulesPaths(start, { paths: paths });
  50. verifyDirs(t, start, dirs, null, paths);
  51. t.end();
  52. });
  53. t.test('with paths=function option', function (t) {
  54. var paths = function paths(request, absoluteStart, getNodeModulesDirs, opts) {
  55. return getNodeModulesDirs().concat(path.join(absoluteStart, 'not node modules', request));
  56. };
  57. var start = path.join(__dirname, 'resolver');
  58. var dirs = nodeModulesPaths(start, { paths: paths }, 'pkg');
  59. verifyDirs(t, start, dirs, null, [path.join(start, 'not node modules', 'pkg')]);
  60. t.end();
  61. });
  62. t.test('with paths=function skipping node modules resolution', function (t) {
  63. var paths = function paths(request, absoluteStart, getNodeModulesDirs, opts) {
  64. return [];
  65. };
  66. var start = path.join(__dirname, 'resolver');
  67. var dirs = nodeModulesPaths(start, { paths: paths });
  68. t.deepEqual(dirs, [], 'no node_modules was computed');
  69. t.end();
  70. });
  71. t.test('with moduleDirectory option', function (t) {
  72. var start = path.join(__dirname, 'resolver');
  73. var moduleDirectory = 'not node modules';
  74. var dirs = nodeModulesPaths(start, { moduleDirectory: moduleDirectory });
  75. verifyDirs(t, start, dirs, moduleDirectory);
  76. t.end();
  77. });
  78. t.test('with 1 moduleDirectory and paths options', function (t) {
  79. var start = path.join(__dirname, 'resolver');
  80. var paths = ['a', 'b'];
  81. var moduleDirectory = 'not node modules';
  82. var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectory });
  83. verifyDirs(t, start, dirs, moduleDirectory, paths);
  84. t.end();
  85. });
  86. t.test('with 1+ moduleDirectory and paths options', function (t) {
  87. var start = path.join(__dirname, 'resolver');
  88. var paths = ['a', 'b'];
  89. var moduleDirectories = ['not node modules', 'other modules'];
  90. var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
  91. verifyDirs(t, start, dirs, moduleDirectories, paths);
  92. t.end();
  93. });
  94. t.test('combine paths correctly on Windows', function (t) {
  95. var start = 'C:\\Users\\username\\myProject\\src';
  96. var paths = [];
  97. var moduleDirectories = ['node_modules', start];
  98. var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
  99. t.equal(dirs.indexOf(path.resolve(start)) > -1, true, 'should contain start dir');
  100. t.end();
  101. });
  102. t.test('combine paths correctly on non-Windows', { skip: process.platform === 'win32' }, function (t) {
  103. var start = '/Users/username/git/myProject/src';
  104. var paths = [];
  105. var moduleDirectories = ['node_modules', '/Users/username/git/myProject/src'];
  106. var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
  107. t.equal(dirs.indexOf(path.resolve(start)) > -1, true, 'should contain start dir');
  108. t.end();
  109. });
  110. });