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.

726 lines
20 KiB

2 years ago
  1. var path = require('path');
  2. var fs = require('fs');
  3. var test = require('tape');
  4. var resolve = require('../');
  5. var sync = require('../sync');
  6. var requireResolveSupportsPaths = require.resolve.length > 1
  7. && !(/^v12\.[012]\./).test(process.version); // broken in v12.0-12.2, see https://github.com/nodejs/node/issues/27794
  8. test('`./sync` entry point', function (t) {
  9. t.equal(resolve.sync, sync, '`./sync` entry point is the same as `.sync` on `main`');
  10. t.end();
  11. });
  12. test('foo', function (t) {
  13. var dir = path.join(__dirname, 'resolver');
  14. t.equal(
  15. resolve.sync('./foo', { basedir: dir }),
  16. path.join(dir, 'foo.js'),
  17. './foo'
  18. );
  19. if (requireResolveSupportsPaths) {
  20. t.equal(
  21. resolve.sync('./foo', { basedir: dir }),
  22. require.resolve('./foo', { paths: [dir] }),
  23. './foo: resolve.sync === require.resolve'
  24. );
  25. }
  26. t.equal(
  27. resolve.sync('./foo.js', { basedir: dir }),
  28. path.join(dir, 'foo.js'),
  29. './foo.js'
  30. );
  31. if (requireResolveSupportsPaths) {
  32. t.equal(
  33. resolve.sync('./foo.js', { basedir: dir }),
  34. require.resolve('./foo.js', { paths: [dir] }),
  35. './foo.js: resolve.sync === require.resolve'
  36. );
  37. }
  38. t.equal(
  39. resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
  40. path.join(dir, 'foo.js')
  41. );
  42. t.throws(function () {
  43. resolve.sync('foo', { basedir: dir });
  44. });
  45. // Test that filename is reported as the "from" value when passed.
  46. t.throws(
  47. function () {
  48. resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
  49. },
  50. {
  51. name: 'Error',
  52. message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
  53. }
  54. );
  55. t.end();
  56. });
  57. test('bar', function (t) {
  58. var dir = path.join(__dirname, 'resolver');
  59. var basedir = path.join(dir, 'bar');
  60. t.equal(
  61. resolve.sync('foo', { basedir: basedir }),
  62. path.join(dir, 'bar/node_modules/foo/index.js'),
  63. 'foo in bar'
  64. );
  65. if (requireResolveSupportsPaths) {
  66. t.equal(
  67. resolve.sync('foo', { basedir: basedir }),
  68. require.resolve('foo', { paths: [basedir] }),
  69. 'foo in bar: resolve.sync === require.resolve'
  70. );
  71. }
  72. t.end();
  73. });
  74. test('baz', function (t) {
  75. var dir = path.join(__dirname, 'resolver');
  76. t.equal(
  77. resolve.sync('./baz', { basedir: dir }),
  78. path.join(dir, 'baz/quux.js'),
  79. './baz'
  80. );
  81. if (requireResolveSupportsPaths) {
  82. t.equal(
  83. resolve.sync('./baz', { basedir: dir }),
  84. require.resolve('./baz', { paths: [dir] }),
  85. './baz: resolve.sync === require.resolve'
  86. );
  87. }
  88. t.end();
  89. });
  90. test('biz', function (t) {
  91. var dir = path.join(__dirname, 'resolver/biz/node_modules');
  92. t.equal(
  93. resolve.sync('./grux', { basedir: dir }),
  94. path.join(dir, 'grux/index.js')
  95. );
  96. if (requireResolveSupportsPaths) {
  97. t.equal(
  98. resolve.sync('./grux', { basedir: dir }),
  99. require.resolve('./grux', { paths: [dir] }),
  100. './grux: resolve.sync === require.resolve'
  101. );
  102. }
  103. var tivDir = path.join(dir, 'grux');
  104. t.equal(
  105. resolve.sync('tiv', { basedir: tivDir }),
  106. path.join(dir, 'tiv/index.js')
  107. );
  108. if (requireResolveSupportsPaths) {
  109. t.equal(
  110. resolve.sync('tiv', { basedir: tivDir }),
  111. require.resolve('tiv', { paths: [tivDir] }),
  112. 'tiv: resolve.sync === require.resolve'
  113. );
  114. }
  115. var gruxDir = path.join(dir, 'tiv');
  116. t.equal(
  117. resolve.sync('grux', { basedir: gruxDir }),
  118. path.join(dir, 'grux/index.js')
  119. );
  120. if (requireResolveSupportsPaths) {
  121. t.equal(
  122. resolve.sync('grux', { basedir: gruxDir }),
  123. require.resolve('grux', { paths: [gruxDir] }),
  124. 'grux: resolve.sync === require.resolve'
  125. );
  126. }
  127. t.end();
  128. });
  129. test('normalize', function (t) {
  130. var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
  131. t.equal(
  132. resolve.sync('../grux', { basedir: dir }),
  133. path.join(dir, 'index.js')
  134. );
  135. if (requireResolveSupportsPaths) {
  136. t.equal(
  137. resolve.sync('../grux', { basedir: dir }),
  138. require.resolve('../grux', { paths: [dir] }),
  139. '../grux: resolve.sync === require.resolve'
  140. );
  141. }
  142. t.end();
  143. });
  144. test('cup', function (t) {
  145. var dir = path.join(__dirname, 'resolver');
  146. t.equal(
  147. resolve.sync('./cup', {
  148. basedir: dir,
  149. extensions: ['.js', '.coffee']
  150. }),
  151. path.join(dir, 'cup.coffee'),
  152. './cup -> ./cup.coffee'
  153. );
  154. t.equal(
  155. resolve.sync('./cup.coffee', { basedir: dir }),
  156. path.join(dir, 'cup.coffee'),
  157. './cup.coffee'
  158. );
  159. t.throws(function () {
  160. resolve.sync('./cup', {
  161. basedir: dir,
  162. extensions: ['.js']
  163. });
  164. });
  165. if (requireResolveSupportsPaths) {
  166. t.equal(
  167. resolve.sync('./cup.coffee', { basedir: dir, extensions: ['.js', '.coffee'] }),
  168. require.resolve('./cup.coffee', { paths: [dir] }),
  169. './cup.coffee: resolve.sync === require.resolve'
  170. );
  171. }
  172. t.end();
  173. });
  174. test('mug', function (t) {
  175. var dir = path.join(__dirname, 'resolver');
  176. t.equal(
  177. resolve.sync('./mug', { basedir: dir }),
  178. path.join(dir, 'mug.js'),
  179. './mug -> ./mug.js'
  180. );
  181. if (requireResolveSupportsPaths) {
  182. t.equal(
  183. resolve.sync('./mug', { basedir: dir }),
  184. require.resolve('./mug', { paths: [dir] }),
  185. './mug: resolve.sync === require.resolve'
  186. );
  187. }
  188. t.equal(
  189. resolve.sync('./mug', {
  190. basedir: dir,
  191. extensions: ['.coffee', '.js']
  192. }),
  193. path.join(dir, 'mug.coffee'),
  194. './mug -> ./mug.coffee'
  195. );
  196. t.equal(
  197. resolve.sync('./mug', {
  198. basedir: dir,
  199. extensions: ['.js', '.coffee']
  200. }),
  201. path.join(dir, 'mug.js'),
  202. './mug -> ./mug.js'
  203. );
  204. t.end();
  205. });
  206. test('other path', function (t) {
  207. var resolverDir = path.join(__dirname, 'resolver');
  208. var dir = path.join(resolverDir, 'bar');
  209. var otherDir = path.join(resolverDir, 'other_path');
  210. t.equal(
  211. resolve.sync('root', {
  212. basedir: dir,
  213. paths: [otherDir]
  214. }),
  215. path.join(resolverDir, 'other_path/root.js')
  216. );
  217. t.equal(
  218. resolve.sync('lib/other-lib', {
  219. basedir: dir,
  220. paths: [otherDir]
  221. }),
  222. path.join(resolverDir, 'other_path/lib/other-lib.js')
  223. );
  224. t.throws(function () {
  225. resolve.sync('root', { basedir: dir });
  226. });
  227. t.throws(function () {
  228. resolve.sync('zzz', {
  229. basedir: dir,
  230. paths: [otherDir]
  231. });
  232. });
  233. t.end();
  234. });
  235. test('path iterator', function (t) {
  236. var resolverDir = path.join(__dirname, 'resolver');
  237. var exactIterator = function (x, start, getPackageCandidates, opts) {
  238. return [path.join(resolverDir, x)];
  239. };
  240. t.equal(
  241. resolve.sync('baz', { packageIterator: exactIterator }),
  242. path.join(resolverDir, 'baz/quux.js')
  243. );
  244. t.end();
  245. });
  246. test('incorrect main', function (t) {
  247. var resolverDir = path.join(__dirname, 'resolver');
  248. var dir = path.join(resolverDir, 'incorrect_main');
  249. t.equal(
  250. resolve.sync('./incorrect_main', { basedir: resolverDir }),
  251. path.join(dir, 'index.js')
  252. );
  253. if (requireResolveSupportsPaths) {
  254. t.equal(
  255. resolve.sync('./incorrect_main', { basedir: resolverDir }),
  256. require.resolve('./incorrect_main', { paths: [resolverDir] }),
  257. './incorrect_main: resolve.sync === require.resolve'
  258. );
  259. }
  260. t.end();
  261. });
  262. test('missing index', function (t) {
  263. t.plan(requireResolveSupportsPaths ? 2 : 1);
  264. var resolverDir = path.join(__dirname, 'resolver');
  265. try {
  266. resolve.sync('./missing_index', { basedir: resolverDir });
  267. t.fail('did not fail');
  268. } catch (err) {
  269. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  270. }
  271. if (requireResolveSupportsPaths) {
  272. try {
  273. require.resolve('./missing_index', { basedir: resolverDir });
  274. t.fail('require.resolve did not fail');
  275. } catch (err) {
  276. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  277. }
  278. }
  279. });
  280. test('missing main', function (t) {
  281. var resolverDir = path.join(__dirname, 'resolver');
  282. try {
  283. resolve.sync('./missing_main', { basedir: resolverDir });
  284. t.fail('require.resolve did not fail');
  285. } catch (err) {
  286. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  287. }
  288. if (requireResolveSupportsPaths) {
  289. try {
  290. resolve.sync('./missing_main', { basedir: resolverDir });
  291. t.fail('require.resolve did not fail');
  292. } catch (err) {
  293. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  294. }
  295. }
  296. t.end();
  297. });
  298. test('null main', function (t) {
  299. var resolverDir = path.join(__dirname, 'resolver');
  300. try {
  301. resolve.sync('./null_main', { basedir: resolverDir });
  302. t.fail('require.resolve did not fail');
  303. } catch (err) {
  304. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  305. }
  306. if (requireResolveSupportsPaths) {
  307. try {
  308. resolve.sync('./null_main', { basedir: resolverDir });
  309. t.fail('require.resolve did not fail');
  310. } catch (err) {
  311. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  312. }
  313. }
  314. t.end();
  315. });
  316. test('main: false', function (t) {
  317. var basedir = path.join(__dirname, 'resolver');
  318. var dir = path.join(basedir, 'false_main');
  319. t.equal(
  320. resolve.sync('./false_main', { basedir: basedir }),
  321. path.join(dir, 'index.js'),
  322. '`"main": false`: resolves to `index.js`'
  323. );
  324. if (requireResolveSupportsPaths) {
  325. t.equal(
  326. resolve.sync('./false_main', { basedir: basedir }),
  327. require.resolve('./false_main', { paths: [basedir] }),
  328. '`"main": false`: resolve.sync === require.resolve'
  329. );
  330. }
  331. t.end();
  332. });
  333. var stubStatSync = function stubStatSync(fn) {
  334. var statSync = fs.statSync;
  335. try {
  336. fs.statSync = function () {
  337. throw new EvalError('Unknown Error');
  338. };
  339. return fn();
  340. } finally {
  341. fs.statSync = statSync;
  342. }
  343. };
  344. test('#79 - re-throw non ENOENT errors from stat', function (t) {
  345. var dir = path.join(__dirname, 'resolver');
  346. stubStatSync(function () {
  347. t.throws(function () {
  348. resolve.sync('foo', { basedir: dir });
  349. }, /Unknown Error/);
  350. });
  351. t.end();
  352. });
  353. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  354. var dir = path.join(__dirname, 'resolver');
  355. var basedir = path.join(dir, 'same_names');
  356. t.equal(
  357. resolve.sync('./foo', { basedir: basedir }),
  358. path.join(dir, 'same_names/foo.js')
  359. );
  360. if (requireResolveSupportsPaths) {
  361. t.equal(
  362. resolve.sync('./foo', { basedir: basedir }),
  363. require.resolve('./foo', { paths: [basedir] }),
  364. './foo: resolve.sync === require.resolve'
  365. );
  366. }
  367. t.equal(
  368. resolve.sync('./foo/', { basedir: basedir }),
  369. path.join(dir, 'same_names/foo/index.js')
  370. );
  371. if (requireResolveSupportsPaths) {
  372. t.equal(
  373. resolve.sync('./foo/', { basedir: basedir }),
  374. require.resolve('./foo/', { paths: [basedir] }),
  375. './foo/: resolve.sync === require.resolve'
  376. );
  377. }
  378. t.end();
  379. });
  380. test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
  381. var dir = path.join(__dirname, 'resolver');
  382. var basedir = path.join(dir, 'same_names/foo');
  383. t.equal(
  384. resolve.sync('./', { basedir: basedir }),
  385. path.join(dir, 'same_names/foo/index.js'),
  386. './'
  387. );
  388. if (requireResolveSupportsPaths) {
  389. t.equal(
  390. resolve.sync('./', { basedir: basedir }),
  391. require.resolve('./', { paths: [basedir] }),
  392. './: resolve.sync === require.resolve'
  393. );
  394. }
  395. t.equal(
  396. resolve.sync('.', { basedir: basedir }),
  397. path.join(dir, 'same_names/foo/index.js'),
  398. '.'
  399. );
  400. if (requireResolveSupportsPaths) {
  401. t.equal(
  402. resolve.sync('.', { basedir: basedir }),
  403. require.resolve('.', { paths: [basedir] }),
  404. '.: resolve.sync === require.resolve',
  405. { todo: true }
  406. );
  407. }
  408. t.end();
  409. });
  410. test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
  411. var testFile = path.basename(__filename);
  412. t.test('sanity check', function (st) {
  413. st.equal(
  414. resolve.sync('./' + testFile),
  415. __filename,
  416. 'sanity check'
  417. );
  418. st.equal(
  419. resolve.sync('./' + testFile),
  420. require.resolve('./' + testFile),
  421. 'sanity check: resolve.sync === require.resolve'
  422. );
  423. st.end();
  424. });
  425. t.test('with a fake directory', function (st) {
  426. function run() { return resolve.sync('./' + testFile + '/blah'); }
  427. st.throws(run, 'throws an error');
  428. try {
  429. run();
  430. } catch (e) {
  431. st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  432. st.equal(
  433. e.message,
  434. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  435. 'can not find nonexistent module'
  436. );
  437. }
  438. st.end();
  439. });
  440. t.end();
  441. });
  442. test('sync dot main', function (t) {
  443. var start = new Date();
  444. t.equal(
  445. resolve.sync('./resolver/dot_main'),
  446. path.join(__dirname, 'resolver/dot_main/index.js'),
  447. './resolver/dot_main'
  448. );
  449. t.equal(
  450. resolve.sync('./resolver/dot_main'),
  451. require.resolve('./resolver/dot_main'),
  452. './resolver/dot_main: resolve.sync === require.resolve'
  453. );
  454. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  455. t.end();
  456. });
  457. test('sync dot slash main', function (t) {
  458. var start = new Date();
  459. t.equal(
  460. resolve.sync('./resolver/dot_slash_main'),
  461. path.join(__dirname, 'resolver/dot_slash_main/index.js')
  462. );
  463. t.equal(
  464. resolve.sync('./resolver/dot_slash_main'),
  465. require.resolve('./resolver/dot_slash_main'),
  466. './resolver/dot_slash_main: resolve.sync === require.resolve'
  467. );
  468. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  469. t.end();
  470. });
  471. test('not a directory', function (t) {
  472. var path = './foo';
  473. try {
  474. resolve.sync(path, { basedir: __filename });
  475. t.fail();
  476. } catch (err) {
  477. t.ok(err, 'a non-directory errors');
  478. t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
  479. t.equal(err && err.code, 'MODULE_NOT_FOUND');
  480. }
  481. t.end();
  482. });
  483. test('non-string "main" field in package.json', function (t) {
  484. var dir = path.join(__dirname, 'resolver');
  485. try {
  486. var result = resolve.sync('./invalid_main', { basedir: dir });
  487. t.equal(result, undefined, 'result should not exist');
  488. t.fail('should not get here');
  489. } catch (err) {
  490. t.ok(err, 'errors on non-string main');
  491. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  492. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  493. }
  494. t.end();
  495. });
  496. test('non-string "main" field in package.json', function (t) {
  497. var dir = path.join(__dirname, 'resolver');
  498. try {
  499. var result = resolve.sync('./invalid_main', { basedir: dir });
  500. t.equal(result, undefined, 'result should not exist');
  501. t.fail('should not get here');
  502. } catch (err) {
  503. t.ok(err, 'errors on non-string main');
  504. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  505. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  506. }
  507. t.end();
  508. });
  509. test('browser field in package.json', function (t) {
  510. var dir = path.join(__dirname, 'resolver');
  511. var res = resolve.sync('./browser_field', {
  512. basedir: dir,
  513. packageFilter: function packageFilter(pkg) {
  514. if (pkg.browser) {
  515. pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
  516. delete pkg.browser; // eslint-disable-line no-param-reassign
  517. }
  518. return pkg;
  519. }
  520. });
  521. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  522. t.end();
  523. });
  524. test('absolute paths', function (t) {
  525. var extensionless = __filename.slice(0, -path.extname(__filename).length);
  526. t.equal(
  527. resolve.sync(__filename),
  528. __filename,
  529. 'absolute path to this file resolves'
  530. );
  531. t.equal(
  532. resolve.sync(__filename),
  533. require.resolve(__filename),
  534. 'absolute path to this file: resolve.sync === require.resolve'
  535. );
  536. t.equal(
  537. resolve.sync(extensionless),
  538. __filename,
  539. 'extensionless absolute path to this file resolves'
  540. );
  541. t.equal(
  542. resolve.sync(__filename),
  543. require.resolve(__filename),
  544. 'absolute path to this file: resolve.sync === require.resolve'
  545. );
  546. t.equal(
  547. resolve.sync(__filename, { basedir: process.cwd() }),
  548. __filename,
  549. 'absolute path to this file with a basedir resolves'
  550. );
  551. if (requireResolveSupportsPaths) {
  552. t.equal(
  553. resolve.sync(__filename, { basedir: process.cwd() }),
  554. require.resolve(__filename, { paths: [process.cwd()] }),
  555. 'absolute path to this file + basedir: resolve.sync === require.resolve'
  556. );
  557. }
  558. t.equal(
  559. resolve.sync(extensionless, { basedir: process.cwd() }),
  560. __filename,
  561. 'extensionless absolute path to this file with a basedir resolves'
  562. );
  563. if (requireResolveSupportsPaths) {
  564. t.equal(
  565. resolve.sync(extensionless, { basedir: process.cwd() }),
  566. require.resolve(extensionless, { paths: [process.cwd()] }),
  567. 'extensionless absolute path to this file + basedir: resolve.sync === require.resolve'
  568. );
  569. }
  570. t.end();
  571. });
  572. test('malformed package.json', function (t) {
  573. t.plan(5 + (requireResolveSupportsPaths ? 1 : 0));
  574. var basedir = path.join(__dirname, 'resolver/malformed_package_json');
  575. var expected = path.join(basedir, 'index.js');
  576. t.equal(
  577. resolve.sync('./index.js', { basedir: basedir }),
  578. expected,
  579. 'malformed package.json is silently ignored'
  580. );
  581. if (requireResolveSupportsPaths) {
  582. t.equal(
  583. resolve.sync('./index.js', { basedir: basedir }),
  584. require.resolve('./index.js', { paths: [basedir] }),
  585. 'malformed package.json: resolve.sync === require.resolve'
  586. );
  587. }
  588. var res1 = resolve.sync(
  589. './index.js',
  590. {
  591. basedir: basedir,
  592. packageFilter: function (pkg, pkgfile, dir) {
  593. t.fail('should not reach here');
  594. }
  595. }
  596. );
  597. t.equal(
  598. res1,
  599. expected,
  600. 'with packageFilter: malformed package.json is silently ignored'
  601. );
  602. var res2 = resolve.sync(
  603. './index.js',
  604. {
  605. basedir: basedir,
  606. readPackageSync: function (readFileSync, pkgfile) {
  607. t.equal(pkgfile, path.join(basedir, 'package.json'), 'readPackageSync: `pkgfile` is package.json path');
  608. var result = String(readFileSync(pkgfile));
  609. try {
  610. return JSON.parse(result);
  611. } catch (e) {
  612. t.ok(e instanceof SyntaxError, 'readPackageSync: malformed package.json parses as a syntax error');
  613. }
  614. }
  615. }
  616. );
  617. t.equal(
  618. res2,
  619. expected,
  620. 'with readPackageSync: malformed package.json is silently ignored'
  621. );
  622. });