Skip to content
Myr

Fix `extendEnv` option not working with `shell` option (#184)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #183 Options parsing was not correct for the `extendEnv` option which made it not work when combined with `shell: true` option.

authored by ehmicky · committed by Sindre Sorhus · parent 1fd9db4 · Browse files · 2 files+27 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+27 added−24 removed

  • other:+27 added−24 removed

2 files+27 added−24 removed

modified:index.js+18 added-23 removed
13 unmodified lines
1415const TEN_MEGABYTES = 1000 * 1000 * 10;1617function handleArgs(command, args, options) {18 options = Object.assign({19 extendEnv: true,20 env: {}21 }, options);2223 if (options.extendEnv) {24 options.env = Object.assign({}, process.env, options.env);25 }2618 const parsed = crossSpawn._parse(command, args, options);19 command = parsed.command;20 args = parsed.args;21 options = parsed.options;2223 options = Object.assign({24 maxBuffer: TEN_MEGABYTES,25 buffer: true,26 stripFinalNewline: true,27 preferLocal: true,34 localDir: parsed.options.cwd || process.cwd(),28 localDir: options.cwd || process.cwd(),29 encoding: 'utf8',30 reject: true,31 cleanup: true38 }, parsed.options, {windowsHide: true});32 }, options, {33 windowsHide: true34 });3536 if (options.extendEnv !== false) {37 options.env = Object.assign({}, process.env, options.env);38 }3940 if (options.preferLocal) {41 options.env = npmRunPath.env(Object.assign({}, options, {cwd: options.localDir}));42 }4344 // TODO: Remove in the next major release45 if (options.stripEof === false) {46 options.stripFinalNewline = false;47 }4849 options.stdio = stdio(options);5047 if (options.preferLocal) {48 options.env = npmRunPath.env(Object.assign({}, options, {cwd: options.localDir}));49 }5051 if (options.detached) {52 // #11553 options.cleanup = false;54 }5556 if (process.platform === 'win32' && path.basename(parsed.command) === 'cmd.exe') {56 if (process.platform === 'win32' && path.basename(command) === 'cmd.exe') {57 // #11658 parsed.args.unshift('/q');58 args.unshift('/q');59 }6061 return {62 command: parsed.command,63 args: parsed.args,64 options,65 parsed66 };61 return {command, args, options, parsed};62}6364function handleInput(spawned, input) {65 if (input === null || input === undefined) {
292 unmodified lines
modified:test.js+9 added-1 removed
485 unmodified lines
486 'bar'487 ]);488});489490test('do not extend environment with `extendEnv` option', async t => {490test('do not extend environment with `extendEnv: false`', async t => {491 const result = await m.stdout('environment', [], {env: {BAR: 'bar', PATH: process.env.PATH}, extendEnv: false});492493 t.deepEqual(result.split('\n'), [494 'undefined',495 'bar'496 ]);497});498499test('use extend environment with `extendEnv: true` and `shell: true`', async t => {500 process.env.TEST = 'test';501 const command = process.platform === 'win32' ? 'echo %TEST%' : 'echo $TEST';502 const stdout = await m.stdout(command, {shell: true, env: {}, extendEnv: true});503 t.is(stdout, 'test');504 delete process.env.TEST;505});506507test('do not buffer when streaming', async t => {508 const result = await getStream(m('max-buffer', ['stdout', '21'], {maxBuffer: 10}).stdout);509510 t.is(result, '....................\n');
64 unmodified lines