From ff1b64a1e9efc8642c6a4a0b47e4237756972cb4 Mon Sep 17 00:00:00 2001 From: Beth Griggs Date: Mon, 14 Aug 2017 12:12:49 +0100 Subject: [PATCH] test: preserve env in test cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows env vars to be passed through to child processes. This is needed for things like NODE_TEST_DIR or LD_LIBRARY_PATH if testing the shared library. PR-URL: https://github.com/nodejs/node/pull/14822 Refs: https://github.com/nodejs/node/pull/13390 Reviewed-By: Colin Ihrig Reviewed-By: Refael Ackermann Reviewed-By: Michael Dawson Reviewed-By: Luigi Pinca Reviewed-By: Anna Henningsen Reviewed-By: Tobias Nießen Reviewed-By: James M Snell --- test/parallel/test-benchmark-crypto.js | 4 +++- test/parallel/test-benchmark-timers.js | 5 +++- test/parallel/test-env-var-no-warnings.js | 3 ++- test/parallel/test-tls-env-bad-extra-ca.js | 4 ++-- test/parallel/test-tls-env-extra-ca.js | 4 ++-- .../test-benchmark-child-process.js | 23 +++++++++++++------ test/sequential/test-benchmark-net.js | 4 +++- 7 files changed, 32 insertions(+), 15 deletions(-) diff --git a/test/parallel/test-benchmark-crypto.js b/test/parallel/test-benchmark-crypto.js index 25dbc95b4c27f4..db0e9f499117a5 100644 --- a/test/parallel/test-benchmark-crypto.js +++ b/test/parallel/test-benchmark-crypto.js @@ -27,7 +27,9 @@ const argv = ['--set', 'algo=sha256', '--set', 'writes=1', 'crypto']; -const child = fork(runjs, argv, {env: {NODEJS_BENCHMARK_ZERO_ALLOWED: 1}}); +const env = Object.assign({}, process.env, + { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); +const child = fork(runjs, argv, { env }); child.on('exit', (code, signal) => { assert.strictEqual(code, 0); assert.strictEqual(signal, null); diff --git a/test/parallel/test-benchmark-timers.js b/test/parallel/test-benchmark-timers.js index 956ab657bc5295..991ffda7186e72 100644 --- a/test/parallel/test-benchmark-timers.js +++ b/test/parallel/test-benchmark-timers.js @@ -15,7 +15,10 @@ const argv = ['--set', 'type=depth', '--set', 'thousands=0.001', 'timers']; -const child = fork(runjs, argv, {env: {NODEJS_BENCHMARK_ZERO_ALLOWED: 1}}); +const env = Object.assign({}, process.env, + { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); + +const child = fork(runjs, argv, { env }); child.on('exit', (code, signal) => { assert.strictEqual(code, 0); assert.strictEqual(signal, null); diff --git a/test/parallel/test-env-var-no-warnings.js b/test/parallel/test-env-var-no-warnings.js index 3a99cbc112d8b9..e2a11656f33df2 100644 --- a/test/parallel/test-env-var-no-warnings.js +++ b/test/parallel/test-env-var-no-warnings.js @@ -6,7 +6,8 @@ const cp = require('child_process'); if (process.argv[2] === 'child') { process.emitWarning('foo'); } else { - function test(env) { + function test(newEnv) { + const env = Object.assign({}, process.env, newEnv); const cmd = `"${process.execPath}" "${__filename}" child`; cp.exec(cmd, { env }, common.mustCall((err, stdout, stderr) => { diff --git a/test/parallel/test-tls-env-bad-extra-ca.js b/test/parallel/test-tls-env-bad-extra-ca.js index ece93f33539d71..5c6e47d52a654a 100644 --- a/test/parallel/test-tls-env-bad-extra-ca.js +++ b/test/parallel/test-tls-env-bad-extra-ca.js @@ -15,10 +15,10 @@ if (process.env.CHILD) { return tls.createServer({}); } -const env = { +const env = Object.assign({}, process.env, { CHILD: 'yes', NODE_EXTRA_CA_CERTS: `${common.fixturesDir}/no-such-file-exists`, -}; +}); const opts = { env: env, diff --git a/test/parallel/test-tls-env-extra-ca.js b/test/parallel/test-tls-env-extra-ca.js index 5f011f33382ac6..4dd12a78b6387c 100644 --- a/test/parallel/test-tls-env-extra-ca.js +++ b/test/parallel/test-tls-env-extra-ca.js @@ -32,11 +32,11 @@ const server = tls.createServer(options, common.mustCall(function(s) { s.end('bye'); server.close(); })).listen(0, common.mustCall(function() { - const env = { + const env = Object.assign({}, process.env, { CHILD: 'yes', PORT: this.address().port, NODE_EXTRA_CA_CERTS: fixtures.path('keys', 'ca1-cert.pem') - }; + }); fork(__filename, {env: env}).on('exit', common.mustCall(function(status) { assert.strictEqual(status, 0, 'client did not succeed in connecting'); diff --git a/test/sequential/test-benchmark-child-process.js b/test/sequential/test-benchmark-child-process.js index 2314c89948c701..f993238549fca4 100644 --- a/test/sequential/test-benchmark-child-process.js +++ b/test/sequential/test-benchmark-child-process.js @@ -8,13 +8,22 @@ const path = require('path'); const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); -const child = fork(runjs, ['--set', 'dur=0', - '--set', 'n=1', - '--set', 'len=1', - '--set', 'params=1', - '--set', 'methodName=execSync', - 'child_process'], - {env: {NODEJS_BENCHMARK_ZERO_ALLOWED: 1}}); +const env = Object.assign({}, process.env, + { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); + +const child = fork( + runjs, + [ + '--set', 'dur=0', + '--set', 'n=1', + '--set', 'len=1', + '--set', 'params=1', + '--set', 'methodName=execSync', + 'child_process' + ], + { env } +); + child.on('exit', (code, signal) => { assert.strictEqual(code, 0); assert.strictEqual(signal, null); diff --git a/test/sequential/test-benchmark-net.js b/test/sequential/test-benchmark-net.js index ef5f4fbb91c760..b2d360328504fb 100644 --- a/test/sequential/test-benchmark-net.js +++ b/test/sequential/test-benchmark-net.js @@ -15,12 +15,14 @@ const path = require('path'); const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); +const env = Object.assign({}, process.env, + { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); const child = fork(runjs, ['--set', 'dur=0', '--set', 'len=1024', '--set', 'type=buf', 'net'], - {env: {NODEJS_BENCHMARK_ZERO_ALLOWED: 1}}); + { env }); child.on('exit', (code, signal) => { assert.strictEqual(code, 0); assert.strictEqual(signal, null);