Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.

Commit 1de1db7

Browse files
GeoffreyBoothnodejs-ci
authored andcommitted
esm: tests for correctly detecting module types of symlinks
1 parent cb4203b commit 1de1db7

6 files changed

+73
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
const common = require('../common');
3+
const path = require('path');
4+
const assert = require('assert');
5+
const exec = require('child_process').execFile;
6+
7+
// Check that running the symlink executes the target as the correct type
8+
const symlinks = [
9+
{
10+
source: 'extensionless-symlink-to-mjs-file',
11+
prints: '.mjs file',
12+
errorsWithPreserveSymlinksMain: false
13+
}, {
14+
source: 'extensionless-symlink-to-cjs-file',
15+
prints: '.cjs file',
16+
errorsWithPreserveSymlinksMain: false
17+
}, {
18+
source: 'extensionless-symlink-to-file-in-module-scope',
19+
prints: 'package-type-module',
20+
// The package scope of the symlinks' sources is commonjs, and this
21+
// symlink's target is a .js file in a module scope, so when the scope
22+
// is evaluated based on the source (commonjs) this esm file should error
23+
errorsWithPreserveSymlinksMain: true
24+
}, {
25+
source: 'extensionless-symlink-to-file-in-explicit-commonjs-scope',
26+
prints: 'package-type-commonjs',
27+
errorsWithPreserveSymlinksMain: false
28+
}, {
29+
source: 'extensionless-symlink-to-file-in-implicit-commonjs-scope',
30+
prints: 'package-without-type',
31+
errorsWithPreserveSymlinksMain: false
32+
}
33+
];
34+
35+
symlinks.forEach((symlink) => {
36+
const argv = [
37+
path.join(__dirname, '../fixtures/es-modules/', symlink.source)
38+
];
39+
// TODO: Update when --experimental-modules is unflagged
40+
const flags = [
41+
'--experimental-modules',
42+
'--experimental-modules --preserve-symlinks-main'
43+
];
44+
flags.forEach((nodeOptions) => {
45+
const opts = {
46+
env: Object.assign({}, process.env, { NODE_OPTIONS: nodeOptions }),
47+
maxBuffer: 1e6,
48+
};
49+
exec(process.execPath, argv, opts, common.mustCall(
50+
(err, stdout, stderr) => {
51+
if (nodeOptions.includes('--preserve-symlinks-main')) {
52+
if (symlink.errorsWithPreserveSymlinksMain &&
53+
stderr.includes('Error')) return;
54+
else if (!symlink.errorsWithPreserveSymlinksMain &&
55+
stdout.includes(symlink.prints)) return;
56+
assert.fail(`For ${JSON.stringify(symlink)}, ${
57+
(symlink.errorsWithPreserveSymlinksMain) ?
58+
'failed to error' : 'errored unexpectedly'
59+
} with --preserve-symlinks-main`);
60+
} else {
61+
if (stdout.includes(symlink.prints)) return;
62+
assert.fail(`For ${JSON.stringify(symlink)}, failed to find ` +
63+
`${symlink.prints} in: <\n${stdout}\n>`);
64+
}
65+
}
66+
));
67+
});
68+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./cjs-file.cjs
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./package-type-commonjs/index.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./package-without-type/index.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./package-type-module/index.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./mjs-file.mjs

0 commit comments

Comments
 (0)