Skip to content
Myr

Add `all` option (#353)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tiago Nápoli · committed by Sindre Sorhus · parent bfef2c2 · Browse files · 8 files+104 added−36 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: ehmicky <ehmicky@gmail.com>

Diff stats

8 files+104 added−36 removed

  • tests:+58 added−13 removed
  • other:+46 added−23 removed

8 files+104 added−36 removed

modified:lib/stream.js+3 added-12 removed
modified:test/fixtures/max-buffer+2 added-2 removed
1#!/usr/bin/env node2'use strict';3const output = process.argv[2];4const bytes = Number(process.argv[3]);3const output = process.argv[2] || 'stdout';4const bytes = Number(process.argv[3] || 1e7);56process[output].write('.'.repeat(bytes - 1) + '\n');
modified:test/error.js+2 added-2 removed
8 unmodified lines
910const getExitRegExp = exitMessage => new RegExp(`failed with exit code ${exitMessage}`);1112test('stdout/stderr/all available on errors', async t => {13 const {stdout, stderr, all} = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});13 const {stdout, stderr, all} = await t.throwsAsync(execa('exit', ['2'], {all: true}), {message: getExitRegExp('2')});14 t.is(typeof stdout, 'string');15 t.is(typeof stderr, 'string');16 t.is(typeof all, 'string');17});
2 unmodified lines
20 '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :21 '';2223test('stdout/stderr/all on process errors', async t => {24 const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command'));24 const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command', {all: true}));25 t.is(stdout, '');26 t.is(stderr, WRONG_COMMAND);27 t.is(all, WRONG_COMMAND);28});
137 unmodified lines
modified:test/stream.js+54 added-9 removed
25 unmodified lines
26 t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');27});2829test.serial('result.all shows both `stdout` and `stderr` intermixed', async t => {30 const {all} = await execa('noop-132');30 const {all} = await execa('noop-132', {all: true});31 t.is(all, '132');32});3334test('result.all is undefined unless opts.all is true', async t => {35 const {all} = await execa('noop');36 t.is(all, undefined);37});3839test('stdout/stderr/all are undefined if ignored', async t => {35 const {stdout, stderr, all} = await execa('noop', {stdio: 'ignore'});40 const {stdout, stderr, all} = await execa('noop', {stdio: 'ignore', all: true});41 t.is(stdout, undefined);42 t.is(stderr, undefined);43 t.is(all, undefined);44});4546test('stdout/stderr/all are undefined if ignored in sync mode', t => {42 const {stdout, stderr, all} = execa.sync('noop', {stdio: 'ignore'});47 const {stdout, stderr, all} = execa.sync('noop', {stdio: 'ignore', all: true});48 t.is(stdout, undefined);49 t.is(stderr, undefined);50 t.is(all, undefined);51});
50 unmodified lines
102});103104test('maxBuffer affects stdout', async t => {105 await t.notThrowsAsync(execa('max-buffer', ['stdout', '10'], {maxBuffer: 10}));101 const {stdout, all} = await t.throwsAsync(execa('max-buffer', ['stdout', '11'], {maxBuffer: 10}), /max-buffer stdout/);106 const {stdout, all} = await t.throwsAsync(execa('max-buffer', ['stdout', '11'], {maxBuffer: 10, all: true}), /max-buffer stdout/);107 t.is(stdout, '.'.repeat(10));108 t.is(all, '.'.repeat(10));109});110111test('maxBuffer affects stderr', async t => {112 await t.notThrowsAsync(execa('max-buffer', ['stderr', '10'], {maxBuffer: 10}));108 const {stderr, all} = await t.throwsAsync(execa('max-buffer', ['stderr', '11'], {maxBuffer: 10}), /max-buffer stderr/);113 const {stderr, all} = await t.throwsAsync(execa('max-buffer', ['stderr', '11'], {maxBuffer: 10, all: true}), /max-buffer stderr/);114 t.is(stderr, '.'.repeat(10));115 t.is(all, '.'.repeat(10));116});117118test('do not buffer stdout when `buffer` set to `false`', async t => {119 const promise = execa('max-buffer', ['stdout', '10'], {buffer: false});120 const [result, stdout] = await Promise.all([121 promise,117 getStream(promise.stdout),118 getStream(promise.all)122 getStream(promise.stdout)123 ]);124125 t.is(result.stdout, undefined);126 t.is(stdout, '.........\n');
2 unmodified lines
129test('do not buffer stderr when `buffer` set to `false`', async t => {130 const promise = execa('max-buffer', ['stderr', '10'], {buffer: false});131 const [result, stderr] = await Promise.all([132 promise,129 getStream(promise.stderr),130 getStream(promise.all)133 getStream(promise.stderr)134 ]);135136 t.is(result.stderr, undefined);137 t.is(stderr, '.........\n');
3 unmodified lines
141 const {stdout} = execa('max-buffer', ['stdout', '21'], {maxBuffer: 10});142 const result = await getStream(stdout);143 t.is(result, '....................\n');144});145146test('buffer: false > promise resolves', async t => {147 await t.notThrowsAsync(execa('noop', {buffer: false}));148});149150test('buffer: false > promise resolves when output is big but is not pipable', async t => {151 await t.notThrowsAsync(execa('max-buffer', {buffer: false, stdout: 'ignore'}));152});153154test('buffer: false > promise resolves when output is big and is read', async t => {155 const cp = execa('max-buffer', {buffer: false});156 cp.stdout.resume();157 cp.stderr.resume();158 await t.notThrowsAsync(cp);159});160161test('buffer: false > promise resolves when output is big and "all" is used and is read', async t => {162 const cp = execa('max-buffer', {buffer: false, all: true});163 cp.all.resume();164 await t.notThrowsAsync(cp);165});166167test('buffer: false > promise rejects when process returns non-zero', async t => {168 const cp = execa('fail', {buffer: false});169 const {exitCode} = await t.throwsAsync(cp);170 t.is(exitCode, 2);171});172173const BUFFER_TIMEOUT = 1e3;174175test.serial('buffer: false > promise does not resolve when output is big and is not read', async t => {176 const {timedOut} = await t.throwsAsync(execa('max-buffer', {buffer: false, timeout: BUFFER_TIMEOUT}));177 t.true(timedOut);178});179180test.serial('buffer: false > promise does not resolve when output is big and "all" is used but not read', async t => {181 const cp = execa('max-buffer', {buffer: false, all: true, timeout: BUFFER_TIMEOUT});182 cp.stdout.resume();183 cp.stderr.resume();184 const {timedOut} = await t.throwsAsync(cp);185 t.true(timedOut);186});
modified:index.d.ts+21 added-4 removed
37 unmodified lines
38 */39 readonly localDir?: string;4041 /**42 Buffer the output from the spawned process. When buffering is disabled you must consume the output of the `stdout` and `stderr` streams because the promise will not be resolved/rejected until they have completed.42 Buffer the output from the spawned process. When set to `false`, you must read the output of `stdout` and `stderr` (or `all` if the `all` option is `true`). Otherwise the returned promise will not be resolved/rejected.4344 If the spawned process fails, `error.stdout`, `error.stderr`, and `error.all` will contain the buffered data.4546 @default true
28 unmodified lines
75 */76 readonly reject?: boolean;7778 /**79 Add an `.all` property on the promise and the resolved value. The property contains the output of the process with `stdout` and `stderr` interleaved.8081 @default false82 */83 readonly all?: boolean;8485 /**86 Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from the output.8788 @default true89 */
181 unmodified lines
271 interface ExecaReturnValue<StdoutErrorType = string>272 extends ExecaSyncReturnValue<StdoutErrorType> {273 /**274 The output of the process with `stdout` and `stderr` interleaved.275276 This is `undefined` if either:277 - the `all` option is `false` (default value)278 - `execa.sync()` was used279 */269 all: StdoutErrorType;280 all?: StdoutErrorType;281282 /**283 Whether the process was canceled.284 */
12 unmodified lines
297 interface ExecaError<StdoutErrorType = string>298 extends ExecaSyncError<StdoutErrorType> {299 /**300 The output of the process with `stdout` and `stderr` interleaved.301302 This is `undefined` if either:303 - the `all` option is `false` (default value)304 - `execa.sync()` was used305 */291 all: StdoutErrorType;306 all?: StdoutErrorType;307308 /**309 Whether the process was canceled.310 */
28 unmodified lines
339340 /**341 Stream combining/interleaving [`stdout`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout) and [`stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).342328 This is `undefined` when both `stdout` and `stderr` options are set to [`'pipe'`, `'ipc'`, `Stream` or `integer`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).343 This is `undefined` if either:344 - the `all` option is `false` (the default value)345 - both `stdout` and `stderr` options are set to [`'inherit'`, `'ipc'`, `Stream` or `integer`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio)346 */347 all?: ReadableStream;348 }349
139 unmodified lines
modified:index.js+2 added-1 removed
38 unmodified lines
39 localDir: options.cwd || process.cwd(),40 encoding: 'utf8',41 reject: true,42 cleanup: true,43 all: false,44 ...options,45 windowsHide: true46 };47
102 unmodified lines
150 crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed);151152 handleInput(spawned, parsed.options.input);153153 spawned.all = makeAllStream(spawned);154 spawned.all = makeAllStream(spawned, parsed.options);155156 return mergePromise(spawned, handlePromiseOnce);157};158
96 unmodified lines
modified:index.test-d.ts+3 added-2 removed
18 unmodified lines
19 expectType<number>(unicornsResult.exitCode);20 expectType<string>(unicornsResult.exitCodeName);21 expectType<string>(unicornsResult.stdout);22 expectType<string>(unicornsResult.stderr);23 expectType<string>(unicornsResult.all);23 expectType<string | undefined>(unicornsResult.all);24 expectType<boolean>(unicornsResult.failed);25 expectType<boolean>(unicornsResult.timedOut);26 expectType<boolean>(unicornsResult.isCanceled);27 expectType<boolean>(unicornsResult.killed);
5 unmodified lines
33 expectType<number>(execaError.exitCode);34 expectType<string>(execaError.exitCodeName);35 expectType<string>(execaError.stdout);36 expectType<string>(execaError.stderr);37 expectType<string>(execaError.all);37 expectType<string | undefined>(execaError.all);38 expectType<boolean>(execaError.failed);39 expectType<boolean>(execaError.timedOut);40 expectType<boolean>(execaError.isCanceled);41 expectType<boolean>(execaError.killed);
56 unmodified lines
98execa('unicorns', {stderr: 'inherit'});99execa('unicorns', {stderr: process.stderr});100execa('unicorns', {stderr: 1});101execa('unicorns', {stderr: undefined});102execa('unicorns', {all: true});103execa('unicorns', {reject: false});104execa('unicorns', {stripFinalNewline: false});105execa('unicorns', {extendEnv: false});106execa('unicorns', {cwd: '.'});
78 unmodified lines
modified:readme.md+17 added-4 removed
159 unmodified lines
160Type: `ReadableStream | undefined`161162Stream combining/interleaving [`stdout`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout) and [`stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).163164This is `undefined` when both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to [`'pipe'`, `'ipc'`, `Stream` or `integer`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).164This is `undefined` if either:165 - the [`all` option](#all-2) is `false` (the default value)166 - both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to [`'inherit'`, `'ipc'`, `Stream` or `integer`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio)167168### execa.sync(file, [arguments], [options])169170Execute a file synchronously.
60 unmodified lines
231The output of the process on stderr.232233#### all234233Type: `string | Buffer`235Type: `string | Buffer | undefined`236235The output of the process on both stdout and stderr. `undefined` if `execa.sync()` was used.237The output of the process with `stdout` and `stderr` interleaved.238239This is `undefined` if either:240 - the [`all` option](#all-2) is `false` (the default value)241 - `execa.sync()` was used242243#### failed244245Type: `boolean`246
55 unmodified lines
302303Type: `boolean`<br>304Default: `true`305300Buffer the output from the spawned process. When buffering is disabled you must consume the output of the `stdout` and `stderr` streams because the promise will not be resolved/rejected until they have completed.306Buffer the output from the spawned process. When set to `false`, you must read the output of [`stdout`](#stdout-1) and [`stderr`](#stderr-1) (or [`all`](#all) if the [`all`](#all-2) option is `true`). Otherwise the returned promise will not be resolved/rejected.307308If the spawned process fails, [`error.stdout`](#stdout), [`error.stderr`](#stderr), and [`error.all`](#all) will contain the buffered data.309310#### input
23 unmodified lines
334Default: `pipe`335336Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).337338#### all339340Type: `boolean`<br>341Default: `false`342343Add an `.all` property on the [promise](#all) and the [resolved value](#all-1). The property contains the output of the process with `stdout` and `stderr` interleaved.344345#### reject346347Type: `boolean`<br>348Default: `true`
196 unmodified lines