Make `stdout` and `stderr` properties `undefined` instead of `null` (#246)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by Sindre Sorhus · parent 21c706a · Browse files · 3 files·+46 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+46 added−7 removed
- other:+46 added−7 removed
Provenance
- Commit
c8a53da- Tree
153fedf- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
21c706a
3 files changed+46 added−7 removed
modified:index.js+10 added-5 removed
107 unmodified lines108 spawned.stdin.end(input);109 }110 }111 112-function handleOutput(options, value) {113- if (value && options.stripFinalNewline) {114- value = stripFinalNewline(value);112+function handleOutput(options, value, error) {113+ if (typeof value !== 'string' && !Buffer.isBuffer(value)) {114+ // When `execa.sync()` errors, we normalize it to '' to mimic `execa()`115+ return error === undefined ? undefined : '';116 }117 118+ if (options.stripFinalNewline) {119+ return stripFinalNewline(value);120+ }121+122 return value;123 }124 125 function makeAllStream(spawned) {301 unmodified lines427 throw new TypeError('The `input` option cannot be a stream in sync mode');428 }429 430 const result = childProcess.spawnSync(parsed.command, parsed.args, parsed.options);426- result.stdout = handleOutput(parsed.options, result.stdout);427- result.stderr = handleOutput(parsed.options, result.stderr);431+ result.stdout = handleOutput(parsed.options, result.stdout, result.error);432+ result.stderr = handleOutput(parsed.options, result.stderr, result.error);433 434 if (result.error || result.status !== 0 || result.signal !== null) {435 const error = makeError(result, {436 code: result.status,26 unmodified linesmodified:readme.md+2 added-2 removed
105 unmodified lines106 spawnargs: ['command'],107 command: 'wrong command',108 exitCode: 2,109 exitCodeName: 'ENOENT',110- stdout: null,111- stderr: null,110+ stdout: '',111+ stderr: '',112 failed: true,113 timedOut: false,114 isCanceled: false,115 killed: false313 unmodified linesmodified:test.js+34 added
49 unmodified lines50 t.is(typeof stderr, 'string');51 t.is(typeof all, 'string');52 });53 54+test('stdout/stderr/all are undefined if ignored', async t => {55+ const {stdout, stderr, all} = await execa('noop', {stdio: 'ignore'});56+ t.is(stdout, undefined);57+ t.is(stderr, undefined);58+ t.is(all, undefined);59+});60+61+test('stdout/stderr/all are undefined if ignored in sync mode', t => {62+ const {stdout, stderr, all} = execa.sync('noop', {stdio: 'ignore'});63+ t.is(stdout, undefined);64+ t.is(stderr, undefined);65+ t.is(all, undefined);66+});67+68+const WRONG_COMMAND_STDERR = process.platform === 'win32' ?69+ '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :70+ '';71+72+test('stdout/stderr/all on process errors', async t => {73+ const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command'));74+ t.is(stdout, '');75+ t.is(stderr, WRONG_COMMAND_STDERR);76+ t.is(all, WRONG_COMMAND_STDERR);77+});78+79+test('stdout/stderr/all on process errors, in sync mode', t => {80+ const {stdout, stderr, all} = t.throws(() => {81+ execa.sync('wrong command');82+ });83+ t.is(stdout, '');84+ t.is(stderr, WRONG_COMMAND_STDERR);85+ t.is(all, undefined);86+});87+88 test('pass `stdout` to a file descriptor', async t => {89 const file = tempfile('.txt');90 await execa('fixtures/noop', ['foo bar'], {stdout: fs.openSync(file, 'w')});91 t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');617 unmodified lines