UI for Zipcoin Blue

resolver.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. var path = require('path');
  2. var test = require('tape');
  3. var resolve = require('../');
  4. test('async foo', function (t) {
  5. t.plan(12);
  6. var dir = path.join(__dirname, 'resolver');
  7. resolve('./foo', { basedir: dir }, function (err, res, pkg) {
  8. if (err) t.fail(err);
  9. t.equal(res, path.join(dir, 'foo.js'));
  10. t.equal(pkg && pkg.name, 'resolve');
  11. });
  12. resolve('./foo.js', { basedir: dir }, function (err, res, pkg) {
  13. if (err) t.fail(err);
  14. t.equal(res, path.join(dir, 'foo.js'));
  15. t.equal(pkg && pkg.name, 'resolve');
  16. });
  17. resolve('./foo', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
  18. if (err) t.fail(err);
  19. t.equal(res, path.join(dir, 'foo.js'));
  20. t.equal(pkg && pkg.main, 'resolver');
  21. });
  22. resolve('./foo.js', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
  23. if (err) t.fail(err);
  24. t.equal(res, path.join(dir, 'foo.js'));
  25. t.equal(pkg.main, 'resolver');
  26. });
  27. resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
  28. if (err) t.fail(err);
  29. t.equal(res, path.join(dir, 'foo.js'));
  30. });
  31. resolve('foo', { basedir: dir }, function (err) {
  32. t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
  33. t.equal(err.code, 'MODULE_NOT_FOUND');
  34. });
  35. // Test that filename is reported as the "from" value when passed.
  36. resolve('foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err) {
  37. t.equal(err.message, "Cannot find module 'foo' from '" + path.join(dir, 'baz.js') + "'");
  38. });
  39. });
  40. test('bar', function (t) {
  41. t.plan(6);
  42. var dir = path.join(__dirname, 'resolver');
  43. resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
  44. if (err) t.fail(err);
  45. t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
  46. t.equal(pkg, undefined);
  47. });
  48. resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
  49. if (err) t.fail(err);
  50. t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
  51. t.equal(pkg, undefined);
  52. });
  53. resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
  54. if (err) t.fail(err);
  55. t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
  56. t.equal(pkg.main, 'bar');
  57. });
  58. });
  59. test('baz', function (t) {
  60. t.plan(4);
  61. var dir = path.join(__dirname, 'resolver');
  62. resolve('./baz', { basedir: dir }, function (err, res, pkg) {
  63. if (err) t.fail(err);
  64. t.equal(res, path.join(dir, 'baz/quux.js'));
  65. t.equal(pkg.main, 'quux.js');
  66. });
  67. resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
  68. if (err) t.fail(err);
  69. t.equal(res, path.join(dir, 'baz/quux.js'));
  70. t.equal(pkg.main, 'quux.js');
  71. });
  72. });
  73. test('biz', function (t) {
  74. t.plan(24);
  75. var dir = path.join(__dirname, 'resolver/biz/node_modules');
  76. resolve('./grux', { basedir: dir }, function (err, res, pkg) {
  77. if (err) t.fail(err);
  78. t.equal(res, path.join(dir, 'grux/index.js'));
  79. t.equal(pkg, undefined);
  80. });
  81. resolve('./grux', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
  82. if (err) t.fail(err);
  83. t.equal(res, path.join(dir, 'grux/index.js'));
  84. t.equal(pkg.main, 'biz');
  85. });
  86. resolve('./garply', { basedir: dir }, function (err, res, pkg) {
  87. if (err) t.fail(err);
  88. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  89. t.equal(pkg.main, './lib');
  90. });
  91. resolve('./garply', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
  92. if (err) t.fail(err);
  93. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  94. t.equal(pkg.main, './lib');
  95. });
  96. resolve('tiv', { basedir: dir + '/grux' }, function (err, res, pkg) {
  97. if (err) t.fail(err);
  98. t.equal(res, path.join(dir, 'tiv/index.js'));
  99. t.equal(pkg, undefined);
  100. });
  101. resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) {
  102. if (err) t.fail(err);
  103. t.equal(res, path.join(dir, 'tiv/index.js'));
  104. t.equal(pkg.main, 'grux');
  105. });
  106. resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) {
  107. if (err) t.fail(err);
  108. t.equal(res, path.join(dir, 'tiv/index.js'));
  109. t.equal(pkg, undefined);
  110. });
  111. resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) {
  112. if (err) t.fail(err);
  113. t.equal(res, path.join(dir, 'tiv/index.js'));
  114. t.equal(pkg.main, './lib');
  115. });
  116. resolve('grux', { basedir: dir + '/tiv' }, function (err, res, pkg) {
  117. if (err) t.fail(err);
  118. t.equal(res, path.join(dir, 'grux/index.js'));
  119. t.equal(pkg, undefined);
  120. });
  121. resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
  122. if (err) t.fail(err);
  123. t.equal(res, path.join(dir, 'grux/index.js'));
  124. t.equal(pkg.main, 'tiv');
  125. });
  126. resolve('garply', { basedir: dir + '/tiv' }, function (err, res, pkg) {
  127. if (err) t.fail(err);
  128. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  129. t.equal(pkg.main, './lib');
  130. });
  131. resolve('garply', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
  132. if (err) t.fail(err);
  133. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  134. t.equal(pkg.main, './lib');
  135. });
  136. });
  137. test('quux', function (t) {
  138. t.plan(2);
  139. var dir = path.join(__dirname, 'resolver/quux');
  140. resolve('./foo', { basedir: dir, 'package': { main: 'quux' } }, function (err, res, pkg) {
  141. if (err) t.fail(err);
  142. t.equal(res, path.join(dir, 'foo/index.js'));
  143. t.equal(pkg.main, 'quux');
  144. });
  145. });
  146. test('normalize', function (t) {
  147. t.plan(2);
  148. var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
  149. resolve('../grux', { basedir: dir }, function (err, res, pkg) {
  150. if (err) t.fail(err);
  151. t.equal(res, path.join(dir, 'index.js'));
  152. t.equal(pkg, undefined);
  153. });
  154. });
  155. test('cup', function (t) {
  156. t.plan(5);
  157. var dir = path.join(__dirname, 'resolver');
  158. resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
  159. if (err) t.fail(err);
  160. t.equal(res, path.join(dir, 'cup.coffee'));
  161. });
  162. resolve('./cup.coffee', { basedir: dir }, function (err, res) {
  163. if (err) t.fail(err);
  164. t.equal(res, path.join(dir, 'cup.coffee'));
  165. });
  166. resolve('./cup', { basedir: dir, extensions: ['.js'] }, function (err, res) {
  167. t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
  168. t.equal(err.code, 'MODULE_NOT_FOUND');
  169. });
  170. // Test that filename is reported as the "from" value when passed.
  171. resolve('./cup', { basedir: dir, extensions: ['.js'], filename: path.join(dir, 'cupboard.js') }, function (err, res) {
  172. t.equal(err.message, "Cannot find module './cup' from '" + path.join(dir, 'cupboard.js') + "'");
  173. });
  174. });
  175. test('mug', function (t) {
  176. t.plan(3);
  177. var dir = path.join(__dirname, 'resolver');
  178. resolve('./mug', { basedir: dir }, function (err, res) {
  179. if (err) t.fail(err);
  180. t.equal(res, path.join(dir, 'mug.js'));
  181. });
  182. resolve('./mug', { basedir: dir, extensions: ['.coffee', '.js'] }, function (err, res) {
  183. if (err) t.fail(err);
  184. t.equal(res, path.join(dir, '/mug.coffee'));
  185. });
  186. resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
  187. t.equal(res, path.join(dir, '/mug.js'));
  188. });
  189. });
  190. test('other path', function (t) {
  191. t.plan(6);
  192. var resolverDir = path.join(__dirname, 'resolver');
  193. var dir = path.join(resolverDir, 'bar');
  194. var otherDir = path.join(resolverDir, 'other_path');
  195. resolve('root', { basedir: dir, paths: [otherDir] }, function (err, res) {
  196. if (err) t.fail(err);
  197. t.equal(res, path.join(resolverDir, 'other_path/root.js'));
  198. });
  199. resolve('lib/other-lib', { basedir: dir, paths: [otherDir] }, function (err, res) {
  200. if (err) t.fail(err);
  201. t.equal(res, path.join(resolverDir, 'other_path/lib/other-lib.js'));
  202. });
  203. resolve('root', { basedir: dir }, function (err, res) {
  204. t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
  205. t.equal(err.code, 'MODULE_NOT_FOUND');
  206. });
  207. resolve('zzz', { basedir: dir, paths: [otherDir] }, function (err, res) {
  208. t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
  209. t.equal(err.code, 'MODULE_NOT_FOUND');
  210. });
  211. });
  212. test('incorrect main', function (t) {
  213. t.plan(1);
  214. var resolverDir = path.join(__dirname, 'resolver');
  215. var dir = path.join(resolverDir, 'incorrect_main');
  216. resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) {
  217. if (err) t.fail(err);
  218. t.equal(res, path.join(dir, 'index.js'));
  219. });
  220. });
  221. test('without basedir', function (t) {
  222. t.plan(1);
  223. var dir = path.join(__dirname, 'resolver/without_basedir');
  224. var tester = require(path.join(dir, 'main.js'));
  225. tester(t, function (err, res, pkg) {
  226. if (err) {
  227. t.fail(err);
  228. } else {
  229. t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
  230. }
  231. });
  232. });
  233. test('#25: node modules with the same name as node stdlib modules', function (t) {
  234. t.plan(1);
  235. var resolverDir = path.join(__dirname, 'resolver/punycode');
  236. resolve('punycode', { basedir: resolverDir }, function (err, res, pkg) {
  237. if (err) t.fail(err);
  238. t.equal(res, path.join(resolverDir, 'node_modules/punycode/index.js'));
  239. });
  240. });
  241. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  242. t.plan(2);
  243. var dir = path.join(__dirname, 'resolver');
  244. resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  245. if (err) t.fail(err);
  246. t.equal(res, path.join(dir, 'same_names/foo.js'));
  247. });
  248. resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  249. if (err) t.fail(err);
  250. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  251. });
  252. });
  253. test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
  254. var testFile = path.basename(__filename);
  255. t.test('sanity check', function (st) {
  256. st.plan(1);
  257. resolve('./' + testFile, function (err, res, pkg) {
  258. if (err) t.fail(err);
  259. st.equal(res, __filename, 'sanity check');
  260. });
  261. });
  262. t.test('with a fake directory', function (st) {
  263. st.plan(4);
  264. resolve('./' + testFile + '/blah', function (err, res, pkg) {
  265. st.ok(err, 'there is an error');
  266. st.notOk(res, 'no result');
  267. st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  268. st.equal(
  269. err && err.message,
  270. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  271. 'can not find nonexistent module'
  272. );
  273. st.end();
  274. });
  275. });
  276. t.end();
  277. });
  278. test('async dot main', function (t) {
  279. var start = new Date();
  280. t.plan(3);
  281. resolve('./resolver/dot_main', function (err, ret) {
  282. t.notOk(err);
  283. t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
  284. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  285. t.end();
  286. });
  287. });
  288. test('async dot slash main', function (t) {
  289. var start = new Date();
  290. t.plan(3);
  291. resolve('./resolver/dot_slash_main', function (err, ret) {
  292. t.notOk(err);
  293. t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
  294. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  295. t.end();
  296. });
  297. });
  298. test('not a directory', function (t) {
  299. t.plan(5);
  300. var path = './foo';
  301. resolve(path, { basedir: __filename }, function (err, res, pkg) {
  302. t.ok(err, 'a non-directory errors');
  303. t.equal(arguments.length, 1);
  304. t.equal(res, undefined);
  305. t.equal(pkg, undefined);
  306. t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
  307. });
  308. });
  309. test('browser field in package.json', function (t) {
  310. t.plan(3);
  311. var dir = path.join(__dirname, 'resolver');
  312. resolve(
  313. './browser_field',
  314. {
  315. basedir: dir,
  316. packageFilter: function packageFilter(pkg) {
  317. if (pkg.browser) {
  318. pkg.main = pkg.browser;
  319. delete pkg.browser;
  320. }
  321. return pkg;
  322. }
  323. },
  324. function (err, res, pkg) {
  325. if (err) t.fail(err);
  326. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  327. t.equal(pkg && pkg.main, 'b');
  328. t.equal(pkg && pkg.browser, undefined);
  329. }
  330. );
  331. });