Skip to content
Myr

readable.js

This file

Path

test/convert/readable.js

454 lines · 15.8 KiB · JavaScript

Last changed by

Refactor tests (#934)ehmickybefore adoption52c4bde

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

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

line 3why, one step back
3import {compose, Readable, Writable, PassThrough} from 'node:stream';

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

lines 4–15why, one step back
4import {pipeline} from 'node:stream/promises';
5import {text} from 'node:stream/consumers';6import {setTimeout} from 'node:timers/promises';7import test from 'ava';8import {execa} from '../../index.js';9import {setFixtureDir} from '../helpers/fixtures-dir.js';10import {11 finishedStream,12 assertReadableAborted,13 assertWritableAborted,14 assertProcessNormalExit,15 assertStreamOutput,

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

line 16why, one step back
16 assertStreamChunks,

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

lines 17–25why, one step back
17 assertStreamError,
18 assertStreamReadError,19 assertSubprocessOutput,20 assertSubprocessError,21 assertPromiseError,22 getReadableSubprocess,23 getReadWriteSubprocess,24} from '../helpers/convert.js';25import {foobarString, foobarBuffer, foobarObject} from '../helpers/input.js';

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

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

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

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

Refactor tests (#934)ehmickybefore adoption52c4bde

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

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

lines 29–47why, one step back
29import {defaultHighWaterMark, defaultObjectHighWaterMark} from '../helpers/stream.js';
3031setFixtureDir();3233test('.readable() success', async t => {34 const subprocess = getReadableSubprocess();35 const stream = subprocess.readable();3637 t.false(stream instanceof Writable);38 t.is(stream.writable, undefined);39 t.true(stream instanceof Readable);40 t.true(stream.readable);4142 await assertStreamOutput(t, stream);43 await assertSubprocessOutput(t, subprocess);44});4546// eslint-disable-next-line max-params47const testReadableDefault = async (t, fdNumber, from, options, hasResult) => {

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

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

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

lines 49–54why, one step back
49 const stream = subprocess.readable({from});
5051 await assertStreamOutput(t, stream, hasResult ? foobarString : '');52 await assertSubprocessOutput(t, subprocess, foobarString, fdNumber);53};54

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

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

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

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

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

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

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

lines 63–245why, one step back
63test('.readable() uses stdout if "all" is used', testReadableDefault, 1, 'all', {all: true}, true);
64test('.readable() uses stderr if "all" is used', testReadableDefault, 2, 'all', {all: true}, true);6566const testBuffering = async (t, methodName) => {67 const subprocess = execa('noop-stdin-fd.js', ['1'], {buffer: false});68 const stream = subprocess[methodName]();6970 subprocess.stdin.write(foobarString);71 await once(subprocess.stdout, 'readable');72 subprocess.stdin.end();7374 await assertStreamOutput(t, stream);75};7677test('.readable() buffers until read', testBuffering, 'readable');78test('.duplex() buffers until read', testBuffering, 'duplex');7980test('.readable() abort -> subprocess fail', async t => {81 const subprocess = execa('noop-repeat.js');82 const stream = subprocess.readable();8384 stream.destroy();8586 const error = await t.throwsAsync(text(stream));87 assertProcessNormalExit(t, error, 1);88 t.true(error.message.includes('EPIPE'));89 assertWritableAborted(t, subprocess.stdin);90 assertReadableAborted(t, subprocess.stdout);91 t.true(subprocess.stderr.readableEnded);92 await assertSubprocessError(t, subprocess, error);93});9495test('.readable() error -> subprocess fail', async t => {96 const subprocess = execa('noop-repeat.js');97 const stream = subprocess.readable();9899 const cause = new Error(foobarString);100 stream.destroy(cause);101102 const error = await assertStreamReadError(t, stream, {cause});103 assertProcessNormalExit(t, error, 1);104 t.true(error.message.includes('EPIPE'));105 assertWritableAborted(t, subprocess.stdin);106 t.is(subprocess.stdout.errored, cause);107 t.true(subprocess.stderr.readableEnded);108 await assertSubprocessError(t, subprocess, error);109});110111const testStdoutAbort = async (t, methodName) => {112 const subprocess = execa('ipc-exit.js', {ipc: true});113 const stream = subprocess[methodName]();114115 subprocess.stdout.destroy();116 subprocess.send(foobarString);117118 const [error, [message]] = await Promise.all([119 t.throwsAsync(finishedStream(stream)),120 once(subprocess, 'message'),121 ]);122 t.like(error, prematureClose);123 t.is(message, foobarString);124 assertWritableAborted(t, subprocess.stdin);125 assertReadableAborted(t, subprocess.stdout);126 t.true(subprocess.stderr.readableEnded);127 await assertSubprocessOutput(t, subprocess, '');128};129130test('subprocess.stdout abort + no more writes -> .readable() error + subprocess success', testStdoutAbort, 'readable');131test('subprocess.stdout abort + no more writes -> .duplex() error + subprocess success', testStdoutAbort, 'duplex');132133const testStdoutError = async (t, methodName) => {134 const subprocess = execa('ipc-exit.js', {ipc: true});135 const stream = subprocess[methodName]();136137 const cause = new Error(foobarString);138 subprocess.stdout.destroy(cause);139 subprocess.send(foobarString);140141 const [error, [message]] = await Promise.all([142 t.throwsAsync(finishedStream(stream)),143 once(subprocess, 'message'),144 ]);145 t.is(message, foobarString);146 t.is(error.cause, cause);147 assertProcessNormalExit(t, error);148 t.is(subprocess.stdout.errored, cause);149 t.true(subprocess.stderr.readableEnded);150 assertWritableAborted(t, subprocess.stdin);151152 await assertSubprocessError(t, subprocess, error);153};154155test('subprocess.stdout error + no more writes -> .readable() error + subprocess fail', testStdoutError, 'readable');156test('subprocess.stdout error + no more writes -> .duplex() error + subprocess fail', testStdoutError, 'duplex');157158const testStdinAbortWrites = async (t, methodName) => {159 const subprocess = getReadWriteSubprocess();160 const stream = subprocess[methodName]();161162 subprocess.stdout.destroy();163 subprocess.stdin.end(foobarString);164165 const error = await t.throwsAsync(finishedStream(stream));166 assertProcessNormalExit(t, error, 1);167 t.true(subprocess.stdin.writableEnded);168 assertReadableAborted(t, subprocess.stdout);169 t.true(subprocess.stderr.readableEnded);170 await assertSubprocessError(t, subprocess, error);171};172173test('subprocess.stdout abort + more writes -> .readable() error + subprocess fail', testStdinAbortWrites, 'readable');174test('subprocess.stdout abort + more writes -> .duplex() error + subprocess fail', testStdinAbortWrites, 'duplex');175176const testStdinErrorWrites = async (t, methodName) => {177 const subprocess = getReadWriteSubprocess();178 const stream = subprocess[methodName]();179180 const cause = new Error(foobarString);181 subprocess.stdout.destroy(cause);182 subprocess.stdin.end(foobarString);183184 const error = await assertStreamError(t, stream, {cause});185 assertProcessNormalExit(t, error, 1);186 t.true(subprocess.stdin.writableEnded);187 t.is(subprocess.stdout.errored, cause);188 t.true(subprocess.stderr.readableEnded);189 await assertSubprocessError(t, subprocess, error);190};191192test('subprocess.stdout error + more writes -> .readable() error + subprocess fail', testStdinErrorWrites, 'readable');193test('subprocess.stdout error + more writes -> .duplex() error + subprocess fail', testStdinErrorWrites, 'duplex');194195test('.readable() can be used with Stream.pipeline()', async t => {196 const subprocess = getReadableSubprocess();197 const stream = subprocess.readable();198 const outputStream = new PassThrough();199200 await pipeline(stream, outputStream);201202 await finishedStream(stream);203 await assertStreamOutput(t, outputStream);204 await assertSubprocessOutput(t, subprocess);205});206207test('.readable() can error with Stream.pipeline()', async t => {208 const subprocess = execa('noop-fail.js', ['1', foobarString]);209 const stream = subprocess.readable();210 const outputStream = new PassThrough();211212 const error = await t.throwsAsync(pipeline(stream, outputStream));213 assertProcessNormalExit(t, error, 2);214 t.like(error, {stdout: foobarString});215216 await assertStreamError(t, stream, error);217 await assertStreamReadError(t, outputStream, error);218 await assertSubprocessError(t, subprocess, error);219});220221test('.readable() can pipe to errored stream with Stream.pipeline()', async t => {222 const subprocess = getReadableSubprocess();223 const stream = subprocess.readable();224 const outputStream = new PassThrough();225226 const cause = new Error('test');227 outputStream.destroy(cause);228229 await assertPromiseError(t, pipeline(stream, outputStream), cause);230231 const error = await assertStreamError(t, stream, {cause});232 await assertStreamReadError(t, outputStream, cause);233 await assertSubprocessError(t, subprocess, error);234});235236test('.readable() can be used with Stream.compose()', async t => {237 const subprocess = getReadableSubprocess();238 const stream = subprocess.readable();239 const outputStream = new PassThrough();240241 await assertStreamOutput(t, compose(stream, outputStream));242 await assertSubprocessOutput(t, subprocess);243});244245test('.readable() works with objectMode', async t => {

Refactor tests (#934)ehmickybefore adoption52c4bde

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

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

lines 247–250why, one step back
247 const stream = subprocess.readable();
248 t.true(stream.readableObjectMode);249 t.is(stream.readableHighWaterMark, defaultObjectHighWaterMark);250

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

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

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

lines 252–255why, one step back
252 await assertSubprocessOutput(t, subprocess, [foobarObject]);
253});254255test('.duplex() works with objectMode and reads', async t => {

Refactor tests (#934)ehmickybefore adoption52c4bde

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

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

lines 257–263why, one step back
257 const stream = subprocess.duplex();
258 t.true(stream.readableObjectMode);259 t.is(stream.readableHighWaterMark, defaultObjectHighWaterMark);260 t.false(stream.writableObjectMode);261 t.is(stream.writableHighWaterMark, defaultHighWaterMark);262 stream.end(foobarString);263

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

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

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

lines 265–272why, one step back
265 await assertSubprocessOutput(t, subprocess, [foobarObject]);
266});267268test('.readable() works with default encoding', async t => {269 const subprocess = getReadableSubprocess();270 const stream = subprocess.readable();271 t.is(stream.readableEncoding, null);272

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

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

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

lines 274–282why, one step back
274 await assertSubprocessOutput(t, subprocess, foobarString);
275});276277test('.duplex() works with default encoding', async t => {278 const subprocess = getReadWriteSubprocess();279 const stream = subprocess.duplex();280 t.is(stream.readableEncoding, null);281 stream.end(foobarString);282

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

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

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

lines 284–292why, one step back
284 await assertSubprocessOutput(t, subprocess, foobarString);
285});286287test('.readable() works with encoding "utf8"', async t => {288 const subprocess = getReadableSubprocess();289 subprocess.stdout.setEncoding('utf8');290 const stream = subprocess.readable();291 t.is(stream.readableEncoding, 'utf8');292

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

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

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

lines 294–303why, one step back
294 await assertSubprocessOutput(t, subprocess, foobarString);
295});296297test('.duplex() works with encoding "utf8"', async t => {298 const subprocess = getReadWriteSubprocess();299 subprocess.stdout.setEncoding('utf8');300 const stream = subprocess.duplex();301 t.is(stream.readableEncoding, 'utf8');302 stream.end(foobarBuffer);303

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

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

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

lines 305–315why, one step back
305 await assertSubprocessOutput(t, subprocess, foobarString);
306});307308test('.readable() has the right highWaterMark', async t => {309 const subprocess = execa('noop.js');310 const stream = subprocess.readable();311 t.is(stream.readableHighWaterMark, defaultHighWaterMark);312 await text(stream);313});314315test('.readable() can iterate over lines', async t => {

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

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

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

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

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

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

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

lines 319–321why, one step back
319 lines.push(line);
320 }321

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

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

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

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

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

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

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

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

Refactor tests (#934)ehmickybefore adoption52c4bde

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

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

lines 329–431why, one step back
329 const stream = subprocess.readable();
330331 t.is(stream.read(), null);332 await once(stream, 'readable');333 t.is(stream.read().toString(), foobarString);334 t.is(stream.read(), null);335 await once(stream, 'readable');336 t.is(stream.read().toString(), foobarString);337 t.is(stream.read(), null);338 await once(stream, 'readable');339 t.is(stream.read(), null);340341 await finishedStream(stream);342 await assertSubprocessOutput(t, subprocess, `${foobarString}${foobarString}`);343});344345const testBufferData = async (t, methodName) => {346 const chunk = '.'.repeat(defaultHighWaterMark).repeat(2);347 const subprocess = getReadWriteSubprocess();348 const stream = subprocess[methodName]();349 subprocess.stdin.end(chunk);350351 await assertStreamOutput(t, stream, chunk);352 await assertSubprocessOutput(t, subprocess, chunk);353};354355test('.readable() can buffer data', testBufferData, 'readable');356test('.duplex() can buffer data', testBufferData, 'duplex');357358const assertDataEvents = async (t, stream, subprocess) => {359 const [output] = await once(stream, 'data');360 t.is(output.toString(), foobarString);361362 await finishedStream(stream);363 await assertSubprocessOutput(t, subprocess);364};365366test('.readable() can be read with "data" events', async t => {367 const subprocess = getReadableSubprocess();368 const stream = subprocess.readable();369370 await assertDataEvents(t, stream, subprocess);371});372373test('.duplex() can be read with "data" events', async t => {374 const subprocess = getReadWriteSubprocess();375 const stream = subprocess.duplex();376 stream.end(foobarString);377378 await assertDataEvents(t, stream, subprocess);379});380381const assertPause = async (t, stream, subprocess) => {382 const onceData = once(stream, 'data');383 stream.pause();384385 t.is(stream.readableLength, 0);386 do {387 // eslint-disable-next-line no-await-in-loop388 await setTimeout(10);389 } while (stream.readableLength === 0);390391 t.false(await Promise.race([onceData, false]));392393 stream.resume();394 const [output] = await onceData;395 t.is(output.toString(), foobarString);396397 await finishedStream(stream);398 await assertSubprocessOutput(t, subprocess);399};400401test('.readable() can be paused', async t => {402 const subprocess = getReadableSubprocess();403 const stream = subprocess.readable();404405 await assertPause(t, stream, subprocess);406});407408test('.duplex() can be paused', async t => {409 const subprocess = getReadWriteSubprocess();410 const stream = subprocess.duplex();411 stream.end(foobarString);412413 await assertPause(t, stream, subprocess);414});415416// This feature does not work on Node 18.417// @todo: remove after dropping support for Node 18.418const majorVersion = Number(process.version.split('.')[0].slice(1));419if (majorVersion >= 20) {420 const testHighWaterMark = async (t, methodName) => {421 const subprocess = execa('stdin.js');422 const stream = subprocess[methodName]();423424 let count = 0;425 const onPause = once(subprocess.stdout, 'pause');426 for (; !subprocess.stdout.isPaused(); count += 1) {427 subprocess.stdin.write('.');428 // eslint-disable-next-line no-await-in-loop429 await Promise.race([onPause, once(subprocess.stdout, 'data')]);430 }431

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

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

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

lines 433–445why, one step back
433 const expectedOutput = '.'.repeat(expectedCount);
434 t.is(count, expectedCount);435 subprocess.stdin.end();436 await assertStreamOutput(t, stream, expectedOutput);437 await assertSubprocessOutput(t, subprocess, expectedOutput);438 };439440 test('.readable() pauses its buffering when too high', testHighWaterMark, 'readable');441 test('.duplex() pauses its buffering when too high', testHighWaterMark, 'duplex');442}443444const testBigOutput = async (t, methodName) => {445 const bigChunk = '.'.repeat(1e6);

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

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

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

lines 447–454why, one step back
447 const stream = subprocess[methodName]();
448449 await assertStreamOutput(t, stream, bigChunk);450 await assertSubprocessOutput(t, subprocess, bigChunk);451};452453test('.readable() with big output', testBigOutput, 'readable');454test('.duplex() with big output', testBigOutput, 'duplex');