Skip to content
Myr

stream.js

This file

Path

test/stream.js

189 lines · 6.0 KiB · JavaScript

Last changed by

Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd

History of this file

Take it

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 1–29why, one step back
1import path from 'path';
2import fs from 'fs';3import stream from 'stream';4import test from 'ava';5import getStream from 'get-stream';6import tempfile from 'tempfile';7import execa from '..';89process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;1011test('buffer', async t => {12 const {stdout} = await execa('noop', ['foo'], {encoding: null});13 t.true(Buffer.isBuffer(stdout));14 t.is(stdout.toString(), 'foo');15});1617test('pass `stdout` to a file descriptor', async t => {18 const file = tempfile('.txt');19 await execa('test/fixtures/noop', ['foo bar'], {stdout: fs.openSync(file, 'w')});20 t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');21});2223test('pass `stderr` to a file descriptor', async t => {24 const file = tempfile('.txt');25 await execa('test/fixtures/noop-err', ['foo bar'], {stderr: fs.openSync(file, 'w')});26 t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');27});2829test.serial('result.all shows both `stdout` and `stderr` intermixed', async t => {

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 30why, one step back
30 const {all} = await execa('noop-132', {all: true});

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 31–33why, one step back
31 t.is(all, '132');
32});33

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

lines 34–38why, one step back
34test('result.all is undefined unless opts.all is true', async t => {
35 const {all} = await execa('noop');36 t.is(all, undefined);37});38

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

line 39why, one step back
39test('stdout/stderr/all are undefined if ignored', async t => {

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 40why, one step back
40 const {stdout, stderr, all} = await execa('noop', {stdio: 'ignore', all: true});

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 41–46why, one step back
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 => {

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 47why, one step back
47 const {stdout, stderr, all} = execa.sync('noop', {stdio: 'ignore', all: true});

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 48–105why, one step back
48 t.is(stdout, undefined);
49 t.is(stderr, undefined);50 t.is(all, undefined);51});5253test('input option can be a String', async t => {54 const {stdout} = await execa('stdin', {input: 'foobar'});55 t.is(stdout, 'foobar');56});5758test('input option can be a Buffer', async t => {59 const {stdout} = await execa('stdin', {input: 'testing12'});60 t.is(stdout, 'testing12');61});6263test('input can be a Stream', async t => {64 const s = new stream.PassThrough();65 s.write('howdy');66 s.end();67 const {stdout} = await execa('stdin', {input: s});68 t.is(stdout, 'howdy');69});7071test('you can write to child.stdin', async t => {72 const child = execa('stdin');73 child.stdin.end('unicorns');74 t.is((await child).stdout, 'unicorns');75});7677test('input option can be a String - sync', t => {78 const {stdout} = execa.sync('stdin', {input: 'foobar'});79 t.is(stdout, 'foobar');80});8182test('input option can be a Buffer - sync', t => {83 const {stdout} = execa.sync('stdin', {input: Buffer.from('testing12', 'utf8')});84 t.is(stdout, 'testing12');85});8687test('opts.stdout:ignore - stdout will not collect data', async t => {88 const {stdout} = await execa('stdin', {89 input: 'hello',90 stdio: [undefined, 'ignore', undefined]91 });92 t.is(stdout, undefined);93});9495test('helpful error trying to provide an input stream in sync mode', t => {96 t.throws(97 () => {98 execa.sync('stdin', {input: new stream.PassThrough()});99 },100 /The `input` option cannot be a stream in sync mode/101 );102});103104test('maxBuffer affects stdout', async t => {105 await t.notThrowsAsync(execa('max-buffer', ['stdout', '10'], {maxBuffer: 10}));

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 106why, one step back
106 const {stdout, all} = await t.throwsAsync(execa('max-buffer', ['stdout', '11'], {maxBuffer: 10, all: true}), /max-buffer stdout/);

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 107–112why, one step back
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}));

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 113why, one step back
113 const {stderr, all} = await t.throwsAsync(execa('max-buffer', ['stderr', '11'], {maxBuffer: 10, all: true}), /max-buffer stderr/);

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 114–121why, one step back
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,

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 122why, one step back
122 getStream(promise.stdout)

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 123–132why, one step back
123 ]);
124125 t.is(result.stdout, undefined);126 t.is(stdout, '.........\n');127});128129test('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,

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 133why, one step back
133 getStream(promise.stderr)

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 134–143why, one step back
134 ]);
135136 t.is(result.stderr, undefined);137 t.is(stderr, '.........\n');138});139140test('do not buffer when streaming', async t => {141 const {stdout} = execa('max-buffer', ['stdout', '21'], {maxBuffer: 10});142 const result = await getStream(stdout);143 t.is(result, '....................\n');

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

lines 144–174why, one step back
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;174

Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd

lines 175–180why, one step back
175// On Unix (not Windows), a process won't exit if stdout has not been read.
176if (process.platform !== 'win32') {177 test.serial('buffer: false > promise does not resolve when output is big and is not read', async t => {178 const {timedOut} = await t.throwsAsync(execa('max-buffer', {buffer: false, timeout: BUFFER_TIMEOUT}));179 t.true(timedOut);180 });

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 181why, one step back
181

Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd

lines 182–189why, one step back
182 test.serial('buffer: false > promise does not resolve when output is big and "all" is used but not read', async t => {
183 const cp = execa('max-buffer', {buffer: false, all: true, timeout: BUFFER_TIMEOUT});184 cp.stdout.resume();185 cp.stderr.resume();186 const {timedOut} = await t.throwsAsync(cp);187 t.true(timedOut);188 });189}