resolver.js 12 KB

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