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) {

0 commit comments

Comments
 (0)