symlinks.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. var path = require('path');
  2. var fs = require('fs');
  3. var test = require('tape');
  4. var map = require('array.prototype.map');
  5. var resolve = require('../');
  6. var symlinkDir = path.join(__dirname, 'resolver', 'symlinked', 'symlink');
  7. var packageDir = path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'package');
  8. var modADir = path.join(__dirname, 'symlinks', 'source', 'node_modules', 'mod-a');
  9. var symlinkModADir = path.join(__dirname, 'symlinks', 'dest', 'node_modules', 'mod-a');
  10. try {
  11. fs.unlinkSync(symlinkDir);
  12. } catch (err) {}
  13. try {
  14. fs.unlinkSync(packageDir);
  15. } catch (err) {}
  16. try {
  17. fs.unlinkSync(modADir);
  18. } catch (err) {}
  19. try {
  20. fs.unlinkSync(symlinkModADir);
  21. } catch (err) {}
  22. try {
  23. fs.symlinkSync('./_/symlink_target', symlinkDir, 'dir');
  24. } catch (err) {
  25. // if fails then it is probably on Windows and lets try to create a junction
  26. fs.symlinkSync(path.join(__dirname, 'resolver', 'symlinked', '_', 'symlink_target') + '\\', symlinkDir, 'junction');
  27. }
  28. try {
  29. fs.symlinkSync('../../package', packageDir, 'dir');
  30. } catch (err) {
  31. // if fails then it is probably on Windows and lets try to create a junction
  32. fs.symlinkSync(path.join(__dirname, '..', '..', 'package') + '\\', packageDir, 'junction');
  33. }
  34. try {
  35. fs.symlinkSync('../../source/node_modules/mod-a', symlinkModADir, 'dir');
  36. } catch (err) {
  37. // if fails then it is probably on Windows and lets try to create a junction
  38. fs.symlinkSync(path.join(__dirname, '..', '..', 'source', 'node_modules', 'mod-a') + '\\', symlinkModADir, 'junction');
  39. }
  40. test('symlink', function (t) {
  41. t.plan(2);
  42. resolve('foo', { basedir: symlinkDir, preserveSymlinks: false }, function (err, res, pkg) {
  43. t.error(err);
  44. t.equal(res, path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
  45. });
  46. });
  47. test('sync symlink when preserveSymlinks = true', function (t) {
  48. t.plan(4);
  49. resolve('foo', { basedir: symlinkDir }, function (err, res, pkg) {
  50. t.ok(err, 'there is an error');
  51. t.notOk(res, 'no result');
  52. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  53. t.equal(
  54. err && err.message,
  55. 'Cannot find module \'foo\' from \'' + symlinkDir + '\'',
  56. 'can not find nonexistent module'
  57. );
  58. });
  59. });
  60. test('sync symlink', function (t) {
  61. var start = new Date();
  62. t.doesNotThrow(function () {
  63. t.equal(
  64. resolve.sync('foo', { basedir: symlinkDir, preserveSymlinks: false }),
  65. path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js')
  66. );
  67. });
  68. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  69. t.end();
  70. });
  71. test('sync symlink when preserveSymlinks = true', function (t) {
  72. t.throws(function () {
  73. resolve.sync('foo', { basedir: symlinkDir });
  74. }, /Cannot find module 'foo'/);
  75. t.end();
  76. });
  77. test('sync symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
  78. var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
  79. var fn = resolve.sync('package', { basedir: basedir, preserveSymlinks: false });
  80. t.equal(fn, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
  81. t.end();
  82. });
  83. test('async symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
  84. t.plan(2);
  85. var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
  86. resolve('package', { basedir: basedir, preserveSymlinks: false }, function (err, result) {
  87. t.notOk(err, 'no error');
  88. t.equal(result, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
  89. });
  90. });
  91. test('packageFilter', function (t) {
  92. function relative(x) {
  93. return path.relative(__dirname, x);
  94. }
  95. function testPackageFilter(preserveSymlinks) {
  96. return function (st) {
  97. st.plan('is 1.x' ? 3 : 5); // eslint-disable-line no-constant-condition
  98. var destMain = 'symlinks/dest/node_modules/mod-a/index.js';
  99. var destPkg = 'symlinks/dest/node_modules/mod-a/package.json';
  100. var sourceMain = 'symlinks/source/node_modules/mod-a/index.js';
  101. var sourcePkg = 'symlinks/source/node_modules/mod-a/package.json';
  102. var destDir = path.join(__dirname, 'symlinks', 'dest');
  103. /* eslint multiline-comment-style: 0 */
  104. /* v2.x will restore these tests
  105. var packageFilterPath = [];
  106. var actualPath = resolve.sync('mod-a', {
  107. basedir: destDir,
  108. preserveSymlinks: preserveSymlinks,
  109. packageFilter: function (pkg, pkgfile, dir) {
  110. packageFilterPath.push(pkgfile);
  111. }
  112. });
  113. st.equal(
  114. relative(actualPath),
  115. path.normalize(preserveSymlinks ? destMain : sourceMain),
  116. 'sync: actual path is correct'
  117. );
  118. st.deepEqual(
  119. map(packageFilterPath, relative),
  120. map(preserveSymlinks ? [destPkg, destPkg] : [sourcePkg, sourcePkg], path.normalize),
  121. 'sync: packageFilter pkgfile arg is correct'
  122. );
  123. */
  124. var asyncPackageFilterPath = [];
  125. resolve(
  126. 'mod-a',
  127. {
  128. basedir: destDir,
  129. preserveSymlinks: preserveSymlinks,
  130. packageFilter: function (pkg, pkgfile) {
  131. asyncPackageFilterPath.push(pkgfile);
  132. }
  133. },
  134. function (err, actualPath) {
  135. st.error(err, 'no error');
  136. st.equal(
  137. relative(actualPath),
  138. path.normalize(preserveSymlinks ? destMain : sourceMain),
  139. 'async: actual path is correct'
  140. );
  141. st.deepEqual(
  142. map(asyncPackageFilterPath, relative),
  143. map(
  144. preserveSymlinks ? [destPkg, destPkg, destPkg] : [sourcePkg, sourcePkg, sourcePkg],
  145. path.normalize
  146. ),
  147. 'async: packageFilter pkgfile arg is correct'
  148. );
  149. }
  150. );
  151. };
  152. }
  153. t.test('preserveSymlinks: false', testPackageFilter(false));
  154. t.test('preserveSymlinks: true', testPackageFilter(true));
  155. t.end();
  156. });