home_paths_sync.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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('../sync');
  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 bazHNMmain = path.join(bazHNMDir, 'quux.js');
  58. t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
  59. var dotMainMain = path.join(dotMainDir, 'index.js');
  60. t.equal(require.resolve('dot_main'), dotMainMain, 'sanity check: require.resolve finds `dot_main`');
  61. makeTempDir(t, hnl, function (err) {
  62. t.error(err, 'no error with HNL temp dir');
  63. if (err) {
  64. return t.end();
  65. }
  66. var bazHNLDir = path.join(hnl, 'baz');
  67. copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNLDir);
  68. var dotSlashMainDir = path.join(hnl, 'dot_slash_main');
  69. var dotSlashMainMain = path.join(dotSlashMainDir, 'index.js');
  70. copyDir.sync(path.join(__dirname, 'resolver/dot_slash_main'), dotSlashMainDir);
  71. t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
  72. t.equal(require.resolve('dot_slash_main'), dotSlashMainMain, 'sanity check: require.resolve finds HNL `dot_slash_main`');
  73. t.test('with temp dirs', function (st) {
  74. st.plan(3);
  75. st.test('just in `$HOME/.node_modules`', function (s2t) {
  76. s2t.plan(1);
  77. var res = resolve('dot_main');
  78. s2t.equal(res, dotMainMain, '`dot_main` resolves in `$HOME/.node_modules`');
  79. });
  80. st.test('just in `$HOME/.node_libraries`', function (s2t) {
  81. s2t.plan(1);
  82. var res = resolve('dot_slash_main');
  83. s2t.equal(res, dotSlashMainMain, '`dot_slash_main` resolves in `$HOME/.node_libraries`');
  84. });
  85. st.test('in `$HOME/.node_libraries` and `$HOME/.node_modules`', function (s2t) {
  86. s2t.plan(1);
  87. var res = resolve('baz');
  88. s2t.equal(res, bazHNMmain, '`baz` resolves in `$HOME/.node_modules` when in both');
  89. });
  90. });
  91. });
  92. });
  93. });