Skip to content

Commit c339dc9

Browse files
committed
test: test cases for circular symlinked deps and peer deps
Adds a test case in the known-test cases to catch nodejs#5950 Adds a test to verify that circular symlinked deps do not recurse forever.
1 parent 8b634e8 commit c339dc9

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* eslint no-irregular-whitespace: 0 */
2+
'use strict';
3+
// Refs: https://github.com/nodejs/node/pull/5950
4+
5+
// This test illustrates the problem that symlinked modules are unable
6+
// to find their peer dependencies. This was fixed in #5950 but that is
7+
// reverted because that particular way of fixing it causes too much
8+
// breakage (breakage that was not caught by either CI or CITGM on multiple
9+
// runs.
10+
11+
const common = require('../common');
12+
const fs = require('fs');
13+
const path = require('path');
14+
const assert = require('assert');
15+
16+
common.refreshTmpDir();
17+
18+
const tmpDir = common.tmpDir;
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+
33+
const moduleA = path.join(tmpDir, 'moduleA');
34+
const app = path.join(tmpDir, 'app');
35+
const moduleB = path.join(app, 'node_modules', 'moduleB');
36+
const moduleA_link = path.join(app, 'node_modules', 'moduleA');
37+
38+
fs.mkdirSync(moduleA);
39+
fs.writeFileSync(path.join(moduleA, 'package.json'),
40+
JSON.stringify({name: 'moduleA', main: 'index.js'}), 'utf8');
41+
fs.writeFileSync(path.join(moduleA, 'index.js'),
42+
'module.exports = require(\'moduleB\');');
43+
44+
fs.mkdirSync(app);
45+
fs.writeFileSync(path.join(app, 'index.js'),
46+
'\'use strict\'; require(\'moduleA\');');
47+
fs.mkdirSync(path.join(app, 'node_modules'));
48+
49+
fs.mkdirSync(moduleB);
50+
fs.writeFileSync(path.join(moduleB, 'package.json'),
51+
JSON.stringify({name: 'moduleB', main: 'index.js'}), 'utf8');
52+
fs.writeFileSync(path.join(moduleB, 'index.js'),
53+
'module.exports = 1;');
54+
55+
fs.symlinkSync(moduleA, moduleA_link);
56+
57+
// This should not throw, but it does
58+
assert.doesNotThrow(() => {
59+
console.log(require(path.join(app, 'index')));
60+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* eslint no-irregular-whitespace: 0 */
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
const path = require('path');
7+
const fs = require('fs');
8+
9+
// {tmpDir}
10+
// ├── index.js
11+
// └── node_modules
12+
// ├── moduleA
13+
// │   ├── index.js
14+
// │   └── node_modules
15+
// │   └── moduleB -> {tmpDir}/node_modules/moduleB
16+
// └── moduleB
17+
// ├── index.js
18+
// └── node_modules
19+
// └── moduleA -> {tmpDir}/node_modules/moduleA
20+
21+
common.refreshTmpDir();
22+
const tmpDir = common.tmpDir;
23+
24+
const node_modules = path.join(tmpDir, 'node_modules');
25+
const moduleA = path.join(node_modules, 'moduleA');
26+
const moduleB = path.join(node_modules, 'moduleB');
27+
const moduleA_link = path.join(moduleB, 'node_modules', 'moduleA');
28+
const moduleB_link = path.join(moduleA, 'node_modules', 'moduleB');
29+
30+
fs.writeFileSync(path.join(tmpDir, 'index.js'),
31+
'module.exports = require(\'moduleA\');', 'utf8');
32+
fs.mkdirSync(node_modules);
33+
fs.mkdirSync(moduleA);
34+
fs.mkdirSync(moduleB);
35+
fs.writeFileSync(path.join(moduleA, 'index.js'),
36+
'module.exports = {b: require(\'moduleB\')};', 'utf8');
37+
fs.writeFileSync(path.join(moduleB, 'index.js'),
38+
'module.exports = {a: require(\'moduleA\')};', 'utf8');
39+
fs.mkdirSync(path.join(moduleA, 'node_modules'));
40+
fs.mkdirSync(path.join(moduleB, 'node_modules'));
41+
fs.symlinkSync(moduleA, moduleA_link);
42+
fs.symlinkSync(moduleB, moduleB_link);
43+
44+
// Ensure that the symlinks are not followed forever...
45+
const obj = require(path.join(tmpDir, 'index'));
46+
assert.ok(obj);
47+
assert.ok(obj.b);
48+
assert.ok(obj.b.a);
49+
assert.ok(!obj.b.a.b);

0 commit comments

Comments
 (0)