Skip to content
Myr

streaming.js

This file

Path

test/pipe/streaming.js

538 lines · 22.6 KiB · JavaScript

Last changed by

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

line 1why, one step back
1import assert from 'node:assert/strict';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 2why, one step back
2import {once} from 'node:events';

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

line 3why, one step back
3import {setTimeout} from 'node:timers/promises';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 4why, one step back
4import {PassThrough} from 'node:stream';

TweaksSindre Sorhusbefore adoption698a1b8

lines 5–6why, one step back
5import test from 'node:test';
6import {assertRejects, assertLike} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 7–11why, one step back
7import {execa} from '../../index.js';
8import {setFixtureDirectory} from '../helpers/fixtures-directory.js';9import {foobarString} from '../helpers/input.js';10import {assertMaxListeners} from '../helpers/listeners.js';11import {fullReadableStdio} from '../helpers/stdio.js';

Refactor parallel tests (#1084)ehmickybefore adoption5cf6312

line 12why, one step back
12import {PARALLEL_COUNT} from '../helpers/parallel.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 13–15why, one step back
13
14setFixtureDirectory();15

TweaksSindre Sorhusbefore adoption698a1b8

line 16why, one step back
16test('Can pipe two sources to same destination', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 17–22why, one step back
17 const source = execa('noop.js', [foobarString]);
18 const secondSource = execa('noop.js', [foobarString]);19 const destination = execa('stdin.js');20 const pipePromise = source.pipe(destination);21 const secondPipePromise = secondSource.pipe(destination);22

TweaksSindre Sorhusbefore adoption698a1b8

lines 23–27why, one step back
23 assertLike(await source, {stdout: foobarString});
24 assertLike(await secondSource, {stdout: foobarString});25 assertLike(await destination, {stdout: `${foobarString}\n${foobarString}`});26 assert.equal(await pipePromise, await destination);27 assert.equal(await secondPipePromise, await destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 28–29why, one step back
28});
29

TweaksSindre Sorhusbefore adoption698a1b8

line 30why, one step back
30test('Can pipe three sources to same destination', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 31–38why, one step back
31 const source = execa('noop.js', [foobarString]);
32 const secondSource = execa('noop.js', [foobarString]);33 const thirdSource = execa('noop.js', [foobarString]);34 const destination = execa('stdin.js');35 const pipePromise = source.pipe(destination);36 const secondPipePromise = secondSource.pipe(destination);37 const thirdPromise = thirdSource.pipe(destination);38

TweaksSindre Sorhusbefore adoption698a1b8

lines 39–45why, one step back
39 assertLike(await source, {stdout: foobarString});
40 assertLike(await secondSource, {stdout: foobarString});41 assertLike(await thirdSource, {stdout: foobarString});42 assertLike(await destination, {stdout: `${foobarString}\n${foobarString}\n${foobarString}`});43 assert.equal(await pipePromise, await destination);44 assert.equal(await secondPipePromise, await destination);45 assert.equal(await thirdPromise, await destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 46–47why, one step back
46});
47

TweaksSindre Sorhusbefore adoption698a1b8

lines 48–49why, one step back
48test('Can pipe many sources to same destination', async () => {
49 const checkMaxListeners = assertMaxListeners();

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 50why, one step back
50

Refactor parallel tests (#1084)ehmickybefore adoption5cf6312

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 52–56why, one step back
52 const sources = expectedResults.map(expectedResult => execa('noop.js', [expectedResult]));
53 const destination = execa('stdin.js');54 const pipePromises = sources.map(source => source.pipe(destination));5556 const results = await Promise.all(sources);

TweaksSindre Sorhusbefore adoption698a1b8

line 57why, one step back
57 assert.deepEqual(results.map(({stdout}) => stdout), expectedResults);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 58why, one step back
58 const destinationResult = await destination;

TweaksSindre Sorhusbefore adoption698a1b8

lines 59–60why, one step back
59 assert.deepEqual(destinationResult.stdout.split('\n').sort(), expectedResults);
60 assert.deepEqual(await Promise.all(pipePromises), sources.map(() => destinationResult));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 61–64why, one step back
61
62 checkMaxListeners();63});64

TweaksSindre Sorhusbefore adoption698a1b8

lines 65–66why, one step back
65test('Can pipe same source to many destinations', async () => {
66 const checkMaxListeners = assertMaxListeners();

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 67–68why, one step back
67
68 const source = execa('noop-fd.js', ['1', foobarString]);

Refactor parallel tests (#1084)ehmickybefore adoption5cf6312

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 70–72why, one step back
70 const destinations = expectedResults.map(expectedResult => execa('noop-stdin-double.js', [expectedResult]));
71 const pipePromises = destinations.map(destination => source.pipe(destination));72

TweaksSindre Sorhusbefore adoption698a1b8

line 73why, one step back
73 assertLike(await source, {stdout: foobarString});

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 74why, one step back
74 const results = await Promise.all(destinations);

TweaksSindre Sorhusbefore adoption698a1b8

lines 75–76why, one step back
75 assert.deepEqual(results.map(({stdout}) => stdout), expectedResults.map(result => `${foobarString} ${result}`));
76 assert.deepEqual(await Promise.all(pipePromises), results);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 77–80why, one step back
77
78 checkMaxListeners();79});80

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 82–86why, one step back
82 const source = execa('noop-both.js', [foobarString]);
83 const destination = execa('stdin.js');84 const pipePromise = source.pipe(destination);85 const secondPipePromise = source.pipe(destination, {from: 'stderr'});86

TweaksSindre Sorhusbefore adoption698a1b8

lines 87–90why, one step back
87 assertLike(await source, {stdout: foobarString, stderr: foobarString});
88 assertLike(await destination, {stdout: `${foobarString}\n${foobarString}`});89 assert.equal(await pipePromise, await destination);90 assert.equal(await secondPipePromise, await destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 91–92why, one step back
91});
92

TweaksSindre Sorhusbefore adoption698a1b8

line 93why, one step back
93test('Can pipe same source to two streams from same subprocess', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 94–98why, one step back
94 const source = execa('noop-fd.js', ['1', foobarString]);
95 const destination = execa('stdin-fd-both.js', ['3'], fullReadableStdio());96 const pipePromise = source.pipe(destination);97 const secondPipePromise = source.pipe(destination, {to: 'fd3'});98

TweaksSindre Sorhusbefore adoption698a1b8

lines 99–102why, one step back
99 assertLike(await source, {stdout: foobarString});
100 assertLike(await destination, {stdout: `${foobarString}${foobarString}`});101 assert.equal(await pipePromise, await destination);102 assert.equal(await secondPipePromise, await destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 103–104why, one step back
103});
104

TweaksSindre Sorhusbefore adoption698a1b8

line 105why, one step back
105test('Can pipe a new source to same destination after some source has already written', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 106–112why, one step back
106 const passThroughStream = new PassThrough();
107 const source = execa('stdin.js', {stdin: ['pipe', passThroughStream]});108 const destination = execa('stdin.js');109 const pipePromise = source.pipe(destination);110111 passThroughStream.write('foo');112 const firstWrite = await once(destination.stdout, 'data');

TweaksSindre Sorhusbefore adoption698a1b8

line 113why, one step back
113 assert.equal(firstWrite.toString(), 'foo');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 114–118why, one step back
114
115 const secondSource = execa('noop.js', ['bar']);116 const secondPipePromise = secondSource.pipe(destination);117 passThroughStream.end();118

TweaksSindre Sorhusbefore adoption698a1b8

lines 119–123why, one step back
119 assertLike(await source, {stdout: 'foo'});
120 assertLike(await secondSource, {stdout: 'bar'});121 assertLike(await destination, {stdout: 'foobar'});122 assert.equal(await pipePromise, await destination);123 assert.equal(await secondPipePromise, await destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 124–125why, one step back
124});
125

TweaksSindre Sorhusbefore adoption698a1b8

line 126why, one step back
126test('Can pipe a second source to same destination after destination has already ended', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 127–130why, one step back
127 const source = execa('noop.js', [foobarString]);
128 const destination = execa('stdin.js');129 const pipePromise = source.pipe(destination);130

TweaksSindre Sorhusbefore adoption698a1b8

lines 131–133why, one step back
131 assertLike(await source, {stdout: foobarString});
132 assertLike(await destination, {stdout: foobarString});133 assert.equal(await pipePromise, await destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 134–137why, one step back
134
135 const secondSource = execa('noop.js', [foobarString]);136 const secondPipePromise = secondSource.pipe(destination);137

TweaksSindre Sorhusbefore adoption698a1b8

lines 138–139why, one step back
138 assertLike(await secondSource, {stdout: ''});
139 assert.equal(await secondPipePromise, await destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 140–141why, one step back
140});
141

TweaksSindre Sorhusbefore adoption698a1b8

line 142why, one step back
142test('Can pipe same source to a second destination after source has already ended', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 143–146why, one step back
143 const source = execa('noop.js', [foobarString]);
144 const destination = execa('stdin.js');145 const pipePromise = source.pipe(destination);146

TweaksSindre Sorhusbefore adoption698a1b8

lines 147–149why, one step back
147 assertLike(await source, {stdout: foobarString});
148 assertLike(await destination, {stdout: foobarString});149 assert.equal(await pipePromise, await destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 150–153why, one step back
150
151 const secondDestination = execa('stdin.js');152 const secondPipePromise = source.pipe(secondDestination);153

TweaksSindre Sorhusbefore adoption698a1b8

lines 154–155why, one step back
154 assertLike(await secondDestination, {stdout: ''});
155 assert.equal(await secondPipePromise, await secondDestination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 156–157why, one step back
156});
157

TweaksSindre Sorhusbefore adoption698a1b8

line 158why, one step back
158test('Can pipe a new source to same destination after some but not all sources have ended', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 159–165why, one step back
159 const source = execa('noop.js', [foobarString]);
160 const passThroughStream = new PassThrough();161 const secondSource = execa('stdin.js', {stdin: ['pipe', passThroughStream]});162 const destination = execa('stdin.js');163 const pipePromise = source.pipe(destination);164 const secondPipePromise = secondSource.pipe(destination);165

TweaksSindre Sorhusbefore adoption698a1b8

line 166why, one step back
166 assertLike(await source, {stdout: foobarString});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 167–171why, one step back
167
168 const thirdSource = execa('noop.js', [foobarString]);169 const thirdPipePromise = thirdSource.pipe(destination);170 passThroughStream.end(`${foobarString}\n`);171

TweaksSindre Sorhusbefore adoption698a1b8

lines 172–177why, one step back
172 assertLike(await secondSource, {stdout: foobarString});
173 assertLike(await thirdSource, {stdout: foobarString});174 assertLike(await destination, {stdout: `${foobarString}\n${foobarString}\n${foobarString}`});175 assert.equal(await pipePromise, await destination);176 assert.equal(await secondPipePromise, await destination);177 assert.equal(await thirdPipePromise, await destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 178–179why, one step back
178});
179

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 181–186why, one step back
181 const source = execa('noop.js', [foobarString]);
182 const destination = execa('stdin.js');183 destination.stdin.end('.');184 await Promise.all([source, destination]);185 const pipePromise = source.pipe(destination);186

TweaksSindre Sorhusbefore adoption698a1b8

lines 187–189why, one step back
187 assertLike(await source, {stdout: foobarString});
188 assertLike(await destination, {stdout: '.'});189 assert.equal(await pipePromise, await destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 190–191why, one step back
190});
191

TweaksSindre Sorhusbefore adoption698a1b8

line 192why, one step back
192test('Can pipe to same destination through multiple paths', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 193–199why, one step back
193 const source = execa('noop.js', [foobarString]);
194 const destination = execa('stdin.js');195 const secondDestination = execa('stdin.js');196 const pipePromise = source.pipe(destination);197 const secondPipePromise = pipePromise.pipe(secondDestination);198 const thirdPipePromise = source.pipe(secondDestination);199

TweaksSindre Sorhusbefore adoption698a1b8

lines 200–205why, one step back
200 assertLike(await source, {stdout: foobarString});
201 assertLike(await destination, {stdout: foobarString});202 assertLike(await secondDestination, {stdout: `${foobarString}\n${foobarString}`});203 assert.equal(await pipePromise, await destination);204 assert.equal(await secondPipePromise, await secondDestination);205 assert.equal(await thirdPipePromise, await secondDestination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 206–207why, one step back
206});
207

TweaksSindre Sorhusbefore adoption698a1b8

line 208why, one step back
208test('Can pipe two sources to same destination in objectMode', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 209–216why, one step back
209 const stdoutTransform = {
210 * transform() {211 yield [foobarString];212 },213 objectMode: true,214 };215 const source = execa('noop.js', [''], {stdout: stdoutTransform});216 const secondSource = execa('noop.js', [''], {stdout: stdoutTransform});

TweaksSindre Sorhusbefore adoption698a1b8

lines 217–218why, one step back
217 assert.equal(source.stdout.readableObjectMode, true);
218 assert.equal(secondSource.stdout.readableObjectMode, true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 219–229why, one step back
219
220 const stdinTransform = {221 * transform([chunk]) {222 yield chunk;223 },224 objectMode: true,225 };226 const destination = execa('stdin.js', {stdin: stdinTransform});227 const pipePromise = source.pipe(destination);228 const secondPipePromise = secondSource.pipe(destination);229

TweaksSindre Sorhusbefore adoption698a1b8

lines 230–234why, one step back
230 assertLike(await source, {stdout: [[foobarString]]});
231 assertLike(await secondSource, {stdout: [[foobarString]]});232 assertLike(await destination, {stdout: `${foobarString}\n${foobarString}`});233 assert.equal(await pipePromise, await destination);234 assert.equal(await secondPipePromise, await destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 235–236why, one step back
235});
236

TweaksSindre Sorhusbefore adoption698a1b8

line 237why, one step back
237test('Can pipe one source to two destinations', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 238–243why, one step back
238 const source = execa('noop.js', [foobarString]);
239 const destination = execa('stdin.js');240 const secondDestination = execa('stdin.js');241 const pipePromise = source.pipe(destination);242 const secondPipePromise = source.pipe(secondDestination);243

TweaksSindre Sorhusbefore adoption698a1b8

lines 244–248why, one step back
244 assertLike(await source, {stdout: foobarString});
245 assertLike(await destination, {stdout: foobarString});246 assertLike(await secondDestination, {stdout: foobarString});247 assert.equal(await pipePromise, await destination);248 assert.equal(await secondPipePromise, await secondDestination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 249–250why, one step back
249});
250

TweaksSindre Sorhusbefore adoption698a1b8

line 251why, one step back
251test('Can pipe one source to three destinations', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 252–259why, one step back
252 const source = execa('noop.js', [foobarString]);
253 const destination = execa('stdin.js');254 const secondDestination = execa('stdin.js');255 const thirdDestination = execa('stdin.js');256 const pipePromise = source.pipe(destination);257 const secondPipePromise = source.pipe(secondDestination);258 const thirdPipePromise = source.pipe(thirdDestination);259

TweaksSindre Sorhusbefore adoption698a1b8

lines 260–266why, one step back
260 assertLike(await source, {stdout: foobarString});
261 assertLike(await destination, {stdout: foobarString});262 assertLike(await secondDestination, {stdout: foobarString});263 assertLike(await thirdDestination, {stdout: foobarString});264 assert.equal(await pipePromise, await destination);265 assert.equal(await secondPipePromise, await secondDestination);266 assert.equal(await thirdPipePromise, await thirdDestination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 267–268why, one step back
267});
268

TweaksSindre Sorhusbefore adoption698a1b8

line 269why, one step back
269test('Can create a series of pipes', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 270–275why, one step back
270 const source = execa('noop.js', [foobarString]);
271 const destination = execa('stdin.js');272 const secondDestination = execa('stdin.js');273 const pipePromise = source.pipe(destination);274 const secondPipePromise = pipePromise.pipe(secondDestination);275

TweaksSindre Sorhusbefore adoption698a1b8

lines 276–280why, one step back
276 assertLike(await source, {stdout: foobarString});
277 assertLike(await destination, {stdout: foobarString});278 assertLike(await secondDestination, {stdout: foobarString});279 assert.equal(await pipePromise, await destination);280 assert.equal(await secondPipePromise, await secondDestination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 281–282why, one step back
281});
282

TweaksSindre Sorhusbefore adoption698a1b8

line 283why, one step back
283test('Returns pipedFrom on success', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 284–288why, one step back
284 const source = execa('noop.js', [foobarString]);
285 const destination = execa('stdin.js');286 const pipePromise = source.pipe(destination);287288 const destinationResult = await destination;

TweaksSindre Sorhusbefore adoption698a1b8

line 289why, one step back
289 assert.deepEqual(destinationResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 290–291why, one step back
290 const sourceResult = await source;
291

TweaksSindre Sorhusbefore adoption698a1b8

lines 292–294why, one step back
292 assertLike(await pipePromise, {pipedFrom: [sourceResult]});
293 assert.deepEqual(destinationResult.pipedFrom, [sourceResult]);294 assert.deepEqual(sourceResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 295–296why, one step back
295});
296

TweaksSindre Sorhusbefore adoption698a1b8

line 297why, one step back
297test('Returns pipedFrom on deep success', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 298–304why, one step back
298 const source = execa('noop.js', [foobarString]);
299 const destination = execa('stdin.js');300 const secondDestination = execa('stdin.js');301 const pipePromise = source.pipe(destination);302 const secondPipePromise = pipePromise.pipe(secondDestination);303304 const destinationResult = await destination;

TweaksSindre Sorhusbefore adoption698a1b8

line 305why, one step back
305 assert.deepEqual(destinationResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 306why, one step back
306 const secondDestinationResult = await secondDestination;

TweaksSindre Sorhusbefore adoption698a1b8

line 307why, one step back
307 assert.deepEqual(secondDestinationResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 308–309why, one step back
308 const sourceResult = await source;
309

TweaksSindre Sorhusbefore adoption698a1b8

lines 310–314why, one step back
310 assertLike(await secondPipePromise, {pipedFrom: [destinationResult]});
311 assert.deepEqual(secondDestinationResult.pipedFrom, [destinationResult]);312 assertLike(await pipePromise, {pipedFrom: [sourceResult]});313 assert.deepEqual(destinationResult.pipedFrom, [sourceResult]);314 assert.deepEqual(sourceResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 315–316why, one step back
315});
316

TweaksSindre Sorhusbefore adoption698a1b8

line 317why, one step back
317test('Returns pipedFrom on source failure', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 318–322why, one step back
318 const source = execa('noop-fail.js', ['1', foobarString]);
319 const destination = execa('stdin.js');320 const pipePromise = source.pipe(destination);321322 const destinationResult = await destination;

TweaksSindre Sorhusbefore adoption698a1b8

lines 323–324why, one step back
323 assert.deepEqual(destinationResult.pipedFrom, []);
324 const sourceResult = await assertRejects(source);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 325why, one step back
325

TweaksSindre Sorhusbefore adoption698a1b8

lines 326–328why, one step back
326 assertLike(await assertRejects(pipePromise), {pipedFrom: []});
327 assert.deepEqual(destinationResult.pipedFrom, [sourceResult]);328 assert.deepEqual(sourceResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 329–330why, one step back
329});
330

TweaksSindre Sorhusbefore adoption698a1b8

line 331why, one step back
331test('Returns pipedFrom on destination failure', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 332–335why, one step back
332 const source = execa('noop.js', [foobarString]);
333 const destination = execa('stdin-fail.js');334 const pipePromise = source.pipe(destination);335

TweaksSindre Sorhusbefore adoption698a1b8

line 336why, one step back
336 const destinationResult = await assertRejects(destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 337–338why, one step back
337 const sourceResult = await source;
338

TweaksSindre Sorhusbefore adoption698a1b8

lines 339–341why, one step back
339 assertLike(await assertRejects(pipePromise), {pipedFrom: [sourceResult]});
340 assert.deepEqual(destinationResult.pipedFrom, [sourceResult]);341 assert.deepEqual(sourceResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 342–343why, one step back
342});
343

TweaksSindre Sorhusbefore adoption698a1b8

line 344why, one step back
344test('Returns pipedFrom on source + destination failure', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 345–348why, one step back
345 const source = execa('noop-fail.js', ['1', foobarString]);
346 const destination = execa('stdin-fail.js');347 const pipePromise = source.pipe(destination);348

TweaksSindre Sorhusbefore adoption698a1b8

lines 349–350why, one step back
349 const destinationResult = await assertRejects(destination);
350 const sourceResult = await assertRejects(source);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 351why, one step back
351

TweaksSindre Sorhusbefore adoption698a1b8

lines 352–354why, one step back
352 assertLike(await assertRejects(pipePromise), {pipedFrom: [sourceResult]});
353 assert.deepEqual(destinationResult.pipedFrom, [sourceResult]);354 assert.deepEqual(sourceResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 355–356why, one step back
355});
356

TweaksSindre Sorhusbefore adoption698a1b8

line 357why, one step back
357test('Returns pipedFrom on deep failure', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 358–363why, one step back
358 const source = execa('noop-fail.js', ['1', foobarString]);
359 const destination = execa('stdin-fail.js');360 const secondDestination = execa('stdin.js');361 const pipePromise = source.pipe(destination);362 const secondPipePromise = pipePromise.pipe(secondDestination);363

TweaksSindre Sorhusbefore adoption698a1b8

line 364why, one step back
364 const destinationResult = await assertRejects(destination);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 365why, one step back
365 const secondDestinationResult = await secondDestination;

TweaksSindre Sorhusbefore adoption698a1b8

lines 366–367why, one step back
366 assert.deepEqual(secondDestinationResult.pipedFrom, []);
367 const sourceResult = await assertRejects(source);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 368why, one step back
368

TweaksSindre Sorhusbefore adoption698a1b8

lines 369–373why, one step back
369 assertLike(await assertRejects(secondPipePromise), {pipedFrom: [sourceResult]});
370 assert.deepEqual(secondDestinationResult.pipedFrom, [destinationResult]);371 assertLike(await assertRejects(pipePromise), {pipedFrom: [sourceResult]});372 assert.deepEqual(destinationResult.pipedFrom, [sourceResult]);373 assert.deepEqual(sourceResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 374–375why, one step back
374});
375

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

lines 376–403why, one step back
376// The second destination is piped from the first destination, not from the source which failed
377test('Returns pipedFrom on deep source failure', async () => {378 const source = execa('noop-fail.js', ['1', foobarString]);379 const destination = execa('stdin.js');380 const secondDestination = execa('stdin.js');381 const pipePromise = source.pipe(destination);382 const secondPipePromise = pipePromise.pipe(secondDestination);383384 const destinationResult = await destination;385 const secondDestinationResult = await secondDestination;386 const sourceResult = await assertRejects(source);387388 assert.equal(await assertRejects(secondPipePromise), sourceResult);389 assert.deepEqual(secondDestinationResult.pipedFrom, [destinationResult]);390 assert.equal(await assertRejects(pipePromise), sourceResult);391 assert.deepEqual(destinationResult.pipedFrom, [sourceResult]);392 assert.deepEqual(sourceResult.pipedFrom, []);393});394395test('Returns pipedFrom on deep success, with a template string', async () => {396 const pipePromise = execa('noop.js', [foobarString]).pipe`stdin.js`.pipe`stdin.js`;397 const {stdout, pipedFrom} = await pipePromise;398 assert.equal(stdout, foobarString);399 const [destinationResult] = pipedFrom;400 assert.equal(destinationResult.command, 'stdin.js');401 assert.equal(destinationResult.pipedFrom[0].command, `noop.js ${foobarString}`);402});403

TweaksSindre Sorhusbefore adoption698a1b8

line 404why, one step back
404test('Returns pipedFrom from multiple sources', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 405–411why, one step back
405 const source = execa('noop.js', [foobarString]);
406 const secondSource = execa('noop.js', [foobarString]);407 const destination = execa('stdin.js');408 const pipePromise = source.pipe(destination);409 const secondPipePromise = secondSource.pipe(destination);410411 const destinationResult = await destination;

TweaksSindre Sorhusbefore adoption698a1b8

line 412why, one step back
412 assert.deepEqual(destinationResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 413–415why, one step back
413 const sourceResult = await source;
414 const secondSourceResult = await secondSource;415

TweaksSindre Sorhusbefore adoption698a1b8

lines 416–420why, one step back
416 assertLike(await pipePromise, {pipedFrom: [sourceResult, secondSourceResult]});
417 assertLike(await secondPipePromise, {pipedFrom: [sourceResult, secondSourceResult]});418 assert.deepEqual(destinationResult.pipedFrom, [sourceResult, secondSourceResult]);419 assert.deepEqual(sourceResult.pipedFrom, []);420 assert.deepEqual(secondSourceResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 421–422why, one step back
421});
422

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 424–430why, one step back
424 const source = execa('noop.js', [foobarString]);
425 const destination = execa('stdin.js');426 destination.stdin.end('.');427 await Promise.all([source, destination]);428 const pipePromise = source.pipe(destination);429430 const destinationResult = await destination;

TweaksSindre Sorhusbefore adoption698a1b8

line 431why, one step back
431 assert.deepEqual(destinationResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 432why, one step back
432 const sourceResult = await source;

TweaksSindre Sorhusbefore adoption698a1b8

line 433why, one step back
433 assert.deepEqual(sourceResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 434why, one step back
434

TweaksSindre Sorhusbefore adoption698a1b8

lines 435–437why, one step back
435 assertLike(await pipePromise, {pipedFrom: [sourceResult]});
436 assert.deepEqual(destinationResult.pipedFrom, [sourceResult]);437 assert.deepEqual(sourceResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 438–439why, one step back
438});
439

TweaksSindre Sorhusbefore adoption698a1b8

line 440why, one step back
440test('Does not return nor set pipedFrom on signal abort', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 441–446why, one step back
441 const abortController = new AbortController();
442 const source = execa('empty.js');443 const destination = execa('empty.js');444 const pipePromise = source.pipe(destination, {unpipeSignal: abortController.signal});445446 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

line 447why, one step back
447 assertLike(await assertRejects(pipePromise), {pipedFrom: []});

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 448why, one step back
448 const destinationResult = await destination;

TweaksSindre Sorhusbefore adoption698a1b8

line 449why, one step back
449 assert.deepEqual(destinationResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 450why, one step back
450 const sourceResult = await source;

TweaksSindre Sorhusbefore adoption698a1b8

line 451why, one step back
451 assert.deepEqual(sourceResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 452–453why, one step back
452});
453

TweaksSindre Sorhusbefore adoption698a1b8

line 454why, one step back
454test('Can pipe same source to same destination twice', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 455–460why, one step back
455 const source = execa('noop.js', [foobarString]);
456 const destination = execa('stdin.js');457 const pipePromise = source.pipe(destination);458 const secondPipePromise = source.pipe(destination);459460 const destinationResult = await destination;

TweaksSindre Sorhusbefore adoption698a1b8

line 461why, one step back
461 assertLike(destinationResult, {pipedFrom: []});

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 462why, one step back
462 const sourceResult = await source;

TweaksSindre Sorhusbefore adoption698a1b8

line 463why, one step back
463 assertLike(sourceResult, {pipedFrom: []});

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 464why, one step back
464

TweaksSindre Sorhusbefore adoption698a1b8

lines 465–470why, one step back
465 assertLike(await source, {stdout: foobarString});
466 assertLike(await destination, {stdout: foobarString});467 assert.equal(await pipePromise, destinationResult);468 assert.equal(await secondPipePromise, destinationResult);469 assert.deepEqual(destinationResult.pipedFrom, [sourceResult]);470 assert.deepEqual(sourceResult.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 471why, one step back
471});

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

lines 472–473why, one step back
472
473// `to: 'fd3'` and higher requires the `stdio` option, which cannot be combined with the `stdin` option that piping sets by default

TweaksSindre Sorhusbefore adoption698a1b8

line 474why, one step back
474test('Can pipe to an additional file descriptor of a destination with the "stdio" option', async () => {

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

lines 475–479why, one step back
475 const source = execa('noop.js', [foobarString]);
476 const {stdout} = await source.pipe('stdin-fd.js', ['3'], {477 to: 'fd3',478 stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe', input: true}],479 });

TweaksSindre Sorhusbefore adoption698a1b8

line 480why, one step back
480 assert.equal(stdout, foobarString);

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

lines 481–537why, one step back
481});
482483// `noop-repeat.js` writes every 10ms. `noop.js` exits right away, since it does not read its `stdin`.484const testNoStarvation = async ({pipeOptions, sourceOptions, getExitingDestination = () => execa('noop.js')}) => {485 const source = execa('noop-repeat.js', ['1', `${foobarString}\n`], sourceOptions);486 const exitingDestination = getExitingDestination();487 const receivingDestination = execa('stdin.js');488 source.pipe(exitingDestination, pipeOptions).catch(() => {});489 source.pipe(receivingDestination, pipeOptions).catch(() => {});490491 await exitingDestination;492 await setTimeout(100);493 source.kill();494 // The source is not aborted with `EPIPE` when the first destination exits495 const {isTerminated} = await assertRejects(source);496 assert.ok(isTerminated);497 const {stdout} = await receivingDestination;498 assert.ok(stdout.split('\n').length > 2);499};500501test('One destination exiting does not starve the other destinations', () => testNoStarvation({}));502test('One destination exiting does not starve the other destinations, from "all"', () => testNoStarvation({sourceOptions: {all: true}, pipeOptions: {from: 'all'}}));503// `stdin.js` exits when its `stdin` ends, while `noop-repeat.js` keeps running504test('One destination ending its stdin does not starve the other destinations', () => testNoStarvation({505 getExitingDestination() {506 const destination = execa('stdin.js');507 destination.stdin.end();508 return destination;509 },510}));511512test('The source is aborted when all its destinations exit', async () => {513 const source = execa('noop-repeat.js', ['1', `${foobarString}\n`]);514 source.pipe(execa('noop.js')).catch(() => {});515 source.pipe(execa('noop.js')).catch(() => {});516 const {isTerminated, exitCode} = await assertRejects(source);517 assert.ok(!isTerminated);518 assert.equal(exitCode, 1);519});520521test('The other sources of an exiting destination are aborted, unless they have other destinations', async () => {522 const source = execa('noop-repeat.js', ['1', `${foobarString}\n`]);523 const secondSource = execa('noop-repeat.js', ['1', `${foobarString}\n`]);524 const exitingDestination = execa('noop.js');525 const receivingDestination = execa('stdin.js');526 source.pipe(exitingDestination).catch(() => {});527 secondSource.pipe(exitingDestination).catch(() => {});528 source.pipe(receivingDestination).catch(() => {});529530 const {exitCode} = await assertRejects(secondSource);531 assert.equal(exitCode, 1);532 await setTimeout(200);533 source.kill();534 const {isTerminated} = await assertRejects(source);535 assert.ok(isTerminated);536 const {stdout} = await receivingDestination;537 assert.ok(stdout.split('\n').length > 2);

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

line 538why, one step back
538});