mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-25 16:07:37 +00:00
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
This commit is contained in:
parent
37acb86e89
commit
2b3b781bbc
@ -96,7 +96,10 @@ function execSync(cmd, opts, pipe) {
|
||||
try { common.unlinkSync(stdoutFile); } catch (e) {}
|
||||
|
||||
if (code !== 0) {
|
||||
common.error(stderr, code, { continue: true });
|
||||
// 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;
|
||||
|
||||
14
test/exec.js
14
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());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user