11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst { spawn } = require('child_process');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst path = require('path');
71cb0ef41Sopenharmony_ciconst fs = require('fs');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
101cb0ef41Sopenharmony_citmpdir.refresh();
111cb0ef41Sopenharmony_ciconst tmpDir = tmpdir.path;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifs.mkdirSync(path.join(tmpDir, 'nested'));
141cb0ef41Sopenharmony_cifs.mkdirSync(path.join(tmpDir, 'nested2'));
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst entry = path.join(tmpDir, 'nested', 'entry.js');
171cb0ef41Sopenharmony_ciconst entry_link_absolute_path = path.join(tmpDir, 'link.js');
181cb0ef41Sopenharmony_ciconst submodule = path.join(tmpDir, 'nested2', 'submodule.js');
191cb0ef41Sopenharmony_ciconst submodule_link_absolute_path = path.join(tmpDir, 'submodule_link.js');
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cifs.writeFileSync(entry, `
221cb0ef41Sopenharmony_ciconst assert = require('assert');
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// this import only resolves with --preserve-symlinks-main set
251cb0ef41Sopenharmony_cirequire('./submodule_link.js');
261cb0ef41Sopenharmony_ci`);
271cb0ef41Sopenharmony_cifs.writeFileSync(submodule, '');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_citry {
301cb0ef41Sopenharmony_ci  fs.symlinkSync(entry, entry_link_absolute_path);
311cb0ef41Sopenharmony_ci  fs.symlinkSync(submodule, submodule_link_absolute_path);
321cb0ef41Sopenharmony_ci} catch (err) {
331cb0ef41Sopenharmony_ci  if (err.code !== 'EPERM') throw err;
341cb0ef41Sopenharmony_ci  common.skip('insufficient privileges for symlinks');
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_cifunction doTest(flags, done) {
381cb0ef41Sopenharmony_ci  // Invoke the main file via a symlink.  In this case --preserve-symlinks-main
391cb0ef41Sopenharmony_ci  // dictates that it'll resolve relative imports in the main file relative to
401cb0ef41Sopenharmony_ci  // the symlink, and not relative to the symlink target; the file structure set
411cb0ef41Sopenharmony_ci  // up above requires this to not crash when loading ./submodule_link.js
421cb0ef41Sopenharmony_ci  spawn(process.execPath, [
431cb0ef41Sopenharmony_ci    '--preserve-symlinks',
441cb0ef41Sopenharmony_ci    '--preserve-symlinks-main',
451cb0ef41Sopenharmony_ci    entry_link_absolute_path,
461cb0ef41Sopenharmony_ci  ], { stdio: 'inherit' })
471cb0ef41Sopenharmony_ci    .on('exit', (code) => {
481cb0ef41Sopenharmony_ci      assert.strictEqual(code, 0);
491cb0ef41Sopenharmony_ci      done();
501cb0ef41Sopenharmony_ci    });
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci// First test the commonjs module loader
541cb0ef41Sopenharmony_cidoTest([], () => {
551cb0ef41Sopenharmony_ci  // Now test the new loader
561cb0ef41Sopenharmony_ci  doTest([], () => {});
571cb0ef41Sopenharmony_ci});
58