node_path.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var fs = require('fs');
  2. var path = require('path');
  3. var test = require('tape');
  4. var resolve = require('../');
  5. test('$NODE_PATH', function (t) {
  6. t.plan(8);
  7. var isDir = function (dir, cb) {
  8. if (dir === '/node_path' || dir === 'node_path/x') {
  9. return cb(null, true);
  10. }
  11. fs.stat(dir, function (err, stat) {
  12. if (!err) {
  13. return cb(null, stat.isDirectory());
  14. }
  15. if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
  16. return cb(err);
  17. });
  18. };
  19. resolve('aaa', {
  20. paths: [
  21. path.join(__dirname, '/node_path/x'),
  22. path.join(__dirname, '/node_path/y')
  23. ],
  24. basedir: __dirname,
  25. isDirectory: isDir
  26. }, function (err, res) {
  27. t.error(err);
  28. t.equal(res, path.join(__dirname, '/node_path/x/aaa/index.js'), 'aaa resolves');
  29. });
  30. resolve('bbb', {
  31. paths: [
  32. path.join(__dirname, '/node_path/x'),
  33. path.join(__dirname, '/node_path/y')
  34. ],
  35. basedir: __dirname,
  36. isDirectory: isDir
  37. }, function (err, res) {
  38. t.error(err);
  39. t.equal(res, path.join(__dirname, '/node_path/y/bbb/index.js'), 'bbb resolves');
  40. });
  41. resolve('ccc', {
  42. paths: [
  43. path.join(__dirname, '/node_path/x'),
  44. path.join(__dirname, '/node_path/y')
  45. ],
  46. basedir: __dirname,
  47. isDirectory: isDir
  48. }, function (err, res) {
  49. t.error(err);
  50. t.equal(res, path.join(__dirname, '/node_path/x/ccc/index.js'), 'ccc resolves');
  51. });
  52. // ensure that relative paths still resolve against the regular `node_modules` correctly
  53. resolve('tap', {
  54. paths: [
  55. 'node_path'
  56. ],
  57. basedir: path.join(__dirname, 'node_path/x'),
  58. isDirectory: isDir
  59. }, function (err, res) {
  60. var root = require('tap/package.json').main; // eslint-disable-line global-require
  61. t.error(err);
  62. t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap', root), 'tap resolves');
  63. });
  64. });