Skip to content
Myr

readable.js

This file

Path

test/convert/readable.js

503 lines · 18.5 KiB · JavaScript

Last changed by

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

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';

TweaksSindre Sorhusbefore adoption698a1b8

lines 3–6why, one step back
3import {pipeline} from 'node:stream/promises';
4import {text} from 'node:stream/consumers';5import {setTimeout} from 'node:timers/promises';6import test from 'node:test';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 7–12why, one step back
7import {
8 compose,9 Readable,10 Writable,11 PassThrough,12} from 'node:stream';

TweaksSindre Sorhusbefore adoption698a1b8

line 13why, one step back
13import {assertRejects, assertLike} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 14–32why, one step back
14import {execa} from '../../index.js';
15import {setFixtureDirectory} from '../helpers/fixtures-directory.js';16import {17 finishedStream,18 assertReadableAborted,19 assertWritableAborted,20 assertProcessNormalExit,21 assertStreamOutput,22 assertStreamChunks,23 assertStreamError,24 assertStreamReadError,25 assertSubprocessOutput,26 assertSubprocessError,27 assertPromiseError,28 getReadableSubprocess,29 getReadWriteSubprocess,30} from '../helpers/convert.js';31import {foobarString, foobarBuffer, foobarObject} from '../helpers/input.js';32import {simpleFull} from '../helpers/lines.js';

Runs CI tests on Node 23 (#1169)ehmickybefore adoptione330bd8

line 33why, one step back
33import {majorNodeVersion} from '../helpers/node-version.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 34–39why, one step back
34import {prematureClose, fullStdio} from '../helpers/stdio.js';
35import {outputObjectGenerator, getOutputsAsyncGenerator} from '../helpers/generator.js';36import {defaultHighWaterMark, defaultObjectHighWaterMark} from '../helpers/stream.js';3738setFixtureDirectory();39

TweaksSindre Sorhusbefore adoption698a1b8

line 40why, one step back
40test('.readable() success', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 41–43why, one step back
41 const subprocess = getReadableSubprocess();
42 const stream = subprocess.readable();43

TweaksSindre Sorhusbefore adoption698a1b8

lines 44–47why, one step back
44 assert.ok(!(stream instanceof Writable));
45 assert.equal(stream.writable, undefined);46 assert.ok(stream instanceof Readable);47 assert.equal(stream.readable, true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 48why, one step back
48

TweaksSindre Sorhusbefore adoption698a1b8

lines 49–50why, one step back
49 await assertStreamOutput(stream);
50 await assertSubprocessOutput(subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 51–52why, one step back
51});
52

TweaksSindre Sorhusbefore adoption698a1b8

line 53why, one step back
53const testReadableDefault = async (fdNumber, from, options, hasResult) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 54–56why, one step back
54 const subprocess = execa('noop-fd.js', [`${fdNumber}`, foobarString], options);
55 const stream = subprocess.readable({from});56

TweaksSindre Sorhusbefore adoption698a1b8

lines 57–58why, one step back
57 await assertStreamOutput(stream, hasResult ? foobarString : '');
58 await assertSubprocessOutput(subprocess, foobarString, fdNumber);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 59–60why, one step back
59};
60

TweaksSindre Sorhusbefore adoption698a1b8

lines 61–70why, one step back
61test('.readable() can use stdout', () => testReadableDefault(1, 'stdout', {}, true));
62test('.readable() can use stderr', () => testReadableDefault(2, 'stderr', {}, true));63test('.readable() can use stdio[*]', () => testReadableDefault(3, 'fd3', fullStdio, true));64test('.readable() uses stdout by default', () => testReadableDefault(1, undefined, {}, true));65test('.readable() does not use stderr by default', () => testReadableDefault(2, undefined, {}, false));66test('.readable() does not use stdio[*] by default', () => testReadableDefault(3, undefined, fullStdio, false));67test('.readable() uses stdout even if stderr is "ignore"', () => testReadableDefault(1, 'stdout', {stderr: 'ignore'}, true));68test('.readable() uses stderr even if stdout is "ignore"', () => testReadableDefault(2, 'stderr', {stdout: 'ignore'}, true));69test('.readable() uses stdout if "all" is used', () => testReadableDefault(1, 'all', {all: true}, true));70test('.readable() uses stderr if "all" is used', () => testReadableDefault(2, 'all', {all: true}, true));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 71why, one step back
71

TweaksSindre Sorhusbefore adoption698a1b8

line 72why, one step back
72const testBuffering = async methodName => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 73–79why, one step back
73 const subprocess = execa('noop-stdin-fd.js', ['1'], {buffer: false});
74 const stream = subprocess[methodName]();7576 subprocess.stdin.write(foobarString);77 await once(subprocess.stdout, 'readable');78 subprocess.stdin.end();79

TweaksSindre Sorhusbefore adoption698a1b8

line 80why, one step back
80 await assertStreamOutput(stream);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 81–82why, one step back
81};
82

TweaksSindre Sorhusbefore adoption698a1b8

lines 83–84why, one step back
83test('.readable() buffers until read', () => testBuffering('readable'));
84test('.duplex() buffers until read', () => testBuffering('duplex'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 85why, one step back
85

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

lines 86–117why, one step back
86// Breaking out of `for await` or cancelling a web stream destroys the stream with an `AbortError`, which aborts it like `stream.destroy()`
87const testStopIteration = async stopIteration => {88 const subprocess = execa('noop-delay.js', ['1', foobarString]);89 await stopIteration(subprocess);90 const {exitCode} = await subprocess;91 assert.equal(exitCode, 0);92};9394const stopIterator = async asyncIterable => {95 const iterator = asyncIterable[Symbol.asyncIterator]();96 await iterator.next();97 await iterator.return();98};99100const cancelReader = async readableStream => {101 const reader = readableStream.getReader();102 await reader.read();103 await reader.cancel();104};105106test('.readable() iteration can be stopped', () => testStopIteration(subprocess => stopIterator(subprocess.readable())));107// This destroys `subprocess.stdout` with an `AbortError`, which cannot be told apart from an `AbortError` thrown by a transform. So this is not supported, and `subprocess.readable()` or `subprocess.iterable()` must be used instead.108test('subprocess.stdout iteration cannot be stopped', async () => {109 const subprocess = execa('noop-delay.js', ['1', foobarString]);110 await stopIterator(subprocess.stdout);111 const {code} = await assertRejects(subprocess);112 assert.equal(code, 'ABORT_ERR');113});114test('.readableStream() can be cancelled', () => testStopIteration(subprocess => cancelReader(subprocess.readableStream())));115test('.readableStream() iteration can be stopped', () => testStopIteration(subprocess => stopIterator(subprocess.readableStream())));116test('.iterable() iteration can be stopped', () => testStopIteration(subprocess => stopIterator(subprocess.iterable())));117

TweaksSindre Sorhusbefore adoption698a1b8

line 118why, one step back
118test('.readable() abort -> subprocess fail', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 119–123why, one step back
119 const subprocess = execa('noop-repeat.js');
120 const stream = subprocess.readable();121122 stream.destroy();123

TweaksSindre Sorhusbefore adoption698a1b8

lines 124–130why, one step back
124 const error = await assertRejects(text(stream));
125 assertProcessNormalExit(error, 1);126 assert.ok(error.message.includes('EPIPE'));127 assertWritableAborted(subprocess.stdin);128 assertReadableAborted(subprocess.stdout);129 assert.equal(subprocess.stderr.readableEnded, true);130 await assertSubprocessError(subprocess, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 131–132why, one step back
131});
132

TweaksSindre Sorhusbefore adoption698a1b8

line 133why, one step back
133test('.readable() error -> subprocess fail', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 134–139why, one step back
134 const subprocess = execa('noop-repeat.js');
135 const stream = subprocess.readable();136137 const cause = new Error(foobarString);138 stream.destroy(cause);139

TweaksSindre Sorhusbefore adoption698a1b8

lines 140–146why, one step back
140 const error = await assertStreamReadError(stream, {cause});
141 assertProcessNormalExit(error, 1);142 assert.ok(error.message.includes('EPIPE'));143 assertWritableAborted(subprocess.stdin);144 assert.equal(subprocess.stdout.errored, cause);145 assert.equal(subprocess.stderr.readableEnded, true);146 await assertSubprocessError(subprocess, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 147–148why, one step back
147});
148

TweaksSindre Sorhusbefore adoption698a1b8

line 149why, one step back
149const testStdoutAbort = async methodName => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 150why, one step back
150 const subprocess = execa('ipc-echo.js', {ipc: true});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 151–154why, one step back
151 const stream = subprocess[methodName]();
152153 subprocess.stdout.destroy();154

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 155why, one step back
155 await subprocess.sendMessage(foobarString);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 156why, one step back
156 const [error, message] = await Promise.all([

TweaksSindre Sorhusbefore adoption698a1b8

line 157why, one step back
157 assertRejects(finishedStream(stream)),

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 158why, one step back
158 subprocess.getOneMessage(),

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 159why, one step back
159 ]);

TweaksSindre Sorhusbefore adoption698a1b8

lines 160–165why, one step back
160 assertLike(error, prematureClose);
161 assert.equal(message, foobarString);162 assertWritableAborted(subprocess.stdin);163 assertReadableAborted(subprocess.stdout);164 assert.equal(subprocess.stderr.readableEnded, true);165 await assertSubprocessOutput(subprocess, '');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 166–167why, one step back
166};
167

TweaksSindre Sorhusbefore adoption698a1b8

lines 168–169why, one step back
168test('subprocess.stdout abort + no more writes -> .readable() error + subprocess success', () => testStdoutAbort('readable'));
169test('subprocess.stdout abort + no more writes -> .duplex() error + subprocess success', () => testStdoutAbort('duplex'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 170why, one step back
170

TweaksSindre Sorhusbefore adoption698a1b8

line 171why, one step back
171const testStdoutError = async methodName => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 172why, one step back
172 const subprocess = execa('ipc-echo.js', {ipc: true});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 173–177why, one step back
173 const stream = subprocess[methodName]();
174175 const cause = new Error(foobarString);176 subprocess.stdout.destroy(cause);177

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 178why, one step back
178 await subprocess.sendMessage(foobarString);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 179why, one step back
179 const [error, message] = await Promise.all([

TweaksSindre Sorhusbefore adoption698a1b8

line 180why, one step back
180 assertRejects(finishedStream(stream)),

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 181why, one step back
181 subprocess.getOneMessage(),

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 182why, one step back
182 ]);

TweaksSindre Sorhusbefore adoption698a1b8

lines 183–188why, one step back
183 assert.equal(message, foobarString);
184 assert.equal(error.cause, cause);185 assertProcessNormalExit(error);186 assert.equal(subprocess.stdout.errored, cause);187 assert.equal(subprocess.stderr.readableEnded, true);188 assertWritableAborted(subprocess.stdin);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 189why, one step back
189

TweaksSindre Sorhusbefore adoption698a1b8

line 190why, one step back
190 await assertSubprocessError(subprocess, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 191–192why, one step back
191};
192

TweaksSindre Sorhusbefore adoption698a1b8

lines 193–194why, one step back
193test('subprocess.stdout error + no more writes -> .readable() error + subprocess fail', () => testStdoutError('readable'));
194test('subprocess.stdout error + no more writes -> .duplex() error + subprocess fail', () => testStdoutError('duplex'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 195why, one step back
195

TweaksSindre Sorhusbefore adoption698a1b8

line 196why, one step back
196const testStdinAbortWrites = async methodName => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 197–202why, one step back
197 const subprocess = getReadWriteSubprocess();
198 const stream = subprocess[methodName]();199200 subprocess.stdout.destroy();201 subprocess.stdin.end(foobarString);202

TweaksSindre Sorhusbefore adoption698a1b8

lines 203–208why, one step back
203 const error = await assertRejects(finishedStream(stream));
204 assertProcessNormalExit(error, 1);205 assert.equal(subprocess.stdin.writableEnded, true);206 assertReadableAborted(subprocess.stdout);207 assert.equal(subprocess.stderr.readableEnded, true);208 await assertSubprocessError(subprocess, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 209–210why, one step back
209};
210

TweaksSindre Sorhusbefore adoption698a1b8

lines 211–212why, one step back
211test('subprocess.stdout abort + more writes -> .readable() error + subprocess fail', () => testStdinAbortWrites('readable'));
212test('subprocess.stdout abort + more writes -> .duplex() error + subprocess fail', () => testStdinAbortWrites('duplex'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 213why, one step back
213

TweaksSindre Sorhusbefore adoption698a1b8

line 214why, one step back
214const testStdinErrorWrites = async methodName => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 215–221why, one step back
215 const subprocess = getReadWriteSubprocess();
216 const stream = subprocess[methodName]();217218 const cause = new Error(foobarString);219 subprocess.stdout.destroy(cause);220 subprocess.stdin.end(foobarString);221

TweaksSindre Sorhusbefore adoption698a1b8

lines 222–227why, one step back
222 const error = await assertStreamError(stream, {cause});
223 assertProcessNormalExit(error, 1);224 assert.equal(subprocess.stdin.writableEnded, true);225 assert.equal(subprocess.stdout.errored, cause);226 assert.equal(subprocess.stderr.readableEnded, true);227 await assertSubprocessError(subprocess, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 228–229why, one step back
228};
229

TweaksSindre Sorhusbefore adoption698a1b8

lines 230–231why, one step back
230test('subprocess.stdout error + more writes -> .readable() error + subprocess fail', () => testStdinErrorWrites('readable'));
231test('subprocess.stdout error + more writes -> .duplex() error + subprocess fail', () => testStdinErrorWrites('duplex'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 232why, one step back
232

TweaksSindre Sorhusbefore adoption698a1b8

line 233why, one step back
233test('.readable() can be used with Stream.pipeline()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 234–240why, one step back
234 const subprocess = getReadableSubprocess();
235 const stream = subprocess.readable();236 const outputStream = new PassThrough();237238 await pipeline(stream, outputStream);239240 await finishedStream(stream);

TweaksSindre Sorhusbefore adoption698a1b8

lines 241–242why, one step back
241 await assertStreamOutput(outputStream);
242 await assertSubprocessOutput(subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 243–244why, one step back
243});
244

TweaksSindre Sorhusbefore adoption698a1b8

line 245why, one step back
245test('.readable() can error with Stream.pipeline()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 246–249why, one step back
246 const subprocess = execa('noop-fail.js', ['1', foobarString]);
247 const stream = subprocess.readable();248 const outputStream = new PassThrough();249

TweaksSindre Sorhusbefore adoption698a1b8

lines 250–252why, one step back
250 const error = await assertRejects(pipeline(stream, outputStream));
251 assertProcessNormalExit(error, 2);252 assertLike(error, {stdout: foobarString});

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 253why, one step back
253

TweaksSindre Sorhusbefore adoption698a1b8

lines 254–256why, one step back
254 await assertStreamError(stream, error);
255 await assertStreamReadError(outputStream, error);256 await assertSubprocessError(subprocess, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 257–258why, one step back
257});
258

TweaksSindre Sorhusbefore adoption698a1b8

line 259why, one step back
259test('.readable() can pipe to errored stream with Stream.pipeline()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 260–266why, one step back
260 const subprocess = getReadableSubprocess();
261 const stream = subprocess.readable();262 const outputStream = new PassThrough();263264 const cause = new Error('test');265 outputStream.destroy(cause);266

Runs CI tests on Node 23 (#1169)ehmickybefore adoptione330bd8

lines 267–269why, one step back
267 // Node 23 does not allow calling `stream.pipeline()` with an already errored stream
268 if (majorNodeVersion >= 23) {269 outputStream.on('error', () => {});

TweaksSindre Sorhusbefore adoption698a1b8

line 270why, one step back
270 await assertRejects(pipeline(stream, outputStream), {code: 'ERR_STREAM_UNABLE_TO_PIPE'});

Runs CI tests on Node 23 (#1169)ehmickybefore adoptione330bd8

line 271why, one step back
271 } else {

TweaksSindre Sorhusbefore adoption698a1b8

lines 272–273why, one step back
272 await assertPromiseError(pipeline(stream, outputStream), cause);
273 await assertRejects(finishedStream(stream));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 274why, one step back
274

TweaksSindre Sorhusbefore adoption698a1b8

lines 275–277why, one step back
275 const error = await assertStreamError(stream, cause);
276 await assertStreamReadError(outputStream, cause);277 await assertSubprocessError(subprocess, {cause: error});

Runs CI tests on Node 23 (#1169)ehmickybefore adoptione330bd8

line 278why, one step back
278 }

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 279–280why, one step back
279});
280

TweaksSindre Sorhusbefore adoption698a1b8

line 281why, one step back
281test('.readable() can be used with Stream.compose()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 282–285why, one step back
282 const subprocess = getReadableSubprocess();
283 const stream = subprocess.readable();284 const outputStream = new PassThrough();285

TweaksSindre Sorhusbefore adoption698a1b8

lines 286–287why, one step back
286 await assertStreamOutput(compose(stream, outputStream));
287 await assertSubprocessOutput(subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 288–289why, one step back
288});
289

TweaksSindre Sorhusbefore adoption698a1b8

line 290why, one step back
290test('.readable() works with objectMode', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 291–292why, one step back
291 const subprocess = execa('noop.js', {stdout: outputObjectGenerator()});
292 const stream = subprocess.readable();

TweaksSindre Sorhusbefore adoption698a1b8

lines 293–294why, one step back
293 assert.equal(stream.readableObjectMode, true);
294 assert.equal(stream.readableHighWaterMark, defaultObjectHighWaterMark);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 295why, one step back
295

TweaksSindre Sorhusbefore adoption698a1b8

lines 296–297why, one step back
296 await assertStreamChunks(stream, [foobarObject]);
297 await assertSubprocessOutput(subprocess, [foobarObject]);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 298–299why, one step back
298});
299

TweaksSindre Sorhusbefore adoption698a1b8

line 300why, one step back
300test('.duplex() works with objectMode and reads', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 301–302why, one step back
301 const subprocess = getReadWriteSubprocess({stdout: outputObjectGenerator()});
302 const stream = subprocess.duplex();

TweaksSindre Sorhusbefore adoption698a1b8

lines 303–306why, one step back
303 assert.equal(stream.readableObjectMode, true);
304 assert.equal(stream.readableHighWaterMark, defaultObjectHighWaterMark);305 assert.equal(stream.writableObjectMode, false);306 assert.equal(stream.writableHighWaterMark, defaultHighWaterMark);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 307–308why, one step back
307 stream.end(foobarString);
308

TweaksSindre Sorhusbefore adoption698a1b8

lines 309–310why, one step back
309 await assertStreamChunks(stream, [foobarObject]);
310 await assertSubprocessOutput(subprocess, [foobarObject]);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 311–312why, one step back
311});
312

TweaksSindre Sorhusbefore adoption698a1b8

line 313why, one step back
313test('.readable() works with default encoding', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 314–315why, one step back
314 const subprocess = getReadableSubprocess();
315 const stream = subprocess.readable();

TweaksSindre Sorhusbefore adoption698a1b8

line 316why, one step back
316 assert.equal(stream.readableEncoding, null);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 317why, one step back
317

TweaksSindre Sorhusbefore adoption698a1b8

lines 318–319why, one step back
318 await assertStreamChunks(stream, [foobarBuffer]);
319 await assertSubprocessOutput(subprocess, foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 320–321why, one step back
320});
321

TweaksSindre Sorhusbefore adoption698a1b8

line 322why, one step back
322test('.duplex() works with default encoding', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 323–324why, one step back
323 const subprocess = getReadWriteSubprocess();
324 const stream = subprocess.duplex();

TweaksSindre Sorhusbefore adoption698a1b8

line 325why, one step back
325 assert.equal(stream.readableEncoding, null);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 326–327why, one step back
326 stream.end(foobarString);
327

TweaksSindre Sorhusbefore adoption698a1b8

lines 328–329why, one step back
328 await assertStreamChunks(stream, [foobarBuffer]);
329 await assertSubprocessOutput(subprocess, foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

line 332why, one step back
332test('.readable() works with encoding "utf8"', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 333–335why, one step back
333 const subprocess = getReadableSubprocess();
334 subprocess.stdout.setEncoding('utf8');335 const stream = subprocess.readable();

TweaksSindre Sorhusbefore adoption698a1b8

line 336why, one step back
336 assert.equal(stream.readableEncoding, 'utf8');

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 337why, one step back
337

TweaksSindre Sorhusbefore adoption698a1b8

lines 338–339why, one step back
338 await assertStreamChunks(stream, [foobarString]);
339 await assertSubprocessOutput(subprocess, foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 340–341why, one step back
340});
341

TweaksSindre Sorhusbefore adoption698a1b8

line 342why, one step back
342test('.duplex() works with encoding "utf8"', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 343–345why, one step back
343 const subprocess = getReadWriteSubprocess();
344 subprocess.stdout.setEncoding('utf8');345 const stream = subprocess.duplex();

TweaksSindre Sorhusbefore adoption698a1b8

line 346why, one step back
346 assert.equal(stream.readableEncoding, 'utf8');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 347–348why, one step back
347 stream.end(foobarBuffer);
348

TweaksSindre Sorhusbefore adoption698a1b8

lines 349–350why, one step back
349 await assertStreamChunks(stream, [foobarString]);
350 await assertSubprocessOutput(subprocess, foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 351–352why, one step back
351});
352

TweaksSindre Sorhusbefore adoption698a1b8

line 353why, one step back
353test('.readable() has the right highWaterMark', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 354–355why, one step back
354 const subprocess = execa('noop.js');
355 const stream = subprocess.readable();

TweaksSindre Sorhusbefore adoption698a1b8

line 356why, one step back
356 assert.equal(stream.readableHighWaterMark, defaultHighWaterMark);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 357–359why, one step back
357 await text(stream);
358});359

TweaksSindre Sorhusbefore adoption698a1b8

line 360why, one step back
360test('.readable() can iterate over lines', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 361why, one step back
361 const subprocess = execa('noop-fd.js', ['1', simpleFull]);

Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d

line 362why, one step back
362 const lines = await Array.fromAsync(subprocess.readable({binary: false, preserveNewlines: false}));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 363–364why, one step back
363
364 const expectedLines = ['aaa', 'bbb', 'ccc'];

TweaksSindre Sorhusbefore adoption698a1b8

lines 365–366why, one step back
365 assert.deepEqual(lines, expectedLines);
366 await assertSubprocessOutput(subprocess, simpleFull);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 367–368why, one step back
367});
368

TweaksSindre Sorhusbefore adoption698a1b8

line 369why, one step back
369test('.readable() can wait for data', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 370–372why, one step back
370 const subprocess = execa('noop.js', {stdout: getOutputsAsyncGenerator([foobarString, foobarString])(false, true)});
371 const stream = subprocess.readable();372

TweaksSindre Sorhusbefore adoption698a1b8

line 373why, one step back
373 assert.equal(stream.read(), null);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 374why, one step back
374 await once(stream, 'readable');

TweaksSindre Sorhusbefore adoption698a1b8

lines 375–376why, one step back
375 assert.equal(stream.read().toString(), foobarString);
376 assert.equal(stream.read(), null);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 377why, one step back
377 await once(stream, 'readable');

TweaksSindre Sorhusbefore adoption698a1b8

lines 378–379why, one step back
378 assert.equal(stream.read().toString(), foobarString);
379 assert.equal(stream.read(), null);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 380why, one step back
380 await once(stream, 'readable');

TweaksSindre Sorhusbefore adoption698a1b8

line 381why, one step back
381 assert.equal(stream.read(), null);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 382–383why, one step back
382
383 await finishedStream(stream);

TweaksSindre Sorhusbefore adoption698a1b8

line 384why, one step back
384 await assertSubprocessOutput(subprocess, `${foobarString}${foobarString}`);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 385–386why, one step back
385});
386

TweaksSindre Sorhusbefore adoption698a1b8

line 387why, one step back
387const testBufferData = async methodName => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 388–392why, one step back
388 const chunk = '.'.repeat(defaultHighWaterMark).repeat(2);
389 const subprocess = getReadWriteSubprocess();390 const stream = subprocess[methodName]();391 subprocess.stdin.end(chunk);392

TweaksSindre Sorhusbefore adoption698a1b8

lines 393–394why, one step back
393 await assertStreamOutput(stream, chunk);
394 await assertSubprocessOutput(subprocess, chunk);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 395–396why, one step back
395};
396

TweaksSindre Sorhusbefore adoption698a1b8

lines 397–398why, one step back
397test('.readable() can buffer data', () => testBufferData('readable'));
398test('.duplex() can buffer data', () => testBufferData('duplex'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 399why, one step back
399

TweaksSindre Sorhusbefore adoption698a1b8

line 400why, one step back
400const assertDataEvents = async (stream, subprocess) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 401why, one step back
401 const [output] = await once(stream, 'data');

TweaksSindre Sorhusbefore adoption698a1b8

line 402why, one step back
402 assert.equal(output.toString(), foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 403–404why, one step back
403
404 await finishedStream(stream);

TweaksSindre Sorhusbefore adoption698a1b8

line 405why, one step back
405 await assertSubprocessOutput(subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 406–407why, one step back
406};
407

TweaksSindre Sorhusbefore adoption698a1b8

line 408why, one step back
408test('.readable() can be read with "data" events', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 409–411why, one step back
409 const subprocess = getReadableSubprocess();
410 const stream = subprocess.readable();411

TweaksSindre Sorhusbefore adoption698a1b8

line 412why, one step back
412 await assertDataEvents(stream, subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 413–414why, one step back
413});
414

TweaksSindre Sorhusbefore adoption698a1b8

line 415why, one step back
415test('.duplex() can be read with "data" events', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 416–419why, one step back
416 const subprocess = getReadWriteSubprocess();
417 const stream = subprocess.duplex();418 stream.end(foobarString);419

TweaksSindre Sorhusbefore adoption698a1b8

line 420why, one step back
420 await assertDataEvents(stream, subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

line 423why, one step back
423const assertPause = async (stream, subprocess) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 424–426why, one step back
424 const onceData = once(stream, 'data');
425 stream.pause();426

TweaksSindre Sorhusbefore adoption698a1b8

line 427why, one step back
427 assert.equal(stream.readableLength, 0);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 428–432why, one step back
428 do {
429 // eslint-disable-next-line no-await-in-loop430 await setTimeout(10);431 } while (stream.readableLength === 0);432

TweaksSindre Sorhusbefore adoption698a1b8

line 433why, one step back
433 assert.ok(!await Promise.race([onceData, false]));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 434–436why, one step back
434
435 stream.resume();436 const [output] = await onceData;

TweaksSindre Sorhusbefore adoption698a1b8

line 437why, one step back
437 assert.equal(output.toString(), foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 438–439why, one step back
438
439 await finishedStream(stream);

TweaksSindre Sorhusbefore adoption698a1b8

line 440why, one step back
440 await assertSubprocessOutput(subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 441–442why, one step back
441};
442

TweaksSindre Sorhusbefore adoption698a1b8

line 443why, one step back
443test('.readable() can be paused', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 444–446why, one step back
444 const subprocess = getReadableSubprocess();
445 const stream = subprocess.readable();446

TweaksSindre Sorhusbefore adoption698a1b8

line 447why, one step back
447 await assertPause(stream, subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 448–449why, one step back
448});
449

TweaksSindre Sorhusbefore adoption698a1b8

line 450why, one step back
450test('.duplex() can be paused', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 451–454why, one step back
451 const subprocess = getReadWriteSubprocess();
452 const stream = subprocess.duplex();453 stream.end(foobarString);454

TweaksSindre Sorhusbefore adoption698a1b8

line 455why, one step back
455 await assertPause(stream, subprocess);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 456–459why, one step back
456});
457458// This feature does not work on Node 18.459// @todo: remove after dropping support for Node 18.

Runs CI tests on Node 23 (#1169)ehmickybefore adoptione330bd8

line 460why, one step back
460if (majorNodeVersion >= 20) {

TweaksSindre Sorhusbefore adoption698a1b8

line 461why, one step back
461 const testHighWaterMark = async methodName => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 462–474why, one step back
462 const subprocess = execa('stdin.js');
463 const stream = subprocess[methodName]();464465 let count = 0;466 const onPause = once(subprocess.stdout, 'pause');467 for (; !subprocess.stdout.isPaused(); count += 1) {468 subprocess.stdin.write('.');469 // eslint-disable-next-line no-await-in-loop470 await Promise.race([onPause, once(subprocess.stdout, 'data')]);471 }472473 const expectedCount = defaultObjectHighWaterMark + 1;474 const expectedOutput = '.'.repeat(expectedCount);

TweaksSindre Sorhusbefore adoption698a1b8

line 475why, one step back
475 assert.equal(count, expectedCount);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 476why, one step back
476 subprocess.stdin.end();

TweaksSindre Sorhusbefore adoption698a1b8

lines 477–478why, one step back
477 await assertStreamOutput(stream, expectedOutput);
478 await assertSubprocessOutput(subprocess, expectedOutput);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 479–480why, one step back
479 };
480

TweaksSindre Sorhusbefore adoption698a1b8

lines 481–482why, one step back
481 test('.readable() pauses its buffering when too high', () => testHighWaterMark('readable'));
482 test('.duplex() pauses its buffering when too high', () => testHighWaterMark('duplex'));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 483–484why, one step back
483}
484

TweaksSindre Sorhusbefore adoption698a1b8

line 485why, one step back
485const testBigOutput = async methodName => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 486–489why, one step back
486 const bigChunk = '.'.repeat(1e6);
487 const subprocess = execa('stdin.js', {input: bigChunk});488 const stream = subprocess[methodName]();489

TweaksSindre Sorhusbefore adoption698a1b8

lines 490–491why, one step back
490 await assertStreamOutput(stream, bigChunk);
491 await assertSubprocessOutput(subprocess, bigChunk);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 492–493why, one step back
492};
493

TweaksSindre Sorhusbefore adoption698a1b8

lines 494–495why, one step back
494test('.readable() with big output', () => testBigOutput('readable'));
495test('.duplex() with big output', () => testBigOutput('duplex'));

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 496–503why, one step back
496
497test('The "binary" readable option must use a boolean', () => {498 assert.throws(() => execa('noop.js').readable({binary: 'true'}), {message: /`binary` option must use a boolean/});499});500501test('The "preserveNewlines" readable option must use a boolean', () => {502 assert.throws(() => execa('noop.js').readable({preserveNewlines: 'true'}), {message: /`preserveNewlines` option must use a boolean/});503});