Skip to content

Commit ead0c22

Browse files
committed
test: add common.envPlus()
Add a helper function to provide an easy way to modify the environment passed to child processes. Fixes: #14823
1 parent c49dcb3 commit ead0c22

25 files changed

+52
-53
lines changed

test/common/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Platform normalizes the `dd` command
5050

5151
Check if there is more than 1gb of total memory.
5252

53+
### envPlus(additionalEnv)
54+
* return [<Object>]
55+
56+
Returns `process.env` plus `additionalEnv`. Used to pass a temporarily modified
57+
environment to a child process.
58+
5359
### expectsError([fn, ]settings[, exact])
5460
* `fn` [<Function>] a function that should throw.
5561
* `settings` [<Object>]

test/common/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ if (exports.isLinux) {
182182
];
183183
}
184184

185+
exports.envPlus = function(additionalEnv) {
186+
return Object.assign({}, process.env, additionalEnv);
187+
};
188+
185189
Object.defineProperty(exports, 'inFreeBSDJail', {
186190
get: function() {
187191
if (inFreeBSDJail !== null) return inFreeBSDJail;

test/parallel/test-benchmark-crypto.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ const argv = ['--set', 'algo=sha256',
2626
'--set', 'v=crypto',
2727
'--set', 'writes=1',
2828
'crypto'];
29-
const env = Object.assign({}, process.env,
30-
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
31-
const child = fork(runjs, argv, { env });
29+
30+
const child = fork(runjs, argv, { env: common.envPlus({
31+
NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }) });
32+
3233
child.on('exit', (code, signal) => {
3334
assert.strictEqual(code, 0);
3435
assert.strictEqual(signal, null);

test/parallel/test-benchmark-timers.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44

55
// Minimal test for timers benchmarks. This makes sure the benchmarks aren't
66
// horribly broken but nothing more than that.
@@ -15,10 +15,9 @@ const argv = ['--set', 'type=depth',
1515
'--set', 'thousands=0.001',
1616
'timers'];
1717

18-
const env = Object.assign({}, process.env,
19-
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
18+
const child = fork(runjs, argv, { env: common.envPlus({
19+
NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }) });
2020

21-
const child = fork(runjs, argv, { env });
2221
child.on('exit', (code, signal) => {
2322
assert.strictEqual(code, 0);
2423
assert.strictEqual(signal, null);

test/parallel/test-child-process-env.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ Object.setPrototypeOf(env, {
3434

3535
let child;
3636
if (common.isWindows) {
37-
child = spawn('cmd.exe', ['/c', 'set'], { env: env });
37+
child = spawn('cmd.exe', ['/c', 'set'], common.envPlus({ env: env }));
3838
} else {
39-
child = spawn('/usr/bin/env', [], { env: env });
39+
child = spawn('/usr/bin/env', [], common.envPlus({ env: env }));
4040
}
4141

4242

test/parallel/test-child-process-exec-env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function after(err, stdout, stderr) {
4444
if (!common.isWindows) {
4545
child = exec('/usr/bin/env', { env: { 'HELLO': 'WORLD' } }, after);
4646
} else {
47-
child = exec('set', { env: { 'HELLO': 'WORLD' } }, after);
47+
child = exec('set', { env: common.envPlus({ 'HELLO': 'WORLD' }) }, after);
4848
}
4949

5050
child.stdout.setEncoding('utf8');

test/parallel/test-child-process-spawn-shell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ command.on('close', common.mustCall((code, signal) => {
5050

5151
// Verify that the environment is properly inherited
5252
const env = cp.spawn(`"${process.execPath}" -pe process.env.BAZ`, {
53-
env: Object.assign({}, process.env, { BAZ: 'buzz' }),
53+
env: common.envPlus({ BAZ: 'buzz' }),
5454
encoding: 'utf8',
5555
shell: true
5656
});

test/parallel/test-child-process-spawnsync-env.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525
const cp = require('child_process');
2626

@@ -29,7 +29,7 @@ if (process.argv[2] === 'child') {
2929
} else {
3030
const expected = 'bar';
3131
const child = cp.spawnSync(process.execPath, [__filename, 'child'], {
32-
env: Object.assign(process.env, { foo: expected })
32+
env: common.envPlus({ foo: expected })
3333
});
3434

3535
assert.strictEqual(child.stdout.toString().trim(), expected);

test/parallel/test-child-process-spawnsync-shell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ assert.strictEqual(command.stdout.toString().trim(), 'bar');
3737

3838
// Verify that the environment is properly inherited
3939
const env = cp.spawnSync(`"${process.execPath}" -pe process.env.BAZ`, {
40-
env: Object.assign({}, process.env, { BAZ: 'buzz' }),
40+
env: common.envPlus({ BAZ: 'buzz' }),
4141
shell: true
4242
});
4343

test/parallel/test-cli-node-options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ disallow('--');
2929
disallow('--no_warnings'); // Node options don't allow '_' instead of '-'.
3030

3131
function disallow(opt) {
32-
const options = { env: { NODE_OPTIONS: opt } };
32+
const options = { env: common.envPlus({ NODE_OPTIONS: opt }) };
3333
exec(process.execPath, options, common.mustCall(function(err) {
3434
const message = err.message.split(/\r?\n/)[1];
3535
const expect = `${process.execPath}: ${opt} is not allowed in NODE_OPTIONS`;
@@ -71,7 +71,7 @@ function expect(opt, want) {
7171
const printB = require.resolve('../fixtures/printB.js');
7272
const argv = [printB];
7373
const opts = {
74-
env: { NODE_OPTIONS: opt },
74+
env: common.envPlus({ NODE_OPTIONS: opt }),
7575
maxBuffer: 1000000000,
7676
};
7777
exec(process.execPath, argv, opts, common.mustCall(function(err, stdout) {

test/parallel/test-crypto-fips.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@ function sharedOpenSSL() {
2626
return process.config.variables.node_shared_openssl;
2727
}
2828

29-
function addToEnv(newVar, value) {
30-
const envCopy = {};
31-
for (const e in process.env) {
32-
envCopy[e] = process.env[e];
33-
}
34-
envCopy[newVar] = value;
35-
return envCopy;
36-
}
37-
3829
function testHelper(stream, args, expectedOutput, cmd, env) {
3930
const fullArgs = args.concat(['-e', `console.log(${cmd})`]);
4031
const child = spawnSync(process.execPath, fullArgs, {
@@ -72,7 +63,7 @@ testHelper(
7263
[],
7364
FIPS_DISABLED,
7465
'require("crypto").fips',
75-
addToEnv('OPENSSL_CONF', ''));
66+
common.envPlus({ 'OPENSSL_CONF': '' }));
7667

7768
// --enable-fips should turn FIPS mode on
7869
testHelper(
@@ -117,23 +108,23 @@ if (!sharedOpenSSL()) {
117108
[],
118109
compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED,
119110
'require("crypto").fips',
120-
addToEnv('OPENSSL_CONF', CNF_FIPS_ON));
111+
common.envPlus({ 'OPENSSL_CONF': CNF_FIPS_ON }));
121112

122113
// --openssl-config option should override OPENSSL_CONF
123114
testHelper(
124115
'stdout',
125116
[`--openssl-config=${CNF_FIPS_ON}`],
126117
compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED,
127118
'require("crypto").fips',
128-
addToEnv('OPENSSL_CONF', CNF_FIPS_OFF));
119+
common.envPlus({ 'OPENSSL_CONF': CNF_FIPS_OFF }));
129120
}
130121

131122
testHelper(
132123
'stdout',
133124
[`--openssl-config=${CNF_FIPS_OFF}`],
134125
FIPS_DISABLED,
135126
'require("crypto").fips',
136-
addToEnv('OPENSSL_CONF', CNF_FIPS_ON));
127+
common.envPlus({ 'OPENSSL_CONF': CNF_FIPS_ON }));
137128

138129
// --enable-fips should take precedence over OpenSSL config file
139130
testHelper(
@@ -149,7 +140,7 @@ testHelper(
149140
['--enable-fips'],
150141
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
151142
'require("crypto").fips',
152-
addToEnv('OPENSSL_CONF', CNF_FIPS_OFF));
143+
common.envPlus({ 'OPENSSL_CONF': CNF_FIPS_OFF }));
153144

154145
// --force-fips should take precedence over OpenSSL config file
155146
testHelper(
@@ -165,7 +156,7 @@ testHelper(
165156
['--force-fips'],
166157
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
167158
'require("crypto").fips',
168-
addToEnv('OPENSSL_CONF', CNF_FIPS_OFF));
159+
common.envPlus({ 'OPENSSL_CONF': CNF_FIPS_OFF }));
169160

170161
// setFipsCrypto should be able to turn FIPS mode on
171162
testHelper(

test/parallel/test-fs-readfile-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const fixtures = require('../common/fixtures');
3333
function test(env, cb) {
3434
const filename = fixtures.path('test-fs-readfile-error.js');
3535
const execPath = `"${process.execPath}" "${filename}"`;
36-
const options = { env: Object.assign(process.env, env) };
36+
const options = { env: common.envPlus(env) };
3737
exec(execPath, options, common.mustCall((err, stdout, stderr) => {
3838
assert(err);
3939
assert.strictEqual(stdout, '');

test/parallel/test-http-server-stale-close.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const http = require('http');
25-
const util = require('util');
2625
const fork = require('child_process').fork;
2726

2827
if (process.env.NODE_TEST_FORK_PORT) {
@@ -45,7 +44,7 @@ if (process.env.NODE_TEST_FORK_PORT) {
4544
});
4645
server.listen(0, function() {
4746
fork(__filename, {
48-
env: util._extend(process.env, {
47+
env: common.envPlus({
4948
NODE_TEST_FORK_PORT: this.address().port
5049
})
5150
});

test/parallel/test-icu-data-dir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const expected =
1717
}
1818

1919
{
20-
const env = { NODE_ICU_DATA: '/' };
20+
const env = common.envPlus({ NODE_ICU_DATA: '/' });
2121
const child = spawnSync(process.execPath, ['-e', '0'], { env });
2222
assert(child.stderr.toString().includes(expected));
2323
}

test/parallel/test-inspector-open.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const url = require('url');
1313
if (process.env.BE_CHILD)
1414
return beChild();
1515

16-
const child = fork(__filename, { env: { BE_CHILD: 1 } });
16+
const child = fork(__filename, { env: common.envPlus({ BE_CHILD: 1 }) });
1717

1818
child.once('message', common.mustCall((msg) => {
1919
assert.strictEqual(msg.cmd, 'started');

test/parallel/test-module-loading-globalpaths.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ if (process.argv[2] === 'child') {
3636
const testFixturesDir = path.join(common.fixturesDir,
3737
path.basename(__filename, '.js'));
3838

39-
const env = Object.assign({}, process.env);
39+
const env = common.envPlus({});
4040
// Turn on module debug to aid diagnosing failures.
4141
env['NODE_DEBUG'] = 'module';
4242
// Unset NODE_PATH.

test/parallel/test-npm-install.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ const pkgPath = path.join(installDir, 'package.json');
3939

4040
fs.writeFileSync(pkgPath, pkgContent);
4141

42-
const env = Object.create(process.env);
43-
env['PATH'] = path.dirname(process.execPath);
44-
env['NPM_CONFIG_PREFIX'] = path.join(npmSandbox, 'npm-prefix');
45-
env['NPM_CONFIG_TMP'] = path.join(npmSandbox, 'npm-tmp');
46-
env['HOME'] = path.join(npmSandbox, 'home');
42+
const env = common.envPlus({
43+
PATH: path.dirname(process.execPath),
44+
NPM_CONFIG_PREFIX: path.join(npmSandbox, 'npm-prefix'),
45+
NPM_CONFIG_TMP: path.join(npmSandbox, 'npm-tmp'),
46+
HOME: path.join(npmSandbox, 'home'),
47+
});
4748

4849
const proc = spawn(process.execPath, args, {
4950
cwd: installDir,

test/parallel/test-pending-deprecation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ switch (process.argv[2]) {
3737

3838
// Test the NODE_PENDING_DEPRECATION environment var.
3939
fork(__filename, ['env'], {
40-
env: { NODE_PENDING_DEPRECATION: 1 },
40+
env: common.envPlus({ NODE_PENDING_DEPRECATION: 1 }),
4141
silent: true
4242
}).on('exit', common.mustCall((code) => {
4343
assert.strictEqual(code, 0, message('NODE_PENDING_DEPRECATION'));

test/parallel/test-process-redirect-warnings-env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ common.refreshTmpDir();
1616
const warnmod = require.resolve(`${common.fixturesDir}/warnings.js`);
1717
const warnpath = path.join(common.tmpDir, 'warnings.txt');
1818

19-
fork(warnmod, { env: { NODE_REDIRECT_WARNINGS: warnpath } })
19+
fork(warnmod, { env: common.envPlus({ NODE_REDIRECT_WARNINGS: warnpath }) })
2020
.on('exit', common.mustCall(() => {
2121
fs.readFile(warnpath, 'utf8', common.mustCall((err, data) => {
2222
assert.ifError(err);

test/parallel/test-repl-envvars.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const tests = [
3636
];
3737

3838
function run(test) {
39-
const env = test.env;
39+
const env = common.envPlus(test.env);
4040
const expected = test.expected;
4141
const opts = {
4242
terminal: true,

test/parallel/test-require-symlink.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const assert = require('assert');
55
const path = require('path');
66
const fs = require('fs');
77
const { exec, spawn } = require('child_process');
8-
const util = require('util');
98
const fixtures = require('../common/fixtures');
109

1110
common.refreshTmpDir();
@@ -61,7 +60,7 @@ function test() {
6160

6261
// Also verify that symlinks works for setting preserve via env variables
6362
const childEnv = spawn(node, [linkScript], {
64-
env: util._extend(process.env, { NODE_PRESERVE_SYMLINKS: '1' })
63+
env: common.envPlus({ NODE_PRESERVE_SYMLINKS: '1' })
6564
});
6665
childEnv.on('close', function(code, signal) {
6766
assert.strictEqual(code, 0);

test/parallel/test-stdin-script-child.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('assert');
55
const { spawn } = require('child_process');
66
for (const args of [[], ['-']]) {
77
const child = spawn(process.execPath, args, {
8-
env: Object.assign(process.env, {
8+
env: common.envPlus({
99
NODE_DEBUG: process.argv[2]
1010
})
1111
});

test/parallel/test-tls-env-bad-extra-ca.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const env = Object.assign({}, process.env, {
2121
});
2222

2323
const opts = {
24-
env: env,
24+
env: common.envPlus(env),
2525
silent: true,
2626
};
2727
let stderr = '';

test/parallel/test-tls-env-extra-ca.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const server = tls.createServer(options, common.mustCall(function(s) {
3232
s.end('bye');
3333
server.close();
3434
})).listen(0, common.mustCall(function() {
35-
const env = Object.assign({}, process.env, {
35+
const env = common.envPlus({
3636
CHILD: 'yes',
3737
PORT: this.address().port,
3838
NODE_EXTRA_CA_CERTS: fixtures.path('keys', 'ca1-cert.pem')

test/sequential/test-benchmark-http.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ const path = require('path');
1818

1919
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
2020

21-
const env = Object.assign({}, process.env,
22-
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
21+
const env = common.envPlus({ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
2322

2423
const child = fork(runjs, ['--set', 'benchmarker=test-double',
2524
'--set', 'c=1',

0 commit comments

Comments
 (0)