|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const { spawn } = require('child_process'); |
| 5 | +const assert = require('assert'); |
| 6 | +const path = require('path'); |
| 7 | +const fs = require('fs'); |
| 8 | + |
| 9 | +const tmpdir = require('../common/tmpdir'); |
| 10 | +tmpdir.refresh(); |
| 11 | +const tmpDir = tmpdir.path; |
| 12 | + |
| 13 | +fs.mkdirSync(path.join(tmpDir, 'nested')); |
| 14 | +fs.mkdirSync(path.join(tmpDir, 'nested2')); |
| 15 | + |
| 16 | +const entry = path.join(tmpDir, 'nested', 'entry.js'); |
| 17 | +const entry_link_absolute_path = path.join(tmpDir, 'link.js'); |
| 18 | +const submodule = path.join(tmpDir, 'nested2', 'submodule.js'); |
| 19 | +const submodule_link_absolute_path = path.join(tmpDir, 'submodule_link.js') |
| 20 | + |
| 21 | +fs.writeFileSync(entry, ` |
| 22 | +const assert = require('assert'); |
| 23 | +
|
| 24 | +// this import only resolves with --preserve-symlinks-main set |
| 25 | +require('./submodule_link.js'); |
| 26 | +`); |
| 27 | +fs.writeFileSync(submodule, ''); |
| 28 | + |
| 29 | +try { |
| 30 | + fs.symlinkSync(entry, entry_link_absolute_path); |
| 31 | + fs.symlinkSync(submodule, submodule_link_absolute_path); |
| 32 | +} catch (err) { |
| 33 | + if (err.code !== 'EPERM') throw err; |
| 34 | + common.skip('insufficient privileges for symlinks'); |
| 35 | +} |
| 36 | + |
| 37 | +function doTest(flags, done) { |
| 38 | + // invoke the main file via a symlink. In this case --preserve-symlinks-main |
| 39 | + // dictates that it'll resolve relative imports in the main file relative to |
| 40 | + // the symlink, and not relative to the symlink target; the file structure set |
| 41 | + // up above requires this to not crash when loading ./submodule_link.js |
| 42 | + spawn(process.execPath, |
| 43 | + flags.concat(['--preserve-symlinks', '--preserve-symlinks-main', entry_link_absolute_path]), |
| 44 | + { stdio: 'inherit' }).on('exit', (code) => { |
| 45 | + assert.strictEqual(code, 0); |
| 46 | + done(); |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +// first test the commonjs module loader |
| 51 | +doTest([], () => { |
| 52 | + // now test the new loader |
| 53 | + doTest(['--experimental-modules'], () => {}); |
| 54 | +}); |
| 55 | + |
0 commit comments