|
| 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 | +}); |
0 commit comments