You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
6.3 KiB

4 years ago
  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(resolve.sync('foo', { basedir: symlinkDir, preserveSymlinks: false }), path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
  64. });
  65. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  66. t.end();
  67. });
  68. test('sync symlink when preserveSymlinks = true', function (t) {
  69. t.throws(function () {
  70. resolve.sync('foo', { basedir: symlinkDir });
  71. }, /Cannot find module 'foo'/);
  72. t.end();
  73. });
  74. test('sync symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
  75. var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
  76. var fn = resolve.sync('package', { basedir: basedir, preserveSymlinks: false });
  77. t.equal(fn, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
  78. t.end();
  79. });
  80. test('async symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
  81. t.plan(2);
  82. var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
  83. resolve('package', { basedir: basedir, preserveSymlinks: false }, function (err, result) {
  84. t.notOk(err, 'no error');
  85. t.equal(result, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
  86. });
  87. });
  88. test('packageFilter', function (t) {
  89. function relative(x) {
  90. return path.relative(__dirname, x);
  91. }
  92. function testPackageFilter(preserveSymlinks) {
  93. return function (st) {
  94. st.plan('is 1.x' ? 3 : 5); // eslint-disable-line no-constant-condition
  95. var destMain = 'symlinks/dest/node_modules/mod-a/index.js';
  96. var destPkg = 'symlinks/dest/node_modules/mod-a/package.json';
  97. var sourceMain = 'symlinks/source/node_modules/mod-a/index.js';
  98. var sourcePkg = 'symlinks/source/node_modules/mod-a/package.json';
  99. var destDir = path.join(__dirname, 'symlinks', 'dest');
  100. /* eslint multiline-comment-style: 0 */
  101. /* v2.x will restore these tests
  102. var packageFilterPath = [];
  103. var actualPath = resolve.sync('mod-a', {
  104. basedir: destDir,
  105. preserveSymlinks: preserveSymlinks,
  106. packageFilter: function (pkg, pkgfile, dir) {
  107. packageFilterPath.push(pkgfile);
  108. }
  109. });
  110. st.equal(
  111. relative(actualPath),
  112. path.normalize(preserveSymlinks ? destMain : sourceMain),
  113. 'sync: actual path is correct'
  114. );
  115. st.deepEqual(
  116. map(packageFilterPath, relative),
  117. map(preserveSymlinks ? [destPkg, destPkg] : [sourcePkg, sourcePkg], path.normalize),
  118. 'sync: packageFilter pkgfile arg is correct'
  119. );
  120. */
  121. var asyncPackageFilterPath = [];
  122. resolve(
  123. 'mod-a',
  124. {
  125. basedir: destDir,
  126. preserveSymlinks: preserveSymlinks,
  127. packageFilter: function (pkg, pkgfile) {
  128. asyncPackageFilterPath.push(pkgfile);
  129. }
  130. },
  131. function (err, actualPath) {
  132. st.error(err, 'no error');
  133. st.equal(
  134. relative(actualPath),
  135. path.normalize(preserveSymlinks ? destMain : sourceMain),
  136. 'async: actual path is correct'
  137. );
  138. st.deepEqual(
  139. map(asyncPackageFilterPath, relative),
  140. map(
  141. preserveSymlinks ? [destPkg, destPkg, destPkg] : [sourcePkg, sourcePkg, sourcePkg],
  142. path.normalize
  143. ),
  144. 'async: packageFilter pkgfile arg is correct'
  145. );
  146. }
  147. );
  148. };
  149. }
  150. t.test('preserveSymlinks: false', testPackageFilter(false));
  151. t.test('preserveSymlinks: true', testPackageFilter(true));
  152. t.end();
  153. });