2017-05-28 00:38:50 +02:00
|
|
|
var path = require('path');
|
2016-10-10 03:43:44 +02:00
|
|
|
var test = require('tape');
|
|
|
|
var resolve = require('../');
|
|
|
|
|
|
|
|
test('foo', function (t) {
|
2017-05-28 00:38:50 +02:00
|
|
|
var dir = path.join(__dirname, 'resolver');
|
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('./foo', { basedir: dir }),
|
|
|
|
path.join(dir, 'foo.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('./foo.js', { basedir: dir }),
|
|
|
|
path.join(dir, 'foo.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.throws(function () {
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('foo', { basedir: dir });
|
2016-10-10 03:43:44 +02:00
|
|
|
});
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('bar', function (t) {
|
2017-05-28 00:38:50 +02:00
|
|
|
var dir = path.join(__dirname, 'resolver');
|
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('foo', { basedir: path.join(dir, 'bar') }),
|
|
|
|
path.join(dir, 'bar/node_modules/foo/index.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('baz', function (t) {
|
2017-05-28 00:38:50 +02:00
|
|
|
var dir = path.join(__dirname, 'resolver');
|
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('./baz', { basedir: dir }),
|
|
|
|
path.join(dir, 'baz/quux.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('biz', function (t) {
|
2017-05-28 00:38:50 +02:00
|
|
|
var dir = path.join(__dirname, 'resolver/biz/node_modules');
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('./grux', { basedir: dir }),
|
|
|
|
path.join(dir, 'grux/index.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('tiv', { basedir: path.join(dir, 'grux') }),
|
|
|
|
path.join(dir, 'tiv/index.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('grux', { basedir: path.join(dir, 'tiv') }),
|
|
|
|
path.join(dir, 'grux/index.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('normalize', function (t) {
|
2017-05-28 00:38:50 +02:00
|
|
|
var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('../grux', { basedir: dir }),
|
|
|
|
path.join(dir, 'index.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('cup', function (t) {
|
2017-05-28 00:38:50 +02:00
|
|
|
var dir = path.join(__dirname, 'resolver');
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
|
|
|
resolve.sync('./cup', {
|
2017-05-28 00:38:50 +02:00
|
|
|
basedir: dir,
|
|
|
|
extensions: ['.js', '.coffee']
|
2016-10-10 03:43:44 +02:00
|
|
|
}),
|
2017-05-28 00:38:50 +02:00
|
|
|
path.join(dir, 'cup.coffee')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('./cup.coffee', { basedir: dir }),
|
|
|
|
path.join(dir, 'cup.coffee')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.throws(function () {
|
|
|
|
resolve.sync('./cup', {
|
2017-05-28 00:38:50 +02:00
|
|
|
basedir: dir,
|
|
|
|
extensions: ['.js']
|
|
|
|
});
|
2016-10-10 03:43:44 +02:00
|
|
|
});
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('mug', function (t) {
|
2017-05-28 00:38:50 +02:00
|
|
|
var dir = path.join(__dirname, 'resolver');
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('./mug', { basedir: dir }),
|
|
|
|
path.join(dir, 'mug.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
|
|
|
resolve.sync('./mug', {
|
2017-05-28 00:38:50 +02:00
|
|
|
basedir: dir,
|
|
|
|
extensions: ['.coffee', '.js']
|
2016-10-10 03:43:44 +02:00
|
|
|
}),
|
2017-05-28 00:38:50 +02:00
|
|
|
path.join(dir, 'mug.coffee')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
|
|
|
resolve.sync('./mug', {
|
2017-05-28 00:38:50 +02:00
|
|
|
basedir: dir,
|
|
|
|
extensions: ['.js', '.coffee']
|
2016-10-10 03:43:44 +02:00
|
|
|
}),
|
2017-05-28 00:38:50 +02:00
|
|
|
path.join(dir, 'mug.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('other path', function (t) {
|
2017-05-28 00:38:50 +02:00
|
|
|
var resolverDir = path.join(__dirname, 'resolver');
|
|
|
|
var dir = path.join(resolverDir, 'bar');
|
|
|
|
var otherDir = path.join(resolverDir, 'other_path');
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
t.equal(
|
|
|
|
resolve.sync('root', {
|
2017-05-28 00:38:50 +02:00
|
|
|
basedir: dir,
|
|
|
|
paths: [otherDir]
|
|
|
|
}),
|
|
|
|
path.join(resolverDir, 'other_path/root.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
|
|
|
resolve.sync('lib/other-lib', {
|
2017-05-28 00:38:50 +02:00
|
|
|
basedir: dir,
|
|
|
|
paths: [otherDir]
|
|
|
|
}),
|
|
|
|
path.join(resolverDir, 'other_path/lib/other-lib.js')
|
2016-10-10 03:43:44 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
t.throws(function () {
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('root', { basedir: dir });
|
2016-10-10 03:43:44 +02:00
|
|
|
});
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.throws(function () {
|
|
|
|
resolve.sync('zzz', {
|
2017-05-28 00:38:50 +02:00
|
|
|
basedir: dir,
|
|
|
|
paths: [otherDir]
|
|
|
|
});
|
2016-10-10 03:43:44 +02:00
|
|
|
});
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('incorrect main', function (t) {
|
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
|
|
|
|
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('./incorrect_main', { basedir: resolverDir }),
|
|
|
|
path.join(dir, 'index.js')
|
|
|
|
);
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-05-28 00:38:50 +02:00
|
|
|
t.end();
|
2016-10-10 03:43:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('#25: node modules with the same name as node stdlib modules', function (t) {
|
2017-05-28 00:38:50 +02:00
|
|
|
var resolverDir = path.join(__dirname, 'resolver/punycode');
|
|
|
|
|
|
|
|
t.equal(
|
|
|
|
resolve.sync('punycode', { basedir: resolverDir }),
|
|
|
|
path.join(resolverDir, 'node_modules/punycode/index.js')
|
|
|
|
);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
var stubStatSync = function stubStatSync(fn) {
|
|
|
|
var fs = require('fs');
|
|
|
|
var statSync = fs.statSync;
|
|
|
|
try {
|
|
|
|
fs.statSync = function () {
|
|
|
|
throw new EvalError('Unknown Error');
|
|
|
|
};
|
|
|
|
return fn();
|
|
|
|
} finally {
|
|
|
|
fs.statSync = statSync;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
test('#79 - re-throw non ENOENT errors from stat', function (t) {
|
|
|
|
var dir = path.join(__dirname, 'resolver');
|
|
|
|
|
|
|
|
stubStatSync(function () {
|
|
|
|
t.throws(function () {
|
|
|
|
resolve.sync('foo', { basedir: dir });
|
|
|
|
}, /Unknown Error/);
|
|
|
|
});
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-05-28 00:38:50 +02:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
|
|
|
|
var dir = path.join(__dirname, 'resolver');
|
|
|
|
|
|
|
|
t.equal(
|
|
|
|
resolve.sync('./foo', { basedir: path.join(dir, 'same_names') }),
|
|
|
|
path.join(dir, 'same_names/foo.js')
|
|
|
|
);
|
2016-10-10 03:43:44 +02:00
|
|
|
t.equal(
|
2017-05-28 00:38:50 +02:00
|
|
|
resolve.sync('./foo/', { basedir: path.join(dir, 'same_names') }),
|
|
|
|
path.join(dir, 'same_names/foo/index.js')
|
|
|
|
);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('sync: #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.equal(
|
|
|
|
resolve.sync('./' + testFile),
|
|
|
|
__filename,
|
|
|
|
'sanity check'
|
|
|
|
);
|
|
|
|
st.end();
|
|
|
|
});
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-05-28 00:38:50 +02:00
|
|
|
t.test('with a fake directory', function (st) {
|
|
|
|
function run() { return resolve.sync('./' + testFile + '/blah'); }
|
|
|
|
|
|
|
|
st.throws(run, 'throws an error');
|
|
|
|
|
|
|
|
try {
|
|
|
|
run();
|
|
|
|
} catch (e) {
|
|
|
|
st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
|
|
|
|
st.equal(
|
|
|
|
e.message,
|
|
|
|
'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
|
|
|
|
'can not find nonexistent module'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
st.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
t.end();
|
2016-10-10 03:43:44 +02:00
|
|
|
});
|