diff options
author | David Goldstein <dgoldstein@dropbox.com> | 2018-04-10 07:40:56 +0000 |
---|---|---|
committer | Vse Mozhet Byt <vsemozhetbyt@gmail.com> | 2018-05-13 00:28:16 +0300 |
commit | 236596590ce5746c66a36bee9ca81203553bd7a2 (patch) | |
tree | 6d077b2859a268a689d30068afa6c28208a22b64 /test/es-module | |
parent | 20509ebee681233443ebc7dc1b58c2fab6d2b12f (diff) | |
download | android-node-v8-236596590ce5746c66a36bee9ca81203553bd7a2.tar.gz android-node-v8-236596590ce5746c66a36bee9ca81203553bd7a2.tar.bz2 android-node-v8-236596590ce5746c66a36bee9ca81203553bd7a2.zip |
module: add --preserve-symlinks-main
Add `--preserve-symlinks-main` option which behaves like
`--preserve-symlinks` but for `require.main`.
PR-URL: https://github.com/nodejs/node/pull/19911
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/es-module')
-rw-r--r-- | test/es-module/test-esm-preserve-symlinks-main.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/test/es-module/test-esm-preserve-symlinks-main.js b/test/es-module/test-esm-preserve-symlinks-main.js new file mode 100644 index 0000000000..fbca5dce74 --- /dev/null +++ b/test/es-module/test-esm-preserve-symlinks-main.js @@ -0,0 +1,57 @@ +'use strict'; + +const common = require('../common'); +const { spawn } = require('child_process'); +const assert = require('assert'); +const path = require('path'); +const fs = require('fs'); + +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); +const tmpDir = tmpdir.path; + +fs.mkdirSync(path.join(tmpDir, 'nested')); +fs.mkdirSync(path.join(tmpDir, 'nested2')); + +const entry = path.join(tmpDir, 'nested', 'entry.js'); +const entry_link_absolute_path = path.join(tmpDir, 'link.js'); +const submodule = path.join(tmpDir, 'nested2', 'submodule.js'); +const submodule_link_absolute_path = path.join(tmpDir, 'submodule_link.js'); + +fs.writeFileSync(entry, ` +const assert = require('assert'); + +// this import only resolves with --preserve-symlinks-main set +require('./submodule_link.js'); +`); +fs.writeFileSync(submodule, ''); + +try { + fs.symlinkSync(entry, entry_link_absolute_path); + fs.symlinkSync(submodule, submodule_link_absolute_path); +} catch (err) { + if (err.code !== 'EPERM') throw err; + common.skip('insufficient privileges for symlinks'); +} + +function doTest(flags, done) { + // invoke the main file via a symlink. In this case --preserve-symlinks-main + // dictates that it'll resolve relative imports in the main file relative to + // the symlink, and not relative to the symlink target; the file structure set + // up above requires this to not crash when loading ./submodule_link.js + spawn(process.execPath, + flags.concat([ + '--preserve-symlinks', + '--preserve-symlinks-main', entry_link_absolute_path + ]), + { stdio: 'inherit' }).on('exit', (code) => { + assert.strictEqual(code, 0); + done(); + }); +} + +// first test the commonjs module loader +doTest([], () => { + // now test the new loader + doTest(['--experimental-modules'], () => {}); +}); |