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>
This commit is contained in:
Rich Trott 2017-08-27 10:50:00 -07:00
parent 8a968e4ee7
commit 6f340762d8
2 changed files with 54 additions and 39 deletions

View File

@ -1,11 +1,12 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('assert');
const foo = require('./foo'); const path = require('path');
const fixtures = require('../../common/fixtures');
const linkScriptTarget = fixtures.path('module-require-symlink', 'symlinked.js'); const foo = require('./foo');
const linkScriptEnding = path.join('module-require-symlink', 'symlinked.js');
assert.strictEqual(foo.dep1.bar.version, 'CORRECT_VERSION'); assert.strictEqual(foo.dep1.bar.version, 'CORRECT_VERSION');
assert.strictEqual(foo.dep2.bar.version, 'CORRECT_VERSION'); assert.strictEqual(foo.dep2.bar.version, 'CORRECT_VERSION');
assert.strictEqual(__filename, linkScriptTarget); assert(__filename.endsWith(linkScriptEnding));
assert(__filename in require.cache); assert(__filename in require.cache);

View File

@ -1,52 +1,66 @@
// Flags: --preserve-symlinks // Flags: --preserve-symlinks
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const { exec, spawn } = require('child_process');
const fixtures = require('../common/fixtures');
if (!common.canCreateSymLink())
common.skip('insufficient privileges');
const assert = require('assert');
const { spawn } = require('child_process');
const fs = require('fs');
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(); common.refreshTmpDir();
const linkTarget = fixtures.path('module-require-symlink', function copyDir(source, target) {
'node_modules', fs.mkdirSync(target);
'dep2'); fs.readdirSync(source).forEach((entry) => {
const fullPathSource = path.join(source, entry);
const linkDir = fixtures.path('module-require-symlink', const fullPathTarget = path.join(target, entry);
'node_modules', const stats = fs.statSync(fullPathSource);
'dep1', if (stats.isDirectory()) {
'node_modules', copyDir(fullPathSource, fullPathTarget);
'dep2'); } else {
fs.copyFileSync(fullPathSource, fullPathTarget);
const linkScriptTarget = fixtures.path('module-require-symlink', }
'symlinked.js');
const linkScript = path.join(common.tmpDir, 'module-require-symlink.js');
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');
test();
}); });
} else {
test();
} }
function test() { copyDir(fixtureSource, tmpDirTarget);
process.on('exit', function() {
fs.unlinkSync(linkDir);
});
// 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 linkDir = path.join(dirName,
'node_modules',
'dep1',
'node_modules',
'dep2');
const linkTarget = path.join('..', '..', 'dep2');
const linkScript = 'linkscript.js';
const linkScriptTarget = path.join(dirName, 'symlinked.js');
test();
function test() {
fs.symlinkSync(linkTarget, linkDir); fs.symlinkSync(linkTarget, linkDir);
fs.symlinkSync(linkScriptTarget, linkScript); fs.symlinkSync(linkScriptTarget, linkScript);
// load symlinked-module // 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.dep1.bar.version, 'CORRECT_VERSION');
assert.strictEqual(fooModule.dep2.bar.version, 'CORRECT_VERSION'); assert.strictEqual(fooModule.dep2.bar.version, 'CORRECT_VERSION');