From d826affea08ec81b9e01bde3ded7216d6728d30b Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 6 Mar 2019 11:28:22 +0100 Subject: [PATCH 1/6] Add test --- test.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test.js b/test.js index 91fd135a7a..e32f2962f8 100644 --- a/test.js +++ b/test.js @@ -478,7 +478,7 @@ test('extend environment variables by default', async t => { ]); }); -test('do not extend environment with `extendEnv` option', async t => { +test('do not extend environment with `extendEnv: false` option', async t => { const result = await m.stdout('environment', [], {env: {BAR: 'bar', PATH: process.env.PATH}, extendEnv: false}); t.deepEqual(result.split('\n'), [ @@ -487,6 +487,13 @@ test('do not extend environment with `extendEnv` option', async t => { ]); }); +test('use extend environment with `extendEnv: true` option', async t => { + process.env.TEST = 'test'; + const result = await m.shellSync('echo $TEST', {env: {}, extendEnv: true}); + t.is(result.stdout, 'test'); + delete process.env.TEST; +}); + test('do not buffer when streaming', async t => { const result = await getStream(m('max-buffer', ['stdout', '21'], {maxBuffer: 10}).stdout); From ad32480fa79f9fab39c821384d83721505783ec2 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 6 Mar 2019 11:28:12 +0100 Subject: [PATCH 2/6] Improve options parsing --- index.js | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index b4b5e340e1..13c0ebb962 100644 --- a/index.js +++ b/index.js @@ -14,27 +14,31 @@ const stdio = require('./lib/stdio'); const TEN_MEGABYTES = 1000 * 1000 * 10; function handleArgs(command, args, options) { - options = Object.assign({ - extendEnv: true, - env: {} - }, options); - - if (options.extendEnv) { - options.env = Object.assign({}, process.env, options.env); - } - const parsed = crossSpawn._parse(command, args, options); + command = parsed.command; + args = parsed.args; + options = parsed.options; - options = Object.assign({ + options = { maxBuffer: TEN_MEGABYTES, buffer: true, stripFinalNewline: true, preferLocal: true, - localDir: parsed.options.cwd || process.cwd(), + localDir: options.cwd || process.cwd(), encoding: 'utf8', reject: true, - cleanup: true - }, parsed.options, {windowsHide: true}); + cleanup: true, + ...options, + windowsHide: true + }; + + if (options.extendEnv !== false) { + options.env = {...process.env, ...options.env}; + } + + if (options.preferLocal) { + options.env = npmRunPath.env({...options, cwd: options.localDir}); + } // TODO: Remove in the next major release if (options.stripEof === false) { @@ -43,26 +47,17 @@ function handleArgs(command, args, options) { options.stdio = stdio(options); - if (options.preferLocal) { - options.env = npmRunPath.env(Object.assign({}, options, {cwd: options.localDir})); - } - if (options.detached) { // #115 options.cleanup = false; } - if (process.platform === 'win32' && path.basename(parsed.command) === 'cmd.exe') { + if (process.platform === 'win32' && path.basename(command) === 'cmd.exe') { // #116 - parsed.args.unshift('/q'); + args.unshift('/q'); } - return { - command: parsed.command, - args: parsed.args, - options, - parsed - }; + return {command, args, options, parsed}; } function handleInput(spawned, input) { From a242991c174afa154afa09165ea841075c09d68b Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 6 Mar 2019 11:37:04 +0100 Subject: [PATCH 3/6] Fix Node 6 support --- index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 13c0ebb962..baa8f998f6 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,7 @@ function handleArgs(command, args, options) { args = parsed.args; options = parsed.options; - options = { + options = Object.assign({ maxBuffer: TEN_MEGABYTES, buffer: true, stripFinalNewline: true, @@ -27,17 +27,17 @@ function handleArgs(command, args, options) { localDir: options.cwd || process.cwd(), encoding: 'utf8', reject: true, - cleanup: true, - ...options, + cleanup: true + }, options, { windowsHide: true - }; + }); if (options.extendEnv !== false) { - options.env = {...process.env, ...options.env}; + options.env = Object.assign({}, process.env, options.env); } if (options.preferLocal) { - options.env = npmRunPath.env({...options, cwd: options.localDir}); + options.env = npmRunPath.env(Object.assign({}, options, {cwd: options.localDir})); } // TODO: Remove in the next major release From f98712b308d6c6786f947c77fe7d6f8b577b7ed9 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 6 Mar 2019 11:40:02 +0100 Subject: [PATCH 4/6] Fix Windows support --- test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test.js b/test.js index e32f2962f8..e8370702a7 100644 --- a/test.js +++ b/test.js @@ -489,7 +489,8 @@ test('do not extend environment with `extendEnv: false` option', async t => { test('use extend environment with `extendEnv: true` option', async t => { process.env.TEST = 'test'; - const result = await m.shellSync('echo $TEST', {env: {}, extendEnv: true}); + const command = process.platform === 'win32' ? 'echo %TEST%' : 'echo $TEST'; + const result = await m.shellSync(command, {env: {}, extendEnv: true}); t.is(result.stdout, 'test'); delete process.env.TEST; }); From 3a537f00149fbf663c6fb22b4d0c1349a66981db Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 6 Mar 2019 18:04:27 +0100 Subject: [PATCH 5/6] Improve test --- test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test.js b/test.js index e8370702a7..7ded914797 100644 --- a/test.js +++ b/test.js @@ -487,11 +487,11 @@ test('do not extend environment with `extendEnv: false` option', async t => { ]); }); -test('use extend environment with `extendEnv: true` option', async t => { +test('use extend environment with `extendEnv: true` option and `shell: true`', async t => { process.env.TEST = 'test'; const command = process.platform === 'win32' ? 'echo %TEST%' : 'echo $TEST'; - const result = await m.shellSync(command, {env: {}, extendEnv: true}); - t.is(result.stdout, 'test'); + const stdout = await m.stdout(command, {shell: true, env: {}, extendEnv: true}); + t.is(stdout, 'test'); delete process.env.TEST; }); From 67c4d91269a6a657f04c5f7e9ce4893ce6ebaa9c Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 6 Mar 2019 18:06:04 +0100 Subject: [PATCH 6/6] Fix test titles --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 7ded914797..35a505e0cf 100644 --- a/test.js +++ b/test.js @@ -478,7 +478,7 @@ test('extend environment variables by default', async t => { ]); }); -test('do not extend environment with `extendEnv: false` option', async t => { +test('do not extend environment with `extendEnv: false`', async t => { const result = await m.stdout('environment', [], {env: {BAR: 'bar', PATH: process.env.PATH}, extendEnv: false}); t.deepEqual(result.split('\n'), [ @@ -487,7 +487,7 @@ test('do not extend environment with `extendEnv: false` option', async t => { ]); }); -test('use extend environment with `extendEnv: true` option and `shell: true`', async t => { +test('use extend environment with `extendEnv: true` and `shell: true`', async t => { process.env.TEST = 'test'; const command = process.platform === 'win32' ? 'echo %TEST%' : 'echo $TEST'; const stdout = await m.stdout(command, {shell: true, env: {}, extendEnv: true});