123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730 |
- var path = require('path');
- var fs = require('fs');
- var test = require('tape');
- var resolve = require('../');
- var sync = require('../sync');
- var requireResolveSupportsPaths = require.resolve.length > 1
- && !(/^v12\.[012]\./).test(process.version); // broken in v12.0-12.2, see https://github.com/nodejs/node/issues/27794
- var requireResolveDefaultPathsBroken = (/^v8\.9\.|^v9\.[01]\.0|^v9\.2\./).test(process.version);
- // broken in node v8.9.x, v9.0, v9.1, v9.2.x. see https://github.com/nodejs/node/pull/17113
- test('`./sync` entry point', function (t) {
- t.equal(resolve.sync, sync, '`./sync` entry point is the same as `.sync` on `main`');
- t.end();
- });
- test('foo', function (t) {
- var dir = path.join(__dirname, 'resolver');
- t.equal(
- resolve.sync('./foo', { basedir: dir }),
- path.join(dir, 'foo.js'),
- './foo'
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./foo', { basedir: dir }),
- require.resolve('./foo', { paths: [dir] }),
- './foo: resolve.sync === require.resolve'
- );
- }
- t.equal(
- resolve.sync('./foo.js', { basedir: dir }),
- path.join(dir, 'foo.js'),
- './foo.js'
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./foo.js', { basedir: dir }),
- require.resolve('./foo.js', { paths: [dir] }),
- './foo.js: resolve.sync === require.resolve'
- );
- }
- t.equal(
- resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
- path.join(dir, 'foo.js')
- );
- t.throws(function () {
- resolve.sync('foo', { basedir: dir });
- });
- // Test that filename is reported as the "from" value when passed.
- t.throws(
- function () {
- resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
- },
- {
- name: 'Error',
- message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
- }
- );
- t.end();
- });
- test('bar', function (t) {
- var dir = path.join(__dirname, 'resolver');
- var basedir = path.join(dir, 'bar');
- t.equal(
- resolve.sync('foo', { basedir: basedir }),
- path.join(dir, 'bar/node_modules/foo/index.js'),
- 'foo in bar'
- );
- if (!requireResolveDefaultPathsBroken && requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('foo', { basedir: basedir }),
- require.resolve('foo', { paths: [basedir] }),
- 'foo in bar: resolve.sync === require.resolve'
- );
- }
- t.end();
- });
- test('baz', function (t) {
- var dir = path.join(__dirname, 'resolver');
- t.equal(
- resolve.sync('./baz', { basedir: dir }),
- path.join(dir, 'baz/quux.js'),
- './baz'
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./baz', { basedir: dir }),
- require.resolve('./baz', { paths: [dir] }),
- './baz: resolve.sync === require.resolve'
- );
- }
- t.end();
- });
- test('biz', function (t) {
- var dir = path.join(__dirname, 'resolver/biz/node_modules');
- t.equal(
- resolve.sync('./grux', { basedir: dir }),
- path.join(dir, 'grux/index.js')
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./grux', { basedir: dir }),
- require.resolve('./grux', { paths: [dir] }),
- './grux: resolve.sync === require.resolve'
- );
- }
- var tivDir = path.join(dir, 'grux');
- t.equal(
- resolve.sync('tiv', { basedir: tivDir }),
- path.join(dir, 'tiv/index.js')
- );
- if (!requireResolveDefaultPathsBroken && requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('tiv', { basedir: tivDir }),
- require.resolve('tiv', { paths: [tivDir] }),
- 'tiv: resolve.sync === require.resolve'
- );
- }
- var gruxDir = path.join(dir, 'tiv');
- t.equal(
- resolve.sync('grux', { basedir: gruxDir }),
- path.join(dir, 'grux/index.js')
- );
- if (!requireResolveDefaultPathsBroken && requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('grux', { basedir: gruxDir }),
- require.resolve('grux', { paths: [gruxDir] }),
- 'grux: resolve.sync === require.resolve'
- );
- }
- t.end();
- });
- test('normalize', function (t) {
- var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
- t.equal(
- resolve.sync('../grux', { basedir: dir }),
- path.join(dir, 'index.js')
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('../grux', { basedir: dir }),
- require.resolve('../grux', { paths: [dir] }),
- '../grux: resolve.sync === require.resolve'
- );
- }
- t.end();
- });
- test('cup', function (t) {
- var dir = path.join(__dirname, 'resolver');
- t.equal(
- resolve.sync('./cup', {
- basedir: dir,
- extensions: ['.js', '.coffee']
- }),
- path.join(dir, 'cup.coffee'),
- './cup -> ./cup.coffee'
- );
- t.equal(
- resolve.sync('./cup.coffee', { basedir: dir }),
- path.join(dir, 'cup.coffee'),
- './cup.coffee'
- );
- t.throws(function () {
- resolve.sync('./cup', {
- basedir: dir,
- extensions: ['.js']
- });
- });
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./cup.coffee', { basedir: dir, extensions: ['.js', '.coffee'] }),
- require.resolve('./cup.coffee', { paths: [dir] }),
- './cup.coffee: resolve.sync === require.resolve'
- );
- }
- t.end();
- });
- test('mug', function (t) {
- var dir = path.join(__dirname, 'resolver');
- t.equal(
- resolve.sync('./mug', { basedir: dir }),
- path.join(dir, 'mug.js'),
- './mug -> ./mug.js'
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./mug', { basedir: dir }),
- require.resolve('./mug', { paths: [dir] }),
- './mug: resolve.sync === require.resolve'
- );
- }
- t.equal(
- resolve.sync('./mug', {
- basedir: dir,
- extensions: ['.coffee', '.js']
- }),
- path.join(dir, 'mug.coffee'),
- './mug -> ./mug.coffee'
- );
- t.equal(
- resolve.sync('./mug', {
- basedir: dir,
- extensions: ['.js', '.coffee']
- }),
- path.join(dir, 'mug.js'),
- './mug -> ./mug.js'
- );
- t.end();
- });
- test('other path', function (t) {
- var resolverDir = path.join(__dirname, 'resolver');
- var dir = path.join(resolverDir, 'bar');
- var otherDir = path.join(resolverDir, 'other_path');
- t.equal(
- resolve.sync('root', {
- basedir: dir,
- paths: [otherDir]
- }),
- path.join(resolverDir, 'other_path/root.js')
- );
- t.equal(
- resolve.sync('lib/other-lib', {
- basedir: dir,
- paths: [otherDir]
- }),
- path.join(resolverDir, 'other_path/lib/other-lib.js')
- );
- t.throws(function () {
- resolve.sync('root', { basedir: dir });
- });
- t.throws(function () {
- resolve.sync('zzz', {
- basedir: dir,
- paths: [otherDir]
- });
- });
- t.end();
- });
- test('path iterator', function (t) {
- var resolverDir = path.join(__dirname, 'resolver');
- var exactIterator = function (x, start, getPackageCandidates, opts) {
- return [path.join(resolverDir, x)];
- };
- t.equal(
- resolve.sync('baz', { packageIterator: exactIterator }),
- path.join(resolverDir, 'baz/quux.js')
- );
- t.end();
- });
- test('incorrect main', function (t) {
- var resolverDir = path.join(__dirname, 'resolver');
- var dir = path.join(resolverDir, 'incorrect_main');
- t.equal(
- resolve.sync('./incorrect_main', { basedir: resolverDir }),
- path.join(dir, 'index.js')
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./incorrect_main', { basedir: resolverDir }),
- require.resolve('./incorrect_main', { paths: [resolverDir] }),
- './incorrect_main: resolve.sync === require.resolve'
- );
- }
- t.end();
- });
- test('missing index', function (t) {
- t.plan(requireResolveSupportsPaths ? 2 : 1);
- var resolverDir = path.join(__dirname, 'resolver');
- try {
- resolve.sync('./missing_index', { basedir: resolverDir });
- t.fail('did not fail');
- } catch (err) {
- t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
- }
- if (requireResolveSupportsPaths) {
- try {
- require.resolve('./missing_index', { basedir: resolverDir });
- t.fail('require.resolve did not fail');
- } catch (err) {
- t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
- }
- }
- });
- test('missing main', function (t) {
- var resolverDir = path.join(__dirname, 'resolver');
- try {
- resolve.sync('./missing_main', { basedir: resolverDir });
- t.fail('require.resolve did not fail');
- } catch (err) {
- t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
- }
- if (requireResolveSupportsPaths) {
- try {
- resolve.sync('./missing_main', { basedir: resolverDir });
- t.fail('require.resolve did not fail');
- } catch (err) {
- t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
- }
- }
- t.end();
- });
- test('null main', function (t) {
- var resolverDir = path.join(__dirname, 'resolver');
- try {
- resolve.sync('./null_main', { basedir: resolverDir });
- t.fail('require.resolve did not fail');
- } catch (err) {
- t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
- }
- if (requireResolveSupportsPaths) {
- try {
- resolve.sync('./null_main', { basedir: resolverDir });
- t.fail('require.resolve did not fail');
- } catch (err) {
- t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
- }
- }
- t.end();
- });
- test('main: false', function (t) {
- var basedir = path.join(__dirname, 'resolver');
- var dir = path.join(basedir, 'false_main');
- t.equal(
- resolve.sync('./false_main', { basedir: basedir }),
- path.join(dir, 'index.js'),
- '`"main": false`: resolves to `index.js`'
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./false_main', { basedir: basedir }),
- require.resolve('./false_main', { paths: [basedir] }),
- '`"main": false`: resolve.sync === require.resolve'
- );
- }
- t.end();
- });
- var stubStatSync = function stubStatSync(fn) {
- 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/);
- });
- 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');
- var basedir = path.join(dir, 'same_names');
- t.equal(
- resolve.sync('./foo', { basedir: basedir }),
- path.join(dir, 'same_names/foo.js')
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./foo', { basedir: basedir }),
- require.resolve('./foo', { paths: [basedir] }),
- './foo: resolve.sync === require.resolve'
- );
- }
- t.equal(
- resolve.sync('./foo/', { basedir: basedir }),
- path.join(dir, 'same_names/foo/index.js')
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./foo/', { basedir: basedir }),
- require.resolve('./foo/', { paths: [basedir] }),
- './foo/: resolve.sync === require.resolve'
- );
- }
- t.end();
- });
- test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
- var dir = path.join(__dirname, 'resolver');
- var basedir = path.join(dir, 'same_names/foo');
- t.equal(
- resolve.sync('./', { basedir: basedir }),
- path.join(dir, 'same_names/foo/index.js'),
- './'
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./', { basedir: basedir }),
- require.resolve('./', { paths: [basedir] }),
- './: resolve.sync === require.resolve'
- );
- }
- t.equal(
- resolve.sync('.', { basedir: basedir }),
- path.join(dir, 'same_names/foo/index.js'),
- '.'
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('.', { basedir: basedir }),
- require.resolve('.', { paths: [basedir] }),
- '.: resolve.sync === require.resolve',
- { todo: true }
- );
- }
- 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.equal(
- resolve.sync('./' + testFile),
- require.resolve('./' + testFile),
- 'sanity check: resolve.sync === require.resolve'
- );
- st.end();
- });
- 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();
- });
- test('sync dot main', function (t) {
- var start = new Date();
- t.equal(
- resolve.sync('./resolver/dot_main'),
- path.join(__dirname, 'resolver/dot_main/index.js'),
- './resolver/dot_main'
- );
- t.equal(
- resolve.sync('./resolver/dot_main'),
- require.resolve('./resolver/dot_main'),
- './resolver/dot_main: resolve.sync === require.resolve'
- );
- t.ok(new Date() - start < 50, 'resolve.sync timedout');
- t.end();
- });
- test('sync dot slash main', function (t) {
- var start = new Date();
- t.equal(
- resolve.sync('./resolver/dot_slash_main'),
- path.join(__dirname, 'resolver/dot_slash_main/index.js')
- );
- t.equal(
- resolve.sync('./resolver/dot_slash_main'),
- require.resolve('./resolver/dot_slash_main'),
- './resolver/dot_slash_main: resolve.sync === require.resolve'
- );
- t.ok(new Date() - start < 50, 'resolve.sync timedout');
- t.end();
- });
- test('not a directory', function (t) {
- var path = './foo';
- try {
- resolve.sync(path, { basedir: __filename });
- t.fail();
- } catch (err) {
- t.ok(err, 'a non-directory errors');
- t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
- t.equal(err && err.code, 'MODULE_NOT_FOUND');
- }
- t.end();
- });
- test('non-string "main" field in package.json', function (t) {
- var dir = path.join(__dirname, 'resolver');
- try {
- var result = resolve.sync('./invalid_main', { basedir: dir });
- t.equal(result, undefined, 'result should not exist');
- t.fail('should not get here');
- } catch (err) {
- t.ok(err, 'errors on non-string main');
- t.equal(err.message, 'package “invalid_main” `main` must be a string');
- t.equal(err.code, 'INVALID_PACKAGE_MAIN');
- }
- t.end();
- });
- test('non-string "main" field in package.json', function (t) {
- var dir = path.join(__dirname, 'resolver');
- try {
- var result = resolve.sync('./invalid_main', { basedir: dir });
- t.equal(result, undefined, 'result should not exist');
- t.fail('should not get here');
- } catch (err) {
- t.ok(err, 'errors on non-string main');
- t.equal(err.message, 'package “invalid_main” `main` must be a string');
- t.equal(err.code, 'INVALID_PACKAGE_MAIN');
- }
- t.end();
- });
- test('browser field in package.json', function (t) {
- var dir = path.join(__dirname, 'resolver');
- var res = resolve.sync('./browser_field', {
- basedir: dir,
- packageFilter: function packageFilter(pkg) {
- if (pkg.browser) {
- pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
- delete pkg.browser; // eslint-disable-line no-param-reassign
- }
- return pkg;
- }
- });
- t.equal(res, path.join(dir, 'browser_field', 'b.js'));
- t.end();
- });
- test('absolute paths', function (t) {
- var extensionless = __filename.slice(0, -path.extname(__filename).length);
- t.equal(
- resolve.sync(__filename),
- __filename,
- 'absolute path to this file resolves'
- );
- t.equal(
- resolve.sync(__filename),
- require.resolve(__filename),
- 'absolute path to this file: resolve.sync === require.resolve'
- );
- t.equal(
- resolve.sync(extensionless),
- __filename,
- 'extensionless absolute path to this file resolves'
- );
- t.equal(
- resolve.sync(__filename),
- require.resolve(__filename),
- 'absolute path to this file: resolve.sync === require.resolve'
- );
- t.equal(
- resolve.sync(__filename, { basedir: process.cwd() }),
- __filename,
- 'absolute path to this file with a basedir resolves'
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync(__filename, { basedir: process.cwd() }),
- require.resolve(__filename, { paths: [process.cwd()] }),
- 'absolute path to this file + basedir: resolve.sync === require.resolve'
- );
- }
- t.equal(
- resolve.sync(extensionless, { basedir: process.cwd() }),
- __filename,
- 'extensionless absolute path to this file with a basedir resolves'
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync(extensionless, { basedir: process.cwd() }),
- require.resolve(extensionless, { paths: [process.cwd()] }),
- 'extensionless absolute path to this file + basedir: resolve.sync === require.resolve'
- );
- }
- t.end();
- });
- var malformedDir = path.join(__dirname, 'resolver/malformed_package_json');
- test('malformed package.json', { skip: !fs.existsSync(malformedDir) }, function (t) {
- t.plan(5 + (requireResolveSupportsPaths ? 1 : 0));
- var basedir = malformedDir;
- var expected = path.join(basedir, 'index.js');
- t.equal(
- resolve.sync('./index.js', { basedir: basedir }),
- expected,
- 'malformed package.json is silently ignored'
- );
- if (requireResolveSupportsPaths) {
- t.equal(
- resolve.sync('./index.js', { basedir: basedir }),
- require.resolve('./index.js', { paths: [basedir] }),
- 'malformed package.json: resolve.sync === require.resolve'
- );
- }
- var res1 = resolve.sync(
- './index.js',
- {
- basedir: basedir,
- packageFilter: function (pkg, pkgfile, dir) {
- t.fail('should not reach here');
- }
- }
- );
- t.equal(
- res1,
- expected,
- 'with packageFilter: malformed package.json is silently ignored'
- );
- var res2 = resolve.sync(
- './index.js',
- {
- basedir: basedir,
- readPackageSync: function (readFileSync, pkgfile) {
- t.equal(pkgfile, path.join(basedir, 'package.json'), 'readPackageSync: `pkgfile` is package.json path');
- var result = String(readFileSync(pkgfile));
- try {
- return JSON.parse(result);
- } catch (e) {
- t.ok(e instanceof SyntaxError, 'readPackageSync: malformed package.json parses as a syntax error');
- }
- }
- }
- );
- t.equal(
- res2,
- expected,
- 'with readPackageSync: malformed package.json is silently ignored'
- );
- });
|