From 2c8fe9748c33abf7f5ce82ca40ca4399639dae34 Mon Sep 17 00:00:00 2001 From: Gibson Fahnestock Date: Tue, 15 Aug 2017 19:14:54 +0100 Subject: [PATCH] test: remove envPlus, use Object.assign everywhere MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/14845 Backport-PR-URL: https://github.com/nodejs/node/pull/15557 Fixes: https://github.com/nodejs/node/issues/14823 Refs: https://github.com/nodejs/node/pull/14822 Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Yuta Hiroto Reviewed-By: Ruben Bridgewater Reviewed-By: Tobias Nießen Reviewed-By: Rich Trott --- test/parallel/test-child-process-env.js | 7 +++++-- test/parallel/test-child-process-exec-env.js | 4 +++- test/parallel/test-crypto-fips.js | 21 ++++++------------- test/parallel/test-fs-readfile-error.js | 2 +- test/parallel/test-http-server-stale-close.js | 5 +++-- test/parallel/test-npm-install.js | 11 +++++----- test/parallel/test-repl-envvars.js | 2 +- test/parallel/test-stdin-script-child.js | 2 +- test/sequential/test-net-GH-5504.js | 2 +- test/sequential/test-util-debug.js | 2 +- 10 files changed, 28 insertions(+), 30 deletions(-) diff --git a/test/parallel/test-child-process-env.js b/test/parallel/test-child-process-env.js index 9430e79593e0fd..f25a6f8bb4f50a 100644 --- a/test/parallel/test-child-process-env.js +++ b/test/parallel/test-child-process-env.js @@ -13,9 +13,12 @@ Object.setPrototypeOf(env, { let child; if (common.isWindows) { - child = spawn('cmd.exe', ['/c', 'set'], {env: env}); + child = spawn('cmd.exe', ['/c', 'set'], + Object.assign({}, process.env, { env: env })); } else { - child = spawn('/usr/bin/env', [], {env: env}); + child = spawn('/usr/bin/env', [], { env: env }); + child = spawn('/usr/bin/env', [], + Object.assign({}, process.env, { env: env })); } diff --git a/test/parallel/test-child-process-exec-env.js b/test/parallel/test-child-process-exec-env.js index 15156bbd311eb3..c3e5eb8ea7daee 100644 --- a/test/parallel/test-child-process-exec-env.js +++ b/test/parallel/test-child-process-exec-env.js @@ -23,7 +23,9 @@ function after(err, stdout, stderr) { if (!common.isWindows) { child = exec('/usr/bin/env', { env: { 'HELLO': 'WORLD' } }, after); } else { - child = exec('set', { env: { 'HELLO': 'WORLD' } }, after); + child = exec('set', + { env: Object.assign({}, process.env, { 'HELLO': 'WORLD' }) }, + after); } child.stdout.setEncoding('utf8'); diff --git a/test/parallel/test-crypto-fips.js b/test/parallel/test-crypto-fips.js index e3f49e61672bb7..7f3ac9e26baefb 100644 --- a/test/parallel/test-crypto-fips.js +++ b/test/parallel/test-crypto-fips.js @@ -23,15 +23,6 @@ function sharedOpenSSL() { 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) { const fullArgs = args.concat(['-e', `console.log(${cmd})`]); const child = spawnSync(process.execPath, fullArgs, { @@ -69,7 +60,7 @@ testHelper( [], FIPS_DISABLED, 'require("crypto").fips', - addToEnv('OPENSSL_CONF', '')); + Object.assign({}, process.env, { 'OPENSSL_CONF': '' })); // --enable-fips should turn FIPS mode on testHelper( @@ -114,7 +105,7 @@ if (!sharedOpenSSL()) { [], compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED, '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 testHelper( @@ -122,7 +113,7 @@ if (!sharedOpenSSL()) { [`--openssl-config=${CNF_FIPS_ON}`], compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED, 'require("crypto").fips', - addToEnv('OPENSSL_CONF', CNF_FIPS_OFF)); + Object.assign({}, process.env, { 'OPENSSL_CONF': CNF_FIPS_OFF })); } testHelper( @@ -130,7 +121,7 @@ testHelper( [`--openssl-config=${CNF_FIPS_OFF}`], FIPS_DISABLED, '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 testHelper( @@ -146,7 +137,7 @@ testHelper( ['--enable-fips'], compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING, '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 testHelper( @@ -162,7 +153,7 @@ testHelper( ['--force-fips'], compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING, '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 testHelper( diff --git a/test/parallel/test-fs-readfile-error.js b/test/parallel/test-fs-readfile-error.js index b3997f7a7d619a..53b9cf7d588cfe 100644 --- a/test/parallel/test-fs-readfile-error.js +++ b/test/parallel/test-fs-readfile-error.js @@ -12,7 +12,7 @@ const path = require('path'); function test(env, cb) { const filename = path.join(common.fixturesDir, 'test-fs-readfile-error.js'); 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) => { assert(err); assert.strictEqual(stdout, ''); diff --git a/test/parallel/test-http-server-stale-close.js b/test/parallel/test-http-server-stale-close.js index 3728453c039447..188fbdbe528cff 100644 --- a/test/parallel/test-http-server-stale-close.js +++ b/test/parallel/test-http-server-stale-close.js @@ -1,7 +1,6 @@ 'use strict'; require('../common'); const http = require('http'); -const util = require('util'); const fork = require('child_process').fork; if (process.env.NODE_TEST_FORK_PORT) { @@ -24,7 +23,9 @@ if (process.env.NODE_TEST_FORK_PORT) { }); server.listen(0, function() { fork(__filename, { - env: util._extend(process.env, {NODE_TEST_FORK_PORT: this.address().port}) + env: Object.assign({}, process.env, { + NODE_TEST_FORK_PORT: this.address().port + }) }); }); } diff --git a/test/parallel/test-npm-install.js b/test/parallel/test-npm-install.js index 46aa6c763e90fc..8c7443c21f82ee 100644 --- a/test/parallel/test-npm-install.js +++ b/test/parallel/test-npm-install.js @@ -32,11 +32,12 @@ const pkgPath = path.join(installDir, 'package.json'); fs.writeFileSync(pkgPath, pkgContent); -const env = Object.create(process.env); -env['PATH'] = path.dirname(process.execPath); -env['NPM_CONFIG_PREFIX'] = path.join(npmSandbox, 'npm-prefix'); -env['NPM_CONFIG_TMP'] = path.join(npmSandbox, 'npm-tmp'); -env['HOME'] = path.join(npmSandbox, 'home'); +const env = Object.assign({}, process.env, { + PATH: path.dirname(process.execPath), + NPM_CONFIG_PREFIX: path.join(npmSandbox, 'npm-prefix'), + NPM_CONFIG_TMP: path.join(npmSandbox, 'npm-tmp'), + HOME: path.join(npmSandbox, 'home'), +}); exec(`${process.execPath} ${npmPath} install`, { cwd: installDir, diff --git a/test/parallel/test-repl-envvars.js b/test/parallel/test-repl-envvars.js index 5f597e0abb78ca..c0b1f53b3c2efc 100644 --- a/test/parallel/test-repl-envvars.js +++ b/test/parallel/test-repl-envvars.js @@ -36,7 +36,7 @@ const tests = [ ]; function run(test) { - const env = test.env; + const env = Object.assign({}, process.env, test.env); const expected = test.expected; const opts = { terminal: true, diff --git a/test/parallel/test-stdin-script-child.js b/test/parallel/test-stdin-script-child.js index f215682fe4c345..af966baff7655d 100644 --- a/test/parallel/test-stdin-script-child.js +++ b/test/parallel/test-stdin-script-child.js @@ -4,7 +4,7 @@ const assert = require('assert'); const spawn = require('child_process').spawn; const child = spawn(process.execPath, [], { - env: Object.assign(process.env, { + env: Object.assign({}, process.env, { NODE_DEBUG: process.argv[2] }) }); diff --git a/test/sequential/test-net-GH-5504.js b/test/sequential/test-net-GH-5504.js index bd0943f9e01566..46448342a2f257 100644 --- a/test/sequential/test-net-GH-5504.js +++ b/test/sequential/test-net-GH-5504.js @@ -49,7 +49,7 @@ function parent() { const node = process.execPath; const s = spawn(node, [__filename, 'server'], { - env: Object.assign(process.env, { + env: Object.assign({}, process.env, { NODE_DEBUG: 'net' }) }); diff --git a/test/sequential/test-util-debug.js b/test/sequential/test-util-debug.js index 96f754c71adab9..b74579097e36e1 100644 --- a/test/sequential/test-util-debug.js +++ b/test/sequential/test-util-debug.js @@ -26,7 +26,7 @@ function test(environ, shouldWrite) { const spawn = require('child_process').spawn; const child = spawn(process.execPath, [__filename, 'child'], { - env: Object.assign(process.env, { NODE_DEBUG: environ }) + env: Object.assign({}, process.env, { NODE_DEBUG: environ }) }); expectErr = expectErr.split('%PID%').join(child.pid);