|
| 1 | +'use strict'; |
| 2 | +// When no `env` option is given, a child process inherits process.env, even |
| 3 | +// if process.env has been replaced with another object. |
| 4 | +// Refs: https://github.com/nodejs/node/issues/66882 |
| 5 | +const common = require('../common'); |
| 6 | +const assert = require('assert'); |
| 7 | +const { |
| 8 | + exec, |
| 9 | + execFile, |
| 10 | + execFileSync, |
| 11 | + execSync, |
| 12 | + fork, |
| 13 | + spawn, |
| 14 | + spawnSync, |
| 15 | +} = require('child_process'); |
| 16 | + |
| 17 | +if (process.argv[2] === 'child') { |
| 18 | + process.send(process.env.TEST_REASSIGNED); |
| 19 | + return; |
| 20 | +} |
| 21 | + |
| 22 | +const printEnv = ['-p', 'process.env.TEST_REASSIGNED']; |
| 23 | +const shellEcho = common.isWindows ? |
| 24 | + 'echo %TEST_REASSIGNED%' : |
| 25 | + 'echo "$TEST_REASSIGNED"'; |
| 26 | + |
| 27 | +function runSync(options) { |
| 28 | + const child = spawnSync(process.execPath, printEnv, |
| 29 | + { encoding: 'utf8', ...options }); |
| 30 | + assert.strictEqual(child.status, 0, child.stderr || String(child.error)); |
| 31 | + return child.stdout.trim(); |
| 32 | +} |
| 33 | + |
| 34 | +const originalEnv = process.env; |
| 35 | +// Lives in the real environment, but not in the replacement object. |
| 36 | +process.env.TEST_ONLY_IN_ORIGINAL = 'original'; |
| 37 | +process.env = { ...process.env, TEST_REASSIGNED: 'reassigned' }; |
| 38 | +delete process.env.TEST_ONLY_IN_ORIGINAL; |
| 39 | + |
| 40 | +assert.strictEqual(runSync(), 'reassigned'); |
| 41 | +assert.strictEqual( |
| 42 | + execFileSync(process.execPath, printEnv, { encoding: 'utf8' }).trim(), |
| 43 | + 'reassigned'); |
| 44 | +assert.strictEqual(execSync(shellEcho, { encoding: 'utf8' }).trim(), |
| 45 | + 'reassigned'); |
| 46 | + |
| 47 | +{ |
| 48 | + const child = spawnSync( |
| 49 | + process.execPath, |
| 50 | + ['-p', 'process.env.TEST_ONLY_IN_ORIGINAL'], |
| 51 | + { encoding: 'utf8' }); |
| 52 | + assert.strictEqual(child.stdout.trim(), 'undefined'); |
| 53 | +} |
| 54 | + |
| 55 | +// The default env matches an explicit { env: process.env }. |
| 56 | +{ |
| 57 | + const printAll = ['-e', 'process.stdout.write(JSON.stringify(process.env))']; |
| 58 | + const run = (options) => JSON.parse( |
| 59 | + spawnSync(process.execPath, printAll, { encoding: 'utf8', ...options }) |
| 60 | + .stdout); |
| 61 | + assert.deepStrictEqual(run(), run({ env: process.env })); |
| 62 | +} |
| 63 | + |
| 64 | +// The replacement object is used for command lookup. |
| 65 | +if (!common.isWindows) { |
| 66 | + const saved = process.env; |
| 67 | + process.env = { ...saved, PATH: '/nonexistent' }; |
| 68 | + const child = spawnSync('ls'); |
| 69 | + process.env = saved; |
| 70 | + assert.strictEqual(child.error?.code, 'ENOENT'); |
| 71 | +} |
| 72 | + |
| 73 | +{ |
| 74 | + const child = spawn(process.execPath, printEnv); |
| 75 | + let out = ''; |
| 76 | + child.stdout.setEncoding('utf8').on('data', (d) => { out += d; }); |
| 77 | + let err = ''; |
| 78 | + child.stderr.setEncoding('utf8').on('data', (d) => { err += d; }); |
| 79 | + child.on('close', common.mustCall((code) => { |
| 80 | + assert.strictEqual(code, 0, err); |
| 81 | + assert.strictEqual(out.trim(), 'reassigned'); |
| 82 | + })); |
| 83 | +} |
| 84 | + |
| 85 | +exec(shellEcho, common.mustSucceed((stdout) => { |
| 86 | + assert.strictEqual(stdout.trim(), 'reassigned'); |
| 87 | +})); |
| 88 | + |
| 89 | +execFile(process.execPath, printEnv, common.mustSucceed((stdout) => { |
| 90 | + assert.strictEqual(stdout.trim(), 'reassigned'); |
| 91 | +})); |
| 92 | + |
| 93 | +fork(__filename, ['child']).on('message', common.mustCall((value) => { |
| 94 | + assert.strictEqual(value, 'reassigned'); |
| 95 | +})); |
| 96 | + |
| 97 | +// Putting the original object back restores inheriting the real environment. |
| 98 | +process.env = originalEnv; |
| 99 | +assert.strictEqual(runSync(), 'undefined'); |
| 100 | +assert.strictEqual( |
| 101 | + spawnSync(process.execPath, ['-p', 'process.env.TEST_ONLY_IN_ORIGINAL'], |
| 102 | + { encoding: 'utf8' }).stdout.trim(), |
| 103 | + 'original'); |