node-modules-paths.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. var foundModuleDirs = {};
  9. var uniqueDirs = {};
  10. var parsedDirs = {};
  11. for (var i = 0; i < dirs.length; ++i) {
  12. var parsed = parse(dirs[i]);
  13. if (!foundModuleDirs[parsed.base]) { foundModuleDirs[parsed.base] = 0; }
  14. foundModuleDirs[parsed.base] += 1;
  15. parsedDirs[parsed.dir] = true;
  16. uniqueDirs[dirs[i]] = true;
  17. }
  18. t.equal(keys(parsedDirs).length >= start.split(path.sep).length, true, 'there are >= dirs than "start" has');
  19. var foundModuleDirNames = keys(foundModuleDirs);
  20. t.deepEqual(foundModuleDirNames, moduleDirs.concat(paths || []), 'all desired module dirs were found');
  21. t.equal(keys(uniqueDirs).length, dirs.length, 'all dirs provided were unique');
  22. var counts = {};
  23. for (var j = 0; j < foundModuleDirNames.length; ++j) {
  24. counts[foundModuleDirs[j]] = true;
  25. }
  26. t.equal(keys(counts).length, 1, 'all found module directories had the same count');
  27. };
  28. test('node-modules-paths', function (t) {
  29. t.test('no options', function (t) {
  30. var start = path.join(__dirname, 'resolver');
  31. var dirs = nodeModulesPaths(start);
  32. verifyDirs(t, start, dirs);
  33. t.end();
  34. });
  35. t.test('empty options', function (t) {
  36. var start = path.join(__dirname, 'resolver');
  37. var dirs = nodeModulesPaths(start, {});
  38. verifyDirs(t, start, dirs);
  39. t.end();
  40. });
  41. t.test('with paths option', function (t) {
  42. var start = path.join(__dirname, 'resolver');
  43. var paths = ['a', 'b'];
  44. var dirs = nodeModulesPaths(start, { paths: paths });
  45. verifyDirs(t, start, dirs, null, paths);
  46. t.end();
  47. });
  48. t.test('with moduleDirectory option', function (t) {
  49. var start = path.join(__dirname, 'resolver');
  50. var moduleDirectory = 'not node modules';
  51. var dirs = nodeModulesPaths(start, { moduleDirectory: moduleDirectory });
  52. verifyDirs(t, start, dirs, moduleDirectory);
  53. t.end();
  54. });
  55. t.test('with 1 moduleDirectory and paths options', function (t) {
  56. var start = path.join(__dirname, 'resolver');
  57. var paths = ['a', 'b'];
  58. var moduleDirectory = 'not node modules';
  59. var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectory });
  60. verifyDirs(t, start, dirs, moduleDirectory, paths);
  61. t.end();
  62. });
  63. t.test('with 1+ moduleDirectory and paths options', function (t) {
  64. var start = path.join(__dirname, 'resolver');
  65. var paths = ['a', 'b'];
  66. var moduleDirectories = ['not node modules', 'other modules'];
  67. var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
  68. verifyDirs(t, start, dirs, moduleDirectories, paths);
  69. t.end();
  70. });
  71. });