resolver_sync.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. test('`./sync` entry point', function (t) {
  7. t.equal(resolve.sync, sync, '`./sync` entry point is the same as `.sync` on `main`');
  8. t.end();
  9. });
  10. test('foo', function (t) {
  11. var dir = path.join(__dirname, 'resolver');
  12. t.equal(
  13. resolve.sync('./foo', { basedir: dir }),
  14. path.join(dir, 'foo.js')
  15. );
  16. t.equal(
  17. resolve.sync('./foo.js', { basedir: dir }),
  18. path.join(dir, 'foo.js')
  19. );
  20. t.equal(
  21. resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
  22. path.join(dir, 'foo.js')
  23. );
  24. t.throws(function () {
  25. resolve.sync('foo', { basedir: dir });
  26. });
  27. // Test that filename is reported as the "from" value when passed.
  28. t.throws(
  29. function () {
  30. resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
  31. },
  32. {
  33. name: 'Error',
  34. message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
  35. }
  36. );
  37. t.end();
  38. });
  39. test('bar', function (t) {
  40. var dir = path.join(__dirname, 'resolver');
  41. t.equal(
  42. resolve.sync('foo', { basedir: path.join(dir, 'bar') }),
  43. path.join(dir, 'bar/node_modules/foo/index.js')
  44. );
  45. t.end();
  46. });
  47. test('baz', function (t) {
  48. var dir = path.join(__dirname, 'resolver');
  49. t.equal(
  50. resolve.sync('./baz', { basedir: dir }),
  51. path.join(dir, 'baz/quux.js')
  52. );
  53. t.end();
  54. });
  55. test('biz', function (t) {
  56. var dir = path.join(__dirname, 'resolver/biz/node_modules');
  57. t.equal(
  58. resolve.sync('./grux', { basedir: dir }),
  59. path.join(dir, 'grux/index.js')
  60. );
  61. t.equal(
  62. resolve.sync('tiv', { basedir: path.join(dir, 'grux') }),
  63. path.join(dir, 'tiv/index.js')
  64. );
  65. t.equal(
  66. resolve.sync('grux', { basedir: path.join(dir, 'tiv') }),
  67. path.join(dir, 'grux/index.js')
  68. );
  69. t.end();
  70. });
  71. test('normalize', function (t) {
  72. var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
  73. t.equal(
  74. resolve.sync('../grux', { basedir: dir }),
  75. path.join(dir, 'index.js')
  76. );
  77. t.end();
  78. });
  79. test('cup', function (t) {
  80. var dir = path.join(__dirname, 'resolver');
  81. t.equal(
  82. resolve.sync('./cup', {
  83. basedir: dir,
  84. extensions: ['.js', '.coffee']
  85. }),
  86. path.join(dir, 'cup.coffee')
  87. );
  88. t.equal(
  89. resolve.sync('./cup.coffee', { basedir: dir }),
  90. path.join(dir, 'cup.coffee')
  91. );
  92. t.throws(function () {
  93. resolve.sync('./cup', {
  94. basedir: dir,
  95. extensions: ['.js']
  96. });
  97. });
  98. t.end();
  99. });
  100. test('mug', function (t) {
  101. var dir = path.join(__dirname, 'resolver');
  102. t.equal(
  103. resolve.sync('./mug', { basedir: dir }),
  104. path.join(dir, 'mug.js')
  105. );
  106. t.equal(
  107. resolve.sync('./mug', {
  108. basedir: dir,
  109. extensions: ['.coffee', '.js']
  110. }),
  111. path.join(dir, 'mug.coffee')
  112. );
  113. t.equal(
  114. resolve.sync('./mug', {
  115. basedir: dir,
  116. extensions: ['.js', '.coffee']
  117. }),
  118. path.join(dir, 'mug.js')
  119. );
  120. t.end();
  121. });
  122. test('other path', function (t) {
  123. var resolverDir = path.join(__dirname, 'resolver');
  124. var dir = path.join(resolverDir, 'bar');
  125. var otherDir = path.join(resolverDir, 'other_path');
  126. t.equal(
  127. resolve.sync('root', {
  128. basedir: dir,
  129. paths: [otherDir]
  130. }),
  131. path.join(resolverDir, 'other_path/root.js')
  132. );
  133. t.equal(
  134. resolve.sync('lib/other-lib', {
  135. basedir: dir,
  136. paths: [otherDir]
  137. }),
  138. path.join(resolverDir, 'other_path/lib/other-lib.js')
  139. );
  140. t.throws(function () {
  141. resolve.sync('root', { basedir: dir });
  142. });
  143. t.throws(function () {
  144. resolve.sync('zzz', {
  145. basedir: dir,
  146. paths: [otherDir]
  147. });
  148. });
  149. t.end();
  150. });
  151. test('path iterator', function (t) {
  152. var resolverDir = path.join(__dirname, 'resolver');
  153. var exactIterator = function (x, start, getPackageCandidates, opts) {
  154. return [path.join(resolverDir, x)];
  155. };
  156. t.equal(
  157. resolve.sync('baz', { packageIterator: exactIterator }),
  158. path.join(resolverDir, 'baz/quux.js')
  159. );
  160. t.end();
  161. });
  162. test('incorrect main', function (t) {
  163. var resolverDir = path.join(__dirname, 'resolver');
  164. var dir = path.join(resolverDir, 'incorrect_main');
  165. t.equal(
  166. resolve.sync('./incorrect_main', { basedir: resolverDir }),
  167. path.join(dir, 'index.js')
  168. );
  169. t.end();
  170. });
  171. var stubStatSync = function stubStatSync(fn) {
  172. var statSync = fs.statSync;
  173. try {
  174. fs.statSync = function () {
  175. throw new EvalError('Unknown Error');
  176. };
  177. return fn();
  178. } finally {
  179. fs.statSync = statSync;
  180. }
  181. };
  182. test('#79 - re-throw non ENOENT errors from stat', function (t) {
  183. var dir = path.join(__dirname, 'resolver');
  184. stubStatSync(function () {
  185. t.throws(function () {
  186. resolve.sync('foo', { basedir: dir });
  187. }, /Unknown Error/);
  188. });
  189. t.end();
  190. });
  191. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  192. var dir = path.join(__dirname, 'resolver');
  193. t.equal(
  194. resolve.sync('./foo', { basedir: path.join(dir, 'same_names') }),
  195. path.join(dir, 'same_names/foo.js')
  196. );
  197. t.equal(
  198. resolve.sync('./foo/', { basedir: path.join(dir, 'same_names') }),
  199. path.join(dir, 'same_names/foo/index.js')
  200. );
  201. t.end();
  202. });
  203. test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
  204. var dir = path.join(__dirname, 'resolver');
  205. t.equal(
  206. resolve.sync('./', { basedir: path.join(dir, 'same_names/foo') }),
  207. path.join(dir, 'same_names/foo/index.js')
  208. );
  209. t.equal(
  210. resolve.sync('.', { basedir: path.join(dir, 'same_names/foo') }),
  211. path.join(dir, 'same_names/foo/index.js')
  212. );
  213. t.end();
  214. });
  215. test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
  216. var testFile = path.basename(__filename);
  217. t.test('sanity check', function (st) {
  218. st.equal(
  219. resolve.sync('./' + testFile),
  220. __filename,
  221. 'sanity check'
  222. );
  223. st.end();
  224. });
  225. t.test('with a fake directory', function (st) {
  226. function run() { return resolve.sync('./' + testFile + '/blah'); }
  227. st.throws(run, 'throws an error');
  228. try {
  229. run();
  230. } catch (e) {
  231. st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  232. st.equal(
  233. e.message,
  234. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  235. 'can not find nonexistent module'
  236. );
  237. }
  238. st.end();
  239. });
  240. t.end();
  241. });
  242. test('sync dot main', function (t) {
  243. var start = new Date();
  244. t.equal(resolve.sync('./resolver/dot_main'), path.join(__dirname, 'resolver/dot_main/index.js'));
  245. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  246. t.end();
  247. });
  248. test('sync dot slash main', function (t) {
  249. var start = new Date();
  250. t.equal(resolve.sync('./resolver/dot_slash_main'), path.join(__dirname, 'resolver/dot_slash_main/index.js'));
  251. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  252. t.end();
  253. });
  254. test('not a directory', function (t) {
  255. var path = './foo';
  256. try {
  257. resolve.sync(path, { basedir: __filename });
  258. t.fail();
  259. } catch (err) {
  260. t.ok(err, 'a non-directory errors');
  261. t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
  262. t.equal(err && err.code, 'MODULE_NOT_FOUND');
  263. }
  264. t.end();
  265. });
  266. test('non-string "main" field in package.json', function (t) {
  267. var dir = path.join(__dirname, 'resolver');
  268. try {
  269. var result = resolve.sync('./invalid_main', { basedir: dir });
  270. t.equal(result, undefined, 'result should not exist');
  271. t.fail('should not get here');
  272. } catch (err) {
  273. t.ok(err, 'errors on non-string main');
  274. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  275. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  276. }
  277. t.end();
  278. });
  279. test('non-string "main" field in package.json', function (t) {
  280. var dir = path.join(__dirname, 'resolver');
  281. try {
  282. var result = resolve.sync('./invalid_main', { basedir: dir });
  283. t.equal(result, undefined, 'result should not exist');
  284. t.fail('should not get here');
  285. } catch (err) {
  286. t.ok(err, 'errors on non-string main');
  287. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  288. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  289. }
  290. t.end();
  291. });
  292. test('browser field in package.json', function (t) {
  293. var dir = path.join(__dirname, 'resolver');
  294. var res = resolve.sync('./browser_field', {
  295. basedir: dir,
  296. packageFilter: function packageFilter(pkg) {
  297. if (pkg.browser) {
  298. pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
  299. delete pkg.browser; // eslint-disable-line no-param-reassign
  300. }
  301. return pkg;
  302. }
  303. });
  304. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  305. t.end();
  306. });
  307. test('absolute paths', function (t) {
  308. var extensionless = __filename.slice(0, -path.extname(__filename).length);
  309. t.equal(
  310. resolve.sync(__filename),
  311. __filename,
  312. 'absolute path to this file resolves'
  313. );
  314. t.equal(
  315. resolve.sync(extensionless),
  316. __filename,
  317. 'extensionless absolute path to this file resolves'
  318. );
  319. t.equal(
  320. resolve.sync(__filename, { basedir: process.cwd() }),
  321. __filename,
  322. 'absolute path to this file with a basedir resolves'
  323. );
  324. t.equal(
  325. resolve.sync(extensionless, { basedir: process.cwd() }),
  326. __filename,
  327. 'extensionless absolute path to this file with a basedir resolves'
  328. );
  329. t.end();
  330. });
  331. test('malformed package.json', function (t) {
  332. t.plan(5);
  333. var basedir = path.join(__dirname, 'resolver/malformed_package_json');
  334. var expected = path.join(basedir, 'index.js');
  335. t.equal(
  336. resolve.sync('./index.js', { basedir: basedir }),
  337. expected,
  338. 'malformed package.json is silently ignored'
  339. );
  340. var res1 = resolve.sync(
  341. './index.js',
  342. {
  343. basedir: basedir,
  344. packageFilter: function (pkg, pkgfile, dir) {
  345. t.fail('should not reach here');
  346. }
  347. }
  348. );
  349. t.equal(
  350. res1,
  351. expected,
  352. 'with packageFilter: malformed package.json is silently ignored'
  353. );
  354. var res2 = resolve.sync(
  355. './index.js',
  356. {
  357. basedir: basedir,
  358. readPackageSync: function (readFileSync, pkgfile) {
  359. t.equal(pkgfile, path.join(basedir, 'package.json'), 'readPackageSync: `pkgfile` is package.json path');
  360. var result = String(readFileSync(pkgfile));
  361. try {
  362. return JSON.parse(result);
  363. } catch (e) {
  364. t.ok(e instanceof SyntaxError, 'readPackageSync: malformed package.json parses as a syntax error');
  365. }
  366. }
  367. }
  368. );
  369. t.equal(
  370. res2,
  371. expected,
  372. 'with readPackageSync: malformed package.json is silently ignored'
  373. );
  374. });