Skip to content
Myr

streaming.js

This file

Path

test/pipe/streaming.js

441 lines · 17.4 KiB · JavaScript

Last changed by

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

History of this file

Take it

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 1–7why, one step back
1import {once} from 'node:events';
2import {PassThrough} from 'node:stream';3import test from 'ava';4import {execa} from '../../index.js';5import {setFixtureDir} from '../helpers/fixtures-dir.js';6import {foobarString} from '../helpers/input.js';7import {assertMaxListeners} from '../helpers/listeners.js';

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 8why, one step back
8import {fullReadableStdio} from '../helpers/stdio.js';

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 9–43why, one step back
9
10setFixtureDir();1112test('Can pipe two sources to same destination', async t => {13 const source = execa('noop.js', [foobarString]);14 const secondSource = execa('noop.js', [foobarString]);15 const destination = execa('stdin.js');16 const pipePromise = source.pipe(destination);17 const secondPipePromise = secondSource.pipe(destination);1819 t.like(await source, {stdout: foobarString});20 t.like(await secondSource, {stdout: foobarString});21 t.like(await destination, {stdout: `${foobarString}\n${foobarString}`});22 t.is(await pipePromise, await destination);23 t.is(await secondPipePromise, await destination);24});2526test('Can pipe three sources to same destination', async t => {27 const source = execa('noop.js', [foobarString]);28 const secondSource = execa('noop.js', [foobarString]);29 const thirdSource = execa('noop.js', [foobarString]);30 const destination = execa('stdin.js');31 const pipePromise = source.pipe(destination);32 const secondPipePromise = secondSource.pipe(destination);33 const thirdPromise = thirdSource.pipe(destination);3435 t.like(await source, {stdout: foobarString});36 t.like(await secondSource, {stdout: foobarString});37 t.like(await thirdSource, {stdout: foobarString});38 t.like(await destination, {stdout: `${foobarString}\n${foobarString}\n${foobarString}`});39 t.is(await pipePromise, await destination);40 t.is(await secondPipePromise, await destination);41 t.is(await thirdPromise, await destination);42});43

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 44why, one step back
44const subprocessesCount = 100;

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 45–48why, one step back
45
46test.serial('Can pipe many sources to same destination', async t => {47 const checkMaxListeners = assertMaxListeners(t);48

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 49why, one step back
49 const expectedResults = Array.from({length: subprocessesCount}, (_, index) => `${index}`).sort();

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 50–66why, one step back
50 const sources = expectedResults.map(expectedResult => execa('noop.js', [expectedResult]));
51 const destination = execa('stdin.js');52 const pipePromises = sources.map(source => source.pipe(destination));5354 const results = await Promise.all(sources);55 t.deepEqual(results.map(({stdout}) => stdout), expectedResults);56 const destinationResult = await destination;57 t.deepEqual(destinationResult.stdout.split('\n').sort(), expectedResults);58 t.deepEqual(await Promise.all(pipePromises), sources.map(() => destinationResult));5960 checkMaxListeners();61});6263test.serial('Can pipe same source to many destinations', async t => {64 const checkMaxListeners = assertMaxListeners(t);6566 const source = execa('noop-fd.js', ['1', foobarString]);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 67why, one step back
67 const expectedResults = Array.from({length: subprocessesCount}, (_, index) => `${index}`);

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 68–78why, one step back
68 const destinations = expectedResults.map(expectedResult => execa('noop-stdin-double.js', [expectedResult]));
69 const pipePromises = destinations.map(destination => source.pipe(destination));7071 t.like(await source, {stdout: foobarString});72 const results = await Promise.all(destinations);73 t.deepEqual(results.map(({stdout}) => stdout), expectedResults.map(result => `${foobarString} ${result}`));74 t.deepEqual(await Promise.all(pipePromises), results);7576 checkMaxListeners();77});78

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 79why, one step back
79test('Can pipe two streams from same subprocess to same destination', async t => {

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 80–86why, one step back
80 const source = execa('noop-both.js', [foobarString]);
81 const destination = execa('stdin.js');82 const pipePromise = source.pipe(destination);83 const secondPipePromise = source.pipe(destination, {from: 'stderr'});8485 t.like(await source, {stdout: foobarString, stderr: foobarString});86 t.like(await destination, {stdout: `${foobarString}\n${foobarString}`});

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 87–94why, one step back
87 t.is(await pipePromise, await destination);
88 t.is(await secondPipePromise, await destination);89});9091test('Can pipe same source to two streams from same subprocess', async t => {92 const source = execa('noop-fd.js', ['1', foobarString]);93 const destination = execa('stdin-fd-both.js', ['3'], fullReadableStdio());94 const pipePromise = source.pipe(destination);

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

line 95why, one step back
95 const secondPipePromise = source.pipe(destination, {to: 'fd3'});

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 96–98why, one step back
96
97 t.like(await source, {stdout: foobarString});98 t.like(await destination, {stdout: `${foobarString}${foobarString}`});

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 99–177why, one step back
99 t.is(await pipePromise, await destination);
100 t.is(await secondPipePromise, await destination);101});102103test('Can pipe a new source to same destination after some source has already written', async t => {104 const passThroughStream = new PassThrough();105 const source = execa('stdin.js', {stdin: ['pipe', passThroughStream]});106 const destination = execa('stdin.js');107 const pipePromise = source.pipe(destination);108109 passThroughStream.write('foo');110 const firstWrite = await once(destination.stdout, 'data');111 t.is(firstWrite.toString(), 'foo');112113 const secondSource = execa('noop.js', ['bar']);114 const secondPipePromise = secondSource.pipe(destination);115 passThroughStream.end();116117 t.like(await source, {stdout: 'foo'});118 t.like(await secondSource, {stdout: 'bar'});119 t.like(await destination, {stdout: 'foobar'});120 t.is(await pipePromise, await destination);121 t.is(await secondPipePromise, await destination);122});123124test('Can pipe a second source to same destination after destination has already ended', async t => {125 const source = execa('noop.js', [foobarString]);126 const destination = execa('stdin.js');127 const pipePromise = source.pipe(destination);128129 t.like(await source, {stdout: foobarString});130 t.like(await destination, {stdout: foobarString});131 t.is(await pipePromise, await destination);132133 const secondSource = execa('noop.js', [foobarString]);134 const secondPipePromise = secondSource.pipe(destination);135136 t.like(await secondSource, {stdout: ''});137 t.is(await secondPipePromise, await destination);138});139140test('Can pipe same source to a second destination after source has already ended', async t => {141 const source = execa('noop.js', [foobarString]);142 const destination = execa('stdin.js');143 const pipePromise = source.pipe(destination);144145 t.like(await source, {stdout: foobarString});146 t.like(await destination, {stdout: foobarString});147 t.is(await pipePromise, await destination);148149 const secondDestination = execa('stdin.js');150 const secondPipePromise = source.pipe(secondDestination);151152 t.like(await secondDestination, {stdout: ''});153 t.is(await secondPipePromise, await secondDestination);154});155156test('Can pipe a new source to same destination after some but not all sources have ended', async t => {157 const source = execa('noop.js', [foobarString]);158 const passThroughStream = new PassThrough();159 const secondSource = execa('stdin.js', {stdin: ['pipe', passThroughStream]});160 const destination = execa('stdin.js');161 const pipePromise = source.pipe(destination);162 const secondPipePromise = secondSource.pipe(destination);163164 t.like(await source, {stdout: foobarString});165166 const thirdSource = execa('noop.js', [foobarString]);167 const thirdPipePromise = thirdSource.pipe(destination);168 passThroughStream.end(`${foobarString}\n`);169170 t.like(await secondSource, {stdout: foobarString});171 t.like(await thirdSource, {stdout: foobarString});172 t.like(await destination, {stdout: `${foobarString}\n${foobarString}\n${foobarString}`});173 t.is(await pipePromise, await destination);174 t.is(await secondPipePromise, await destination);175 t.is(await thirdPipePromise, await destination);176});177

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 178why, one step back
178test('Can pipe two subprocesses already ended', async t => {

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 179–194why, one step back
179 const source = execa('noop.js', [foobarString]);
180 const destination = execa('stdin.js');181 destination.stdin.end('.');182 await Promise.all([source, destination]);183 const pipePromise = source.pipe(destination);184185 t.like(await source, {stdout: foobarString});186 t.like(await destination, {stdout: '.'});187 t.is(await pipePromise, await destination);188});189190test('Can pipe to same destination through multiple paths', async t => {191 const source = execa('noop.js', [foobarString]);192 const destination = execa('stdin.js');193 const secondDestination = execa('stdin.js');194 const pipePromise = source.pipe(destination);

Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa

line 195why, one step back
195 const secondPipePromise = pipePromise.pipe(secondDestination);

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 196–229why, one step back
196 const thirdPipePromise = source.pipe(secondDestination);
197198 t.like(await source, {stdout: foobarString});199 t.like(await destination, {stdout: foobarString});200 t.like(await secondDestination, {stdout: `${foobarString}\n${foobarString}`});201 t.is(await pipePromise, await destination);202 t.is(await secondPipePromise, await secondDestination);203 t.is(await thirdPipePromise, await secondDestination);204});205206test('Can pipe two sources to same destination in objectMode', async t => {207 const stdoutTransform = {208 * transform() {209 yield [foobarString];210 },211 objectMode: true,212 };213 const source = execa('noop.js', [''], {stdout: stdoutTransform});214 const secondSource = execa('noop.js', [''], {stdout: stdoutTransform});215 t.true(source.stdout.readableObjectMode);216 t.true(secondSource.stdout.readableObjectMode);217218 const stdinTransform = {219 * transform([chunk]) {220 yield chunk;221 },222 objectMode: true,223 };224 const destination = execa('stdin.js', {stdin: stdinTransform});225 const pipePromise = source.pipe(destination);226 const secondPipePromise = secondSource.pipe(destination);227228 t.like(await source, {stdout: [[foobarString]]});229 t.like(await secondSource, {stdout: [[foobarString]]});

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 230why, one step back
230 t.like(await destination, {stdout: `${foobarString}\n${foobarString}`});

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 231–271why, one step back
231 t.is(await pipePromise, await destination);
232 t.is(await secondPipePromise, await destination);233});234235test('Can pipe one source to two destinations', async t => {236 const source = execa('noop.js', [foobarString]);237 const destination = execa('stdin.js');238 const secondDestination = execa('stdin.js');239 const pipePromise = source.pipe(destination);240 const secondPipePromise = source.pipe(secondDestination);241242 t.like(await source, {stdout: foobarString});243 t.like(await destination, {stdout: foobarString});244 t.like(await secondDestination, {stdout: foobarString});245 t.is(await pipePromise, await destination);246 t.is(await secondPipePromise, await secondDestination);247});248249test('Can pipe one source to three destinations', async t => {250 const source = execa('noop.js', [foobarString]);251 const destination = execa('stdin.js');252 const secondDestination = execa('stdin.js');253 const thirdDestination = execa('stdin.js');254 const pipePromise = source.pipe(destination);255 const secondPipePromise = source.pipe(secondDestination);256 const thirdPipePromise = source.pipe(thirdDestination);257258 t.like(await source, {stdout: foobarString});259 t.like(await destination, {stdout: foobarString});260 t.like(await secondDestination, {stdout: foobarString});261 t.like(await thirdDestination, {stdout: foobarString});262 t.is(await pipePromise, await destination);263 t.is(await secondPipePromise, await secondDestination);264 t.is(await thirdPipePromise, await thirdDestination);265});266267test('Can create a series of pipes', async t => {268 const source = execa('noop.js', [foobarString]);269 const destination = execa('stdin.js');270 const secondDestination = execa('stdin.js');271 const pipePromise = source.pipe(destination);

Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa

line 272why, one step back
272 const secondPipePromise = pipePromise.pipe(secondDestination);

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 273–299why, one step back
273
274 t.like(await source, {stdout: foobarString});275 t.like(await destination, {stdout: foobarString});276 t.like(await secondDestination, {stdout: foobarString});277 t.is(await pipePromise, await destination);278 t.is(await secondPipePromise, await secondDestination);279});280281test('Returns pipedFrom on success', async t => {282 const source = execa('noop.js', [foobarString]);283 const destination = execa('stdin.js');284 const pipePromise = source.pipe(destination);285286 const destinationResult = await destination;287 t.deepEqual(destinationResult.pipedFrom, []);288 const sourceResult = await source;289290 t.like(await pipePromise, {pipedFrom: [sourceResult]});291 t.deepEqual(destinationResult.pipedFrom, [sourceResult]);292 t.deepEqual(sourceResult.pipedFrom, []);293});294295test('Returns pipedFrom on deep success', async t => {296 const source = execa('noop.js', [foobarString]);297 const destination = execa('stdin.js');298 const secondDestination = execa('stdin.js');299 const pipePromise = source.pipe(destination);

Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa

line 300why, one step back
300 const secondPipePromise = pipePromise.pipe(secondDestination);

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 301–359why, one step back
301
302 const destinationResult = await destination;303 t.deepEqual(destinationResult.pipedFrom, []);304 const secondDestinationResult = await secondDestination;305 t.deepEqual(secondDestinationResult.pipedFrom, []);306 const sourceResult = await source;307308 t.like(await secondPipePromise, {pipedFrom: [destinationResult]});309 t.deepEqual(secondDestinationResult.pipedFrom, [destinationResult]);310 t.like(await pipePromise, {pipedFrom: [sourceResult]});311 t.deepEqual(destinationResult.pipedFrom, [sourceResult]);312 t.deepEqual(sourceResult.pipedFrom, []);313});314315test('Returns pipedFrom on source failure', async t => {316 const source = execa('noop-fail.js', ['1', foobarString]);317 const destination = execa('stdin.js');318 const pipePromise = source.pipe(destination);319320 const destinationResult = await destination;321 t.deepEqual(destinationResult.pipedFrom, []);322 const sourceResult = await t.throwsAsync(source);323324 t.like(await t.throwsAsync(pipePromise), {pipedFrom: []});325 t.deepEqual(destinationResult.pipedFrom, [sourceResult]);326 t.deepEqual(sourceResult.pipedFrom, []);327});328329test('Returns pipedFrom on destination failure', async t => {330 const source = execa('noop.js', [foobarString]);331 const destination = execa('stdin-fail.js');332 const pipePromise = source.pipe(destination);333334 const destinationResult = await t.throwsAsync(destination);335 const sourceResult = await source;336337 t.like(await t.throwsAsync(pipePromise), {pipedFrom: [sourceResult]});338 t.deepEqual(destinationResult.pipedFrom, [sourceResult]);339 t.deepEqual(sourceResult.pipedFrom, []);340});341342test('Returns pipedFrom on source + destination failure', async t => {343 const source = execa('noop-fail.js', ['1', foobarString]);344 const destination = execa('stdin-fail.js');345 const pipePromise = source.pipe(destination);346347 const destinationResult = await t.throwsAsync(destination);348 const sourceResult = await t.throwsAsync(source);349350 t.like(await t.throwsAsync(pipePromise), {pipedFrom: [sourceResult]});351 t.deepEqual(destinationResult.pipedFrom, [sourceResult]);352 t.deepEqual(sourceResult.pipedFrom, []);353});354355test('Returns pipedFrom on deep failure', async t => {356 const source = execa('noop-fail.js', ['1', foobarString]);357 const destination = execa('stdin-fail.js');358 const secondDestination = execa('stdin.js');359 const pipePromise = source.pipe(destination);

Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa

line 360why, one step back
360 const secondPipePromise = pipePromise.pipe(secondDestination);

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 361–392why, one step back
361
362 const destinationResult = await t.throwsAsync(destination);363 const secondDestinationResult = await secondDestination;364 t.deepEqual(secondDestinationResult.pipedFrom, []);365 const sourceResult = await t.throwsAsync(source);366367 t.like(await t.throwsAsync(secondPipePromise), {pipedFrom: [sourceResult]});368 t.deepEqual(secondDestinationResult.pipedFrom, [destinationResult]);369 t.like(await t.throwsAsync(pipePromise), {pipedFrom: [sourceResult]});370 t.deepEqual(destinationResult.pipedFrom, [sourceResult]);371 t.deepEqual(sourceResult.pipedFrom, []);372});373374test('Returns pipedFrom from multiple sources', async t => {375 const source = execa('noop.js', [foobarString]);376 const secondSource = execa('noop.js', [foobarString]);377 const destination = execa('stdin.js');378 const pipePromise = source.pipe(destination);379 const secondPipePromise = secondSource.pipe(destination);380381 const destinationResult = await destination;382 t.deepEqual(destinationResult.pipedFrom, []);383 const sourceResult = await source;384 const secondSourceResult = await secondSource;385386 t.like(await pipePromise, {pipedFrom: [sourceResult, secondSourceResult]});387 t.like(await secondPipePromise, {pipedFrom: [sourceResult, secondSourceResult]});388 t.deepEqual(destinationResult.pipedFrom, [sourceResult, secondSourceResult]);389 t.deepEqual(sourceResult.pipedFrom, []);390 t.deepEqual(secondSourceResult.pipedFrom, []);391});392

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 393why, one step back
393test('Returns pipedFrom from already ended subprocesses', async t => {

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 394–413why, one step back
394 const source = execa('noop.js', [foobarString]);
395 const destination = execa('stdin.js');396 destination.stdin.end('.');397 await Promise.all([source, destination]);398 const pipePromise = source.pipe(destination);399400 const destinationResult = await destination;401 t.deepEqual(destinationResult.pipedFrom, []);402 const sourceResult = await source;403 t.deepEqual(sourceResult.pipedFrom, []);404405 t.like(await pipePromise, {pipedFrom: [sourceResult]});406 t.deepEqual(destinationResult.pipedFrom, [sourceResult]);407 t.deepEqual(sourceResult.pipedFrom, []);408});409410test('Does not return nor set pipedFrom on signal abort', async t => {411 const abortController = new AbortController();412 const source = execa('empty.js');413 const destination = execa('empty.js');

Rename `signal` option to `unpipeSignal` (#852)ehmickybefore adoption9dc1141

line 414why, one step back
414 const pipePromise = source.pipe(destination, {unpipeSignal: abortController.signal});

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 415–441why, one step back
415
416 abortController.abort();417 t.like(await t.throwsAsync(pipePromise), {pipedFrom: []});418 const destinationResult = await destination;419 t.deepEqual(destinationResult.pipedFrom, []);420 const sourceResult = await source;421 t.deepEqual(sourceResult.pipedFrom, []);422});423424test('Can pipe same source to same destination twice', async t => {425 const source = execa('noop.js', [foobarString]);426 const destination = execa('stdin.js');427 const pipePromise = source.pipe(destination);428 const secondPipePromise = source.pipe(destination);429430 const destinationResult = await destination;431 t.like(destinationResult, {pipedFrom: []});432 const sourceResult = await source;433 t.like(sourceResult, {pipedFrom: []});434435 t.like(await source, {stdout: foobarString});436 t.like(await destination, {stdout: foobarString});437 t.is(await pipePromise, destinationResult);438 t.is(await secondPipePromise, destinationResult);439 t.deepEqual(destinationResult.pipedFrom, [sourceResult]);440 t.deepEqual(sourceResult.pipedFrom, []);441});