|
1 | 1 | // Flags: --preserve-symlinks
|
2 | 2 | 'use strict';
|
3 | 3 | const common = require('../common');
|
| 4 | + |
| 5 | +if (!common.canCreateSymLink()) |
| 6 | + common.skip('insufficient privileges'); |
| 7 | + |
4 | 8 | const assert = require('assert');
|
5 |
| -const path = require('path'); |
| 9 | +const { spawn } = require('child_process'); |
6 | 10 | 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 | + |
8 | 16 | 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); |
9 | 20 |
|
| 21 | +// Copy fixtureSource to linkTarget recursively. |
10 | 22 | common.refreshTmpDir();
|
11 | 23 |
|
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); |
15 | 39 |
|
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); |
21 | 43 |
|
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'); |
24 | 49 |
|
25 |
| -const linkScript = path.join(common.tmpDir, 'module-require-symlink.js'); |
| 50 | +const linkTarget = path.join('..', '..', 'dep2'); |
26 | 51 |
|
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'; |
33 | 53 |
|
34 |
| - test(); |
35 |
| - }); |
36 |
| -} else { |
37 |
| - test(); |
38 |
| -} |
| 54 | +const linkScriptTarget = path.join(dirName, 'symlinked.js'); |
39 | 55 |
|
40 |
| -function test() { |
41 |
| - process.on('exit', function() { |
42 |
| - fs.unlinkSync(linkDir); |
43 |
| - }); |
| 56 | +test(); |
44 | 57 |
|
| 58 | +function test() { |
45 | 59 | fs.symlinkSync(linkTarget, linkDir);
|
46 | 60 | fs.symlinkSync(linkScriptTarget, linkScript);
|
47 | 61 |
|
48 | 62 | // load symlinked-module
|
49 |
| - const fooModule = require(fixtures.path('/module-require-symlink/foo.js')); |
| 63 | + const fooModule = require(path.join(tmpDirTarget, 'foo.js')); |
50 | 64 | assert.strictEqual(fooModule.dep1.bar.version, 'CORRECT_VERSION');
|
51 | 65 | assert.strictEqual(fooModule.dep2.bar.version, 'CORRECT_VERSION');
|
52 | 66 |
|
|
0 commit comments