mock_sync.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var path = require('path');
  2. var test = require('tape');
  3. var resolve = require('../');
  4. test('mock', function (t) {
  5. t.plan(4);
  6. var files = {};
  7. files[path.resolve('/foo/bar/baz.js')] = 'beep';
  8. var dirs = {};
  9. dirs[path.resolve('/foo/bar')] = true;
  10. function opts(basedir) {
  11. return {
  12. basedir: path.resolve(basedir),
  13. isFile: function (file) {
  14. return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
  15. },
  16. isDirectory: function (dir) {
  17. return !!dirs[path.resolve(dir)];
  18. },
  19. readFileSync: function (file) {
  20. return files[path.resolve(file)];
  21. },
  22. realpathSync: function (file) {
  23. return file;
  24. }
  25. };
  26. }
  27. t.equal(
  28. resolve.sync('./baz', opts('/foo/bar')),
  29. path.resolve('/foo/bar/baz.js')
  30. );
  31. t.equal(
  32. resolve.sync('./baz.js', opts('/foo/bar')),
  33. path.resolve('/foo/bar/baz.js')
  34. );
  35. t.throws(function () {
  36. resolve.sync('baz', opts('/foo/bar'));
  37. });
  38. t.throws(function () {
  39. resolve.sync('../baz', opts('/foo/bar'));
  40. });
  41. });
  42. test('mock package', function (t) {
  43. t.plan(1);
  44. var files = {};
  45. files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
  46. files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
  47. main: './baz.js'
  48. });
  49. var dirs = {};
  50. dirs[path.resolve('/foo')] = true;
  51. dirs[path.resolve('/foo/node_modules')] = true;
  52. function opts(basedir) {
  53. return {
  54. basedir: path.resolve(basedir),
  55. isFile: function (file) {
  56. return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
  57. },
  58. isDirectory: function (dir) {
  59. return !!dirs[path.resolve(dir)];
  60. },
  61. readFileSync: function (file) {
  62. return files[path.resolve(file)];
  63. },
  64. realpathSync: function (file) {
  65. return file;
  66. }
  67. };
  68. }
  69. t.equal(
  70. resolve.sync('bar', opts('/foo')),
  71. path.resolve('/foo/node_modules/bar/baz.js')
  72. );
  73. });
  74. test('symlinked', function (t) {
  75. t.plan(2);
  76. var files = {};
  77. files[path.resolve('/foo/bar/baz.js')] = 'beep';
  78. files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
  79. var dirs = {};
  80. dirs[path.resolve('/foo/bar')] = true;
  81. dirs[path.resolve('/foo/bar/symlinked')] = true;
  82. function opts(basedir) {
  83. return {
  84. preserveSymlinks: false,
  85. basedir: path.resolve(basedir),
  86. isFile: function (file) {
  87. return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
  88. },
  89. isDirectory: function (dir) {
  90. return !!dirs[path.resolve(dir)];
  91. },
  92. readFileSync: function (file) {
  93. return files[path.resolve(file)];
  94. },
  95. realpathSync: function (file) {
  96. var resolved = path.resolve(file);
  97. if (resolved.indexOf('symlinked') >= 0) {
  98. return resolved;
  99. }
  100. var ext = path.extname(resolved);
  101. if (ext) {
  102. var dir = path.dirname(resolved);
  103. var base = path.basename(resolved);
  104. return path.join(dir, 'symlinked', base);
  105. } else {
  106. return path.join(resolved, 'symlinked');
  107. }
  108. }
  109. };
  110. }
  111. t.equal(
  112. resolve.sync('./baz', opts('/foo/bar')),
  113. path.resolve('/foo/bar/symlinked/baz.js')
  114. );
  115. t.equal(
  116. resolve.sync('./baz.js', opts('/foo/bar')),
  117. path.resolve('/foo/bar/symlinked/baz.js')
  118. );
  119. });