source: imaps-frontend/node_modules/resolve/test/resolver_sync.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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