mock_sync.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. });
  120. test('readPackageSync', function (t) {
  121. t.plan(3);
  122. var files = {};
  123. files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
  124. files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
  125. main: './baz.js'
  126. });
  127. files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
  128. var dirs = {};
  129. dirs[path.resolve('/foo')] = true;
  130. dirs[path.resolve('/foo/node_modules')] = true;
  131. function opts(basedir, useReadPackage) {
  132. return {
  133. basedir: path.resolve(basedir),
  134. isFile: function (file) {
  135. return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
  136. },
  137. isDirectory: function (dir) {
  138. return !!dirs[path.resolve(dir)];
  139. },
  140. readFileSync: useReadPackage ? null : function (file) {
  141. return files[path.resolve(file)];
  142. },
  143. realpathSync: function (file) {
  144. return file;
  145. }
  146. };
  147. }
  148. t.test('with readFile', function (st) {
  149. st.plan(1);
  150. st.equal(
  151. resolve.sync('bar', opts('/foo')),
  152. path.resolve('/foo/node_modules/bar/baz.js')
  153. );
  154. });
  155. var readPackageSync = function (readFileSync, file) {
  156. if (file.indexOf(path.join('bar', 'package.json')) >= 0) {
  157. return { main: './something-else.js' };
  158. } else {
  159. return JSON.parse(files[path.resolve(file)]);
  160. }
  161. };
  162. t.test('with readPackage', function (st) {
  163. st.plan(1);
  164. var options = opts('/foo');
  165. delete options.readFileSync;
  166. options.readPackageSync = readPackageSync;
  167. st.equal(
  168. resolve.sync('bar', options),
  169. path.resolve('/foo/node_modules/bar/something-else.js')
  170. );
  171. });
  172. t.test('with readFile and readPackage', function (st) {
  173. st.plan(1);
  174. var options = opts('/foo');
  175. options.readPackageSync = readPackageSync;
  176. st.throws(
  177. function () { resolve.sync('bar', options); },
  178. TypeError,
  179. 'errors when both readFile and readPackage are provided'
  180. );
  181. });
  182. });