wallet-core/node_modules/resolve/test/resolver.js

350 lines
11 KiB
JavaScript
Raw Normal View History

2016-10-10 03:43:44 +02:00
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('async foo', function (t) {
2017-05-28 00:38:50 +02:00
t.plan(10);
var dir = path.join(__dirname, 'resolver');
resolve('./foo', { basedir: dir }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'foo.js'));
t.equal(pkg && pkg.name, 'resolve');
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
resolve('./foo.js', { basedir: dir }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'foo.js'));
t.equal(pkg && pkg.name, 'resolve');
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
resolve('./foo', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'foo.js'));
t.equal(pkg && pkg.main, 'resolver');
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
resolve('./foo.js', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'foo.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg.main, 'resolver');
});
2017-05-28 00:38:50 +02:00
resolve('foo', { basedir: dir }, function (err) {
2016-10-10 03:43:44 +02:00
t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
2017-05-28 00:38:50 +02:00
t.equal(err.code, 'MODULE_NOT_FOUND');
2016-10-10 03:43:44 +02:00
});
});
test('bar', function (t) {
t.plan(6);
2017-05-28 00:38:50 +02:00
var dir = path.join(__dirname, 'resolver');
resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
2017-05-28 00:38:50 +02:00
resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
2017-05-28 00:38:50 +02:00
resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
});
test('baz', function (t) {
t.plan(4);
2017-05-28 00:38:50 +02:00
var dir = path.join(__dirname, 'resolver');
resolve('./baz', { basedir: dir }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'baz/quux.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg.main, 'quux.js');
});
2017-05-28 00:38:50 +02:00
resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'baz/quux.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg.main, 'quux.js');
});
});
test('biz', function (t) {
t.plan(24);
2017-05-28 00:38:50 +02:00
var dir = path.join(__dirname, 'resolver/biz/node_modules');
resolve('./grux', { basedir: dir }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'grux/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
2017-05-28 00:38:50 +02:00
resolve('./grux', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'grux/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg.main, 'biz');
});
2017-05-28 00:38:50 +02:00
resolve('./garply', { basedir: dir }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'garply/lib/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg.main, './lib');
});
2017-05-28 00:38:50 +02:00
resolve('./garply', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'garply/lib/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg.main, './lib');
});
2017-05-28 00:38:50 +02:00
resolve('tiv', { basedir: dir + '/grux' }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'tiv/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
2017-05-28 00:38:50 +02:00
resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'tiv/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
2017-05-28 00:38:50 +02:00
resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'tiv/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
2017-05-28 00:38:50 +02:00
resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'tiv/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
2017-05-28 00:38:50 +02:00
resolve('grux', { basedir: dir + '/tiv' }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'grux/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
2017-05-28 00:38:50 +02:00
resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'grux/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
2017-05-28 00:38:50 +02:00
resolve('garply', { basedir: dir + '/tiv' }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'garply/lib/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg.main, './lib');
});
2017-05-28 00:38:50 +02:00
resolve('garply', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'garply/lib/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg.main, './lib');
});
});
test('quux', function (t) {
t.plan(2);
2017-05-28 00:38:50 +02:00
var dir = path.join(__dirname, 'resolver/quux');
resolve('./foo', { basedir: dir, 'package': { main: 'quux' } }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'foo/index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg.main, 'quux');
});
});
test('normalize', function (t) {
t.plan(2);
2017-05-28 00:38:50 +02:00
var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
resolve('../grux', { basedir: dir }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'index.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
});
test('cup', function (t) {
2017-05-28 00:38:50 +02:00
t.plan(4);
var dir = path.join(__dirname, 'resolver');
resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'cup.coffee'));
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
resolve('./cup.coffee', { basedir: dir }, function (err, res) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'cup.coffee'));
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
resolve('./cup', { basedir: dir, extensions: ['.js'] }, function (err, res) {
2016-10-10 03:43:44 +02:00
t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
2017-05-28 00:38:50 +02:00
t.equal(err.code, 'MODULE_NOT_FOUND');
2016-10-10 03:43:44 +02:00
});
});
test('mug', function (t) {
t.plan(3);
2017-05-28 00:38:50 +02:00
var dir = path.join(__dirname, 'resolver');
resolve('./mug', { basedir: dir }, function (err, res) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'mug.js'));
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
resolve('./mug', { basedir: dir, extensions: ['.coffee', '.js'] }, function (err, res) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, '/mug.coffee'));
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
t.equal(res, path.join(dir, '/mug.js'));
2016-10-10 03:43:44 +02:00
});
});
test('other path', function (t) {
2017-05-28 00:38:50 +02:00
t.plan(6);
var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'bar');
var otherDir = path.join(resolverDir, 'other_path');
resolve('root', { basedir: dir, paths: [otherDir] }, function (err, res) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(resolverDir, 'other_path/root.js'));
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
resolve('lib/other-lib', { basedir: dir, paths: [otherDir] }, function (err, res) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(resolverDir, 'other_path/lib/other-lib.js'));
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
resolve('root', { basedir: dir }, function (err, res) {
2016-10-10 03:43:44 +02:00
t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
2017-05-28 00:38:50 +02:00
t.equal(err.code, 'MODULE_NOT_FOUND');
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
resolve('zzz', { basedir: dir, paths: [otherDir] }, function (err, res) {
2016-10-10 03:43:44 +02:00
t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
2017-05-28 00:38:50 +02:00
t.equal(err.code, 'MODULE_NOT_FOUND');
2016-10-10 03:43:44 +02:00
});
});
test('incorrect main', function (t) {
2017-05-28 00:38:50 +02:00
t.plan(1);
2016-10-10 03:43:44 +02:00
2017-05-28 00:38:50 +02:00
var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'incorrect_main');
2016-10-10 03:43:44 +02:00
2017-05-28 00:38:50 +02:00
resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(dir, 'index.js'));
2016-10-10 03:43:44 +02:00
});
});
test('without basedir', function (t) {
t.plan(1);
2017-05-28 00:38:50 +02:00
var dir = path.join(__dirname, 'resolver/without_basedir');
var tester = require(path.join(dir, 'main.js'));
2016-10-10 03:43:44 +02:00
2017-05-28 00:38:50 +02:00
tester(t, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) {
2017-05-28 00:38:50 +02:00
t.fail(err);
} else {
t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
}
2016-10-10 03:43:44 +02:00
});
});
test('#25: node modules with the same name as node stdlib modules', function (t) {
t.plan(1);
2017-05-28 00:38:50 +02:00
var resolverDir = path.join(__dirname, 'resolver/punycode');
2016-10-10 03:43:44 +02:00
2017-05-28 00:38:50 +02:00
resolve('punycode', { basedir: resolverDir }, function (err, res, pkg) {
2016-10-10 03:43:44 +02:00
if (err) t.fail(err);
2017-05-28 00:38:50 +02:00
t.equal(res, path.join(resolverDir, 'node_modules/punycode/index.js'));
2016-10-10 03:43:44 +02:00
});
});
2017-05-28 00:38:50 +02:00
test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
t.plan(2);
var dir = path.join(__dirname, 'resolver');
resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'same_names/foo.js'));
});
resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
});
});
test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
var testFile = path.basename(__filename);
t.test('sanity check', function (st) {
st.plan(1);
resolve('./' + testFile, function (err, res, pkg) {
if (err) t.fail(err);
st.equal(res, __filename, 'sanity check');
});
});
t.test('with a fake directory', function (st) {
st.plan(4);
resolve('./' + testFile + '/blah', function (err, res, pkg) {
st.ok(err, 'there is an error');
st.notOk(res, 'no result');
st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
st.equal(
err && err.message,
'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
'can not find nonexistent module'
);
st.end();
});
});
t.end();
});
2017-08-14 05:01:11 +02:00
test('async dot main', function (t) {
var start = new Date();
t.plan(3);
resolve('./resolver/dot_main', function (err, ret) {
t.notOk(err);
t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
t.ok(new Date() - start < 50, 'resolve.sync timedout');
t.end();
});
});
test('async dot slash main', function (t) {
var start = new Date();
t.plan(3);
resolve('./resolver/dot_slash_main', function (err, ret) {
t.notOk(err);
t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
t.ok(new Date() - start < 50, 'resolve.sync timedout');
t.end();
});
});