Skip to content
Myr

readable.js

This file

Path

test/convert/readable.js

459 lines · 15.8 KiB · JavaScript

Last changed by

Upgrade xo (#980)ehmickybefore adoptionccfa412

History of this file

Take it

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 1–2why, one step back
1import {once} from 'node:events';
2import process from 'node:process';

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 3–8why, one step back
3import {
4 compose,5 Readable,6 Writable,7 PassThrough,8} from 'node:stream';

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 9–13why, one step back
9import {pipeline} from 'node:stream/promises';
10import {text} from 'node:stream/consumers';11import {setTimeout} from 'node:timers/promises';12import test from 'ava';13import {execa} from '../../index.js';

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 14why, one step back
14import {setFixtureDirectory} from '../helpers/fixtures-directory.js';

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 15–20why, one step back
15import {
16 finishedStream,17 assertReadableAborted,18 assertWritableAborted,19 assertProcessNormalExit,20 assertStreamOutput,

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 21why, one step back
21 assertStreamChunks,

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 22–30why, one step back
22 assertStreamError,
23 assertStreamReadError,24 assertSubprocessOutput,25 assertSubprocessError,26 assertPromiseError,27 getReadableSubprocess,28 getReadWriteSubprocess,29} from '../helpers/convert.js';30import {foobarString, foobarBuffer, foobarObject} from '../helpers/input.js';

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 31why, one step back
31import {simpleFull} from '../helpers/lines.js';

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 32why, one step back
32import {prematureClose, fullStdio} from '../helpers/stdio.js';

Refactor tests (#934)ehmickybefore adoption52c4bde

line 33why, one step back
33import {outputObjectGenerator, getOutputsAsyncGenerator} from '../helpers/generator.js';

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 34–35why, one step back
34import {defaultHighWaterMark, defaultObjectHighWaterMark} from '../helpers/stream.js';
35

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 36why, one step back
36setFixtureDirectory();

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 37–52why, one step back
37
38test('.readable() success', async t => {39 const subprocess = getReadableSubprocess();40 const stream = subprocess.readable();4142 t.false(stream instanceof Writable);43 t.is(stream.writable, undefined);44 t.true(stream instanceof Readable);45 t.true(stream.readable);4647 await assertStreamOutput(t, stream);48 await assertSubprocessOutput(t, subprocess);49});5051// eslint-disable-next-line max-params52const testReadableDefault = async (t, fdNumber, from, options, hasResult) => {

Fix using both `encoding: 'utf16le'` and `lines`/`verbose` options (#931)ehmickybefore adoptiona480e1d

line 53why, one step back
53 const subprocess = execa('noop-fd.js', [`${fdNumber}`, foobarString], options);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 54–59why, one step back
54 const stream = subprocess.readable({from});
5556 await assertStreamOutput(t, stream, hasResult ? foobarString : '');57 await assertSubprocessOutput(t, subprocess, foobarString, fdNumber);58};59

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

lines 60–62why, one step back
60test('.readable() can use stdout', testReadableDefault, 1, 'stdout', {}, true);
61test('.readable() can use stderr', testReadableDefault, 2, 'stderr', {}, true);62test('.readable() can use stdio[*]', testReadableDefault, 3, 'fd3', fullStdio, true);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 63–65why, one step back
63test('.readable() uses stdout by default', testReadableDefault, 1, undefined, {}, true);
64test('.readable() does not use stderr by default', testReadableDefault, 2, undefined, {}, false);65test('.readable() does not use stdio[*] by default', testReadableDefault, 3, undefined, fullStdio, false);

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

lines 66–67why, one step back
66test('.readable() uses stdout even if stderr is "ignore"', testReadableDefault, 1, 'stdout', {stderr: 'ignore'}, true);
67test('.readable() uses stderr even if stdout is "ignore"', testReadableDefault, 2, 'stderr', {stdout: 'ignore'}, true);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 68–250why, one step back
68test('.readable() uses stdout if "all" is used', testReadableDefault, 1, 'all', {all: true}, true);
69test('.readable() uses stderr if "all" is used', testReadableDefault, 2, 'all', {all: true}, true);7071const testBuffering = async (t, methodName) => {72 const subprocess = execa('noop-stdin-fd.js', ['1'], {buffer: false});73 const stream = subprocess[methodName]();7475 subprocess.stdin.write(foobarString);76 await once(subprocess.stdout, 'readable');77 subprocess.stdin.end();7879 await assertStreamOutput(t, stream);80};8182test('.readable() buffers until read', testBuffering, 'readable');83test('.duplex() buffers until read', testBuffering, 'duplex');8485test('.readable() abort -> subprocess fail', async t => {86 const subprocess = execa('noop-repeat.js');87 const stream = subprocess.readable();8889 stream.destroy();9091 const error = await t.throwsAsync(text(stream));92 assertProcessNormalExit(t, error, 1);93 t.true(error.message.includes('EPIPE'));94 assertWritableAborted(t, subprocess.stdin);95 assertReadableAborted(t, subprocess.stdout);96 t.true(subprocess.stderr.readableEnded);97 await assertSubprocessError(t, subprocess, error);98});99100test('.readable() error -> subprocess fail', async t => {101 const subprocess = execa('noop-repeat.js');102 const stream = subprocess.readable();103104 const cause = new Error(foobarString);105 stream.destroy(cause);106107 const error = await assertStreamReadError(t, stream, {cause});108 assertProcessNormalExit(t, error, 1);109 t.true(error.message.includes('EPIPE'));110 assertWritableAborted(t, subprocess.stdin);111 t.is(subprocess.stdout.errored, cause);112 t.true(subprocess.stderr.readableEnded);113 await assertSubprocessError(t, subprocess, error);114});115116const testStdoutAbort = async (t, methodName) => {117 const subprocess = execa('ipc-exit.js', {ipc: true});118 const stream = subprocess[methodName]();119120 subprocess.stdout.destroy();121 subprocess.send(foobarString);122123 const [error, [message]] = await Promise.all([124 t.throwsAsync(finishedStream(stream)),125 once(subprocess, 'message'),126 ]);127 t.like(error, prematureClose);128 t.is(message, foobarString);129 assertWritableAborted(t, subprocess.stdin);130 assertReadableAborted(t, subprocess.stdout);131 t.true(subprocess.stderr.readableEnded);132 await assertSubprocessOutput(t, subprocess, '');133};134135test('subprocess.stdout abort + no more writes -> .readable() error + subprocess success', testStdoutAbort, 'readable');136test('subprocess.stdout abort + no more writes -> .duplex() error + subprocess success', testStdoutAbort, 'duplex');137138const testStdoutError = async (t, methodName) => {139 const subprocess = execa('ipc-exit.js', {ipc: true});140 const stream = subprocess[methodName]();141142 const cause = new Error(foobarString);143 subprocess.stdout.destroy(cause);144 subprocess.send(foobarString);145146 const [error, [message]] = await Promise.all([147 t.throwsAsync(finishedStream(stream)),148 once(subprocess, 'message'),149 ]);150 t.is(message, foobarString);151 t.is(error.cause, cause);152 assertProcessNormalExit(t, error);153 t.is(subprocess.stdout.errored, cause);154 t.true(subprocess.stderr.readableEnded);155 assertWritableAborted(t, subprocess.stdin);156157 await assertSubprocessError(t, subprocess, error);158};159160test('subprocess.stdout error + no more writes -> .readable() error + subprocess fail', testStdoutError, 'readable');161test('subprocess.stdout error + no more writes -> .duplex() error + subprocess fail', testStdoutError, 'duplex');162163const testStdinAbortWrites = async (t, methodName) => {164 const subprocess = getReadWriteSubprocess();165 const stream = subprocess[methodName]();166167 subprocess.stdout.destroy();168 subprocess.stdin.end(foobarString);169170 const error = await t.throwsAsync(finishedStream(stream));171 assertProcessNormalExit(t, error, 1);172 t.true(subprocess.stdin.writableEnded);173 assertReadableAborted(t, subprocess.stdout);174 t.true(subprocess.stderr.readableEnded);175 await assertSubprocessError(t, subprocess, error);176};177178test('subprocess.stdout abort + more writes -> .readable() error + subprocess fail', testStdinAbortWrites, 'readable');179test('subprocess.stdout abort + more writes -> .duplex() error + subprocess fail', testStdinAbortWrites, 'duplex');180181const testStdinErrorWrites = async (t, methodName) => {182 const subprocess = getReadWriteSubprocess();183 const stream = subprocess[methodName]();184185 const cause = new Error(foobarString);186 subprocess.stdout.destroy(cause);187 subprocess.stdin.end(foobarString);188189 const error = await assertStreamError(t, stream, {cause});190 assertProcessNormalExit(t, error, 1);191 t.true(subprocess.stdin.writableEnded);192 t.is(subprocess.stdout.errored, cause);193 t.true(subprocess.stderr.readableEnded);194 await assertSubprocessError(t, subprocess, error);195};196197test('subprocess.stdout error + more writes -> .readable() error + subprocess fail', testStdinErrorWrites, 'readable');198test('subprocess.stdout error + more writes -> .duplex() error + subprocess fail', testStdinErrorWrites, 'duplex');199200test('.readable() can be used with Stream.pipeline()', async t => {201 const subprocess = getReadableSubprocess();202 const stream = subprocess.readable();203 const outputStream = new PassThrough();204205 await pipeline(stream, outputStream);206207 await finishedStream(stream);208 await assertStreamOutput(t, outputStream);209 await assertSubprocessOutput(t, subprocess);210});211212test('.readable() can error with Stream.pipeline()', async t => {213 const subprocess = execa('noop-fail.js', ['1', foobarString]);214 const stream = subprocess.readable();215 const outputStream = new PassThrough();216217 const error = await t.throwsAsync(pipeline(stream, outputStream));218 assertProcessNormalExit(t, error, 2);219 t.like(error, {stdout: foobarString});220221 await assertStreamError(t, stream, error);222 await assertStreamReadError(t, outputStream, error);223 await assertSubprocessError(t, subprocess, error);224});225226test('.readable() can pipe to errored stream with Stream.pipeline()', async t => {227 const subprocess = getReadableSubprocess();228 const stream = subprocess.readable();229 const outputStream = new PassThrough();230231 const cause = new Error('test');232 outputStream.destroy(cause);233234 await assertPromiseError(t, pipeline(stream, outputStream), cause);235236 const error = await assertStreamError(t, stream, {cause});237 await assertStreamReadError(t, outputStream, cause);238 await assertSubprocessError(t, subprocess, error);239});240241test('.readable() can be used with Stream.compose()', async t => {242 const subprocess = getReadableSubprocess();243 const stream = subprocess.readable();244 const outputStream = new PassThrough();245246 await assertStreamOutput(t, compose(stream, outputStream));247 await assertSubprocessOutput(t, subprocess);248});249250test('.readable() works with objectMode', async t => {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 251why, one step back
251 const subprocess = execa('noop.js', {stdout: outputObjectGenerator()});

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 252–255why, one step back
252 const stream = subprocess.readable();
253 t.true(stream.readableObjectMode);254 t.is(stream.readableHighWaterMark, defaultObjectHighWaterMark);255

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 256why, one step back
256 await assertStreamChunks(t, stream, [foobarObject]);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 257–260why, one step back
257 await assertSubprocessOutput(t, subprocess, [foobarObject]);
258});259260test('.duplex() works with objectMode and reads', async t => {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 261why, one step back
261 const subprocess = getReadWriteSubprocess({stdout: outputObjectGenerator()});

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 262–268why, one step back
262 const stream = subprocess.duplex();
263 t.true(stream.readableObjectMode);264 t.is(stream.readableHighWaterMark, defaultObjectHighWaterMark);265 t.false(stream.writableObjectMode);266 t.is(stream.writableHighWaterMark, defaultHighWaterMark);267 stream.end(foobarString);268

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 269why, one step back
269 await assertStreamChunks(t, stream, [foobarObject]);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 270–277why, one step back
270 await assertSubprocessOutput(t, subprocess, [foobarObject]);
271});272273test('.readable() works with default encoding', async t => {274 const subprocess = getReadableSubprocess();275 const stream = subprocess.readable();276 t.is(stream.readableEncoding, null);277

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 278why, one step back
278 await assertStreamChunks(t, stream, [foobarBuffer]);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 279–287why, one step back
279 await assertSubprocessOutput(t, subprocess, foobarString);
280});281282test('.duplex() works with default encoding', async t => {283 const subprocess = getReadWriteSubprocess();284 const stream = subprocess.duplex();285 t.is(stream.readableEncoding, null);286 stream.end(foobarString);287

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 288why, one step back
288 await assertStreamChunks(t, stream, [foobarBuffer]);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 289–297why, one step back
289 await assertSubprocessOutput(t, subprocess, foobarString);
290});291292test('.readable() works with encoding "utf8"', async t => {293 const subprocess = getReadableSubprocess();294 subprocess.stdout.setEncoding('utf8');295 const stream = subprocess.readable();296 t.is(stream.readableEncoding, 'utf8');297

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 298why, one step back
298 await assertStreamChunks(t, stream, [foobarString]);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 299–308why, one step back
299 await assertSubprocessOutput(t, subprocess, foobarString);
300});301302test('.duplex() works with encoding "utf8"', async t => {303 const subprocess = getReadWriteSubprocess();304 subprocess.stdout.setEncoding('utf8');305 const stream = subprocess.duplex();306 t.is(stream.readableEncoding, 'utf8');307 stream.end(foobarBuffer);308

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 309why, one step back
309 await assertStreamChunks(t, stream, [foobarString]);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 310–320why, one step back
310 await assertSubprocessOutput(t, subprocess, foobarString);
311});312313test('.readable() has the right highWaterMark', async t => {314 const subprocess = execa('noop.js');315 const stream = subprocess.readable();316 t.is(stream.readableHighWaterMark, defaultHighWaterMark);317 await text(stream);318});319320test('.readable() can iterate over lines', async t => {

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

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

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 322why, one step back
322 const lines = [];

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 323why, one step back
323 for await (const line of subprocess.readable({binary: false, preserveNewlines: false})) {

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 324–326why, one step back
324 lines.push(line);
325 }326

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 327why, one step back
327 const expectedLines = ['aaa', 'bbb', 'ccc'];

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 328why, one step back
328 t.deepEqual(lines, expectedLines);

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 329why, one step back
329 await assertSubprocessOutput(t, subprocess, simpleFull);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 330–332why, one step back
330});
331332test('.readable() can wait for data', async t => {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 333why, one step back
333 const subprocess = execa('noop.js', {stdout: getOutputsAsyncGenerator([foobarString, foobarString])(false, true)});

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 334–436why, one step back
334 const stream = subprocess.readable();
335336 t.is(stream.read(), null);337 await once(stream, 'readable');338 t.is(stream.read().toString(), foobarString);339 t.is(stream.read(), null);340 await once(stream, 'readable');341 t.is(stream.read().toString(), foobarString);342 t.is(stream.read(), null);343 await once(stream, 'readable');344 t.is(stream.read(), null);345346 await finishedStream(stream);347 await assertSubprocessOutput(t, subprocess, `${foobarString}${foobarString}`);348});349350const testBufferData = async (t, methodName) => {351 const chunk = '.'.repeat(defaultHighWaterMark).repeat(2);352 const subprocess = getReadWriteSubprocess();353 const stream = subprocess[methodName]();354 subprocess.stdin.end(chunk);355356 await assertStreamOutput(t, stream, chunk);357 await assertSubprocessOutput(t, subprocess, chunk);358};359360test('.readable() can buffer data', testBufferData, 'readable');361test('.duplex() can buffer data', testBufferData, 'duplex');362363const assertDataEvents = async (t, stream, subprocess) => {364 const [output] = await once(stream, 'data');365 t.is(output.toString(), foobarString);366367 await finishedStream(stream);368 await assertSubprocessOutput(t, subprocess);369};370371test('.readable() can be read with "data" events', async t => {372 const subprocess = getReadableSubprocess();373 const stream = subprocess.readable();374375 await assertDataEvents(t, stream, subprocess);376});377378test('.duplex() can be read with "data" events', async t => {379 const subprocess = getReadWriteSubprocess();380 const stream = subprocess.duplex();381 stream.end(foobarString);382383 await assertDataEvents(t, stream, subprocess);384});385386const assertPause = async (t, stream, subprocess) => {387 const onceData = once(stream, 'data');388 stream.pause();389390 t.is(stream.readableLength, 0);391 do {392 // eslint-disable-next-line no-await-in-loop393 await setTimeout(10);394 } while (stream.readableLength === 0);395396 t.false(await Promise.race([onceData, false]));397398 stream.resume();399 const [output] = await onceData;400 t.is(output.toString(), foobarString);401402 await finishedStream(stream);403 await assertSubprocessOutput(t, subprocess);404};405406test('.readable() can be paused', async t => {407 const subprocess = getReadableSubprocess();408 const stream = subprocess.readable();409410 await assertPause(t, stream, subprocess);411});412413test('.duplex() can be paused', async t => {414 const subprocess = getReadWriteSubprocess();415 const stream = subprocess.duplex();416 stream.end(foobarString);417418 await assertPause(t, stream, subprocess);419});420421// This feature does not work on Node 18.422// @todo: remove after dropping support for Node 18.423const majorVersion = Number(process.version.split('.')[0].slice(1));424if (majorVersion >= 20) {425 const testHighWaterMark = async (t, methodName) => {426 const subprocess = execa('stdin.js');427 const stream = subprocess[methodName]();428429 let count = 0;430 const onPause = once(subprocess.stdout, 'pause');431 for (; !subprocess.stdout.isPaused(); count += 1) {432 subprocess.stdin.write('.');433 // eslint-disable-next-line no-await-in-loop434 await Promise.race([onPause, once(subprocess.stdout, 'data')]);435 }436

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 437why, one step back
437 const expectedCount = defaultObjectHighWaterMark + 1;

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 438–450why, one step back
438 const expectedOutput = '.'.repeat(expectedCount);
439 t.is(count, expectedCount);440 subprocess.stdin.end();441 await assertStreamOutput(t, stream, expectedOutput);442 await assertSubprocessOutput(t, subprocess, expectedOutput);443 };444445 test('.readable() pauses its buffering when too high', testHighWaterMark, 'readable');446 test('.duplex() pauses its buffering when too high', testHighWaterMark, 'duplex');447}448449const testBigOutput = async (t, methodName) => {450 const bigChunk = '.'.repeat(1e6);

Fix using both `encoding: 'utf16le'` and `lines`/`verbose` options (#931)ehmickybefore adoptiona480e1d

line 451why, one step back
451 const subprocess = execa('stdin.js', {input: bigChunk});

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 452–459why, one step back
452 const stream = subprocess[methodName]();
453454 await assertStreamOutput(t, stream, bigChunk);455 await assertSubprocessOutput(t, subprocess, bigChunk);456};457458test('.readable() with big output', testBigOutput, 'readable');459test('.duplex() with big output', testBigOutput, 'duplex');