Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

239 рядки
7.4 KiB

4 роки тому
  1. var path = require('path');
  2. var test = require('tape');
  3. var resolve = require('../');
  4. test('mock', function (t) {
  5. t.plan(8);
  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, cb) {
  14. cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
  15. },
  16. isDirectory: function (dir, cb) {
  17. cb(null, !!dirs[path.resolve(dir)]);
  18. },
  19. readFile: function (file, cb) {
  20. cb(null, files[path.resolve(file)]);
  21. },
  22. realpath: function (file, cb) {
  23. cb(null, file);
  24. }
  25. };
  26. }
  27. resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
  28. if (err) return t.fail(err);
  29. t.equal(res, path.resolve('/foo/bar/baz.js'));
  30. t.equal(pkg, undefined);
  31. });
  32. resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
  33. if (err) return t.fail(err);
  34. t.equal(res, path.resolve('/foo/bar/baz.js'));
  35. t.equal(pkg, undefined);
  36. });
  37. resolve('baz', opts('/foo/bar'), function (err, res) {
  38. t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
  39. t.equal(err.code, 'MODULE_NOT_FOUND');
  40. });
  41. resolve('../baz', opts('/foo/bar'), function (err, res) {
  42. t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
  43. t.equal(err.code, 'MODULE_NOT_FOUND');
  44. });
  45. });
  46. test('mock from package', function (t) {
  47. t.plan(8);
  48. var files = {};
  49. files[path.resolve('/foo/bar/baz.js')] = 'beep';
  50. var dirs = {};
  51. dirs[path.resolve('/foo/bar')] = true;
  52. function opts(basedir) {
  53. return {
  54. basedir: path.resolve(basedir),
  55. isFile: function (file, cb) {
  56. cb(null, Object.prototype.hasOwnProperty.call(files, file));
  57. },
  58. isDirectory: function (dir, cb) {
  59. cb(null, !!dirs[path.resolve(dir)]);
  60. },
  61. 'package': { main: 'bar' },
  62. readFile: function (file, cb) {
  63. cb(null, files[file]);
  64. },
  65. realpath: function (file, cb) {
  66. cb(null, file);
  67. }
  68. };
  69. }
  70. resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
  71. if (err) return t.fail(err);
  72. t.equal(res, path.resolve('/foo/bar/baz.js'));
  73. t.equal(pkg && pkg.main, 'bar');
  74. });
  75. resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
  76. if (err) return t.fail(err);
  77. t.equal(res, path.resolve('/foo/bar/baz.js'));
  78. t.equal(pkg && pkg.main, 'bar');
  79. });
  80. resolve('baz', opts('/foo/bar'), function (err, res) {
  81. t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
  82. t.equal(err.code, 'MODULE_NOT_FOUND');
  83. });
  84. resolve('../baz', opts('/foo/bar'), function (err, res) {
  85. t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
  86. t.equal(err.code, 'MODULE_NOT_FOUND');
  87. });
  88. });
  89. test('mock package', function (t) {
  90. t.plan(2);
  91. var files = {};
  92. files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
  93. files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
  94. main: './baz.js'
  95. });
  96. var dirs = {};
  97. dirs[path.resolve('/foo')] = true;
  98. dirs[path.resolve('/foo/node_modules')] = true;
  99. function opts(basedir) {
  100. return {
  101. basedir: path.resolve(basedir),
  102. isFile: function (file, cb) {
  103. cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
  104. },
  105. isDirectory: function (dir, cb) {
  106. cb(null, !!dirs[path.resolve(dir)]);
  107. },
  108. readFile: function (file, cb) {
  109. cb(null, files[path.resolve(file)]);
  110. },
  111. realpath: function (file, cb) {
  112. cb(null, file);
  113. }
  114. };
  115. }
  116. resolve('bar', opts('/foo'), function (err, res, pkg) {
  117. if (err) return t.fail(err);
  118. t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
  119. t.equal(pkg && pkg.main, './baz.js');
  120. });
  121. });
  122. test('mock package from package', function (t) {
  123. t.plan(2);
  124. var files = {};
  125. files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
  126. files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
  127. main: './baz.js'
  128. });
  129. var dirs = {};
  130. dirs[path.resolve('/foo')] = true;
  131. dirs[path.resolve('/foo/node_modules')] = true;
  132. function opts(basedir) {
  133. return {
  134. basedir: path.resolve(basedir),
  135. isFile: function (file, cb) {
  136. cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
  137. },
  138. isDirectory: function (dir, cb) {
  139. cb(null, !!dirs[path.resolve(dir)]);
  140. },
  141. 'package': { main: 'bar' },
  142. readFile: function (file, cb) {
  143. cb(null, files[path.resolve(file)]);
  144. },
  145. realpath: function (file, cb) {
  146. cb(null, file);
  147. }
  148. };
  149. }
  150. resolve('bar', opts('/foo'), function (err, res, pkg) {
  151. if (err) return t.fail(err);
  152. t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
  153. t.equal(pkg && pkg.main, './baz.js');
  154. });
  155. });
  156. test('symlinked', function (t) {
  157. t.plan(4);
  158. var files = {};
  159. files[path.resolve('/foo/bar/baz.js')] = 'beep';
  160. files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
  161. var dirs = {};
  162. dirs[path.resolve('/foo/bar')] = true;
  163. dirs[path.resolve('/foo/bar/symlinked')] = true;
  164. function opts(basedir) {
  165. return {
  166. preserveSymlinks: false,
  167. basedir: path.resolve(basedir),
  168. isFile: function (file, cb) {
  169. cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
  170. },
  171. isDirectory: function (dir, cb) {
  172. cb(null, !!dirs[path.resolve(dir)]);
  173. },
  174. readFile: function (file, cb) {
  175. cb(null, files[path.resolve(file)]);
  176. },
  177. realpath: function (file, cb) {
  178. var resolved = path.resolve(file);
  179. if (resolved.indexOf('symlinked') >= 0) {
  180. cb(null, resolved);
  181. return;
  182. }
  183. var ext = path.extname(resolved);
  184. if (ext) {
  185. var dir = path.dirname(resolved);
  186. var base = path.basename(resolved);
  187. cb(null, path.join(dir, 'symlinked', base));
  188. } else {
  189. cb(null, path.join(resolved, 'symlinked'));
  190. }
  191. }
  192. };
  193. }
  194. resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
  195. if (err) return t.fail(err);
  196. t.equal(res, path.resolve('/foo/bar/symlinked/baz.js'));
  197. t.equal(pkg, undefined);
  198. });
  199. resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
  200. if (err) return t.fail(err);
  201. t.equal(res, path.resolve('/foo/bar/symlinked/baz.js'));
  202. t.equal(pkg, undefined);
  203. });
  204. });