Skip to content

Commit 6f34076

Browse files
committed
test: do not write fixture in test-require-symlink
test-require-symlink modifies the fixture directory by adding a symlink. Copy the fixture to the test tmpdir instead of modifying the fixture directory. This also uses a more empirical test for checking for the ability to make symlinks on Windows. PR-URL: #15067 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8a968e4 commit 6f34076

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22
const assert = require('assert');
3+
const path = require('path');
4+
35
const foo = require('./foo');
4-
const fixtures = require('../../common/fixtures');
56

6-
const linkScriptTarget = fixtures.path('module-require-symlink', 'symlinked.js');
7+
const linkScriptEnding = path.join('module-require-symlink', 'symlinked.js');
78

89
assert.strictEqual(foo.dep1.bar.version, 'CORRECT_VERSION');
910
assert.strictEqual(foo.dep2.bar.version, 'CORRECT_VERSION');
10-
assert.strictEqual(__filename, linkScriptTarget);
11+
assert(__filename.endsWith(linkScriptEnding));
1112
assert(__filename in require.cache);

test/parallel/test-require-symlink.js

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,66 @@
11
// Flags: --preserve-symlinks
22
'use strict';
33
const common = require('../common');
4+
5+
if (!common.canCreateSymLink())
6+
common.skip('insufficient privileges');
7+
48
const assert = require('assert');
5-
const path = require('path');
9+
const { spawn } = require('child_process');
610
const fs = require('fs');
7-
const { exec, spawn } = require('child_process');
11+
const path = require('path');
12+
const process = require('process');
13+
14+
// Setup: Copy fixtures to tmp directory.
15+
816
const fixtures = require('../common/fixtures');
17+
const dirName = 'module-require-symlink';
18+
const fixtureSource = fixtures.path(dirName);
19+
const tmpDirTarget = path.join(common.tmpDir, dirName);
920

21+
// Copy fixtureSource to linkTarget recursively.
1022
common.refreshTmpDir();
1123

12-
const linkTarget = fixtures.path('module-require-symlink',
13-
'node_modules',
14-
'dep2');
24+
function copyDir(source, target) {
25+
fs.mkdirSync(target);
26+
fs.readdirSync(source).forEach((entry) => {
27+
const fullPathSource = path.join(source, entry);
28+
const fullPathTarget = path.join(target, entry);
29+
const stats = fs.statSync(fullPathSource);
30+
if (stats.isDirectory()) {
31+
copyDir(fullPathSource, fullPathTarget);
32+
} else {
33+
fs.copyFileSync(fullPathSource, fullPathTarget);
34+
}
35+
});
36+
}
37+
38+
copyDir(fixtureSource, tmpDirTarget);
1539

16-
const linkDir = fixtures.path('module-require-symlink',
17-
'node_modules',
18-
'dep1',
19-
'node_modules',
20-
'dep2');
40+
// Move to tmp dir and do everything with relative paths there so that the test
41+
// doesn't incorrectly fail due to a symlink somewhere else in the absolte path.
42+
process.chdir(common.tmpDir);
2143

22-
const linkScriptTarget = fixtures.path('module-require-symlink',
23-
'symlinked.js');
44+
const linkDir = path.join(dirName,
45+
'node_modules',
46+
'dep1',
47+
'node_modules',
48+
'dep2');
2449

25-
const linkScript = path.join(common.tmpDir, 'module-require-symlink.js');
50+
const linkTarget = path.join('..', '..', 'dep2');
2651

27-
if (common.isWindows) {
28-
// On Windows, creating symlinks requires admin privileges.
29-
// We'll only try to run symlink test if we have enough privileges.
30-
exec('whoami /priv', function(err, o) {
31-
if (err || !o.includes('SeCreateSymbolicLinkPrivilege'))
32-
common.skip('insufficient privileges');
52+
const linkScript = 'linkscript.js';
3353

34-
test();
35-
});
36-
} else {
37-
test();
38-
}
54+
const linkScriptTarget = path.join(dirName, 'symlinked.js');
3955

40-
function test() {
41-
process.on('exit', function() {
42-
fs.unlinkSync(linkDir);
43-
});
56+
test();
4457

58+
function test() {
4559
fs.symlinkSync(linkTarget, linkDir);
4660
fs.symlinkSync(linkScriptTarget, linkScript);
4761

4862
// load symlinked-module
49-
const fooModule = require(fixtures.path('/module-require-symlink/foo.js'));
63+
const fooModule = require(path.join(tmpDirTarget, 'foo.js'));
5064
assert.strictEqual(fooModule.dep1.bar.version, 'CORRECT_VERSION');
5165
assert.strictEqual(fooModule.dep2.bar.version, 'CORRECT_VERSION');
5266

0 commit comments

Comments
 (0)