Ajout de promotion et de commande

This commit is contained in:
Aubert Marvin
2026-04-25 15:28:39 +02:00
parent eddb103755
commit faa3d7718c
8428 changed files with 1126442 additions and 6 deletions
+29
View File
@@ -0,0 +1,29 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('dotdot', function (t) {
t.plan(4);
var dir = path.join(__dirname, '/dotdot/abc');
resolve('..', { basedir: dir }, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(__dirname, 'dotdot/index.js'));
});
resolve('.', { basedir: dir }, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, 'index.js'));
});
});
test('dotdot sync', function (t) {
t.plan(2);
var dir = path.join(__dirname, '/dotdot/abc');
var a = resolve.sync('..', { basedir: dir });
t.equal(a, path.join(__dirname, 'dotdot/index.js'));
var b = resolve.sync('.', { basedir: dir });
t.equal(b, path.join(dir, 'index.js'));
});
View File
View File
+461
View File
@@ -0,0 +1,461 @@
'use strict';
var fs = require('fs');
var path = require('path');
var test = require('tape');
var resolve = require('../');
var fixturesDir = path.join(__dirname, 'list-exports', 'packages', 'tests', 'fixtures');
var categories = [
'broken',
'broken-dir-slash-conditions',
'conditions',
'experimental',
'pattern-trailers',
'pattern-trailers+json-imports',
'pattern-trailers-no-dir-slash',
'pattern-trailers-no-dir-slash+json-imports',
'patterns',
'require-esm',
'strips-types',
'subpath-imports-slash'
// 'pre-exports' is tested separately since it uses main/index resolution
];
// Fixtures that are symlinks pointing outside the fixture dir cause path confusion
// ex-private is a private package whose expected files don't include exports data
var skipFixtures = ['list-exports', 'ls-exports', 'ex-private'];
function getFixtures() {
return fs.readdirSync(fixturesDir).filter(function (name) {
if (skipFixtures.indexOf(name) !== -1) {
return false;
}
var stat = fs.statSync(path.join(fixturesDir, name));
return stat.isDirectory();
});
}
function loadExpected(fixtureName, category) {
var expectedPath = path.join(fixturesDir, fixtureName, 'expected', category + '.json');
if (!fs.existsSync(expectedPath)) {
return null;
}
try {
return JSON.parse(fs.readFileSync(expectedPath, 'utf8'));
} catch (e) {
return null;
}
}
function loadProjectPkg(fixtureName) {
var pkgPath = path.join(fixturesDir, fixtureName, 'project', 'package.json');
try {
return JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
} catch (e) {
return null;
}
}
test('async exports resolution - exportsCategory option', function (t) {
var fixtures = getFixtures();
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
return;
}
var projectDir = path.join(fixturesDir, fixtureName, 'project');
var pkgName = projectPkg.name;
categories.forEach(function (category) {
var expected = loadExpected(fixtureName, category);
if (!expected || !expected.exports || !expected.exports[category]) {
return;
}
var requireMap = expected.exports[category].require;
if (!requireMap || typeof requireMap !== 'object') {
return;
}
Object.keys(requireMap).forEach(function (subpath) {
var expectedFile = requireMap[subpath];
var specifier = subpath === '.' ? pkgName : pkgName + subpath.substring(1);
t.test(fixtureName + ' / ' + category + ' / ' + subpath, function (st) {
st.plan(1);
resolve(specifier, {
basedir: __dirname,
exportsCategory: category,
extensions: ['.js', '.json'],
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail('Unexpected error for ' + specifier + ': ' + err.message);
return;
}
var relativeResult = './' + path.relative(projectDir, result).split(path.sep).join('/');
st.equal(relativeResult, expectedFile, specifier + ' resolves to ' + expectedFile);
});
});
});
});
});
t.end();
});
test('async exports resolution - pre-exports category uses main/index', function (t) {
var fixtures = getFixtures();
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
return;
}
var projectDir = path.join(fixturesDir, fixtureName, 'project');
var pkgName = projectPkg.name;
var expected = loadExpected(fixtureName, 'pre-exports');
if (!expected || !expected.exports || !expected.exports['pre-exports']) {
return;
}
var requireMap = expected.exports['pre-exports'].require;
if (!requireMap || typeof requireMap !== 'object') {
return;
}
// For pre-exports, only test the main entry point (.)
var mainEntry = requireMap['.'];
if (!mainEntry) {
return;
}
t.test(fixtureName + ' / pre-exports / .', function (st) {
st.plan(1);
resolve(pkgName, {
basedir: __dirname,
exportsCategory: 'pre-exports',
extensions: ['.js', '.json'],
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail('Unexpected error for ' + pkgName + ': ' + err.message);
return;
}
var relativeResult = './' + path.relative(projectDir, result).split(path.sep).join('/');
st.equal(relativeResult, mainEntry, pkgName + ' resolves to ' + mainEntry);
});
});
});
t.end();
});
test('async exports resolution - mutual exclusivity of options', function (t) {
t.test('exportsCategory and engines (string) are mutually exclusive', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
exportsCategory: 'conditions',
engines: '>= 14'
}, function (err) {
st.ok(err && (/mutually exclusive/).test(err.message), 'throws with mutually exclusive message');
});
});
t.test('exportsCategory and engines (true) are mutually exclusive', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
exportsCategory: 'conditions',
engines: true
}, function (err) {
st.ok(err && (/mutually exclusive/).test(err.message), 'throws with mutually exclusive message');
});
});
t.end();
});
test('async exports resolution - invalid category', function (t) {
t.plan(2);
resolve('tape', {
basedir: __dirname,
exportsCategory: 'not-a-real-category'
}, function (err) {
t.equal(err && err.code, 'INVALID_EXPORTS_CATEGORY', 'has correct error code');
t.ok(err && (/Invalid exports category/).test(err.message), 'has correct error message');
});
});
test('async exports resolution - engines option', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.test('engines string maps to category', function (st) {
st.plan(1);
resolve('ex-exports-string', {
basedir: __dirname,
engines: '>= 14',
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('index.js') > -1, 'resolves to index.js');
});
});
t.test('engines: false is same as omitting', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
engines: false
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('tape') > -1, 'resolves without exports resolution');
});
});
t.test('engines: empty string throws', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
engines: ''
}, function (err) {
st.ok(err && (/must be.*true.*false.*non-empty string/i).test(err.message), 'throws with correct message');
});
});
t.test('engines: number throws', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
engines: 14
}, function (err) {
st.ok(err && (/must be.*true.*false.*non-empty string/i).test(err.message), 'throws with correct message');
});
});
t.test('engines: object throws', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
engines: { node: '>= 14' }
}, function (err) {
st.ok(err && (/must be.*true.*false.*non-empty string/i).test(err.message), 'throws with correct message');
});
});
t.end();
});
test('async exports resolution - conditions override', function (t) {
var projectDir = path.join(fixturesDir, 'ex-conditions', 'project');
t.test('default category conditions resolve to require.js', function (st) {
st.plan(1);
resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('require.js') > -1, 'resolves to require.js with default conditions');
});
});
t.test('conditions override to [default] resolves to default.js', function (st) {
st.plan(1);
resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
conditions: ['default'],
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('default.js') > -1, 'resolves to default.js with conditions override');
});
});
t.test('conditions override to [node] resolves to node.js', function (st) {
st.plan(1);
resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
conditions: ['node'],
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('node.js') > -1, 'resolves to node.js with conditions override');
});
});
t.end();
});
test('async exports resolution - subpath not exported throws', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.plan(2);
resolve('ex-exports-string/not-exported', {
basedir: __dirname,
exportsCategory: 'conditions',
packageIterator: function () {
return [projectDir];
}
}, function (err) {
t.equal(err && err.code, 'ERR_PACKAGE_PATH_NOT_EXPORTED', 'has correct error code');
t.ok(err && (/not defined by "exports"/).test(err.message), 'has correct error message');
});
});
test('async existing resolution without exports options still works', function (t) {
t.plan(1);
resolve('tape', { basedir: __dirname }, function (err, result) {
if (err) {
t.fail(err.message);
return;
}
t.ok(result.indexOf('tape') > -1, 'resolves tape without exports options');
});
});
test('all fixtures are tested', function (t) {
var fixtures = getFixtures();
var testedFixtures = [];
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
t.fail('Fixture ' + fixtureName + ' has no loadable package.json');
return;
}
var hasAnyTests = false;
// Check if at least one category has expected results
categories.forEach(function (category) {
var expected = loadExpected(fixtureName, category);
if (expected && expected.exports && expected.exports[category]) {
var requireMap = expected.exports[category].require;
if (requireMap && typeof requireMap === 'object' && Object.keys(requireMap).length > 0) {
hasAnyTests = true;
}
}
});
// Also check pre-exports
var preExpected = loadExpected(fixtureName, 'pre-exports');
if (preExpected && preExpected.exports && preExpected.exports['pre-exports']) {
var preRequireMap = preExpected.exports['pre-exports'].require;
if (preRequireMap && preRequireMap['.']) {
hasAnyTests = true;
}
}
if (hasAnyTests) {
testedFixtures.push(fixtureName);
} else {
t.fail('Fixture ' + fixtureName + ' has no testable entrypoints');
}
});
t.ok(testedFixtures.length > 0, 'At least one fixture is tested');
t.equal(testedFixtures.length, fixtures.length, 'All ' + fixtures.length + ' fixtures have testable entrypoints');
t.end();
});
test('async exports resolution - self-reference', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.test('self-reference resolves via exports when inside package', function (st) {
st.plan(1);
// basedir is inside the package, specifier is the package name
resolve('ex-exports-string', {
basedir: projectDir,
exportsCategory: 'conditions'
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('index.js') > -1, 'self-reference resolves to index.js via exports');
});
});
t.test('self-reference with subpath resolves via exports', function (st) {
var conditionsDir = path.join(fixturesDir, 'ex-conditions', 'project');
st.plan(1);
resolve('ex-conditions/rdni', {
basedir: conditionsDir,
exportsCategory: 'conditions'
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('require.js') > -1, 'self-reference subpath resolves correctly');
});
});
t.test('self-reference without exports falls back to main', function (st) {
var mainDotlessDir = path.join(fixturesDir, 'ex-main-dotless', 'project');
st.plan(1);
resolve('ex-main-dotless', {
basedir: mainDotlessDir,
exportsCategory: 'conditions'
}, function (err, result) {
// If it throws, that's also acceptable behavior (no exports means not exported)
st.ok(!err || result, 'self-reference without exports throws or resolves');
});
});
t.test('self-reference does not cross node_modules boundary', function (st) {
// basedir is inside node_modules, should NOT self-reference parent package
var nodeModulesDir = path.join(__dirname, '..', 'node_modules', 'tape');
st.plan(1);
resolve('resolve', {
basedir: nodeModulesDir,
exportsCategory: 'conditions'
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
// The result should be from node_modules, not from a self-reference
st.ok(result.indexOf('node_modules') > -1 || result === 'resolve', 'does not self-reference across node_modules');
});
});
t.end();
});
+442
View File
@@ -0,0 +1,442 @@
'use strict';
var fs = require('fs');
var path = require('path');
var test = require('tape');
var resolve = require('../sync');
var fixturesDir = path.join(__dirname, 'list-exports', 'packages', 'tests', 'fixtures');
var categories = [
'broken',
'broken-dir-slash-conditions',
'conditions',
'experimental',
'pattern-trailers',
'pattern-trailers+json-imports',
'pattern-trailers-no-dir-slash',
'pattern-trailers-no-dir-slash+json-imports',
'patterns',
'require-esm',
'strips-types',
'subpath-imports-slash'
// 'pre-exports' is tested separately since it uses main/index resolution
];
// Fixtures that are symlinks pointing outside the fixture dir cause path confusion
// ex-private is a private package whose expected files don't include exports data
var skipFixtures = ['list-exports', 'ls-exports', 'ex-private'];
function getFixtures() {
return fs.readdirSync(fixturesDir).filter(function (name) {
if (skipFixtures.indexOf(name) !== -1) {
return false;
}
var stat = fs.statSync(path.join(fixturesDir, name));
return stat.isDirectory();
});
}
function loadExpected(fixtureName, category) {
var expectedPath = path.join(fixturesDir, fixtureName, 'expected', category + '.json');
if (!fs.existsSync(expectedPath)) {
return null;
}
try {
return JSON.parse(fs.readFileSync(expectedPath, 'utf8'));
} catch (e) {
return null;
}
}
function loadProjectPkg(fixtureName) {
var pkgPath = path.join(fixturesDir, fixtureName, 'project', 'package.json');
try {
return JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
} catch (e) {
return null;
}
}
test('exports resolution - exportsCategory option', function (t) {
var fixtures = getFixtures();
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
return;
}
var projectDir = path.join(fixturesDir, fixtureName, 'project');
var pkgName = projectPkg.name;
categories.forEach(function (category) {
var expected = loadExpected(fixtureName, category);
if (!expected || !expected.exports || !expected.exports[category]) {
return;
}
var requireMap = expected.exports[category].require;
if (!requireMap || typeof requireMap !== 'object') {
return;
}
Object.keys(requireMap).forEach(function (subpath) {
var expectedFile = requireMap[subpath];
var specifier = subpath === '.' ? pkgName : pkgName + subpath.substring(1);
t.test(fixtureName + ' / ' + category + ' / ' + subpath, function (st) {
st.plan(1);
try {
var result = resolve(specifier, {
basedir: __dirname,
exportsCategory: category,
extensions: ['.js', '.json'],
packageIterator: function () {
return [projectDir];
}
});
var relativeResult = './' + path.relative(projectDir, result).split(path.sep).join('/');
st.equal(relativeResult, expectedFile, specifier + ' resolves to ' + expectedFile);
} catch (e) {
st.fail('Unexpected error for ' + specifier + ': ' + e.message);
}
});
});
});
});
t.end();
});
test('exports resolution - pre-exports category uses main/index', function (t) {
var fixtures = getFixtures();
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
return;
}
var projectDir = path.join(fixturesDir, fixtureName, 'project');
var pkgName = projectPkg.name;
var expected = loadExpected(fixtureName, 'pre-exports');
if (!expected || !expected.exports || !expected.exports['pre-exports']) {
return;
}
var requireMap = expected.exports['pre-exports'].require;
if (!requireMap || typeof requireMap !== 'object') {
return;
}
// For pre-exports, only test the main entry point (.)
var mainEntry = requireMap['.'];
if (!mainEntry) {
return;
}
t.test(fixtureName + ' / pre-exports / .', function (st) {
st.plan(1);
try {
var result = resolve(pkgName, {
basedir: __dirname,
exportsCategory: 'pre-exports',
extensions: ['.js', '.json'],
packageIterator: function () {
return [projectDir];
}
});
var relativeResult = './' + path.relative(projectDir, result).split(path.sep).join('/');
st.equal(relativeResult, mainEntry, pkgName + ' resolves to ' + mainEntry);
} catch (e) {
st.fail('Unexpected error for ' + pkgName + ': ' + e.message);
}
});
});
t.end();
});
test('exports resolution - mutual exclusivity of options', function (t) {
t.test('exportsCategory and engines (string) are mutually exclusive', function (st) {
st.plan(1);
try {
resolve('tape', {
basedir: __dirname,
exportsCategory: 'conditions',
engines: '>= 14'
});
st.fail('should have thrown');
} catch (e) {
st.ok((/mutually exclusive/).test(e.message), 'throws with mutually exclusive message');
}
});
t.test('exportsCategory and engines (true) are mutually exclusive', function (st) {
st.plan(1);
try {
resolve('tape', {
basedir: __dirname,
exportsCategory: 'conditions',
engines: true
});
st.fail('should have thrown');
} catch (e) {
st.ok((/mutually exclusive/).test(e.message), 'throws with mutually exclusive message');
}
});
t.end();
});
test('exports resolution - invalid category', function (t) {
t.plan(2);
try {
resolve('tape', {
basedir: __dirname,
exportsCategory: 'not-a-real-category'
});
t.fail('should have thrown');
} catch (e) {
t.equal(e.code, 'INVALID_EXPORTS_CATEGORY', 'has correct error code');
t.ok((/Invalid exports category/).test(e.message), 'has correct error message');
}
});
test('exports resolution - engines option', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.test('engines string maps to category', function (st) {
st.plan(1);
var result = resolve('ex-exports-string', {
basedir: __dirname,
engines: '>= 14',
packageIterator: function () {
return [projectDir];
}
});
st.ok(result.indexOf('index.js') > -1, 'resolves to index.js');
});
t.test('engines: false is same as omitting', function (st) {
st.plan(1);
var result = resolve('tape', {
basedir: __dirname,
engines: false
});
st.ok(result.indexOf('tape') > -1, 'resolves without exports resolution');
});
t.test('engines: empty string throws', function (st) {
st.plan(1);
try {
resolve('tape', {
basedir: __dirname,
engines: ''
});
st.fail('should have thrown');
} catch (e) {
st.ok((/must be.*true.*false.*non-empty string/i).test(e.message), 'throws with correct message');
}
});
t.test('engines: number throws', function (st) {
st.plan(1);
try {
resolve('tape', {
basedir: __dirname,
engines: 14
});
st.fail('should have thrown');
} catch (e) {
st.ok((/must be.*true.*false.*non-empty string/i).test(e.message), 'throws with correct message');
}
});
t.test('engines: object throws', function (st) {
st.plan(1);
try {
resolve('tape', {
basedir: __dirname,
engines: { node: '>= 14' }
});
st.fail('should have thrown');
} catch (e) {
st.ok((/must be.*true.*false.*non-empty string/i).test(e.message), 'throws with correct message');
}
});
t.end();
});
test('exports resolution - conditions override', function (t) {
var projectDir = path.join(fixturesDir, 'ex-conditions', 'project');
t.test('default category conditions resolve to require.js', function (st) {
st.plan(1);
var result = resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
packageIterator: function () {
return [projectDir];
}
});
st.ok(result.indexOf('require.js') > -1, 'resolves to require.js with default conditions');
});
t.test('conditions override to [default] resolves to default.js', function (st) {
st.plan(1);
var result = resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
conditions: ['default'],
packageIterator: function () {
return [projectDir];
}
});
st.ok(result.indexOf('default.js') > -1, 'resolves to default.js with conditions override');
});
t.test('conditions override to [node] resolves to node.js', function (st) {
st.plan(1);
var result = resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
conditions: ['node'],
packageIterator: function () {
return [projectDir];
}
});
st.ok(result.indexOf('node.js') > -1, 'resolves to node.js with conditions override');
});
t.end();
});
test('exports resolution - subpath not exported throws', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.plan(2);
try {
resolve('ex-exports-string/not-exported', {
basedir: __dirname,
exportsCategory: 'conditions',
packageIterator: function () {
return [projectDir];
}
});
t.fail('should have thrown');
} catch (e) {
t.equal(e.code, 'ERR_PACKAGE_PATH_NOT_EXPORTED', 'has correct error code');
t.ok((/not defined by "exports"/).test(e.message), 'has correct error message');
}
});
test('existing resolution without exports options still works', function (t) {
t.plan(1);
var result = resolve('tape', { basedir: __dirname });
t.ok(result.indexOf('tape') > -1, 'resolves tape without exports options');
});
test('all fixtures are tested', function (t) {
var fixtures = getFixtures();
var testedFixtures = [];
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
t.fail('Fixture ' + fixtureName + ' has no loadable package.json');
return;
}
var hasAnyTests = false;
// Check if at least one category has expected results
categories.forEach(function (category) {
var expected = loadExpected(fixtureName, category);
if (expected && expected.exports && expected.exports[category]) {
var requireMap = expected.exports[category].require;
if (requireMap && typeof requireMap === 'object' && Object.keys(requireMap).length > 0) {
hasAnyTests = true;
}
}
});
// Also check pre-exports
var preExpected = loadExpected(fixtureName, 'pre-exports');
if (preExpected && preExpected.exports && preExpected.exports['pre-exports']) {
var preRequireMap = preExpected.exports['pre-exports'].require;
if (preRequireMap && preRequireMap['.']) {
hasAnyTests = true;
}
}
if (hasAnyTests) {
testedFixtures.push(fixtureName);
} else {
t.fail('Fixture ' + fixtureName + ' has no testable entrypoints');
}
});
t.ok(testedFixtures.length > 0, 'At least one fixture is tested');
t.equal(testedFixtures.length, fixtures.length, 'All ' + fixtures.length + ' fixtures have testable entrypoints');
t.end();
});
test('exports resolution - self-reference', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.test('self-reference resolves via exports when inside package', function (st) {
st.plan(1);
// basedir is inside the package, specifier is the package name
var result = resolve('ex-exports-string', {
basedir: projectDir,
exportsCategory: 'conditions'
});
st.ok(result.indexOf('index.js') > -1, 'self-reference resolves to index.js via exports');
});
t.test('self-reference with subpath resolves via exports', function (st) {
var conditionsDir = path.join(fixturesDir, 'ex-conditions', 'project');
st.plan(1);
var result = resolve('ex-conditions/rdni', {
basedir: conditionsDir,
exportsCategory: 'conditions'
});
st.ok(result.indexOf('require.js') > -1, 'self-reference subpath resolves correctly');
});
t.test('self-reference without exports falls back to main', function (st) {
// Create a scenario where there's no exports field
var mainDotlessDir = path.join(fixturesDir, 'ex-main-dotless', 'project');
st.plan(1);
try {
var result = resolve('ex-main-dotless', {
basedir: mainDotlessDir,
exportsCategory: 'conditions'
});
st.ok(result.indexOf('main.js') > -1 || result.indexOf('index.js') > -1, 'self-reference without exports uses main/index');
} catch (e) {
// If it throws, that's also acceptable behavior (no exports means not exported)
st.ok(true, 'self-reference without exports throws or resolves');
}
});
t.test('self-reference does not cross node_modules boundary', function (st) {
// basedir is inside node_modules, should NOT self-reference parent package
var nodeModulesDir = path.join(__dirname, '..', 'node_modules', 'tape');
st.plan(1);
// Trying to resolve 'resolve' from inside node_modules/tape should NOT
// self-reference the root resolve package - it should use normal resolution
var result = resolve('resolve', {
basedir: nodeModulesDir,
exportsCategory: 'conditions'
});
// The result should be from node_modules, not from a self-reference
// (self-reference would give us the current working directory's resolve)
st.ok(result.indexOf('node_modules') > -1 || result === 'resolve', 'does not self-reference across node_modules');
});
t.end();
});
+29
View File
@@ -0,0 +1,29 @@
var test = require('tape');
var path = require('path');
var resolve = require('../');
test('faulty basedir must produce error in windows', { skip: process.platform !== 'win32' }, function (t) {
t.plan(1);
var resolverDir = 'C:\\a\\b\\c\\d';
resolve('tape/lib/test.js', { basedir: resolverDir }, function (err, res, pkg) {
t.equal(!!err, true);
});
});
test('non-existent basedir should not throw when preserveSymlinks is false', function (t) {
t.plan(2);
var opts = {
basedir: path.join(path.sep, 'unreal', 'path', 'that', 'does', 'not', 'exist'),
preserveSymlinks: false
};
var module = './dotdot/abc';
resolve(module, opts, function (err, res) {
t.equal(err.code, 'INVALID_BASEDIR');
t.equal(res, undefined);
});
});
+37
View File
@@ -0,0 +1,37 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('filter', function (t) {
t.plan(5);
var dir = path.join(__dirname, 'resolver');
var packageFilterArgs;
resolve('./baz', {
basedir: dir,
packageFilter: function (pkg, pkgfile, dir) {
pkg.main = 'doom'; // eslint-disable-line no-param-reassign
packageFilterArgs = [pkg, pkgfile, dir];
return pkg;
}
}, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'baz/doom.js'), 'changing the package "main" works');
var packageData = packageFilterArgs[0];
t.equal(pkg, packageData, 'first packageFilter argument is "pkg"');
t.equal(packageData.main, 'doom', 'package "main" was altered');
var packageFile = packageFilterArgs[1];
t.equal(
packageFile,
path.join(dir, 'baz/package.json'),
'second packageFilter argument is "pkgfile"'
);
var packageFileDir = packageFilterArgs[2];
t.equal(packageFileDir, path.join(dir, 'baz'), 'third packageFilter argument is "dir"');
t.end();
});
});
+33
View File
@@ -0,0 +1,33 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('filter', function (t) {
var dir = path.join(__dirname, 'resolver');
var packageFilterArgs;
var res = resolve.sync('./baz', {
basedir: dir,
packageFilter: function (pkg, pkgfile, dir) {
pkg.main = 'doom'; // eslint-disable-line no-param-reassign
packageFilterArgs = [pkg, pkgfile, dir];
return pkg;
}
});
t.equal(res, path.join(dir, 'baz/doom.js'), 'changing the package "main" works');
var packageData = packageFilterArgs[0];
t.equal(packageData.main, 'doom', 'package "main" was altered');
var packageFile = packageFilterArgs[1];
t.equal(
packageFile,
path.join(dir, 'baz/package.json'),
'second packageFilter argument is "pkgfile"'
);
var packageDir = packageFilterArgs[2];
t.equal(packageDir, path.join(dir, 'baz'), 'third packageFilter argument is "dir"');
t.end();
});
+127
View File
@@ -0,0 +1,127 @@
'use strict';
var fs = require('fs');
var homedir = require('../lib/homedir');
var path = require('path');
var test = require('tape');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var mv = require('mv');
var copyDir = require('copy-dir');
var tmp = require('tmp');
var HOME = homedir();
var hnm = path.join(HOME, '.node_modules');
var hnl = path.join(HOME, '.node_libraries');
var resolve = require('../async');
function makeDir(t, dir, cb) {
mkdirp(dir, function (err) {
if (err) {
cb(err);
} else {
t.teardown(function cleanup() {
rimraf.sync(dir);
});
cb();
}
});
}
function makeTempDir(t, dir, cb) {
if (fs.existsSync(dir)) {
var tmpResult = tmp.dirSync();
t.teardown(tmpResult.removeCallback);
var backup = path.join(tmpResult.name, path.basename(dir));
mv(dir, backup, function (err) {
if (err) {
cb(err);
} else {
t.teardown(function () {
mv(backup, dir, cb);
});
makeDir(t, dir, cb);
}
});
} else {
makeDir(t, dir, cb);
}
}
test('homedir module paths', function (t) {
t.plan(7);
makeTempDir(t, hnm, function (err) {
t.error(err, 'no error with HNM temp dir');
if (err) {
return t.end();
}
var bazHNMDir = path.join(hnm, 'baz');
var dotMainDir = path.join(hnm, 'dot_main');
copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNMDir);
copyDir.sync(path.join(__dirname, 'resolver/dot_main'), dotMainDir);
var bazPkg = { name: 'baz', main: 'quux.js' };
var dotMainPkg = { main: 'index' };
var bazHNMmain = path.join(bazHNMDir, 'quux.js');
t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
var dotMainMain = path.join(dotMainDir, 'index.js');
t.equal(require.resolve('dot_main'), dotMainMain, 'sanity check: require.resolve finds `dot_main`');
makeTempDir(t, hnl, function (err) {
t.error(err, 'no error with HNL temp dir');
if (err) {
return t.end();
}
var bazHNLDir = path.join(hnl, 'baz');
copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNLDir);
var dotSlashMainDir = path.join(hnl, 'dot_slash_main');
var dotSlashMainMain = path.join(dotSlashMainDir, 'index.js');
var dotSlashMainPkg = { main: 'index' };
copyDir.sync(path.join(__dirname, 'resolver/dot_slash_main'), dotSlashMainDir);
t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
t.equal(require.resolve('dot_slash_main'), dotSlashMainMain, 'sanity check: require.resolve finds HNL `dot_slash_main`');
t.test('with temp dirs', function (st) {
st.plan(3);
st.test('just in `$HOME/.node_modules`', function (s2t) {
s2t.plan(3);
resolve('dot_main', function (err, res, pkg) {
s2t.error(err, 'no error resolving `dot_main`');
s2t.equal(res, dotMainMain, '`dot_main` resolves in `$HOME/.node_modules`');
s2t.deepEqual(pkg, dotMainPkg);
});
});
st.test('just in `$HOME/.node_libraries`', function (s2t) {
s2t.plan(3);
resolve('dot_slash_main', function (err, res, pkg) {
s2t.error(err, 'no error resolving `dot_slash_main`');
s2t.equal(res, dotSlashMainMain, '`dot_slash_main` resolves in `$HOME/.node_libraries`');
s2t.deepEqual(pkg, dotSlashMainPkg);
});
});
st.test('in `$HOME/.node_libraries` and `$HOME/.node_modules`', function (s2t) {
s2t.plan(3);
resolve('baz', function (err, res, pkg) {
s2t.error(err, 'no error resolving `baz`');
s2t.equal(res, bazHNMmain, '`baz` resolves in `$HOME/.node_modules` when in both');
s2t.deepEqual(pkg, bazPkg);
});
});
});
});
});
});
+114
View File
@@ -0,0 +1,114 @@
'use strict';
var fs = require('fs');
var homedir = require('../lib/homedir');
var path = require('path');
var test = require('tape');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var mv = require('mv');
var copyDir = require('copy-dir');
var tmp = require('tmp');
var HOME = homedir();
var hnm = path.join(HOME, '.node_modules');
var hnl = path.join(HOME, '.node_libraries');
var resolve = require('../sync');
function makeDir(t, dir, cb) {
mkdirp(dir, function (err) {
if (err) {
cb(err);
} else {
t.teardown(function cleanup() {
rimraf.sync(dir);
});
cb();
}
});
}
function makeTempDir(t, dir, cb) {
if (fs.existsSync(dir)) {
var tmpResult = tmp.dirSync();
t.teardown(tmpResult.removeCallback);
var backup = path.join(tmpResult.name, path.basename(dir));
mv(dir, backup, function (err) {
if (err) {
cb(err);
} else {
t.teardown(function () {
mv(backup, dir, cb);
});
makeDir(t, dir, cb);
}
});
} else {
makeDir(t, dir, cb);
}
}
test('homedir module paths', function (t) {
t.plan(7);
makeTempDir(t, hnm, function (err) {
t.error(err, 'no error with HNM temp dir');
if (err) {
return t.end();
}
var bazHNMDir = path.join(hnm, 'baz');
var dotMainDir = path.join(hnm, 'dot_main');
copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNMDir);
copyDir.sync(path.join(__dirname, 'resolver/dot_main'), dotMainDir);
var bazHNMmain = path.join(bazHNMDir, 'quux.js');
t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
var dotMainMain = path.join(dotMainDir, 'index.js');
t.equal(require.resolve('dot_main'), dotMainMain, 'sanity check: require.resolve finds `dot_main`');
makeTempDir(t, hnl, function (err) {
t.error(err, 'no error with HNL temp dir');
if (err) {
return t.end();
}
var bazHNLDir = path.join(hnl, 'baz');
copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNLDir);
var dotSlashMainDir = path.join(hnl, 'dot_slash_main');
var dotSlashMainMain = path.join(dotSlashMainDir, 'index.js');
copyDir.sync(path.join(__dirname, 'resolver/dot_slash_main'), dotSlashMainDir);
t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
t.equal(require.resolve('dot_slash_main'), dotSlashMainMain, 'sanity check: require.resolve finds HNL `dot_slash_main`');
t.test('with temp dirs', function (st) {
st.plan(3);
st.test('just in `$HOME/.node_modules`', function (s2t) {
s2t.plan(1);
var res = resolve('dot_main');
s2t.equal(res, dotMainMain, '`dot_main` resolves in `$HOME/.node_modules`');
});
st.test('just in `$HOME/.node_libraries`', function (s2t) {
s2t.plan(1);
var res = resolve('dot_slash_main');
s2t.equal(res, dotSlashMainMain, '`dot_slash_main` resolves in `$HOME/.node_libraries`');
});
st.test('in `$HOME/.node_libraries` and `$HOME/.node_modules`', function (s2t) {
s2t.plan(1);
var res = resolve('baz');
s2t.equal(res, bazHNMmain, '`baz` resolves in `$HOME/.node_modules` when in both');
});
});
});
});
});
+315
View File
@@ -0,0 +1,315 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('mock', function (t) {
t.plan(8);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
var dirs = {};
dirs[path.resolve('/foo/bar')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
},
realpath: function (file, cb) {
cb(null, file);
}
};
}
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
t.equal(pkg, undefined);
});
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
t.equal(pkg, undefined);
});
resolve('baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
resolve('../baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
});
test('mock from package', function (t) {
t.plan(8);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
var dirs = {};
dirs[path.resolve('/foo/bar')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, file));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
'package': { main: 'bar' },
readFile: function (file, cb) {
cb(null, files[file]);
},
realpath: function (file, cb) {
cb(null, file);
}
};
}
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
t.equal(pkg && pkg.main, 'bar');
});
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
t.equal(pkg && pkg.main, 'bar');
});
resolve('baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
resolve('../baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
});
test('mock package', function (t) {
t.plan(2);
var files = {};
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
var dirs = {};
dirs[path.resolve('/foo')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
},
realpath: function (file, cb) {
cb(null, file);
}
};
}
resolve('bar', opts('/foo'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
t.equal(pkg && pkg.main, './baz.js');
});
});
test('mock package from package', function (t) {
t.plan(2);
var files = {};
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
var dirs = {};
dirs[path.resolve('/foo')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
'package': { main: 'bar' },
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
},
realpath: function (file, cb) {
cb(null, file);
}
};
}
resolve('bar', opts('/foo'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
t.equal(pkg && pkg.main, './baz.js');
});
});
test('symlinked', function (t) {
t.plan(4);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
var dirs = {};
dirs[path.resolve('/foo/bar')] = true;
dirs[path.resolve('/foo/bar/symlinked')] = true;
function opts(basedir) {
return {
preserveSymlinks: false,
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
},
realpath: function (file, cb) {
var resolved = path.resolve(file);
if (resolved.indexOf('symlinked') >= 0) {
cb(null, resolved);
return;
}
var ext = path.extname(resolved);
if (ext) {
var dir = path.dirname(resolved);
var base = path.basename(resolved);
cb(null, path.join(dir, 'symlinked', base));
} else {
cb(null, path.join(resolved, 'symlinked'));
}
}
};
}
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/symlinked/baz.js'));
t.equal(pkg, undefined);
});
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/symlinked/baz.js'));
t.equal(pkg, undefined);
});
});
test('readPackage', function (t) {
t.plan(3);
var files = {};
files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
var dirs = {};
dirs[path.resolve('/foo')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
'package': { main: 'bar' },
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
},
realpath: function (file, cb) {
cb(null, file);
}
};
}
t.test('with readFile', function (st) {
st.plan(3);
resolve('bar', opts('/foo'), function (err, res, pkg) {
st.error(err);
st.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
st.equal(pkg && pkg.main, './baz.js');
});
});
function readPackage(readFile, file, cb) {
var barPackage = path.join('bar', 'package.json');
if (file.slice(-barPackage.length) === barPackage) {
cb(null, { main: './something-else.js' });
} else {
cb(null, JSON.parse(files[path.resolve(file)]));
}
}
t.test('with readPackage', function (st) {
st.plan(3);
var options = opts('/foo');
delete options.readFile;
options.readPackage = readPackage;
resolve('bar', options, function (err, res, pkg) {
st.error(err);
st.equal(res, path.resolve('/foo/node_modules/bar/something-else.js'));
st.equal(pkg && pkg.main, './something-else.js');
});
});
t.test('with readFile and readPackage', function (st) {
st.plan(1);
var options = opts('/foo');
options.readPackage = readPackage;
resolve('bar', options, function (err) {
st.throws(function () { throw err; }, TypeError, 'errors when both readFile and readPackage are provided');
});
});
});
+215
View File
@@ -0,0 +1,215 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('mock', function (t) {
t.plan(4);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
var dirs = {};
dirs[path.resolve('/foo/bar')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file) {
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
},
isDirectory: function (dir) {
return !!dirs[path.resolve(dir)];
},
readFileSync: function (file) {
return files[path.resolve(file)];
},
realpathSync: function (file) {
return file;
}
};
}
t.equal(
resolve.sync('./baz', opts('/foo/bar')),
path.resolve('/foo/bar/baz.js')
);
t.equal(
resolve.sync('./baz.js', opts('/foo/bar')),
path.resolve('/foo/bar/baz.js')
);
t.throws(function () {
resolve.sync('baz', opts('/foo/bar'));
});
t.throws(function () {
resolve.sync('../baz', opts('/foo/bar'));
});
});
test('mock package', function (t) {
t.plan(1);
var files = {};
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
var dirs = {};
dirs[path.resolve('/foo')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file) {
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
},
isDirectory: function (dir) {
return !!dirs[path.resolve(dir)];
},
readFileSync: function (file) {
return files[path.resolve(file)];
},
realpathSync: function (file) {
return file;
}
};
}
t.equal(
resolve.sync('bar', opts('/foo')),
path.resolve('/foo/node_modules/bar/baz.js')
);
});
test('symlinked', function (t) {
t.plan(2);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
var dirs = {};
dirs[path.resolve('/foo/bar')] = true;
dirs[path.resolve('/foo/bar/symlinked')] = true;
function opts(basedir) {
return {
preserveSymlinks: false,
basedir: path.resolve(basedir),
isFile: function (file) {
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
},
isDirectory: function (dir) {
return !!dirs[path.resolve(dir)];
},
readFileSync: function (file) {
return files[path.resolve(file)];
},
realpathSync: function (file) {
var resolved = path.resolve(file);
if (resolved.indexOf('symlinked') >= 0) {
return resolved;
}
var ext = path.extname(resolved);
if (ext) {
var dir = path.dirname(resolved);
var base = path.basename(resolved);
return path.join(dir, 'symlinked', base);
}
return path.join(resolved, 'symlinked');
}
};
}
t.equal(
resolve.sync('./baz', opts('/foo/bar')),
path.resolve('/foo/bar/symlinked/baz.js')
);
t.equal(
resolve.sync('./baz.js', opts('/foo/bar')),
path.resolve('/foo/bar/symlinked/baz.js')
);
});
test('readPackageSync', function (t) {
t.plan(3);
var files = {};
files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
var dirs = {};
dirs[path.resolve('/foo')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir, useReadPackage) {
return {
basedir: path.resolve(basedir),
isFile: function (file) {
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
},
isDirectory: function (dir) {
return !!dirs[path.resolve(dir)];
},
readFileSync: useReadPackage ? null : function (file) {
return files[path.resolve(file)];
},
realpathSync: function (file) {
return file;
}
};
}
t.test('with readFile', function (st) {
st.plan(1);
st.equal(
resolve.sync('bar', opts('/foo')),
path.resolve('/foo/node_modules/bar/baz.js')
);
});
function readPackageSync(readFileSync, file) {
if (file.indexOf(path.join('bar', 'package.json')) >= 0) {
return { main: './something-else.js' };
}
return JSON.parse(files[path.resolve(file)]);
}
t.test('with readPackage', function (st) {
st.plan(1);
var options = opts('/foo');
delete options.readFileSync;
options.readPackageSync = readPackageSync;
st.equal(
resolve.sync('bar', options),
path.resolve('/foo/node_modules/bar/something-else.js')
);
});
t.test('with readFile and readPackage', function (st) {
st.plan(1);
var options = opts('/foo');
options.readPackageSync = readPackageSync;
st.throws(
function () { resolve.sync('bar', options); },
TypeError,
'errors when both readFile and readPackage are provided'
);
});
});
+56
View File
@@ -0,0 +1,56 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('moduleDirectory strings', function (t) {
t.plan(4);
var dir = path.join(__dirname, 'module_dir');
var xopts = {
basedir: dir,
moduleDirectory: 'xmodules'
};
resolve('aaa', xopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, '/xmodules/aaa/index.js'));
});
var yopts = {
basedir: dir,
moduleDirectory: 'ymodules'
};
resolve('aaa', yopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, '/ymodules/aaa/index.js'));
});
});
test('moduleDirectory array', function (t) {
t.plan(6);
var dir = path.join(__dirname, 'module_dir');
var aopts = {
basedir: dir,
moduleDirectory: ['xmodules', 'ymodules', 'zmodules']
};
resolve('aaa', aopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, '/xmodules/aaa/index.js'));
});
var bopts = {
basedir: dir,
moduleDirectory: ['zmodules', 'ymodules', 'xmodules']
};
resolve('aaa', bopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, '/ymodules/aaa/index.js'));
});
var copts = {
basedir: dir,
moduleDirectory: ['xmodules', 'ymodules', 'zmodules']
};
resolve('bbb', copts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, '/zmodules/bbb/main.js'));
});
});
View File
View File
View File
+3
View File
@@ -0,0 +1,3 @@
{
"main": "main.js"
}
+143
View File
@@ -0,0 +1,143 @@
var test = require('tape');
var path = require('path');
var parse = path.parse || require('path-parse');
var keys = require('object-keys');
var nodeModulesPaths = require('../lib/node-modules-paths');
function verifyDirs(t, start, dirs, moduleDirectories, paths) {
var moduleDirs = [].concat(moduleDirectories || 'node_modules');
if (paths) {
for (var k = 0; k < paths.length; ++k) {
moduleDirs.push(path.basename(paths[k]));
}
}
var foundModuleDirs = {};
var uniqueDirs = {};
var parsedDirs = {};
for (var i = 0; i < dirs.length; ++i) {
var parsed = parse(dirs[i]);
if (!foundModuleDirs[parsed.base]) { foundModuleDirs[parsed.base] = 0; }
foundModuleDirs[parsed.base] += 1;
parsedDirs[parsed.dir] = true;
uniqueDirs[dirs[i]] = true;
}
t.equal(keys(parsedDirs).length >= start.split(path.sep).length, true, 'there are >= dirs than "start" has');
var foundModuleDirNames = keys(foundModuleDirs);
t.deepEqual(foundModuleDirNames, moduleDirs, 'all desired module dirs were found');
t.equal(keys(uniqueDirs).length, dirs.length, 'all dirs provided were unique');
var counts = {};
for (var j = 0; j < foundModuleDirNames.length; ++j) {
counts[foundModuleDirs[j]] = true;
}
t.equal(keys(counts).length, 1, 'all found module directories had the same count');
}
test('node-modules-paths', function (t) {
t.test('no options', function (t) {
var start = path.join(__dirname, 'resolver');
var dirs = nodeModulesPaths(start);
verifyDirs(t, start, dirs);
t.end();
});
t.test('empty options', function (t) {
var start = path.join(__dirname, 'resolver');
var dirs = nodeModulesPaths(start, {});
verifyDirs(t, start, dirs);
t.end();
});
t.test('with paths=array option', function (t) {
var start = path.join(__dirname, 'resolver');
var paths = ['a', 'b'];
var dirs = nodeModulesPaths(start, { paths: paths });
verifyDirs(t, start, dirs, null, paths);
t.end();
});
t.test('with paths=function option', function (t) {
function paths(request, absoluteStart, getNodeModulesDirs, opts) {
return getNodeModulesDirs().concat(path.join(absoluteStart, 'not node modules', request));
}
var start = path.join(__dirname, 'resolver');
var dirs = nodeModulesPaths(start, { paths: paths }, 'pkg');
verifyDirs(t, start, dirs, null, [path.join(start, 'not node modules', 'pkg')]);
t.end();
});
t.test('with paths=function skipping node modules resolution', function (t) {
function paths(request, absoluteStart, getNodeModulesDirs, opts) {
return [];
}
var start = path.join(__dirname, 'resolver');
var dirs = nodeModulesPaths(start, { paths: paths });
t.deepEqual(dirs, [], 'no node_modules was computed');
t.end();
});
t.test('with moduleDirectory option', function (t) {
var start = path.join(__dirname, 'resolver');
var moduleDirectory = 'not node modules';
var dirs = nodeModulesPaths(start, { moduleDirectory: moduleDirectory });
verifyDirs(t, start, dirs, moduleDirectory);
t.end();
});
t.test('with 1 moduleDirectory and paths options', function (t) {
var start = path.join(__dirname, 'resolver');
var paths = ['a', 'b'];
var moduleDirectory = 'not node modules';
var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectory });
verifyDirs(t, start, dirs, moduleDirectory, paths);
t.end();
});
t.test('with 1+ moduleDirectory and paths options', function (t) {
var start = path.join(__dirname, 'resolver');
var paths = ['a', 'b'];
var moduleDirectories = ['not node modules', 'other modules'];
var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
verifyDirs(t, start, dirs, moduleDirectories, paths);
t.end();
});
t.test('combine paths correctly on Windows', function (t) {
var start = 'C:\\Users\\username\\myProject\\src';
var paths = [];
var moduleDirectories = ['node_modules', start];
var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
t.equal(dirs.indexOf(path.resolve(start)) > -1, true, 'should contain start dir');
t.end();
});
t.test('combine paths correctly on non-Windows', { skip: process.platform === 'win32' }, function (t) {
var start = '/Users/username/git/myProject/src';
var paths = [];
var moduleDirectories = ['node_modules', '/Users/username/git/myProject/src'];
var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
t.equal(dirs.indexOf(path.resolve(start)) > -1, true, 'should contain start dir');
t.end();
});
});
+70
View File
@@ -0,0 +1,70 @@
var fs = require('fs');
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('$NODE_PATH', function (t) {
t.plan(8);
function isDir(dir, cb) {
if (dir === '/node_path' || dir === 'node_path/x') {
return cb(null, true);
}
fs.stat(dir, function (err, stat) {
if (!err) {
return cb(null, stat.isDirectory());
}
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
return cb(err);
});
}
resolve('aaa', {
paths: [
path.join(__dirname, '/node_path/x'),
path.join(__dirname, '/node_path/y')
],
basedir: __dirname,
isDirectory: isDir
}, function (err, res) {
t.error(err);
t.equal(res, path.join(__dirname, '/node_path/x/aaa/index.js'), 'aaa resolves');
});
resolve('bbb', {
paths: [
path.join(__dirname, '/node_path/x'),
path.join(__dirname, '/node_path/y')
],
basedir: __dirname,
isDirectory: isDir
}, function (err, res) {
t.error(err);
t.equal(res, path.join(__dirname, '/node_path/y/bbb/index.js'), 'bbb resolves');
});
resolve('ccc', {
paths: [
path.join(__dirname, '/node_path/x'),
path.join(__dirname, '/node_path/y')
],
basedir: __dirname,
isDirectory: isDir
}, function (err, res) {
t.error(err);
t.equal(res, path.join(__dirname, '/node_path/x/ccc/index.js'), 'ccc resolves');
});
// ensure that relative paths still resolve against the regular `node_modules` correctly
resolve('tap', {
paths: [
'node_path'
],
basedir: path.join(__dirname, 'node_path/x'),
isDirectory: isDir
}, function (err, res) {
var root = require('tap/package.json').main; // eslint-disable-line global-require
t.error(err);
t.equal(res.replace('/node_modules/.vlt/··tap@0.4.13/', '/'), path.resolve(__dirname, '..', 'node_modules/tap', root), 'tap resolves');
});
});
View File
View File
View File
View File
+9
View File
@@ -0,0 +1,9 @@
var test = require('tape');
var resolve = require('../');
test('nonstring', function (t) {
t.plan(1);
resolve(555, function (err, res, pkg) {
t.ok(err);
});
});
+75
View File
@@ -0,0 +1,75 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
var resolverDir = path.join(__dirname, '/pathfilter/deep_ref');
function pathFilterFactory(t) {
return function (pkg, x, remainder) {
t.equal(pkg.version, '1.2.3');
t.equal(x, path.join(resolverDir, 'node_modules/deep/ref'));
t.equal(remainder, 'ref');
return 'alt';
};
}
test('#62: deep module references and the pathFilter', function (t) {
t.test('deep/ref.js', function (st) {
st.plan(3);
resolve('deep/ref', { basedir: resolverDir }, function (err, res, pkg) {
if (err) st.fail(err);
st.equal(pkg.version, '1.2.3');
st.equal(res, path.join(resolverDir, 'node_modules/deep/ref.js'));
});
var res = resolve.sync('deep/ref', { basedir: resolverDir });
st.equal(res, path.join(resolverDir, 'node_modules/deep/ref.js'));
});
t.test('deep/deeper/ref', function (st) {
st.plan(4);
resolve(
'deep/deeper/ref',
{ basedir: resolverDir },
function (err, res, pkg) {
if (err) t.fail(err);
st.notEqual(pkg, undefined);
st.equal(pkg.version, '1.2.3');
st.equal(res, path.join(resolverDir, 'node_modules/deep/deeper/ref.js'));
}
);
var res = resolve.sync(
'deep/deeper/ref',
{ basedir: resolverDir }
);
st.equal(res, path.join(resolverDir, 'node_modules/deep/deeper/ref.js'));
});
t.test('deep/ref alt', function (st) {
st.plan(8);
var pathFilter = pathFilterFactory(st);
var res = resolve.sync(
'deep/ref',
{ basedir: resolverDir, pathFilter: pathFilter }
);
st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js'));
resolve(
'deep/ref',
{ basedir: resolverDir, pathFilter: pathFilter },
function (err, res, pkg) {
if (err) st.fail(err);
st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js'));
st.end();
}
);
});
t.end();
});
View File
+24
View File
@@ -0,0 +1,24 @@
var test = require('tape');
var path = require('path');
var resolve = require('../');
test('synchronous pathfilter', function (t) {
var res;
var resolverDir = __dirname + '/pathfilter/deep_ref';
function pathFilter(pkg, x, remainder) {
t.equal(pkg.version, '1.2.3');
t.equal(x, path.join(resolverDir, 'node_modules', 'deep', 'ref'));
t.equal(remainder, 'ref');
return 'alt';
}
res = resolve.sync('deep/ref', { basedir: resolverDir });
t.equal(res, path.join(resolverDir, 'node_modules', 'deep', 'ref.js'));
res = resolve.sync('deep/deeper/ref', { basedir: resolverDir });
t.equal(res, path.join(resolverDir, 'node_modules', 'deep', 'deeper', 'ref.js'));
res = resolve.sync('deep/ref', { basedir: resolverDir, pathFilter: pathFilter });
t.equal(res, path.join(resolverDir, 'node_modules', 'deep', 'alt.js'));
t.end();
});
+23
View File
@@ -0,0 +1,23 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('precedence', function (t) {
t.plan(3);
var dir = path.join(__dirname, 'precedence/aaa');
resolve('./', { basedir: dir }, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, 'index.js'));
t.equal(pkg.name, 'resolve');
});
});
test('./ should not load ${dir}.js', function (t) { // eslint-disable-line no-template-curly-in-string
t.plan(1);
var dir = path.join(__dirname, 'precedence/bbb');
resolve('./', { basedir: dir }, function (err, res, pkg) {
t.ok(err);
});
});
View File
View File
View File
View File
View File
+613
View File
File diff suppressed because it is too large Load Diff
View File
+4
View File
@@ -0,0 +1,4 @@
{
"name": "baz",
"main": "quux.js"
}
View File
View File
View File
+5
View File
@@ -0,0 +1,5 @@
{
"name": "browser_field",
"main": "a",
"browser": "b"
}
+1
View File
@@ -0,0 +1 @@
View File
+3
View File
@@ -0,0 +1,3 @@
{
"main": "."
}
View File
+3
View File
@@ -0,0 +1,3 @@
{
"main": "./"
}
View File
+3
View File
@@ -0,0 +1,3 @@
{
"main": ""
}
View File
+4
View File
@@ -0,0 +1,4 @@
{
"name": "false_main",
"main": false
}
View File

Some files were not shown because too many files have changed in this diff Show More