-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tests] add list-exports-tests tests
- Loading branch information
Showing
1,126 changed files
with
17,350 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
test/list-exports-tests/fixtures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var test = require('tape'); | ||
var resolve = require('../'); | ||
|
||
var fixturesPath = path.join(__dirname, 'list-exports-tests/fixtures'); | ||
|
||
fs.readdirSync(fixturesPath).forEach(function (fixtureName) { | ||
var fixtureSpec = require(path.join(fixturesPath, fixtureName, 'expected.json')); | ||
var fixturePackagePath = path.join(fixturesPath, fixtureName, 'project'); | ||
|
||
function packageIterator(identifier) { | ||
var slashIdx = identifier.indexOf('/'); | ||
|
||
if (slashIdx === -1) { | ||
return identifier === fixtureSpec.name ? [fixturePackagePath] : null; | ||
} | ||
|
||
if (identifier.slice(0, slashIdx) === fixtureSpec.name) { | ||
return [fixturePackagePath + identifier.slice(slashIdx)]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
var optsWithExports = { packageIterator: packageIterator, ignoreExportsField: false, extensions: ['.js', '.json'] }; | ||
var optsWithoutExports = { packageIterator: packageIterator, ignoreExportsField: true, extensions: ['.js', '.json'] }; | ||
|
||
test('list-exports-tests fixture ' + fixtureName, function (t) { | ||
/* | ||
* Sanity check: package.json should be resolvable with exports disabled | ||
* All other tests are configured via the expected.json file | ||
*/ | ||
resolve(fixtureSpec.name + '/package.json', optsWithoutExports, function (err, res, pkg) { | ||
t.ifErr(err); | ||
t.equal(res, path.join(fixturePackagePath, 'package.json'), 'sanity check'); | ||
}); | ||
|
||
// with exports enabled | ||
|
||
if (fixtureSpec.private) { | ||
t.plan(2); | ||
return; | ||
} | ||
|
||
t.plan(2 * (1 + fixtureSpec.require.length + fixtureSpec['require (pre-exports)'].length)); | ||
|
||
fixtureSpec.require.forEach(function (identifier) { | ||
if (identifier === fixtureSpec.name + '/') { | ||
t.skip(); | ||
t.skip(); | ||
return; | ||
} | ||
|
||
resolve(identifier, optsWithExports, function (err, res, pkg) { | ||
t.ifErr(err); | ||
var tree = fixtureSpec.tree[fixtureSpec.name]; | ||
|
||
var relativeResolvedParts = path.relative(fixturePackagePath, res).split(path.sep); | ||
|
||
for (var i = 0; i < relativeResolvedParts.length; i++) { | ||
tree = tree[relativeResolvedParts[i]]; | ||
|
||
if (!tree) { | ||
t.fail('Unexpected resolved path ' + JSON.stringify(res) + ' for ' + JSON.stringify(identifier)); | ||
} | ||
} | ||
|
||
t.notEqual(tree.indexOf(identifier), -1, 'resolved path ' + JSON.stringify(res) + ' for ' + JSON.stringify(identifier)); | ||
}); | ||
}); | ||
|
||
fixtureSpec['require (pre-exports)'].forEach(function (identifier) { | ||
resolve(identifier, optsWithoutExports, function (err, res, pkg) { | ||
t.ifErr(err); | ||
var tree = fixtureSpec['tree (pre-exports)'][fixtureSpec.name]; | ||
|
||
var relativeResolvedParts = path.relative(fixturePackagePath, res).split(path.sep); | ||
|
||
for (var i = 0; i < relativeResolvedParts.length; i++) { | ||
tree = tree[relativeResolvedParts[i]]; | ||
|
||
if (!tree) { | ||
t.fail('Unexpected resolved path ' + JSON.stringify(res) + ' for ' + JSON.stringify(identifier)); | ||
} | ||
} | ||
|
||
t.notEqual(tree.indexOf(identifier), -1, 'resolved path ' + JSON.stringify(res) + ' for ' + JSON.stringify(identifier)); | ||
}); | ||
}); | ||
}); | ||
|
||
test('list-exports-tests fixture ' + fixtureName + ' sync', function (t) { | ||
/* | ||
* Sanity check: package.json should be resolvable with exports disabled | ||
* All other tests are configured via the expected.json file | ||
*/ | ||
t.equal(resolve.sync(fixtureSpec.name + '/package.json', optsWithoutExports), path.join(fixturePackagePath, 'package.json'), 'sanity check'); | ||
|
||
// with exports enabled | ||
|
||
if (fixtureSpec.private) { | ||
t.end(); | ||
return; | ||
} | ||
|
||
fixtureSpec.require.forEach(function (identifier) { | ||
if (identifier === fixtureSpec.name + '/') { | ||
t.skip(); | ||
return; | ||
} | ||
|
||
var resolved = resolve.sync(identifier, optsWithExports); | ||
var tree = fixtureSpec.tree[fixtureSpec.name]; | ||
|
||
var relativeResolvedParts = path.relative(fixturePackagePath, resolved).split(path.sep); | ||
|
||
for (var i = 0; i < relativeResolvedParts.length; i++) { | ||
tree = tree[relativeResolvedParts[i]]; | ||
|
||
if (!tree) { | ||
t.fail('Unexpected resolved path ' + JSON.stringify(resolved) + ' for ' + JSON.stringify(identifier)); | ||
} | ||
} | ||
|
||
t.notEqual(tree.indexOf(identifier), -1, 'resolved path ' + JSON.stringify(resolved) + ' for ' + JSON.stringify(identifier)); | ||
}); | ||
|
||
fixtureSpec['require (pre-exports)'].forEach(function (identifier) { | ||
var resolved = resolve.sync(identifier, optsWithoutExports); | ||
var tree = fixtureSpec['tree (pre-exports)'][fixtureSpec.name]; | ||
|
||
var relativeResolvedParts = path.relative(fixturePackagePath, resolved).split(path.sep); | ||
|
||
for (var i = 0; i < relativeResolvedParts.length; i++) { | ||
tree = tree[relativeResolvedParts[i]]; | ||
|
||
if (!tree) { | ||
t.fail('Unexpected resolved path ' + JSON.stringify(resolved) + ' for ' + JSON.stringify(identifier)); | ||
} | ||
} | ||
|
||
t.notEqual(tree.indexOf(identifier), -1, 'resolved path ' + JSON.stringify(resolved) + ' for ' + JSON.stringify(identifier)); | ||
}); | ||
|
||
t.end(); | ||
}); | ||
}); |
Oops, something went wrong.