Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b3b781

Browse files
authored
fix: silent exec (#892)
Unconditionally apply `silent: true` when calling `common.error()` from `exec()`. This is because errors are already printed to stderr, or are intentionally silenced by `shell.config.silent`. Based on #861 Fixes #851
1 parent 37acb86 commit 2b3b781

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/exec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ function execSync(cmd, opts, pipe) {
9696
try { common.unlinkSync(stdoutFile); } catch (e) {}
9797

9898
if (code !== 0) {
99-
common.error(stderr, code, { continue: true });
99+
// Note: `silent` should be unconditionally true to avoid double-printing
100+
// the command's stderr, and to avoid printing any stderr when the user has
101+
// set `shell.config.silent`.
102+
common.error(stderr, code, { continue: true, silent: true });
100103
}
101104
var obj = common.ShellString(stdout, stderr, code);
102105
return obj;

test/exec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ import test from 'ava';
66

77
import shell from '..';
88
import utils from './utils/utils';
9+
import mocks from './utils/mocks';
910

1011
const CWD = process.cwd();
1112
const ORIG_EXEC_PATH = shell.config.execPath;
1213
shell.config.silent = true;
1314

15+
test.beforeEach(() => {
16+
mocks.init();
17+
});
18+
1419
test.afterEach.always(() => {
1520
process.chdir(CWD);
1621
shell.config.execPath = ORIG_EXEC_PATH;
22+
mocks.restore();
1723
});
1824

1925
//
@@ -85,6 +91,14 @@ test('check if stdout + stderr go to output', t => {
8591
t.is(result.stderr, '1234\n');
8692
});
8793

94+
test('check if stdout + stderr should not be printed to console if silent', t => {
95+
shell.exec(`${JSON.stringify(shell.config.execPath)} -e "console.error(1234); console.log(666); process.exit(12);"`, { silent: true });
96+
const stdout = mocks.stdout();
97+
const stderr = mocks.stderr();
98+
t.is(stdout, '');
99+
t.is(stderr, '');
100+
});
101+
88102
test('check exit code', t => {
89103
const result = shell.exec(`${JSON.stringify(shell.config.execPath)} -e "process.exit(12);"`);
90104
t.truthy(shell.error());

0 commit comments

Comments
 (0)