2016-05-03 01:31:20 +02:00
|
|
|
// Flags: --preserve-symlinks
|
2016-03-30 05:28:35 +02:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
const spawn = require('child_process').spawn;
|
2016-09-21 00:21:44 +02:00
|
|
|
const util = require('util');
|
2016-03-30 05:28:35 +02:00
|
|
|
|
2016-08-28 00:29:13 +02:00
|
|
|
common.refreshTmpDir();
|
|
|
|
|
2016-03-30 05:28:35 +02:00
|
|
|
const linkTarget = path.join(common.fixturesDir,
|
|
|
|
'/module-require-symlink/node_modules/dep2/');
|
|
|
|
|
|
|
|
const linkDir = path.join(common.fixturesDir,
|
|
|
|
'/module-require-symlink/node_modules/dep1/node_modules/dep2');
|
|
|
|
|
|
|
|
const linkScriptTarget = path.join(common.fixturesDir,
|
|
|
|
'/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) {
|
2016-08-28 00:29:13 +02:00
|
|
|
if (err || !o.includes('SeCreateSymbolicLinkPrivilege')) {
|
|
|
|
common.skip('insufficient privileges');
|
2016-03-30 05:28:35 +02:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
test();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test() {
|
|
|
|
process.on('exit', function() {
|
|
|
|
fs.unlinkSync(linkDir);
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.symlinkSync(linkTarget, linkDir);
|
|
|
|
fs.symlinkSync(linkScriptTarget, linkScript);
|
|
|
|
|
|
|
|
// load symlinked-module
|
2016-08-28 00:29:13 +02:00
|
|
|
const fooModule =
|
2016-03-30 05:28:35 +02:00
|
|
|
require(path.join(common.fixturesDir, '/module-require-symlink/foo.js'));
|
2016-08-28 00:29:13 +02:00
|
|
|
assert.strictEqual(fooModule.dep1.bar.version, 'CORRECT_VERSION');
|
|
|
|
assert.strictEqual(fooModule.dep2.bar.version, 'CORRECT_VERSION');
|
2016-03-30 05:28:35 +02:00
|
|
|
|
|
|
|
// load symlinked-script as main
|
2016-08-28 00:29:13 +02:00
|
|
|
const node = process.execPath;
|
|
|
|
const child = spawn(node, ['--preserve-symlinks', linkScript]);
|
2016-03-30 05:28:35 +02:00
|
|
|
child.on('close', function(code, signal) {
|
2016-09-21 00:21:44 +02:00
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
assert(!signal);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Also verify that symlinks works for setting preserve via env variables
|
|
|
|
const childEnv = spawn(node, [linkScript], {
|
|
|
|
env: util._extend(process.env, {NODE_PRESERVE_SYMLINKS: '1'})
|
|
|
|
});
|
|
|
|
childEnv.on('close', function(code, signal) {
|
|
|
|
assert.strictEqual(code, 0);
|
2016-03-30 05:28:35 +02:00
|
|
|
assert(!signal);
|
|
|
|
});
|
|
|
|
}
|