You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

595 lines
20 KiB

2 years ago
  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('missing index', function (t) {
  239. t.plan(2);
  240. var resolverDir = path.join(__dirname, 'resolver');
  241. resolve('./missing_index', { basedir: resolverDir }, function (err, res, pkg) {
  242. t.ok(err instanceof Error);
  243. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  244. });
  245. });
  246. test('missing main', function (t) {
  247. t.plan(1);
  248. var resolverDir = path.join(__dirname, 'resolver');
  249. resolve('./missing_main', { basedir: resolverDir }, function (err, res, pkg) {
  250. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  251. });
  252. });
  253. test('null main', function (t) {
  254. t.plan(1);
  255. var resolverDir = path.join(__dirname, 'resolver');
  256. resolve('./null_main', { basedir: resolverDir }, function (err, res, pkg) {
  257. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  258. });
  259. });
  260. test('main: false', function (t) {
  261. t.plan(2);
  262. var basedir = path.join(__dirname, 'resolver');
  263. var dir = path.join(basedir, 'false_main');
  264. resolve('./false_main', { basedir: basedir }, function (err, res, pkg) {
  265. if (err) t.fail(err);
  266. t.equal(
  267. res,
  268. path.join(dir, 'index.js'),
  269. '`"main": false`: resolves to `index.js`'
  270. );
  271. t.deepEqual(pkg, {
  272. name: 'false_main',
  273. main: false
  274. });
  275. });
  276. });
  277. test('without basedir', function (t) {
  278. t.plan(1);
  279. var dir = path.join(__dirname, 'resolver/without_basedir');
  280. var tester = require(path.join(dir, 'main.js')); // eslint-disable-line global-require
  281. tester(t, function (err, res, pkg) {
  282. if (err) {
  283. t.fail(err);
  284. } else {
  285. t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
  286. }
  287. });
  288. });
  289. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  290. t.plan(2);
  291. var dir = path.join(__dirname, 'resolver');
  292. resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  293. if (err) t.fail(err);
  294. t.equal(res, path.join(dir, 'same_names/foo.js'));
  295. });
  296. resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  297. if (err) t.fail(err);
  298. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  299. });
  300. });
  301. test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
  302. t.plan(2);
  303. var dir = path.join(__dirname, 'resolver');
  304. resolve('./', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
  305. if (err) t.fail(err);
  306. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  307. });
  308. resolve('.', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
  309. if (err) t.fail(err);
  310. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  311. });
  312. });
  313. test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
  314. var testFile = path.basename(__filename);
  315. t.test('sanity check', function (st) {
  316. st.plan(1);
  317. resolve('./' + testFile, function (err, res, pkg) {
  318. if (err) t.fail(err);
  319. st.equal(res, __filename, 'sanity check');
  320. });
  321. });
  322. t.test('with a fake directory', function (st) {
  323. st.plan(4);
  324. resolve('./' + testFile + '/blah', function (err, res, pkg) {
  325. st.ok(err, 'there is an error');
  326. st.notOk(res, 'no result');
  327. st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  328. st.equal(
  329. err && err.message,
  330. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  331. 'can not find nonexistent module'
  332. );
  333. st.end();
  334. });
  335. });
  336. t.end();
  337. });
  338. test('async dot main', function (t) {
  339. var start = new Date();
  340. t.plan(3);
  341. resolve('./resolver/dot_main', function (err, ret) {
  342. t.notOk(err);
  343. t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
  344. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  345. t.end();
  346. });
  347. });
  348. test('async dot slash main', function (t) {
  349. var start = new Date();
  350. t.plan(3);
  351. resolve('./resolver/dot_slash_main', function (err, ret) {
  352. t.notOk(err);
  353. t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
  354. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  355. t.end();
  356. });
  357. });
  358. test('not a directory', function (t) {
  359. t.plan(6);
  360. var path = './foo';
  361. resolve(path, { basedir: __filename }, function (err, res, pkg) {
  362. t.ok(err, 'a non-directory errors');
  363. t.equal(arguments.length, 1);
  364. t.equal(res, undefined);
  365. t.equal(pkg, undefined);
  366. t.equal(err && err.message, 'Cannot find module \'' + path + '\' from \'' + __filename + '\'');
  367. t.equal(err && err.code, 'MODULE_NOT_FOUND');
  368. });
  369. });
  370. test('non-string "main" field in package.json', function (t) {
  371. t.plan(5);
  372. var dir = path.join(__dirname, 'resolver');
  373. resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
  374. t.ok(err, 'errors on non-string main');
  375. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  376. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  377. t.equal(res, undefined, 'res is undefined');
  378. t.equal(pkg, undefined, 'pkg is undefined');
  379. });
  380. });
  381. test('non-string "main" field in package.json', function (t) {
  382. t.plan(5);
  383. var dir = path.join(__dirname, 'resolver');
  384. resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
  385. t.ok(err, 'errors on non-string main');
  386. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  387. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  388. t.equal(res, undefined, 'res is undefined');
  389. t.equal(pkg, undefined, 'pkg is undefined');
  390. });
  391. });
  392. test('browser field in package.json', function (t) {
  393. t.plan(3);
  394. var dir = path.join(__dirname, 'resolver');
  395. resolve(
  396. './browser_field',
  397. {
  398. basedir: dir,
  399. packageFilter: function packageFilter(pkg) {
  400. if (pkg.browser) {
  401. pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
  402. delete pkg.browser; // eslint-disable-line no-param-reassign
  403. }
  404. return pkg;
  405. }
  406. },
  407. function (err, res, pkg) {
  408. if (err) t.fail(err);
  409. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  410. t.equal(pkg && pkg.main, 'b');
  411. t.equal(pkg && pkg.browser, undefined);
  412. }
  413. );
  414. });
  415. test('absolute paths', function (t) {
  416. t.plan(4);
  417. var extensionless = __filename.slice(0, -path.extname(__filename).length);
  418. resolve(__filename, function (err, res) {
  419. t.equal(
  420. res,
  421. __filename,
  422. 'absolute path to this file resolves'
  423. );
  424. });
  425. resolve(extensionless, function (err, res) {
  426. t.equal(
  427. res,
  428. __filename,
  429. 'extensionless absolute path to this file resolves'
  430. );
  431. });
  432. resolve(__filename, { basedir: process.cwd() }, function (err, res) {
  433. t.equal(
  434. res,
  435. __filename,
  436. 'absolute path to this file with a basedir resolves'
  437. );
  438. });
  439. resolve(extensionless, { basedir: process.cwd() }, function (err, res) {
  440. t.equal(
  441. res,
  442. __filename,
  443. 'extensionless absolute path to this file with a basedir resolves'
  444. );
  445. });
  446. });
  447. test('malformed package.json', function (t) {
  448. /* eslint operator-linebreak: ["error", "before"], function-paren-newline: "off" */
  449. t.plan(
  450. (3 * 3) // 3 sets of 3 assertions in the final callback
  451. + 2 // 1 readPackage call with malformed package.json
  452. );
  453. var basedir = path.join(__dirname, 'resolver/malformed_package_json');
  454. var expected = path.join(basedir, 'index.js');
  455. resolve('./index.js', { basedir: basedir }, function (err, res, pkg) {
  456. t.error(err, 'no error');
  457. t.equal(res, expected, 'malformed package.json is silently ignored');
  458. t.equal(pkg, undefined, 'malformed package.json gives an undefined `pkg` argument');
  459. });
  460. resolve(
  461. './index.js',
  462. {
  463. basedir: basedir,
  464. packageFilter: function (pkg, pkgfile, dir) {
  465. t.fail('should not reach here');
  466. }
  467. },
  468. function (err, res, pkg) {
  469. t.error(err, 'with packageFilter: no error');
  470. t.equal(res, expected, 'with packageFilter: malformed package.json is silently ignored');
  471. t.equal(pkg, undefined, 'with packageFilter: malformed package.json gives an undefined `pkg` argument');
  472. }
  473. );
  474. resolve(
  475. './index.js',
  476. {
  477. basedir: basedir,
  478. readPackage: function (readFile, pkgfile, cb) {
  479. t.equal(pkgfile, path.join(basedir, 'package.json'), 'readPackageSync: `pkgfile` is package.json path');
  480. readFile(pkgfile, function (err, result) {
  481. try {
  482. cb(null, JSON.parse(result));
  483. } catch (e) {
  484. t.ok(e instanceof SyntaxError, 'readPackage: malformed package.json parses as a syntax error');
  485. cb(null);
  486. }
  487. });
  488. }
  489. },
  490. function (err, res, pkg) {
  491. t.error(err, 'with readPackage: no error');
  492. t.equal(res, expected, 'with readPackage: malformed package.json is silently ignored');
  493. t.equal(pkg, undefined, 'with readPackage: malformed package.json gives an undefined `pkg` argument');
  494. }
  495. );
  496. });