Skip to content
Myr

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

3 files+46 added−7 removed

modified:index.js+10 added-5 removed
107 unmodified lines
108 spawned.stdin.end(input);109 }110}111112function handleOutput(options, value) {113 if (value && options.stripFinalNewline) {114 value = stripFinalNewline(value);112function 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 }117118 if (options.stripFinalNewline) {119 return stripFinalNewline(value);120 }121122 return value;123}124125function makeAllStream(spawned) {
301 unmodified lines
427 throw new TypeError('The `input` option cannot be a stream in sync mode');428 }429430 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);433434 if (result.error || result.status !== 0 || result.signal !== null) {435 const error = makeError(result, {436 code: result.status,
26 unmodified lines
modified:readme.md+2 added-2 removed
105 unmodified lines
106 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: false
313 unmodified lines
modified:test.js+34 added
49 unmodified lines
50 t.is(typeof stderr, 'string');51 t.is(typeof all, 'string');52});5354test('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});6061test('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});6768const WRONG_COMMAND_STDERR = process.platform === 'win32' ?69 '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :70 '';7172test('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});7879test('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});8788test('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