123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- var path = require('path');
- var test = require('tape');
- var resolve = require('../');
- test('async foo', function (t) {
- t.plan(9);
- var dir = __dirname + '/resolver';
-
- resolve('./foo', { basedir : dir }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/foo.js');
- t.equal(pkg.name, 'resolve');
- });
-
- resolve('./foo.js', { basedir : dir }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/foo.js');
- t.equal(pkg.name, 'resolve');
- });
-
- resolve('./foo', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/foo.js');
- t.equal(pkg.main, 'resolver');
- });
-
- resolve('./foo.js', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/foo.js');
- t.equal(pkg.main, 'resolver');
- });
-
- resolve('foo', { basedir : dir }, function (err) {
- t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
- });
- });
- test('bar', function (t) {
- t.plan(6);
- var dir = __dirname + '/resolver';
-
- resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/bar/node_modules/foo/index.js');
- t.equal(pkg, undefined);
- });
-
- resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/bar/node_modules/foo/index.js');
- t.equal(pkg, undefined);
- });
-
- resolve('foo', { basedir : dir + '/bar', package: { main: 'bar' } }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/bar/node_modules/foo/index.js');
- t.equal(pkg, undefined);
- });
- });
- test('baz', function (t) {
- t.plan(4);
- var dir = __dirname + '/resolver';
-
- resolve('./baz', { basedir : dir }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/baz/quux.js');
- t.equal(pkg.main, 'quux.js');
- });
-
- resolve('./baz', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/baz/quux.js');
- t.equal(pkg.main, 'quux.js');
- });
- });
- test('biz', function (t) {
- t.plan(24);
- var dir = __dirname + '/resolver/biz/node_modules';
-
- resolve('./grux', { basedir : dir }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/grux/index.js');
- t.equal(pkg, undefined);
- });
-
- resolve('./grux', { basedir : dir, package: { main: 'biz' } }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/grux/index.js');
- t.equal(pkg.main, 'biz');
- });
-
- resolve('./garply', { basedir : dir }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/garply/lib/index.js');
- t.equal(pkg.main, './lib');
- });
-
- resolve('./garply', { basedir : dir, package: { main: 'biz' } }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/garply/lib/index.js');
- t.equal(pkg.main, './lib');
- });
-
- resolve('tiv', { basedir : dir + '/grux' }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/tiv/index.js');
- t.equal(pkg, undefined);
- });
-
- resolve('tiv', { basedir : dir + '/grux', package: { main: 'grux' } }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/tiv/index.js');
- t.equal(pkg, undefined);
- });
-
- resolve('tiv', { basedir : dir + '/garply' }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/tiv/index.js');
- t.equal(pkg, undefined);
- });
-
- resolve('tiv', { basedir : dir + '/garply', package: { main: './lib' } }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/tiv/index.js');
- t.equal(pkg, undefined);
- });
-
- resolve('grux', { basedir : dir + '/tiv' }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/grux/index.js');
- t.equal(pkg, undefined);
- });
-
- resolve('grux', { basedir : dir + '/tiv', package: { main: 'tiv' } }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/grux/index.js');
- t.equal(pkg, undefined);
- });
-
- resolve('garply', { basedir : dir + '/tiv' }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/garply/lib/index.js');
- t.equal(pkg.main, './lib');
- });
-
- resolve('garply', { basedir : dir + '/tiv', package: { main: 'tiv' } }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/garply/lib/index.js');
- t.equal(pkg.main, './lib');
- });
- });
- test('quux', function (t) {
- t.plan(2);
- var dir = __dirname + '/resolver/quux';
-
- resolve('./foo', { basedir : dir, package: { main: 'quux' } }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/foo/index.js');
- t.equal(pkg.main, 'quux');
- });
- });
- test('normalize', function (t) {
- t.plan(2);
- var dir = __dirname + '/resolver/biz/node_modules/grux';
-
- resolve('../grux', { basedir : dir }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/index.js');
- t.equal(pkg, undefined);
- });
- });
- test('cup', function (t) {
- t.plan(3);
- var dir = __dirname + '/resolver';
-
- resolve('./cup', { basedir : dir, extensions : [ '.js', '.coffee' ] },
- function (err, res) {
- if (err) t.fail(err);
- t.equal(res, dir + '/cup.coffee');
- });
-
- resolve('./cup.coffee', { basedir : dir }, function (err, res) {
- if (err) t.fail(err);
- t.equal(res, dir + '/cup.coffee');
- });
-
- resolve('./cup', { basedir : dir, extensions : [ '.js' ] },
- function (err, res) {
- t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
- });
- });
- test('mug', function (t) {
- t.plan(3);
- var dir = __dirname + '/resolver';
-
- resolve('./mug', { basedir : dir }, function (err, res) {
- if (err) t.fail(err);
- t.equal(res, dir + '/mug.js');
- });
-
- resolve('./mug', { basedir : dir, extensions : [ '.coffee', '.js' ] },
- function (err, res) {
- if (err) t.fail(err);
- t.equal(res, dir + '/mug.coffee');
- });
-
- resolve('./mug', { basedir : dir, extensions : [ '.js', '.coffee' ] },
- function (err, res) {
- t.equal(res, dir + '/mug.js');
- });
- });
- test('other path', function (t) {
- t.plan(4);
- var resolverDir = __dirname + '/resolver';
- var dir = resolverDir + '/bar';
- var otherDir = resolverDir + '/other_path';
-
- resolve('root', { basedir : dir, paths: [otherDir] }, function (err, res) {
- if (err) t.fail(err);
- t.equal(res, resolverDir + '/other_path/root.js');
- });
-
- resolve('lib/other-lib', { basedir : dir, paths: [otherDir] },
- function (err, res) {
- if (err) t.fail(err);
- t.equal(res, resolverDir + '/other_path/lib/other-lib.js');
- });
-
- resolve('root', { basedir : dir, }, function (err, res) {
- t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
- });
-
- resolve('zzz', { basedir : dir, paths: [otherDir] }, function (err, res) {
- t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
- });
- });
- test('incorrect main', function (t) {
- t.plan(1)
- var resolverDir = __dirname + '/resolver';
- var dir = resolverDir + '/incorrect_main';
- resolve('./incorrect_main', { basedir : resolverDir }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, dir + '/index.js');
- });
- });
- test('without basedir', function (t) {
- t.plan(1);
- var dir = __dirname + '/resolver/without_basedir';
- var tester = require(dir + '/main.js');
- tester(t, function (err, res, pkg){
- if (err) {
- t.fail(err);
- } else {
- t.equal(res, dir + '/node_modules/mymodule.js');
- }
- });
- });
- test('#25: node modules with the same name as node stdlib modules', function (t) {
- t.plan(1);
- var resolverDir = __dirname + '/resolver/punycode';
- resolve('punycode', { basedir : resolverDir }, function (err, res, pkg) {
- if (err) t.fail(err);
- t.equal(res, resolverDir + '/node_modules/punycode/index.js');
- });
- });
|