|
| 1 | +var fs = require('fs'); |
| 2 | +var path = require('path'); |
| 3 | +var test = require('tape'); |
| 4 | +var resolve = require('../'); |
| 5 | + |
| 6 | +var fixturesPath = path.join(__dirname, 'list-exports-tests/fixtures'); |
| 7 | + |
| 8 | +fs.readdirSync(fixturesPath).forEach(function (fixtureName) { |
| 9 | + var fixtureSpec = require(path.join(fixturesPath, fixtureName, 'expected.json')); |
| 10 | + var fixturePackagePath = path.join(fixturesPath, fixtureName, 'project'); |
| 11 | + |
| 12 | + function packageIterator(identifier) { |
| 13 | + var slashIdx = identifier.indexOf('/'); |
| 14 | + |
| 15 | + if (slashIdx === -1) { |
| 16 | + return identifier === fixtureSpec.name ? [fixturePackagePath] : null; |
| 17 | + } |
| 18 | + |
| 19 | + if (identifier.slice(0, slashIdx) === fixtureSpec.name) { |
| 20 | + return [fixturePackagePath + identifier.slice(slashIdx)]; |
| 21 | + } else { |
| 22 | + return null; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + var optsWithExports = { packageIterator: packageIterator, ignoreExportsField: false, extensions: ['.js', '.json'] }; |
| 27 | + var optsWithoutExports = { packageIterator: packageIterator, ignoreExportsField: true, extensions: ['.js', '.json'] }; |
| 28 | + |
| 29 | + test('list-exports-tests fixture ' + fixtureName, function (t) { |
| 30 | + /* |
| 31 | + * Sanity check: package.json should be resolvable with exports disabled |
| 32 | + * All other tests are configured via the expected.json file |
| 33 | + */ |
| 34 | + resolve(fixtureSpec.name + '/package.json', optsWithoutExports, function (err, res, pkg) { |
| 35 | + t.ifErr(err); |
| 36 | + t.equal(res, path.join(fixturePackagePath, 'package.json'), 'sanity check'); |
| 37 | + }); |
| 38 | + |
| 39 | + // with exports enabled |
| 40 | + |
| 41 | + if (fixtureSpec.private) { |
| 42 | + t.plan(2); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + t.plan(2 * (1 + fixtureSpec.require.length + fixtureSpec['require (pre-exports)'].length)); |
| 47 | + |
| 48 | + fixtureSpec.require.forEach(function (identifier) { |
| 49 | + if (identifier === fixtureSpec.name + '/') { |
| 50 | + t.skip(); |
| 51 | + t.skip(); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + resolve(identifier, optsWithExports, function (err, res, pkg) { |
| 56 | + t.ifErr(err); |
| 57 | + var tree = fixtureSpec.tree[fixtureSpec.name]; |
| 58 | + |
| 59 | + var relativeResolvedParts = path.relative(fixturePackagePath, res).split(path.sep); |
| 60 | + |
| 61 | + for (var i = 0; i < relativeResolvedParts.length; i++) { |
| 62 | + tree = tree[relativeResolvedParts[i]]; |
| 63 | + |
| 64 | + if (!tree) { |
| 65 | + t.fail('Unexpected resolved path ' + JSON.stringify(res) + ' for ' + JSON.stringify(identifier)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + t.notEqual(tree.indexOf(identifier), -1, 'resolved path ' + JSON.stringify(res) + ' for ' + JSON.stringify(identifier)); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + fixtureSpec['require (pre-exports)'].forEach(function (identifier) { |
| 74 | + resolve(identifier, optsWithoutExports, function (err, res, pkg) { |
| 75 | + t.ifErr(err); |
| 76 | + var tree = fixtureSpec['tree (pre-exports)'][fixtureSpec.name]; |
| 77 | + |
| 78 | + var relativeResolvedParts = path.relative(fixturePackagePath, res).split(path.sep); |
| 79 | + |
| 80 | + for (var i = 0; i < relativeResolvedParts.length; i++) { |
| 81 | + tree = tree[relativeResolvedParts[i]]; |
| 82 | + |
| 83 | + if (!tree) { |
| 84 | + t.fail('Unexpected resolved path ' + JSON.stringify(res) + ' for ' + JSON.stringify(identifier)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + t.notEqual(tree.indexOf(identifier), -1, 'resolved path ' + JSON.stringify(res) + ' for ' + JSON.stringify(identifier)); |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + test('list-exports-tests fixture ' + fixtureName + ' sync', function (t) { |
| 94 | + /* |
| 95 | + * Sanity check: package.json should be resolvable with exports disabled |
| 96 | + * All other tests are configured via the expected.json file |
| 97 | + */ |
| 98 | + t.equal(resolve.sync(fixtureSpec.name + '/package.json', optsWithoutExports), path.join(fixturePackagePath, 'package.json'), 'sanity check'); |
| 99 | + |
| 100 | + // with exports enabled |
| 101 | + |
| 102 | + if (fixtureSpec.private) { |
| 103 | + t.end(); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + fixtureSpec.require.forEach(function (identifier) { |
| 108 | + if (identifier === fixtureSpec.name + '/') { |
| 109 | + t.skip(); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + var resolved = resolve.sync(identifier, optsWithExports); |
| 114 | + var tree = fixtureSpec.tree[fixtureSpec.name]; |
| 115 | + |
| 116 | + var relativeResolvedParts = path.relative(fixturePackagePath, resolved).split(path.sep); |
| 117 | + |
| 118 | + for (var i = 0; i < relativeResolvedParts.length; i++) { |
| 119 | + tree = tree[relativeResolvedParts[i]]; |
| 120 | + |
| 121 | + if (!tree) { |
| 122 | + t.fail('Unexpected resolved path ' + JSON.stringify(resolved) + ' for ' + JSON.stringify(identifier)); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + t.notEqual(tree.indexOf(identifier), -1, 'resolved path ' + JSON.stringify(resolved) + ' for ' + JSON.stringify(identifier)); |
| 127 | + }); |
| 128 | + |
| 129 | + fixtureSpec['require (pre-exports)'].forEach(function (identifier) { |
| 130 | + var resolved = resolve.sync(identifier, optsWithoutExports); |
| 131 | + var tree = fixtureSpec['tree (pre-exports)'][fixtureSpec.name]; |
| 132 | + |
| 133 | + var relativeResolvedParts = path.relative(fixturePackagePath, resolved).split(path.sep); |
| 134 | + |
| 135 | + for (var i = 0; i < relativeResolvedParts.length; i++) { |
| 136 | + tree = tree[relativeResolvedParts[i]]; |
| 137 | + |
| 138 | + if (!tree) { |
| 139 | + t.fail('Unexpected resolved path ' + JSON.stringify(resolved) + ' for ' + JSON.stringify(identifier)); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + t.notEqual(tree.indexOf(identifier), -1, 'resolved path ' + JSON.stringify(resolved) + ' for ' + JSON.stringify(identifier)); |
| 144 | + }); |
| 145 | + |
| 146 | + t.end(); |
| 147 | + }); |
| 148 | +}); |
0 commit comments