mock_sync.js 5.9 KB

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