aboutsummaryrefslogtreecommitdiff
path: root/test/parallel/test-require-symlink.js
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2017-08-27 10:50:00 -0700
committerRich Trott <rtrott@gmail.com>2017-09-19 15:42:16 -0700
commit6f340762d8296902de079208bd55bf7118814db7 (patch)
treea258c75425dcf84851d962d826f3dd32180e6b85 /test/parallel/test-require-symlink.js
parent8a968e4ee7b298496c0e781deccbd95e3717d386 (diff)
downloadandroid-node-v8-6f340762d8296902de079208bd55bf7118814db7.tar.gz
android-node-v8-6f340762d8296902de079208bd55bf7118814db7.tar.bz2
android-node-v8-6f340762d8296902de079208bd55bf7118814db7.zip
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: https://github.com/nodejs/node/pull/15067 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-require-symlink.js')
-rw-r--r--test/parallel/test-require-symlink.js72
1 files changed, 43 insertions, 29 deletions
diff --git a/test/parallel/test-require-symlink.js b/test/parallel/test-require-symlink.js
index 4dd9bf51ed..7dde2a1a97 100644
--- a/test/parallel/test-require-symlink.js
+++ b/test/parallel/test-require-symlink.js
@@ -1,52 +1,66 @@
// Flags: --preserve-symlinks
'use strict';
const common = require('../common');
+
+if (!common.canCreateSymLink())
+ common.skip('insufficient privileges');
+
const assert = require('assert');
-const path = require('path');
+const { spawn } = require('child_process');
const fs = require('fs');
-const { exec, spawn } = require('child_process');
+const path = require('path');
+const process = require('process');
+
+// Setup: Copy fixtures to tmp directory.
+
const fixtures = require('../common/fixtures');
+const dirName = 'module-require-symlink';
+const fixtureSource = fixtures.path(dirName);
+const tmpDirTarget = path.join(common.tmpDir, dirName);
+// Copy fixtureSource to linkTarget recursively.
common.refreshTmpDir();
-const linkTarget = fixtures.path('module-require-symlink',
- 'node_modules',
- 'dep2');
+function copyDir(source, target) {
+ fs.mkdirSync(target);
+ fs.readdirSync(source).forEach((entry) => {
+ const fullPathSource = path.join(source, entry);
+ const fullPathTarget = path.join(target, entry);
+ const stats = fs.statSync(fullPathSource);
+ if (stats.isDirectory()) {
+ copyDir(fullPathSource, fullPathTarget);
+ } else {
+ fs.copyFileSync(fullPathSource, fullPathTarget);
+ }
+ });
+}
+
+copyDir(fixtureSource, tmpDirTarget);
-const linkDir = fixtures.path('module-require-symlink',
- 'node_modules',
- 'dep1',
- 'node_modules',
- 'dep2');
+// Move to tmp dir and do everything with relative paths there so that the test
+// doesn't incorrectly fail due to a symlink somewhere else in the absolte path.
+process.chdir(common.tmpDir);
-const linkScriptTarget = fixtures.path('module-require-symlink',
- 'symlinked.js');
+const linkDir = path.join(dirName,
+ 'node_modules',
+ 'dep1',
+ 'node_modules',
+ 'dep2');
-const linkScript = path.join(common.tmpDir, 'module-require-symlink.js');
+const linkTarget = path.join('..', '..', 'dep2');
-if (common.isWindows) {
- // On Windows, creating symlinks requires admin privileges.
- // We'll only try to run symlink test if we have enough privileges.
- exec('whoami /priv', function(err, o) {
- if (err || !o.includes('SeCreateSymbolicLinkPrivilege'))
- common.skip('insufficient privileges');
+const linkScript = 'linkscript.js';
- test();
- });
-} else {
- test();
-}
+const linkScriptTarget = path.join(dirName, 'symlinked.js');
-function test() {
- process.on('exit', function() {
- fs.unlinkSync(linkDir);
- });
+test();
+function test() {
fs.symlinkSync(linkTarget, linkDir);
fs.symlinkSync(linkScriptTarget, linkScript);
// load symlinked-module
- const fooModule = require(fixtures.path('/module-require-symlink/foo.js'));
+ const fooModule = require(path.join(tmpDirTarget, 'foo.js'));
assert.strictEqual(fooModule.dep1.bar.version, 'CORRECT_VERSION');
assert.strictEqual(fooModule.dep2.bar.version, 'CORRECT_VERSION');