Skip to content
Myr

writable.js

This file

Path

test/convert/writable.js

389 lines · 12.7 KiB · JavaScript

Last changed by

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

History of this file

Take it

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

lines 1–58why, one step back
1import {once} from 'node:events';
2import {compose, Readable, Writable} from 'node:stream';3import {pipeline} from 'node:stream/promises';4import {text} from 'node:stream/consumers';5import {setTimeout, scheduler} from 'node:timers/promises';6import {promisify} from 'node:util';7import test from 'ava';8import {execa} from '../../index.js';9import {setFixtureDir} from '../helpers/fixtures-dir.js';10import {11 finishedStream,12 assertWritableAborted,13 assertProcessNormalExit,14 assertStreamOutput,15 assertStreamError,16 assertSubprocessOutput,17 assertSubprocessError,18 assertPromiseError,19 getWritableSubprocess,20 getReadableSubprocess,21 getReadWriteSubprocess,22} from '../helpers/convert.js';23import {foobarString, foobarBuffer, foobarObject, foobarObjectString} from '../helpers/input.js';24import {prematureClose, fullReadableStdio} from '../helpers/stdio.js';25import {26 throwingGenerator,27 serializeGenerator,28 noopAsyncGenerator,29} from '../helpers/generator.js';30import {defaultHighWaterMark, defaultObjectHighWaterMark} from '../helpers/stream.js';3132setFixtureDir();3334test('.writable() success', async t => {35 const subprocess = getWritableSubprocess();36 const stream = subprocess.writable();3738 t.true(stream instanceof Writable);39 t.true(stream.writable);40 t.false(stream instanceof Readable);41 t.is(stream.readable, undefined);4243 stream.end(foobarString);4445 await finishedStream(stream);46 await assertSubprocessOutput(t, subprocess, foobarString, 2);47});4849const testWritableDefault = async (t, fdNumber, to, options) => {50 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], options);51 const stream = subprocess.writable({to});5253 stream.end(foobarString);5455 await finishedStream(stream);56 await assertSubprocessOutput(t, subprocess);57};58

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

lines 59–60why, one step back
59test('.writable() can use stdin', testWritableDefault, 0, 'stdin', {});
60test('.writable() can use stdio[*]', testWritableDefault, 3, 'fd3', fullReadableStdio());

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

lines 61–255why, one step back
61test('.writable() uses stdin by default', testWritableDefault, 0, undefined, {});
6263test('.writable() hangs until ended', async t => {64 const subprocess = getReadWriteSubprocess();65 const stream = subprocess.writable();6667 stream.write(foobarString);68 await setTimeout(1e2);69 stream.end();7071 await finishedStream(stream);72 await assertSubprocessOutput(t, subprocess);73});7475test('.duplex() hangs until ended', async t => {76 const subprocess = getReadWriteSubprocess();77 const stream = subprocess.duplex();7879 stream.write(foobarString);80 await setTimeout(1e2);81 stream.end();8283 await assertStreamOutput(t, stream);84 await assertSubprocessOutput(t, subprocess);85});8687const testEarlySuccess = async (t, methodName, hasWrites) => {88 const subprocess = hasWrites ? getReadableSubprocess() : execa('empty.js');89 const stream = subprocess[methodName]();9091 const error = await t.throwsAsync(finishedStream(stream));92 t.like(error, prematureClose);93 assertWritableAborted(t, subprocess.stdin);94 t.true(subprocess.stdout.readableEnded);95 t.true(subprocess.stderr.readableEnded);96 await assertSubprocessOutput(t, subprocess, hasWrites ? foobarString : '');97};9899test('subprocess early success with no writes -> .writable() abort', testEarlySuccess, 'writable', false);100test('subprocess early success with no writes -> .duplex() abort', testEarlySuccess, 'duplex', false);101test('subprocess early success with writes -> .writable() abort', testEarlySuccess, 'writable', true);102test('subprocess early success with writes -> .duplex() abort', testEarlySuccess, 'duplex', true);103104test('.writable() abort -> subprocess fail', async t => {105 const subprocess = getWritableSubprocess();106 const stream = subprocess.writable();107108 stream.destroy();109110 const error = await t.throwsAsync(finishedStream(stream));111 t.like(error, prematureClose);112 assertProcessNormalExit(t, error);113 assertWritableAborted(t, subprocess.stdin);114 t.true(subprocess.stdout.readableEnded);115 t.true(subprocess.stderr.readableEnded);116 await assertSubprocessError(t, subprocess, error);117});118119test('.writable() error -> subprocess fail', async t => {120 const subprocess = getWritableSubprocess();121 const stream = subprocess.writable();122123 const cause = new Error(foobarString);124 stream.destroy(cause);125126 const error = await assertStreamError(t, stream, {cause});127 assertProcessNormalExit(t, error);128 t.is(subprocess.stdin.errored, cause);129 t.true(subprocess.stdout.readableEnded);130 t.true(subprocess.stderr.readableEnded);131 await assertSubprocessError(t, subprocess, error);132});133134test('.writable() EPIPE error -> subprocess success', async t => {135 const subprocess = getWritableSubprocess();136 const stream = subprocess.writable();137138 const error = new Error(foobarString);139 error.code = 'EPIPE';140 stream.destroy(error);141142 await assertStreamError(t, stream, error);143 t.is(subprocess.stdin.errored, error);144 t.true(subprocess.stdout.readableEnded);145 t.true(subprocess.stderr.readableEnded);146 await subprocess;147});148149test('subprocess.stdin end -> .writable() end + subprocess success', async t => {150 const subprocess = getReadWriteSubprocess();151 const stream = subprocess.writable();152153 subprocess.stdin.end(foobarString);154155 await finishedStream(stream);156 await assertSubprocessOutput(t, subprocess);157});158159test('subprocess.stdin end -> .duplex() end + subprocess success', async t => {160 const subprocess = getReadWriteSubprocess();161 const stream = subprocess.duplex();162163 subprocess.stdin.end(foobarString);164165 await assertStreamOutput(t, stream);166 await assertSubprocessOutput(t, subprocess);167});168169const testStdinAbort = async (t, methodName) => {170 const subprocess = getReadWriteSubprocess();171 const stream = subprocess[methodName]();172173 subprocess.stdin.destroy();174175 const error = await t.throwsAsync(finishedStream(stream));176 t.like(error, prematureClose);177 assertProcessNormalExit(t, error);178 assertWritableAborted(t, subprocess.stdin);179 t.true(subprocess.stdout.readableEnded);180 t.true(subprocess.stderr.readableEnded);181 await assertSubprocessError(t, subprocess, error);182};183184test('subprocess.stdin abort -> .writable() error + subprocess fail', testStdinAbort, 'writable');185test('subprocess.stdin abort -> .duplex() error + subprocess fail', testStdinAbort, 'duplex');186187const testStdinError = async (t, methodName) => {188 const subprocess = getReadWriteSubprocess();189 const stream = subprocess[methodName]();190191 const cause = new Error(foobarString);192 subprocess.stdin.destroy(cause);193194 const error = await assertStreamError(t, stream, {cause});195 assertProcessNormalExit(t, error);196 t.is(subprocess.stdin.errored, cause);197 t.true(subprocess.stderr.readableEnded);198 t.true(subprocess.stdout.readableEnded);199 await assertSubprocessError(t, subprocess, error);200};201202test('subprocess.stdin error -> .writable() error + subprocess fail', testStdinError, 'writable');203test('subprocess.stdin error -> .duplex() error + subprocess fail', testStdinError, 'duplex');204205test('.writable() can be used with Stream.pipeline()', async t => {206 const subprocess = getWritableSubprocess();207 const inputStream = Readable.from([foobarString]);208 const stream = subprocess.writable();209210 await pipeline(inputStream, stream);211212 await finishedStream(inputStream);213 await finishedStream(stream);214 await assertSubprocessOutput(t, subprocess, foobarString, 2);215});216217test('.writable() can error with Stream.pipeline()', async t => {218 const subprocess = execa('noop-stdin-fail.js', ['2']);219 const inputStream = Readable.from([foobarString]);220 const stream = subprocess.writable();221222 const error = await t.throwsAsync(pipeline(inputStream, stream));223 assertProcessNormalExit(t, error, 2);224 t.is(error.stderr, foobarString);225226 await finishedStream(inputStream);227 await assertStreamError(t, stream, error);228 await assertSubprocessError(t, subprocess, error);229});230231test('.writable() can pipe to errored stream with Stream.pipeline()', async t => {232 const subprocess = getWritableSubprocess();233 const inputStream = Readable.from([foobarString]);234 const stream = subprocess.writable();235236 const cause = new Error('test');237 inputStream.destroy(cause);238239 await assertPromiseError(t, pipeline(inputStream, stream), cause);240241 await assertStreamError(t, inputStream, cause);242 const error = await assertStreamError(t, stream, {cause});243 await assertSubprocessError(t, subprocess, error);244});245246test('.writable() can be used with Stream.compose()', async t => {247 const subprocess = getWritableSubprocess();248 const inputStream = Readable.from([foobarString]);249 const stream = subprocess.writable();250251 await finishedStream(compose(inputStream, stream));252 await assertSubprocessOutput(t, subprocess, foobarString, 2);253});254255test('.writable() works with objectMode', async t => {

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 256why, one step back
256 const subprocess = getReadWriteSubprocess({stdin: serializeGenerator(true, true)});

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

lines 257–266why, one step back
257 const stream = subprocess.writable();
258 t.true(stream.writableObjectMode);259 t.is(stream.writableHighWaterMark, defaultObjectHighWaterMark);260 stream.end(foobarObject);261262 await finishedStream(stream);263 await assertSubprocessOutput(t, subprocess, foobarObjectString);264});265266test('.duplex() works with objectMode and writes', async t => {

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 267why, one step back
267 const subprocess = getReadWriteSubprocess({stdin: serializeGenerator(true, true)});

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

lines 268–320why, one step back
268 const stream = subprocess.duplex();
269 t.false(stream.readableObjectMode);270 t.is(stream.readableHighWaterMark, defaultHighWaterMark);271 t.true(stream.writableObjectMode);272 t.is(stream.writableHighWaterMark, defaultObjectHighWaterMark);273 stream.end(foobarObject);274275 await assertStreamOutput(t, stream, foobarObjectString);276 await assertSubprocessOutput(t, subprocess, foobarObjectString);277});278279test('.writable() has the right highWaterMark', async t => {280 const subprocess = getReadWriteSubprocess();281 const stream = subprocess.writable();282 t.is(stream.writableHighWaterMark, defaultHighWaterMark);283 stream.end();284 await finishedStream(stream);285});286287const writeUntilFull = async (t, stream, subprocess) => {288 const size = stream.writableHighWaterMark / 2;289 const chunk = '.'.repeat(size);290291 t.is(subprocess.stdin.writableLength, 0);292 t.is(stream.writableLength, 0);293 t.false(subprocess.stdin.writableNeedDrain);294 t.false(stream.writableNeedDrain);295296 t.true(stream.write(chunk));297 t.is(subprocess.stdin.writableLength, size);298 t.is(stream.writableLength, 0);299 t.false(subprocess.stdin.writableNeedDrain);300 t.false(stream.writableNeedDrain);301302 t.true(stream.write(chunk));303 t.is(subprocess.stdin.writableLength, size * 2);304 t.is(stream.writableLength, size);305 t.true(subprocess.stdin.writableNeedDrain);306 t.false(stream.writableNeedDrain);307308 t.false(stream.write(chunk));309 t.is(subprocess.stdin.writableLength, size * 2);310 t.is(stream.writableLength, size * 2);311 t.true(subprocess.stdin.writableNeedDrain);312 t.true(stream.writableNeedDrain);313314 await once(stream, 'drain');315 stream.end();316317 return '.'.repeat(size * 3);318};319320test('.writable() waits when its buffer is full', async t => {

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 321why, one step back
321 const subprocess = getReadWriteSubprocess({stdin: noopAsyncGenerator(false, true)});

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

lines 322–329why, one step back
322 const stream = subprocess.writable();
323324 const expectedOutput = await writeUntilFull(t, stream, subprocess);325326 await assertSubprocessOutput(t, subprocess, expectedOutput);327});328329test('.duplex() waits when its buffer is full', async t => {

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 330why, one step back
330 const subprocess = getReadWriteSubprocess({stdin: noopAsyncGenerator(false, true)});

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

lines 331–339why, one step back
331 const stream = subprocess.duplex();
332333 const expectedOutput = await writeUntilFull(t, stream, subprocess);334335 await assertStreamOutput(t, stream, expectedOutput);336 await assertSubprocessOutput(t, subprocess, expectedOutput);337});338339const testPropagateError = async (t, methodName) => {

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 340–341why, one step back
340 const cause = new Error(foobarString);
341 const subprocess = getReadWriteSubprocess({stdin: throwingGenerator(cause)()});

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

lines 342–343why, one step back
342 const stream = subprocess[methodName]();
343 stream.end('.');

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 344why, one step back
344 await assertStreamError(t, stream, {cause});

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

lines 345–389why, one step back
345};
346347test('.writable() propagates write errors', testPropagateError, 'writable');348test('.duplex() propagates write errors', testPropagateError, 'duplex');349350const testWritev = async (t, methodName, waitForStream) => {351 const subprocess = getReadWriteSubprocess({stdin: noopAsyncGenerator()});352 const stream = subprocess[methodName]();353354 const chunk = '.'.repeat(stream.writableHighWaterMark);355 stream.write(chunk);356 t.true(stream.writableNeedDrain);357358 const [writeInOneTick] = await Promise.race([359 Promise.all([true, promisify(stream.write.bind(stream))(chunk)]),360 Promise.all([false, scheduler.yield()]),361 ]);362 t.true(writeInOneTick);363364 stream.end();365 await waitForStream(stream);366};367368test('.writable() can use .writev()', testWritev, 'writable', finishedStream);369test('.duplex() can use .writev()', testWritev, 'duplex', text);370371test('.writable() can set encoding', async t => {372 const subprocess = getReadWriteSubprocess();373 const stream = subprocess.writable();374375 stream.end(foobarBuffer.toString('hex'), 'hex');376377 await finishedStream(stream);378 await assertSubprocessOutput(t, subprocess);379});380381test('.duplex() can set encoding', async t => {382 const subprocess = getReadWriteSubprocess();383 const stream = subprocess.duplex();384385 stream.end(foobarBuffer.toString('hex'), 'hex');386387 await assertStreamOutput(t, stream);388 await assertSubprocessOutput(t, subprocess);389});