symlinks.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var path = require('path');
  2. var fs = require('fs');
  3. var test = require('tape');
  4. var resolve = require('../');
  5. var symlinkDir = path.join(__dirname, 'resolver', 'symlinked', 'symlink');
  6. try {
  7. fs.unlinkSync(symlinkDir);
  8. } catch (err) {}
  9. try {
  10. fs.symlinkSync('./_/symlink_target', symlinkDir, 'dir');
  11. } catch (err) {
  12. // if fails then it is probably on Windows and lets try to create a junction
  13. fs.symlinkSync(path.join(__dirname, 'resolver', 'symlinked', '_', 'symlink_target') + '\\', symlinkDir, 'junction');
  14. }
  15. test('symlink', function (t) {
  16. t.plan(1);
  17. resolve('foo', { basedir: symlinkDir, preserveSymlinks: false }, function (err, res, pkg) {
  18. if (err) t.fail(err);
  19. t.equal(res, path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
  20. });
  21. });
  22. test('sync symlink when preserveSymlinks = true', function (t) {
  23. t.plan(4);
  24. resolve('foo', { basedir: symlinkDir }, function (err, res, pkg) {
  25. t.ok(err, 'there is an error');
  26. t.notOk(res, 'no result');
  27. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  28. t.equal(
  29. err && err.message,
  30. 'Cannot find module \'foo\' from \'' + symlinkDir + '\'',
  31. 'can not find nonexistent module'
  32. );
  33. });
  34. });
  35. test('sync symlink', function (t) {
  36. var start = new Date();
  37. t.doesNotThrow(function () {
  38. t.equal(resolve.sync('foo', { basedir: symlinkDir, preserveSymlinks: false }), path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
  39. });
  40. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  41. t.end();
  42. });
  43. test('sync symlink when preserveSymlinks = true', function (t) {
  44. t.throws(function () {
  45. resolve.sync('foo', { basedir: symlinkDir });
  46. }, /Cannot find module 'foo'/);
  47. t.end();
  48. });