From d2499cae505a8e301ab604172de16fc1790072d3 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 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-benchmark-crypto.js | 6 +++--- test/parallel/test-child-process-env.js | 6 ++++-- test/parallel/test-child-process-exec-env.js | 4 +++- test/parallel/test-cli-node-options.js | 5 +++-- 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-icu-data-dir.js | 2 +- test/parallel/test-inspector-open.js | 3 ++- test/parallel/test-npm-install.js | 11 +++++----- test/parallel/test-pending-deprecation.js | 2 +- .../test-process-redirect-warnings-env.js | 3 ++- test/parallel/test-repl-envvars.js | 2 +- test/parallel/test-require-symlink.js | 3 +-- test/parallel/test-stdin-script-child.js | 2 +- 15 files changed, 38 insertions(+), 39 deletions(-) diff --git a/test/parallel/test-benchmark-crypto.js b/test/parallel/test-benchmark-crypto.js index db0e9f499117a5..3675c38b9ea280 100644 --- a/test/parallel/test-benchmark-crypto.js +++ b/test/parallel/test-benchmark-crypto.js @@ -27,9 +27,9 @@ const argv = ['--set', 'algo=sha256', '--set', 'writes=1', 'crypto']; -const env = Object.assign({}, process.env, - { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); -const child = fork(runjs, argv, { env }); +const child = fork(runjs, argv, { env: Object.assign({}, process.env, { + NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }) }); + child.on('exit', (code, signal) => { assert.strictEqual(code, 0); assert.strictEqual(signal, null); diff --git a/test/parallel/test-child-process-env.js b/test/parallel/test-child-process-env.js index 015ab4e0c01975..4582c48fda5db1 100644 --- a/test/parallel/test-child-process-env.js +++ b/test/parallel/test-child-process-env.js @@ -34,9 +34,11 @@ 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', [], + 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 3b1ef741e6c9b2..c34fdb7b49a161 100644 --- a/test/parallel/test-child-process-exec-env.js +++ b/test/parallel/test-child-process-exec-env.js @@ -44,7 +44,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-cli-node-options.js b/test/parallel/test-cli-node-options.js index 83b387fa04a8f4..f52497e80839cb 100644 --- a/test/parallel/test-cli-node-options.js +++ b/test/parallel/test-cli-node-options.js @@ -29,7 +29,8 @@ disallow('--'); disallow('--no_warnings'); // Node options don't allow '_' instead of '-'. 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) { const message = err.message.split(/\r?\n/)[1]; 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 argv = [printB]; const opts = { - env: {NODE_OPTIONS: opt}, + env: Object.assign({}, process.env, { NODE_OPTIONS: opt }), maxBuffer: 1000000000, }; exec(process.execPath, argv, opts, common.mustCall(function(err, stdout) { diff --git a/test/parallel/test-crypto-fips.js b/test/parallel/test-crypto-fips.js index d72918756a200b..da2dd7b0ba8765 100644 --- a/test/parallel/test-crypto-fips.js +++ b/test/parallel/test-crypto-fips.js @@ -26,15 +26,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, { @@ -72,7 +63,7 @@ testHelper( [], FIPS_DISABLED, 'require("crypto").fips', - addToEnv('OPENSSL_CONF', '')); + Object.assign({}, process.env, { 'OPENSSL_CONF': '' })); // --enable-fips should turn FIPS mode on testHelper( @@ -117,7 +108,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( @@ -125,7 +116,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( @@ -133,7 +124,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( @@ -149,7 +140,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( @@ -165,7 +156,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 aa6e6bdecb0b08..e7c52f19a83d1c 100644 --- a/test/parallel/test-fs-readfile-error.js +++ b/test/parallel/test-fs-readfile-error.js @@ -33,7 +33,7 @@ const fixtures = require('../common/fixtures'); function test(env, cb) { const filename = fixtures.path('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 5d903f0c42f74e..77c0e4aa7e2849 100644 --- a/test/parallel/test-http-server-stale-close.js +++ b/test/parallel/test-http-server-stale-close.js @@ -22,7 +22,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) { @@ -45,7 +44,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-icu-data-dir.js b/test/parallel/test-icu-data-dir.js index 1a97699ca51691..800e20c48bab03 100644 --- a/test/parallel/test-icu-data-dir.js +++ b/test/parallel/test-icu-data-dir.js @@ -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 }); assert(child.stderr.toString().includes(expected)); } diff --git a/test/parallel/test-inspector-open.js b/test/parallel/test-inspector-open.js index 26647c7e276716..0bbf168471a72e 100644 --- a/test/parallel/test-inspector-open.js +++ b/test/parallel/test-inspector-open.js @@ -13,7 +13,8 @@ const url = require('url'); if (process.env.BE_CHILD) 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) => { assert.strictEqual(msg.cmd, 'started'); diff --git a/test/parallel/test-npm-install.js b/test/parallel/test-npm-install.js index 3340bc62a8d9c4..186b2e2b16099c 100644 --- a/test/parallel/test-npm-install.js +++ b/test/parallel/test-npm-install.js @@ -34,11 +34,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-pending-deprecation.js b/test/parallel/test-pending-deprecation.js index 402ec79b1bc04c..9b1fd5addfa0c6 100644 --- a/test/parallel/test-pending-deprecation.js +++ b/test/parallel/test-pending-deprecation.js @@ -37,7 +37,7 @@ switch (process.argv[2]) { // Test the NODE_PENDING_DEPRECATION environment var. fork(__filename, ['env'], { - env: {NODE_PENDING_DEPRECATION: 1}, + env: Object.assign({}, process.env, { NODE_PENDING_DEPRECATION: 1 }), silent: true }).on('exit', common.mustCall((code) => { assert.strictEqual(code, 0, message('NODE_PENDING_DEPRECATION')); diff --git a/test/parallel/test-process-redirect-warnings-env.js b/test/parallel/test-process-redirect-warnings-env.js index ae21cff37806cc..6748723cfe0bf3 100644 --- a/test/parallel/test-process-redirect-warnings-env.js +++ b/test/parallel/test-process-redirect-warnings-env.js @@ -16,7 +16,8 @@ common.refreshTmpDir(); const warnmod = require.resolve(`${common.fixturesDir}/warnings.js`); 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(() => { fs.readFile(warnpath, 'utf8', common.mustCall((err, data) => { assert.ifError(err); diff --git a/test/parallel/test-repl-envvars.js b/test/parallel/test-repl-envvars.js index c4efd184c4c91c..d29e7b3574c1f2 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-require-symlink.js b/test/parallel/test-require-symlink.js index 927afa1a870071..4dd9bf51ed9213 100644 --- a/test/parallel/test-require-symlink.js +++ b/test/parallel/test-require-symlink.js @@ -5,7 +5,6 @@ const assert = require('assert'); const path = require('path'); const fs = require('fs'); const { exec, spawn } = require('child_process'); -const util = require('util'); const fixtures = require('../common/fixtures'); common.refreshTmpDir(); @@ -61,7 +60,7 @@ function test() { // 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'}) + env: Object.assign({}, process.env, { NODE_PRESERVE_SYMLINKS: '1' }) }); childEnv.on('close', function(code, signal) { assert.strictEqual(code, 0); diff --git a/test/parallel/test-stdin-script-child.js b/test/parallel/test-stdin-script-child.js index 6e37fad9414de4..06adc9e113eda7 100644 --- a/test/parallel/test-stdin-script-child.js +++ b/test/parallel/test-stdin-script-child.js @@ -5,7 +5,7 @@ const assert = require('assert'); const { spawn } = require('child_process'); for (const args of [[], ['-']]) { const child = spawn(process.execPath, args, { - env: Object.assign(process.env, { + env: Object.assign({}, process.env, { NODE_DEBUG: process.argv[2] }) });