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