symlinks.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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.equal(resolve.sync('foo', { basedir: symlinkDir, preserveSymlinks: false }), path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
  38. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  39. t.end();
  40. });
  41. test('sync symlink when preserveSymlinks = true', function (t) {
  42. t.throws(function () {
  43. resolve.sync('foo', { basedir: symlinkDir });
  44. }, /Cannot find module 'foo'/);
  45. t.end();
  46. });