• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const { spawn } = require('child_process');
5const assert = require('assert');
6const path = require('path');
7const fs = require('fs');
8
9const tmpdir = require('../common/tmpdir');
10tmpdir.refresh();
11const tmpDir = tmpdir.path;
12
13fs.mkdirSync(path.join(tmpDir, 'nested'));
14fs.mkdirSync(path.join(tmpDir, 'nested2'));
15
16const entry = path.join(tmpDir, 'nested', 'entry.js');
17const entry_link_absolute_path = path.join(tmpDir, 'link.js');
18const submodule = path.join(tmpDir, 'nested2', 'submodule.js');
19const submodule_link_absolute_path = path.join(tmpDir, 'submodule_link.js');
20
21fs.writeFileSync(entry, `
22const assert = require('assert');
23
24// this import only resolves with --preserve-symlinks-main set
25require('./submodule_link.js');
26`);
27fs.writeFileSync(submodule, '');
28
29try {
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
37function 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    '--preserve-symlinks',
44    '--preserve-symlinks-main',
45    entry_link_absolute_path,
46  ], { stdio: 'inherit' })
47    .on('exit', (code) => {
48      assert.strictEqual(code, 0);
49      done();
50    });
51}
52
53// First test the commonjs module loader
54doTest([], () => {
55  // Now test the new loader
56  doTest([], () => {});
57});
58