From c368d562a932821058b6360f36ca57c994dad441 Mon Sep 17 00:00:00 2001 From: uttpal Date: Sat, 23 Jun 2018 20:26:12 +0530 Subject: [PATCH 1/2] fix: silent does not always work #851 --- src/exec.js | 2 +- test/exec.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/exec.js b/src/exec.js index 66ef3d73..9127ff50 100644 --- a/src/exec.js +++ b/src/exec.js @@ -96,7 +96,7 @@ function execSync(cmd, opts, pipe) { try { common.unlinkSync(stdoutFile); } catch (e) {} if (code !== 0) { - common.error(stderr, code, { continue: true }); + common.error(stderr, code, { continue: true, silent: opts.silent }); } var obj = common.ShellString(stdout, stderr, code); return obj; diff --git a/test/exec.js b/test/exec.js index 2cd2a67f..18c9b1b5 100644 --- a/test/exec.js +++ b/test/exec.js @@ -6,14 +6,20 @@ import test from 'ava'; import shell from '..'; import utils from './utils/utils'; +import mocks from './utils/mocks'; const CWD = process.cwd(); const ORIG_EXEC_PATH = shell.config.execPath; shell.config.silent = true; +test.beforeEach(() => { + mocks.init(); +}); + test.afterEach.always(() => { process.chdir(CWD); shell.config.execPath = ORIG_EXEC_PATH; + mocks.restore(); }); // @@ -85,6 +91,14 @@ test('check if stdout + stderr go to output', t => { t.is(result.stderr, '1234\n'); }); +test('check if stdout + stderr should not be printed to console if silent', t => { + shell.exec(`${JSON.stringify(shell.config.execPath)} -e "console.error(1234); console.log(666); process.exit(12);"`, { silent: true }); + const stdout = mocks.stdout(); + const stderr = mocks.stderr(); + t.is(stdout, ''); + t.is(stderr, ''); +}); + test('check exit code', t => { const result = shell.exec(`${JSON.stringify(shell.config.execPath)} -e "process.exit(12);"`); t.truthy(shell.error()); From 5f3042dbc1477aeb76ac3502a803a03c1bd503b5 Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Tue, 9 Oct 2018 19:43:51 -0700 Subject: [PATCH 2/2] Unconditionally silence error() --- src/exec.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/exec.js b/src/exec.js index 9127ff50..a73e2cd4 100644 --- a/src/exec.js +++ b/src/exec.js @@ -96,7 +96,10 @@ function execSync(cmd, opts, pipe) { try { common.unlinkSync(stdoutFile); } catch (e) {} if (code !== 0) { - common.error(stderr, code, { continue: true, silent: opts.silent }); + // Note: `silent` should be unconditionally true to avoid double-printing + // the command's stderr, and to avoid printing any stderr when the user has + // set `shell.config.silent`. + common.error(stderr, code, { continue: true, silent: true }); } var obj = common.ShellString(stdout, stderr, code); return obj;