test: remove envPlus, use Object.assign everywhere

PR-URL: https://github.com/nodejs/node/pull/14845
Fixes: https://github.com/nodejs/node/issues/14823
Refs: https://github.com/nodejs/node/pull/14822
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Gibson Fahnestock 2017-08-15 19:14:54 +01:00 committed by Ruben Bridgewater
parent ed084a035c
commit 180f86507d
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
15 changed files with 37 additions and 39 deletions

View File

@ -26,9 +26,10 @@ const argv = ['--set', 'algo=sha256',
'--set', 'v=crypto', '--set', 'v=crypto',
'--set', 'writes=1', '--set', 'writes=1',
'crypto']; 'crypto'];
const env = Object.assign({}, process.env,
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); const child = fork(runjs, argv, { env: Object.assign({}, process.env, {
const child = fork(runjs, argv, { env }); NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }) });
child.on('exit', (code, signal) => { child.on('exit', (code, signal) => {
assert.strictEqual(code, 0); assert.strictEqual(code, 0);
assert.strictEqual(signal, null); assert.strictEqual(signal, null);

View File

@ -34,9 +34,11 @@ Object.setPrototypeOf(env, {
let child; let child;
if (common.isWindows) { if (common.isWindows) {
child = spawn('cmd.exe', ['/c', 'set'], { env: env }); child = spawn('cmd.exe', ['/c', 'set'],
Object.assign({}, process.env, { env: env }));
} else { } else {
child = spawn('/usr/bin/env', [], { env: env }); child = spawn('/usr/bin/env', [],
Object.assign({}, process.env, { env: env }));
} }

View File

@ -44,7 +44,9 @@ function after(err, stdout, stderr) {
if (!common.isWindows) { if (!common.isWindows) {
child = exec('/usr/bin/env', { env: { 'HELLO': 'WORLD' } }, after); child = exec('/usr/bin/env', { env: { 'HELLO': 'WORLD' } }, after);
} else { } else {
child = exec('set', { env: { 'HELLO': 'WORLD' } }, after); child = exec('set',
{ env: Object.assign({}, process.env, { 'HELLO': 'WORLD' }) },
after);
} }
child.stdout.setEncoding('utf8'); child.stdout.setEncoding('utf8');

View File

@ -29,7 +29,8 @@ disallow('--');
disallow('--no_warnings'); // Node options don't allow '_' instead of '-'. disallow('--no_warnings'); // Node options don't allow '_' instead of '-'.
function disallow(opt) { function disallow(opt) {
const options = { env: { NODE_OPTIONS: opt } }; const options = { env: Object.assign({}, process.env,
{ NODE_OPTIONS: opt }) };
exec(process.execPath, options, common.mustCall(function(err) { exec(process.execPath, options, common.mustCall(function(err) {
const message = err.message.split(/\r?\n/)[1]; const message = err.message.split(/\r?\n/)[1];
const expect = `${process.execPath}: ${opt} is not allowed in NODE_OPTIONS`; const expect = `${process.execPath}: ${opt} is not allowed in NODE_OPTIONS`;
@ -71,7 +72,7 @@ function expect(opt, want) {
const printB = require.resolve('../fixtures/printB.js'); const printB = require.resolve('../fixtures/printB.js');
const argv = [printB]; const argv = [printB];
const opts = { const opts = {
env: { NODE_OPTIONS: opt }, env: Object.assign({}, process.env, { NODE_OPTIONS: opt }),
maxBuffer: 1000000000, maxBuffer: 1000000000,
}; };
exec(process.execPath, argv, opts, common.mustCall(function(err, stdout) { exec(process.execPath, argv, opts, common.mustCall(function(err, stdout) {

View File

@ -26,15 +26,6 @@ function sharedOpenSSL() {
return process.config.variables.node_shared_openssl; return process.config.variables.node_shared_openssl;
} }
function addToEnv(newVar, value) {
const envCopy = {};
for (const e in process.env) {
envCopy[e] = process.env[e];
}
envCopy[newVar] = value;
return envCopy;
}
function testHelper(stream, args, expectedOutput, cmd, env) { function testHelper(stream, args, expectedOutput, cmd, env) {
const fullArgs = args.concat(['-e', `console.log(${cmd})`]); const fullArgs = args.concat(['-e', `console.log(${cmd})`]);
const child = spawnSync(process.execPath, fullArgs, { const child = spawnSync(process.execPath, fullArgs, {
@ -72,7 +63,7 @@ testHelper(
[], [],
FIPS_DISABLED, FIPS_DISABLED,
'require("crypto").fips', 'require("crypto").fips',
addToEnv('OPENSSL_CONF', '')); Object.assign({}, process.env, { 'OPENSSL_CONF': '' }));
// --enable-fips should turn FIPS mode on // --enable-fips should turn FIPS mode on
testHelper( testHelper(
@ -117,7 +108,7 @@ if (!sharedOpenSSL()) {
[], [],
compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED, compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED,
'require("crypto").fips', 'require("crypto").fips',
addToEnv('OPENSSL_CONF', CNF_FIPS_ON)); Object.assign({}, process.env, { 'OPENSSL_CONF': CNF_FIPS_ON }));
// --openssl-config option should override OPENSSL_CONF // --openssl-config option should override OPENSSL_CONF
testHelper( testHelper(
@ -125,7 +116,7 @@ if (!sharedOpenSSL()) {
[`--openssl-config=${CNF_FIPS_ON}`], [`--openssl-config=${CNF_FIPS_ON}`],
compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED, compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED,
'require("crypto").fips', 'require("crypto").fips',
addToEnv('OPENSSL_CONF', CNF_FIPS_OFF)); Object.assign({}, process.env, { 'OPENSSL_CONF': CNF_FIPS_OFF }));
} }
testHelper( testHelper(
@ -133,7 +124,7 @@ testHelper(
[`--openssl-config=${CNF_FIPS_OFF}`], [`--openssl-config=${CNF_FIPS_OFF}`],
FIPS_DISABLED, FIPS_DISABLED,
'require("crypto").fips', 'require("crypto").fips',
addToEnv('OPENSSL_CONF', CNF_FIPS_ON)); Object.assign({}, process.env, { 'OPENSSL_CONF': CNF_FIPS_ON }));
// --enable-fips should take precedence over OpenSSL config file // --enable-fips should take precedence over OpenSSL config file
testHelper( testHelper(
@ -149,7 +140,7 @@ testHelper(
['--enable-fips'], ['--enable-fips'],
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING, compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
'require("crypto").fips', 'require("crypto").fips',
addToEnv('OPENSSL_CONF', CNF_FIPS_OFF)); Object.assign({}, process.env, { 'OPENSSL_CONF': CNF_FIPS_OFF }));
// --force-fips should take precedence over OpenSSL config file // --force-fips should take precedence over OpenSSL config file
testHelper( testHelper(
@ -165,7 +156,7 @@ testHelper(
['--force-fips'], ['--force-fips'],
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING, compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
'require("crypto").fips', 'require("crypto").fips',
addToEnv('OPENSSL_CONF', CNF_FIPS_OFF)); Object.assign({}, process.env, { 'OPENSSL_CONF': CNF_FIPS_OFF }));
// setFipsCrypto should be able to turn FIPS mode on // setFipsCrypto should be able to turn FIPS mode on
testHelper( testHelper(

View File

@ -33,7 +33,7 @@ const fixtures = require('../common/fixtures');
function test(env, cb) { function test(env, cb) {
const filename = fixtures.path('test-fs-readfile-error.js'); const filename = fixtures.path('test-fs-readfile-error.js');
const execPath = `"${process.execPath}" "${filename}"`; const execPath = `"${process.execPath}" "${filename}"`;
const options = { env: Object.assign(process.env, env) }; const options = { env: Object.assign({}, process.env, env) };
exec(execPath, options, common.mustCall((err, stdout, stderr) => { exec(execPath, options, common.mustCall((err, stdout, stderr) => {
assert(err); assert(err);
assert.strictEqual(stdout, ''); assert.strictEqual(stdout, '');

View File

@ -22,7 +22,6 @@
'use strict'; 'use strict';
require('../common'); require('../common');
const http = require('http'); const http = require('http');
const util = require('util');
const fork = require('child_process').fork; const fork = require('child_process').fork;
if (process.env.NODE_TEST_FORK_PORT) { if (process.env.NODE_TEST_FORK_PORT) {
@ -45,7 +44,7 @@ if (process.env.NODE_TEST_FORK_PORT) {
}); });
server.listen(0, function() { server.listen(0, function() {
fork(__filename, { fork(__filename, {
env: util._extend(process.env, { env: Object.assign({}, process.env, {
NODE_TEST_FORK_PORT: this.address().port NODE_TEST_FORK_PORT: this.address().port
}) })
}); });

View File

@ -17,7 +17,7 @@ const expected =
} }
{ {
const env = { NODE_ICU_DATA: '/' }; const env = Object.assign({}, process.env, { NODE_ICU_DATA: '/' });
const child = spawnSync(process.execPath, ['-e', '0'], { env }); const child = spawnSync(process.execPath, ['-e', '0'], { env });
assert(child.stderr.toString().includes(expected)); assert(child.stderr.toString().includes(expected));
} }

View File

@ -13,7 +13,8 @@ const url = require('url');
if (process.env.BE_CHILD) if (process.env.BE_CHILD)
return beChild(); return beChild();
const child = fork(__filename, { env: { BE_CHILD: 1 } }); const child = fork(__filename,
{ env: Object.assign({}, process.env, { BE_CHILD: 1 }) });
child.once('message', common.mustCall((msg) => { child.once('message', common.mustCall((msg) => {
assert.strictEqual(msg.cmd, 'started'); assert.strictEqual(msg.cmd, 'started');

View File

@ -34,11 +34,12 @@ const pkgPath = path.join(installDir, 'package.json');
fs.writeFileSync(pkgPath, pkgContent); fs.writeFileSync(pkgPath, pkgContent);
const env = Object.create(process.env); const env = Object.assign({}, process.env, {
env['PATH'] = path.dirname(process.execPath); PATH: path.dirname(process.execPath),
env['NPM_CONFIG_PREFIX'] = path.join(npmSandbox, 'npm-prefix'); NPM_CONFIG_PREFIX: path.join(npmSandbox, 'npm-prefix'),
env['NPM_CONFIG_TMP'] = path.join(npmSandbox, 'npm-tmp'); NPM_CONFIG_TMP: path.join(npmSandbox, 'npm-tmp'),
env['HOME'] = path.join(npmSandbox, 'home'); HOME: path.join(npmSandbox, 'home'),
});
exec(`${process.execPath} ${npmPath} install`, { exec(`${process.execPath} ${npmPath} install`, {
cwd: installDir, cwd: installDir,

View File

@ -37,7 +37,7 @@ switch (process.argv[2]) {
// Test the NODE_PENDING_DEPRECATION environment var. // Test the NODE_PENDING_DEPRECATION environment var.
fork(__filename, ['env'], { fork(__filename, ['env'], {
env: { NODE_PENDING_DEPRECATION: 1 }, env: Object.assign({}, process.env, { NODE_PENDING_DEPRECATION: 1 }),
silent: true silent: true
}).on('exit', common.mustCall((code) => { }).on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0, message('NODE_PENDING_DEPRECATION')); assert.strictEqual(code, 0, message('NODE_PENDING_DEPRECATION'));

View File

@ -16,7 +16,8 @@ common.refreshTmpDir();
const warnmod = require.resolve(`${common.fixturesDir}/warnings.js`); const warnmod = require.resolve(`${common.fixturesDir}/warnings.js`);
const warnpath = path.join(common.tmpDir, 'warnings.txt'); const warnpath = path.join(common.tmpDir, 'warnings.txt');
fork(warnmod, { env: { NODE_REDIRECT_WARNINGS: warnpath } }) fork(warnmod, { env: Object.assign({}, process.env,
{ NODE_REDIRECT_WARNINGS: warnpath }) })
.on('exit', common.mustCall(() => { .on('exit', common.mustCall(() => {
fs.readFile(warnpath, 'utf8', common.mustCall((err, data) => { fs.readFile(warnpath, 'utf8', common.mustCall((err, data) => {
assert.ifError(err); assert.ifError(err);

View File

@ -36,7 +36,7 @@ const tests = [
]; ];
function run(test) { function run(test) {
const env = test.env; const env = Object.assign({}, process.env, test.env);
const expected = test.expected; const expected = test.expected;
const opts = { const opts = {
terminal: true, terminal: true,

View File

@ -5,7 +5,6 @@ const assert = require('assert');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const { exec, spawn } = require('child_process'); const { exec, spawn } = require('child_process');
const util = require('util');
const fixtures = require('../common/fixtures'); const fixtures = require('../common/fixtures');
common.refreshTmpDir(); common.refreshTmpDir();
@ -61,7 +60,7 @@ function test() {
// Also verify that symlinks works for setting preserve via env variables // Also verify that symlinks works for setting preserve via env variables
const childEnv = spawn(node, [linkScript], { const childEnv = spawn(node, [linkScript], {
env: util._extend(process.env, { NODE_PRESERVE_SYMLINKS: '1' }) env: Object.assign({}, process.env, { NODE_PRESERVE_SYMLINKS: '1' })
}); });
childEnv.on('close', function(code, signal) { childEnv.on('close', function(code, signal) {
assert.strictEqual(code, 0); assert.strictEqual(code, 0);

View File

@ -5,7 +5,7 @@ const assert = require('assert');
const { spawn } = require('child_process'); const { spawn } = require('child_process');
for (const args of [[], ['-']]) { for (const args of [[], ['-']]) {
const child = spawn(process.execPath, args, { const child = spawn(process.execPath, args, {
env: Object.assign(process.env, { env: Object.assign({}, process.env, {
NODE_DEBUG: process.argv[2] NODE_DEBUG: process.argv[2]
}) })
}); });