home_paths.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. var fs = require('fs');
  3. var homedir = require('../lib/homedir');
  4. var path = require('path');
  5. var test = require('tape');
  6. var mkdirp = require('mkdirp');
  7. var rimraf = require('rimraf');
  8. var mv = require('mv');
  9. var copyDir = require('copy-dir');
  10. var tmp = require('tmp');
  11. var HOME = homedir();
  12. var hnm = path.join(HOME, '.node_modules');
  13. var hnl = path.join(HOME, '.node_libraries');
  14. var resolve = require('../async');
  15. function makeDir(t, dir, cb) {
  16. mkdirp(dir, function (err) {
  17. if (err) {
  18. cb(err);
  19. } else {
  20. t.teardown(function cleanup() {
  21. rimraf.sync(dir);
  22. });
  23. cb();
  24. }
  25. });
  26. }
  27. function makeTempDir(t, dir, cb) {
  28. if (fs.existsSync(dir)) {
  29. var tmpResult = tmp.dirSync();
  30. t.teardown(tmpResult.removeCallback);
  31. var backup = path.join(tmpResult.name, path.basename(dir));
  32. mv(dir, backup, function (err) {
  33. if (err) {
  34. cb(err);
  35. } else {
  36. t.teardown(function () {
  37. mv(backup, dir, cb);
  38. });
  39. makeDir(t, dir, cb);
  40. }
  41. });
  42. } else {
  43. makeDir(t, dir, cb);
  44. }
  45. }
  46. test('homedir module paths', function (t) {
  47. t.plan(7);
  48. makeTempDir(t, hnm, function (err) {
  49. t.error(err, 'no error with HNM temp dir');
  50. if (err) {
  51. return t.end();
  52. }
  53. var bazHNMDir = path.join(hnm, 'baz');
  54. var dotMainDir = path.join(hnm, 'dot_main');
  55. copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNMDir);
  56. copyDir.sync(path.join(__dirname, 'resolver/dot_main'), dotMainDir);
  57. var bazPkg = { name: 'baz', main: 'quux.js' };
  58. var dotMainPkg = { main: 'index' };
  59. var bazHNMmain = path.join(bazHNMDir, 'quux.js');
  60. t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
  61. var dotMainMain = path.join(dotMainDir, 'index.js');
  62. t.equal(require.resolve('dot_main'), dotMainMain, 'sanity check: require.resolve finds `dot_main`');
  63. makeTempDir(t, hnl, function (err) {
  64. t.error(err, 'no error with HNL temp dir');
  65. if (err) {
  66. return t.end();
  67. }
  68. var bazHNLDir = path.join(hnl, 'baz');
  69. copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNLDir);
  70. var dotSlashMainDir = path.join(hnl, 'dot_slash_main');
  71. var dotSlashMainMain = path.join(dotSlashMainDir, 'index.js');
  72. var dotSlashMainPkg = { main: 'index' };
  73. copyDir.sync(path.join(__dirname, 'resolver/dot_slash_main'), dotSlashMainDir);
  74. t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
  75. t.equal(require.resolve('dot_slash_main'), dotSlashMainMain, 'sanity check: require.resolve finds HNL `dot_slash_main`');
  76. t.test('with temp dirs', function (st) {
  77. st.plan(3);
  78. st.test('just in `$HOME/.node_modules`', function (s2t) {
  79. s2t.plan(3);
  80. resolve('dot_main', function (err, res, pkg) {
  81. s2t.error(err, 'no error resolving `dot_main`');
  82. s2t.equal(res, dotMainMain, '`dot_main` resolves in `$HOME/.node_modules`');
  83. s2t.deepEqual(pkg, dotMainPkg);
  84. });
  85. });
  86. st.test('just in `$HOME/.node_libraries`', function (s2t) {
  87. s2t.plan(3);
  88. resolve('dot_slash_main', function (err, res, pkg) {
  89. s2t.error(err, 'no error resolving `dot_slash_main`');
  90. s2t.equal(res, dotSlashMainMain, '`dot_slash_main` resolves in `$HOME/.node_libraries`');
  91. s2t.deepEqual(pkg, dotSlashMainPkg);
  92. });
  93. });
  94. st.test('in `$HOME/.node_libraries` and `$HOME/.node_modules`', function (s2t) {
  95. s2t.plan(3);
  96. resolve('baz', function (err, res, pkg) {
  97. s2t.error(err, 'no error resolving `baz`');
  98. s2t.equal(res, bazHNMmain, '`baz` resolves in `$HOME/.node_modules` when in both');
  99. s2t.deepEqual(pkg, bazPkg);
  100. });
  101. });
  102. });
  103. });
  104. });
  105. });