• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --preserve-symlinks
2'use strict';
3// Refs: https://github.com/nodejs/node/pull/5950
4
5// This test verifies that symlinked modules are able to find their peer
6// dependencies when using the --preserve-symlinks command line flag.
7
8// This test passes in v6.2+ with --preserve-symlinks on and in v6.0/v6.1.
9// This test will fail in Node.js v4 and v5 and should not be backported.
10
11const common = require('../common');
12const fs = require('fs');
13const path = require('path');
14
15const tmpdir = require('../common/tmpdir');
16tmpdir.refresh();
17
18const tmpDir = tmpdir.path;
19
20// Creates the following structure
21// {tmpDir}
22// ├── app
23// │   ├── index.js
24// │   └── node_modules
25// │       ├── moduleA -> {tmpDir}/moduleA
26// │       └── moduleB
27// │           ├── index.js
28// │           └── package.json
29// └── moduleA
30//     ├── index.js
31//     └── package.json
32
33const moduleA = path.join(tmpDir, 'moduleA');
34const app = path.join(tmpDir, 'app');
35const moduleB = path.join(app, 'node_modules', 'moduleB');
36const moduleA_link = path.join(app, 'node_modules', 'moduleA');
37fs.mkdirSync(moduleA);
38fs.mkdirSync(app);
39fs.mkdirSync(path.join(app, 'node_modules'));
40fs.mkdirSync(moduleB);
41
42// Attempt to make the symlink. If this fails due to lack of sufficient
43// permissions, the test will bail out and be skipped.
44try {
45  fs.symlinkSync(moduleA, moduleA_link, 'dir');
46} catch (err) {
47  if (err.code !== 'EPERM') throw err;
48  common.skip('insufficient privileges for symlinks');
49}
50
51fs.writeFileSync(path.join(moduleA, 'package.json'),
52                 JSON.stringify({ name: 'moduleA', main: 'index.js' }), 'utf8');
53fs.writeFileSync(path.join(moduleA, 'index.js'),
54                 'module.exports = require(\'moduleB\');', 'utf8');
55fs.writeFileSync(path.join(app, 'index.js'),
56                 '\'use strict\'; require(\'moduleA\');', 'utf8');
57fs.writeFileSync(path.join(moduleB, 'package.json'),
58                 JSON.stringify({ name: 'moduleB', main: 'index.js' }), 'utf8');
59fs.writeFileSync(path.join(moduleB, 'index.js'),
60                 'module.exports = 1;', 'utf8');
61
62require(path.join(app, 'index')); // Should not throw.
63