resolver.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. var path = require('path');
  2. var test = require('tape');
  3. var resolve = require('../');
  4. var async = require('../async');
  5. test('`./async` entry point', function (t) {
  6. t.equal(resolve, async, '`./async` entry point is the same as `main`');
  7. t.end();
  8. });
  9. test('async foo', function (t) {
  10. t.plan(12);
  11. var dir = path.join(__dirname, 'resolver');
  12. resolve('./foo', { 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.js', { basedir: dir }, function (err, res, pkg) {
  18. if (err) t.fail(err);
  19. t.equal(res, path.join(dir, 'foo.js'));
  20. t.equal(pkg && pkg.name, 'resolve');
  21. });
  22. resolve('./foo', { 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 && pkg.main, 'resolver');
  26. });
  27. resolve('./foo.js', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
  28. if (err) t.fail(err);
  29. t.equal(res, path.join(dir, 'foo.js'));
  30. t.equal(pkg.main, 'resolver');
  31. });
  32. resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
  33. if (err) t.fail(err);
  34. t.equal(res, path.join(dir, 'foo.js'));
  35. });
  36. resolve('foo', { basedir: dir }, function (err) {
  37. t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
  38. t.equal(err.code, 'MODULE_NOT_FOUND');
  39. });
  40. // Test that filename is reported as the "from" value when passed.
  41. resolve('foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err) {
  42. t.equal(err.message, "Cannot find module 'foo' from '" + path.join(dir, 'baz.js') + "'");
  43. });
  44. });
  45. test('bar', function (t) {
  46. t.plan(6);
  47. var dir = path.join(__dirname, 'resolver');
  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' }, 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, undefined);
  57. });
  58. resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
  59. if (err) t.fail(err);
  60. t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
  61. t.equal(pkg.main, 'bar');
  62. });
  63. });
  64. test('baz', function (t) {
  65. t.plan(4);
  66. var dir = path.join(__dirname, 'resolver');
  67. resolve('./baz', { basedir: dir }, 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. resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
  73. if (err) t.fail(err);
  74. t.equal(res, path.join(dir, 'baz/quux.js'));
  75. t.equal(pkg.main, 'quux.js');
  76. });
  77. });
  78. test('biz', function (t) {
  79. t.plan(24);
  80. var dir = path.join(__dirname, 'resolver/biz/node_modules');
  81. resolve('./grux', { basedir: dir }, function (err, res, pkg) {
  82. if (err) t.fail(err);
  83. t.equal(res, path.join(dir, 'grux/index.js'));
  84. t.equal(pkg, undefined);
  85. });
  86. resolve('./grux', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
  87. if (err) t.fail(err);
  88. t.equal(res, path.join(dir, 'grux/index.js'));
  89. t.equal(pkg.main, 'biz');
  90. });
  91. resolve('./garply', { basedir: dir }, 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('./garply', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
  97. if (err) t.fail(err);
  98. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  99. t.equal(pkg.main, './lib');
  100. });
  101. resolve('tiv', { basedir: dir + '/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, undefined);
  105. });
  106. resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) {
  107. if (err) t.fail(err);
  108. t.equal(res, path.join(dir, 'tiv/index.js'));
  109. t.equal(pkg.main, 'grux');
  110. });
  111. resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) {
  112. if (err) t.fail(err);
  113. t.equal(res, path.join(dir, 'tiv/index.js'));
  114. t.equal(pkg, undefined);
  115. });
  116. resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) {
  117. if (err) t.fail(err);
  118. t.equal(res, path.join(dir, 'tiv/index.js'));
  119. t.equal(pkg.main, './lib');
  120. });
  121. resolve('grux', { basedir: dir + '/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, undefined);
  125. });
  126. resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
  127. if (err) t.fail(err);
  128. t.equal(res, path.join(dir, 'grux/index.js'));
  129. t.equal(pkg.main, 'tiv');
  130. });
  131. resolve('garply', { basedir: dir + '/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. resolve('garply', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
  137. if (err) t.fail(err);
  138. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  139. t.equal(pkg.main, './lib');
  140. });
  141. });
  142. test('quux', function (t) {
  143. t.plan(2);
  144. var dir = path.join(__dirname, 'resolver/quux');
  145. resolve('./foo', { basedir: dir, 'package': { main: 'quux' } }, function (err, res, pkg) {
  146. if (err) t.fail(err);
  147. t.equal(res, path.join(dir, 'foo/index.js'));
  148. t.equal(pkg.main, 'quux');
  149. });
  150. });
  151. test('normalize', function (t) {
  152. t.plan(2);
  153. var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
  154. resolve('../grux', { basedir: dir }, function (err, res, pkg) {
  155. if (err) t.fail(err);
  156. t.equal(res, path.join(dir, 'index.js'));
  157. t.equal(pkg, undefined);
  158. });
  159. });
  160. test('cup', function (t) {
  161. t.plan(5);
  162. var dir = path.join(__dirname, 'resolver');
  163. resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
  164. if (err) t.fail(err);
  165. t.equal(res, path.join(dir, 'cup.coffee'));
  166. });
  167. resolve('./cup.coffee', { basedir: dir }, function (err, res) {
  168. if (err) t.fail(err);
  169. t.equal(res, path.join(dir, 'cup.coffee'));
  170. });
  171. resolve('./cup', { basedir: dir, extensions: ['.js'] }, function (err, res) {
  172. t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
  173. t.equal(err.code, 'MODULE_NOT_FOUND');
  174. });
  175. // Test that filename is reported as the "from" value when passed.
  176. resolve('./cup', { basedir: dir, extensions: ['.js'], filename: path.join(dir, 'cupboard.js') }, function (err, res) {
  177. t.equal(err.message, "Cannot find module './cup' from '" + path.join(dir, 'cupboard.js') + "'");
  178. });
  179. });
  180. test('mug', function (t) {
  181. t.plan(3);
  182. var dir = path.join(__dirname, 'resolver');
  183. resolve('./mug', { basedir: dir }, function (err, res) {
  184. if (err) t.fail(err);
  185. t.equal(res, path.join(dir, 'mug.js'));
  186. });
  187. resolve('./mug', { basedir: dir, extensions: ['.coffee', '.js'] }, function (err, res) {
  188. if (err) t.fail(err);
  189. t.equal(res, path.join(dir, '/mug.coffee'));
  190. });
  191. resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
  192. t.equal(res, path.join(dir, '/mug.js'));
  193. });
  194. });
  195. test('other path', function (t) {
  196. t.plan(6);
  197. var resolverDir = path.join(__dirname, 'resolver');
  198. var dir = path.join(resolverDir, 'bar');
  199. var otherDir = path.join(resolverDir, 'other_path');
  200. resolve('root', { basedir: dir, paths: [otherDir] }, function (err, res) {
  201. if (err) t.fail(err);
  202. t.equal(res, path.join(resolverDir, 'other_path/root.js'));
  203. });
  204. resolve('lib/other-lib', { basedir: dir, paths: [otherDir] }, function (err, res) {
  205. if (err) t.fail(err);
  206. t.equal(res, path.join(resolverDir, 'other_path/lib/other-lib.js'));
  207. });
  208. resolve('root', { basedir: dir }, function (err, res) {
  209. t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
  210. t.equal(err.code, 'MODULE_NOT_FOUND');
  211. });
  212. resolve('zzz', { basedir: dir, paths: [otherDir] }, function (err, res) {
  213. t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
  214. t.equal(err.code, 'MODULE_NOT_FOUND');
  215. });
  216. });
  217. test('path iterator', function (t) {
  218. t.plan(2);
  219. var resolverDir = path.join(__dirname, 'resolver');
  220. var exactIterator = function (x, start, getPackageCandidates, opts) {
  221. return [path.join(resolverDir, x)];
  222. };
  223. resolve('baz', { packageIterator: exactIterator }, function (err, res, pkg) {
  224. if (err) t.fail(err);
  225. t.equal(res, path.join(resolverDir, 'baz/quux.js'));
  226. t.equal(pkg && pkg.name, 'baz');
  227. });
  228. });
  229. test('incorrect main', function (t) {
  230. t.plan(1);
  231. var resolverDir = path.join(__dirname, 'resolver');
  232. var dir = path.join(resolverDir, 'incorrect_main');
  233. resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) {
  234. if (err) t.fail(err);
  235. t.equal(res, path.join(dir, 'index.js'));
  236. });
  237. });
  238. test('without basedir', function (t) {
  239. t.plan(1);
  240. var dir = path.join(__dirname, 'resolver/without_basedir');
  241. var tester = require(path.join(dir, 'main.js')); // eslint-disable-line global-require
  242. tester(t, function (err, res, pkg) {
  243. if (err) {
  244. t.fail(err);
  245. } else {
  246. t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
  247. }
  248. });
  249. });
  250. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  251. t.plan(2);
  252. var dir = path.join(__dirname, 'resolver');
  253. resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  254. if (err) t.fail(err);
  255. t.equal(res, path.join(dir, 'same_names/foo.js'));
  256. });
  257. resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  258. if (err) t.fail(err);
  259. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  260. });
  261. });
  262. test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
  263. t.plan(2);
  264. var dir = path.join(__dirname, 'resolver');
  265. resolve('./', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
  266. if (err) t.fail(err);
  267. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  268. });
  269. resolve('.', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
  270. if (err) t.fail(err);
  271. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  272. });
  273. });
  274. test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
  275. var testFile = path.basename(__filename);
  276. t.test('sanity check', function (st) {
  277. st.plan(1);
  278. resolve('./' + testFile, function (err, res, pkg) {
  279. if (err) t.fail(err);
  280. st.equal(res, __filename, 'sanity check');
  281. });
  282. });
  283. t.test('with a fake directory', function (st) {
  284. st.plan(4);
  285. resolve('./' + testFile + '/blah', function (err, res, pkg) {
  286. st.ok(err, 'there is an error');
  287. st.notOk(res, 'no result');
  288. st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  289. st.equal(
  290. err && err.message,
  291. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  292. 'can not find nonexistent module'
  293. );
  294. st.end();
  295. });
  296. });
  297. t.end();
  298. });
  299. test('async dot main', function (t) {
  300. var start = new Date();
  301. t.plan(3);
  302. resolve('./resolver/dot_main', function (err, ret) {
  303. t.notOk(err);
  304. t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
  305. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  306. t.end();
  307. });
  308. });
  309. test('async dot slash main', function (t) {
  310. var start = new Date();
  311. t.plan(3);
  312. resolve('./resolver/dot_slash_main', function (err, ret) {
  313. t.notOk(err);
  314. t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
  315. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  316. t.end();
  317. });
  318. });
  319. test('not a directory', function (t) {
  320. t.plan(6);
  321. var path = './foo';
  322. resolve(path, { basedir: __filename }, function (err, res, pkg) {
  323. t.ok(err, 'a non-directory errors');
  324. t.equal(arguments.length, 1);
  325. t.equal(res, undefined);
  326. t.equal(pkg, undefined);
  327. t.equal(err && err.message, 'Cannot find module \'' + path + '\' from \'' + __filename + '\'');
  328. t.equal(err && err.code, 'MODULE_NOT_FOUND');
  329. });
  330. });
  331. test('non-string "main" field in package.json', function (t) {
  332. t.plan(5);
  333. var dir = path.join(__dirname, 'resolver');
  334. resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
  335. t.ok(err, 'errors on non-string main');
  336. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  337. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  338. t.equal(res, undefined, 'res is undefined');
  339. t.equal(pkg, undefined, 'pkg is undefined');
  340. });
  341. });
  342. test('non-string "main" field in package.json', function (t) {
  343. t.plan(5);
  344. var dir = path.join(__dirname, 'resolver');
  345. resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
  346. t.ok(err, 'errors on non-string main');
  347. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  348. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  349. t.equal(res, undefined, 'res is undefined');
  350. t.equal(pkg, undefined, 'pkg is undefined');
  351. });
  352. });
  353. test('browser field in package.json', function (t) {
  354. t.plan(3);
  355. var dir = path.join(__dirname, 'resolver');
  356. resolve(
  357. './browser_field',
  358. {
  359. basedir: dir,
  360. packageFilter: function packageFilter(pkg) {
  361. if (pkg.browser) {
  362. pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
  363. delete pkg.browser; // eslint-disable-line no-param-reassign
  364. }
  365. return pkg;
  366. }
  367. },
  368. function (err, res, pkg) {
  369. if (err) t.fail(err);
  370. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  371. t.equal(pkg && pkg.main, 'b');
  372. t.equal(pkg && pkg.browser, undefined);
  373. }
  374. );
  375. });
  376. test('absolute paths', function (t) {
  377. t.plan(4);
  378. var extensionless = __filename.slice(0, -path.extname(__filename).length);
  379. resolve(__filename, function (err, res) {
  380. t.equal(
  381. res,
  382. __filename,
  383. 'absolute path to this file resolves'
  384. );
  385. });
  386. resolve(extensionless, function (err, res) {
  387. t.equal(
  388. res,
  389. __filename,
  390. 'extensionless absolute path to this file resolves'
  391. );
  392. });
  393. resolve(__filename, { basedir: process.cwd() }, function (err, res) {
  394. t.equal(
  395. res,
  396. __filename,
  397. 'absolute path to this file with a basedir resolves'
  398. );
  399. });
  400. resolve(extensionless, { basedir: process.cwd() }, function (err, res) {
  401. t.equal(
  402. res,
  403. __filename,
  404. 'extensionless absolute path to this file with a basedir resolves'
  405. );
  406. });
  407. });
  408. test('malformed package.json', function (t) {
  409. /* eslint operator-linebreak: ["error", "before"], function-paren-newline: "off" */
  410. t.plan(
  411. (3 * 3) // 3 sets of 3 assertions in the final callback
  412. + 2 // 1 readPackage call with malformed package.json
  413. );
  414. var basedir = path.join(__dirname, 'resolver/malformed_package_json');
  415. var expected = path.join(basedir, 'index.js');
  416. resolve('./index.js', { basedir: basedir }, function (err, res, pkg) {
  417. t.error(err, 'no error');
  418. t.equal(res, expected, 'malformed package.json is silently ignored');
  419. t.equal(pkg, undefined, 'malformed package.json gives an undefined `pkg` argument');
  420. });
  421. resolve(
  422. './index.js',
  423. {
  424. basedir: basedir,
  425. packageFilter: function (pkg, pkgfile, dir) {
  426. t.fail('should not reach here');
  427. }
  428. },
  429. function (err, res, pkg) {
  430. t.error(err, 'with packageFilter: no error');
  431. t.equal(res, expected, 'with packageFilter: malformed package.json is silently ignored');
  432. t.equal(pkg, undefined, 'with packageFilter: malformed package.json gives an undefined `pkg` argument');
  433. }
  434. );
  435. resolve(
  436. './index.js',
  437. {
  438. basedir: basedir,
  439. readPackage: function (readFile, pkgfile, cb) {
  440. t.equal(pkgfile, path.join(basedir, 'package.json'), 'readPackageSync: `pkgfile` is package.json path');
  441. readFile(pkgfile, function (err, result) {
  442. try {
  443. cb(null, JSON.parse(result));
  444. } catch (e) {
  445. t.ok(e instanceof SyntaxError, 'readPackage: malformed package.json parses as a syntax error');
  446. cb(null);
  447. }
  448. });
  449. }
  450. },
  451. function (err, res, pkg) {
  452. t.error(err, 'with readPackage: no error');
  453. t.equal(res, expected, 'with readPackage: malformed package.json is silently ignored');
  454. t.equal(pkg, undefined, 'with readPackage: malformed package.json gives an undefined `pkg` argument');
  455. }
  456. );
  457. });