11cb0ef41Sopenharmony_ci// Flags: --preserve-symlinks
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/pull/5950
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// This test verifies that symlinked modules are able to find their peer
61cb0ef41Sopenharmony_ci// dependencies when using the --preserve-symlinks command line flag.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// This test passes in v6.2+ with --preserve-symlinks on and in v6.0/v6.1.
91cb0ef41Sopenharmony_ci// This test will fail in Node.js v4 and v5 and should not be backported.
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst common = require('../common');
121cb0ef41Sopenharmony_ciconst fs = require('fs');
131cb0ef41Sopenharmony_ciconst path = require('path');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
161cb0ef41Sopenharmony_citmpdir.refresh();
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst tmpDir = tmpdir.path;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci// Creates the following structure
211cb0ef41Sopenharmony_ci// {tmpDir}
221cb0ef41Sopenharmony_ci// ├── app
231cb0ef41Sopenharmony_ci// │   ├── index.js
241cb0ef41Sopenharmony_ci// │   └── node_modules
251cb0ef41Sopenharmony_ci// │       ├── moduleA -> {tmpDir}/moduleA
261cb0ef41Sopenharmony_ci// │       └── moduleB
271cb0ef41Sopenharmony_ci// │           ├── index.js
281cb0ef41Sopenharmony_ci// │           └── package.json
291cb0ef41Sopenharmony_ci// └── moduleA
301cb0ef41Sopenharmony_ci//     ├── index.js
311cb0ef41Sopenharmony_ci//     └── package.json
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst moduleA = path.join(tmpDir, 'moduleA');
341cb0ef41Sopenharmony_ciconst app = path.join(tmpDir, 'app');
351cb0ef41Sopenharmony_ciconst moduleB = path.join(app, 'node_modules', 'moduleB');
361cb0ef41Sopenharmony_ciconst moduleA_link = path.join(app, 'node_modules', 'moduleA');
371cb0ef41Sopenharmony_cifs.mkdirSync(moduleA);
381cb0ef41Sopenharmony_cifs.mkdirSync(app);
391cb0ef41Sopenharmony_cifs.mkdirSync(path.join(app, 'node_modules'));
401cb0ef41Sopenharmony_cifs.mkdirSync(moduleB);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// Attempt to make the symlink. If this fails due to lack of sufficient
431cb0ef41Sopenharmony_ci// permissions, the test will bail out and be skipped.
441cb0ef41Sopenharmony_citry {
451cb0ef41Sopenharmony_ci  fs.symlinkSync(moduleA, moduleA_link, 'dir');
461cb0ef41Sopenharmony_ci} catch (err) {
471cb0ef41Sopenharmony_ci  if (err.code !== 'EPERM') throw err;
481cb0ef41Sopenharmony_ci  common.skip('insufficient privileges for symlinks');
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_cifs.writeFileSync(path.join(moduleA, 'package.json'),
521cb0ef41Sopenharmony_ci                 JSON.stringify({ name: 'moduleA', main: 'index.js' }), 'utf8');
531cb0ef41Sopenharmony_cifs.writeFileSync(path.join(moduleA, 'index.js'),
541cb0ef41Sopenharmony_ci                 'module.exports = require(\'moduleB\');', 'utf8');
551cb0ef41Sopenharmony_cifs.writeFileSync(path.join(app, 'index.js'),
561cb0ef41Sopenharmony_ci                 '\'use strict\'; require(\'moduleA\');', 'utf8');
571cb0ef41Sopenharmony_cifs.writeFileSync(path.join(moduleB, 'package.json'),
581cb0ef41Sopenharmony_ci                 JSON.stringify({ name: 'moduleB', main: 'index.js' }), 'utf8');
591cb0ef41Sopenharmony_cifs.writeFileSync(path.join(moduleB, 'index.js'),
601cb0ef41Sopenharmony_ci                 'module.exports = 1;', 'utf8');
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cirequire(path.join(app, 'index')); // Should not throw.
63