node-stream.js
This file
Path
test/stdio/node-stream.js
268 lines · 12.5 KiB · JavaScript
Last changed by
Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
History of this fileTake it
Refactor tests (#628)ehmickybefore adoption30ecc81
lines 1–3why, one step back1import {once} from 'node:events';2import {createReadStream, createWriteStream} from 'node:fs';3import {readFile, writeFile, rm} from 'node:fs/promises';Fix stream error handling (#826)ehmickybefore adoption35c0fb0
line 4why, one step back4import {Writable, PassThrough} from 'node:stream';Improve tests related to passing Node.js streams (#788)ehmickybefore adoption81f0b2d
line 5why, one step back5import {text} from 'node:stream/consumers';Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf
line 6why, one step back6import {setImmediate} from 'node:timers/promises';Wait for streams completion on errors (#722)ehmickybefore adoption195369f
line 7why, one step back7import {callbackify} from 'node:util';Refactor tests (#628)ehmickybefore adoption30ecc81
lines 8–11why, one step back8import test from 'ava';9import tempfile from 'tempfile';10import {execa, execaSync} from '../../index.js';11import {setFixtureDir} from '../helpers/fixtures-dir.js';Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 12why, one step back12import {getStdio} from '../helpers/stdio.js';Improve tests related to passing Node.js streams (#788)ehmickybefore adoption81f0b2d
line 13why, one step back13import {foobarString} from '../helpers/input.js';Fix stream error handling (#826)ehmickybefore adoption35c0fb0
line 14why, one step back14import {noopReadable, noopWritable, noopDuplex, simpleReadable} from '../helpers/stream.js';Refactor tests (#628)ehmickybefore adoption30ecc81
lines 15–17why, one step back1516setFixtureDir();17Improve tests related to passing Node.js streams (#788)ehmickybefore adoption81f0b2d
line 18why, one step back18const testNoFileStreamSync = async (t, index, stream) => {Refactor tests (#628)ehmickybefore adoption30ecc81
line 19why, one step back19 t.throws(() => {Improve tests related to passing Node.js streams (#788)ehmickybefore adoption81f0b2d
line 20why, one step back20 execaSync('empty.js', getStdio(index, stream));Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
line 21why, one step back21 }, {code: 'ERR_INVALID_ARG_VALUE'});Refactor tests (#628)ehmickybefore adoption30ecc81
lines 22–23why, one step back22};23Improve tests related to passing Node.js streams (#788)ehmickybefore adoption81f0b2d
lines 24–32why, one step back24test('stdin cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 0, noopReadable());25test('stdin cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 0, noopDuplex());26test('stdout cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 1, noopWritable());27test('stdout cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 1, noopDuplex());28test('stderr cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 2, noopWritable());29test('stderr cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 2, noopDuplex());30test('stdio[*] cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 3, noopReadable());31test('stdio[*] cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 3, noopWritable());32test('stdio[*] cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 3, noopDuplex());Refactor tests (#628)ehmickybefore adoption30ecc81
lines 33–34why, one step back3334test('input can be a Node.js Readable without a file descriptor', async t => {Improve tests related to passing Node.js streams (#788)ehmickybefore adoption81f0b2d
lines 35–36why, one step back35 const {stdout} = await execa('stdin.js', {input: simpleReadable()});36 t.is(stdout, foobarString);Refactor tests (#628)ehmickybefore adoption30ecc81
lines 37–38why, one step back37});38Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
lines 39–40why, one step back39test('input cannot be a Node.js Readable without a file descriptor - sync', t => {40 t.throws(() => {Improve tests related to passing Node.js streams (#788)ehmickybefore adoption81f0b2d
line 41why, one step back41 execaSync('empty.js', {input: simpleReadable()});Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
lines 42–44why, one step back42 }, {message: 'The `input` option cannot be a Node.js stream in sync mode.'});43});44Improve tests related to passing Node.js streams (#788)ehmickybefore adoption81f0b2d
lines 45–46why, one step back45const testNoFileStream = async (t, index, stream) => {46 await t.throwsAsync(execa('empty.js', getStdio(index, stream)), {code: 'ERR_INVALID_ARG_VALUE'});Refactor tests (#628)ehmickybefore adoption30ecc81
lines 47–48why, one step back47};48Improve tests related to passing Node.js streams (#788)ehmickybefore adoption81f0b2d
lines 49–57why, one step back49test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 0, noopReadable());50test('stdin cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 0, noopDuplex());51test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 1, noopWritable());52test('stdout cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 1, noopDuplex());53test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 2, noopWritable());54test('stderr cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 2, noopDuplex());55test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, noopReadable());56test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, noopWritable());57test('stdio[*] cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 3, noopDuplex());Refactor tests (#628)ehmickybefore adoption30ecc81
line 58why, one step back58Fix stream error handling (#826)ehmickybefore adoption35c0fb0
line 59why, one step back59const createFileReadStream = async () => {Refactor tests (#628)ehmickybefore adoption30ecc81
lines 60–63why, one step back60 const filePath = tempfile();61 await writeFile(filePath, 'foobar');62 const stream = createReadStream(filePath);63 await once(stream, 'open');Fix stream error handling (#826)ehmickybefore adoption35c0fb0
lines 64–65why, one step back64 return {stream, filePath};65};Refactor tests (#628)ehmickybefore adoption30ecc81
line 66why, one step back66Fix stream error handling (#826)ehmickybefore adoption35c0fb0
lines 67–87why, one step back67const createFileWriteStream = async () => {68 const filePath = tempfile();69 const stream = createWriteStream(filePath);70 await once(stream, 'open');71 return {stream, filePath};72};7374const assertFileStreamError = async (t, childProcess, stream, filePath) => {75 const error = new Error('test');76 stream.destroy(error);7778 t.is(await t.throwsAsync(childProcess), error);79 t.is(error.exitCode, 0);80 t.is(error.signal, undefined);8182 await rm(filePath);83};8485const testFileReadable = async (t, index, execaMethod) => {86 const {stream, filePath} = await createFileReadStream();87Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 88why, one step back88 const indexString = index === 'input' ? '0' : `${index}`;Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
line 89why, one step back89 const {stdout} = await execaMethod('stdin-fd.js', [indexString], getStdio(index, stream));Refactor tests (#628)ehmickybefore adoption30ecc81
lines 90–94why, one step back90 t.is(stdout, 'foobar');9192 await rm(filePath);93};94Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
lines 95–99why, one step back95test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'input', execa);96test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 0, execa);97test('stdio[*] can be a Node.js Readable with a file descriptor', testFileReadable, 3, execa);98test('stdin can be a Node.js Readable with a file descriptor - sync', testFileReadable, 0, execaSync);99test('stdio[*] can be a Node.js Readable with a file descriptor - sync', testFileReadable, 3, execaSync);Refactor tests (#628)ehmickybefore adoption30ecc81
line 100why, one step back100Fix stream error handling (#826)ehmickybefore adoption35c0fb0
lines 101–109why, one step back101const testFileReadableError = async (t, index) => {102 const {stream, filePath} = await createFileReadStream();103104 const indexString = index === 'input' ? '0' : `${index}`;105 const childProcess = execa('stdin-fd.js', [indexString], getStdio(index, stream));106107 await assertFileStreamError(t, childProcess, stream, filePath);108};109Fix CI tests (#827)ehmickybefore adoption9435a34
lines 110–112why, one step back110test.serial('input handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 'input');111test.serial('stdin handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 0);112test.serial('stdio[*] handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 3);Fix stream error handling (#826)ehmickybefore adoption35c0fb0
lines 113–133why, one step back113114const testFileReadableOpen = async (t, index, useSingle, execaMethod) => {115 const {stream, filePath} = await createFileReadStream();116 t.deepEqual(stream.eventNames(), []);117118 const stdioOption = useSingle ? stream : [stream, 'pipe'];119 await execaMethod('empty.js', getStdio(index, stdioOption));120121 t.is(stream.readable, useSingle && index !== 'input');122 t.deepEqual(stream.eventNames(), []);123124 await rm(filePath);125};126127test('input closes a Node.js Readable with a file descriptor', testFileReadableOpen, 'input', true, execa);128test('stdin leaves open a single Node.js Readable with a file descriptor', testFileReadableOpen, 0, true, execa);129test('stdin closes a combined Node.js Readable with a file descriptor', testFileReadableOpen, 0, false, execa);130test('stdio[*] leaves open a single Node.js Readable with a file descriptor', testFileReadableOpen, 3, true, execa);131test('stdin leaves open a single Node.js Readable with a file descriptor - sync', testFileReadableOpen, 0, true, execaSync);132test('stdio[*] leaves open a single Node.js Readable with a file descriptor - sync', testFileReadableOpen, 3, true, execaSync);133Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
line 134why, one step back134const testFileWritable = async (t, index, execaMethod) => {Fix stream error handling (#826)ehmickybefore adoption35c0fb0
line 135why, one step back135 const {stream, filePath} = await createFileWriteStream();Refactor tests (#628)ehmickybefore adoption30ecc81
line 136why, one step back136Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
line 137why, one step back137 await execaMethod('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, stream));Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 138why, one step back138 t.is(await readFile(filePath, 'utf8'), 'foobar');Refactor tests (#628)ehmickybefore adoption30ecc81
lines 139–142why, one step back139140 await rm(filePath);141};142Allow Node.js streams with synchronous methods (#708)ehmickybefore adoption86bdadf
lines 143–148why, one step back143test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, 1, execa);144test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, 2, execa);145test('stdio[*] can be a Node.js Writable with a file descriptor', testFileWritable, 3, execa);146test('stdout can be a Node.js Writable with a file descriptor - sync', testFileWritable, 1, execaSync);147test('stderr can be a Node.js Writable with a file descriptor - sync', testFileWritable, 2, execaSync);148test('stdio[*] can be a Node.js Writable with a file descriptor - sync', testFileWritable, 3, execaSync);Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 149why, one step back149Fix stream error handling (#826)ehmickybefore adoption35c0fb0
lines 150–158why, one step back150const testFileWritableError = async (t, index) => {151 const {stream, filePath} = await createFileWriteStream();152153 const childProcess = execa('noop-stdin-fd.js', [`${index}`], getStdio(index, stream));154 childProcess.stdin.end(foobarString);155156 await assertFileStreamError(t, childProcess, stream, filePath);157};158Fix CI tests (#827)ehmickybefore adoption9435a34
lines 159–161why, one step back159test.serial('stdout handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 1);160test.serial('stderr handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 2);161test.serial('stdio[*] handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 3);Fix stream error handling (#826)ehmickybefore adoption35c0fb0
lines 162–185why, one step back162163const testFileWritableOpen = async (t, index, useSingle, execaMethod) => {164 const {stream, filePath} = await createFileWriteStream();165 t.deepEqual(stream.eventNames(), []);166167 const stdioOption = useSingle ? stream : [stream, 'pipe'];168 await execaMethod('empty.js', getStdio(index, stdioOption));169170 t.is(stream.writable, useSingle);171 t.deepEqual(stream.eventNames(), []);172173 await rm(filePath);174};175176test('stdout leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 1, true, execa);177test('stdout closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 1, false, execa);178test('stderr leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 2, true, execa);179test('stderr closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 2, false, execa);180test('stdio[*] leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 3, true, execa);181test('stdio[*] closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 3, false, execa);182test('stdout leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 1, true, execaSync);183test('stderr leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 2, true, execaSync);184test('stdio[*] leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 3, true, execaSync);185Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 186why, one step back186const testLazyFileReadable = async (t, index) => {Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 187–190why, one step back187 const filePath = tempfile();188 await writeFile(filePath, 'foobar');189 const stream = createReadStream(filePath);190Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 191why, one step back191 const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, [stream, 'pipe']));Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 192–196why, one step back192 t.is(stdout, 'foobar');193194 await rm(filePath);195};196Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 197–198why, one step back197test('stdin can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 0);198test('stdio[*] can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 3);Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 199why, one step back199Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 200why, one step back200const testLazyFileWritable = async (t, index) => {Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 201–203why, one step back201 const filePath = tempfile();202 const stream = createWriteStream(filePath);203Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 204–205why, one step back204 await execa('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, [stream, 'pipe']));205 t.is(await readFile(filePath, 'utf8'), 'foobar');Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 206–209why, one step back206207 await rm(filePath);208};209Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 210–212why, one step back210test('stdout can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 1);211test('stderr can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 2);212test('stdio[*] can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 3);Wait for streams completion on errors (#722)ehmickybefore adoption195369f
line 213why, one step back213Improve stream cleanup and termination logic (#775)ehmickybefore adoption5aa90bd
line 214why, one step back214test('Waits for custom streams destroy on process errors', async t => {Wait for streams completion on errors (#722)ehmickybefore adoption195369f
lines 215–217why, one step back215 let waitedForDestroy = false;216 const stream = new Writable({217 destroy: callbackify(async error => {Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf
line 218why, one step back218 await setImmediate();Wait for streams completion on errors (#722)ehmickybefore adoption195369f
lines 219–222why, one step back219 waitedForDestroy = true;220 return error;221 }),222 });Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 223why, one step back223 const {timedOut} = await t.throwsAsync(execa('forever.js', {stdout: [stream, 'pipe'], timeout: 1}));Wait for streams completion on errors (#722)ehmickybefore adoption195369f
lines 224–225why, one step back224 t.true(timedOut);225 t.true(waitedForDestroy);Improve process failure logic (#807)ehmickybefore adoption8ff263c
lines 226–236why, one step back226});227228test('Handles custom streams destroy errors on process success', async t => {229 const error = new Error('test');230 const stream = new Writable({231 destroy(destroyError, done) {232 done(destroyError ?? error);233 },234 });235 const thrownError = await t.throwsAsync(execa('empty.js', {stdout: [stream, 'pipe']}));236 t.is(thrownError, error);Wait for streams completion on errors (#722)ehmickybefore adoption195369f
line 237why, one step back237});Destroy internal streams when process exits early (#770)ehmickybefore adoption8db0b09
lines 238–245why, one step back238239const testStreamEarlyExit = async (t, stream, streamName) => {240 await t.throwsAsync(execa('noop.js', {[streamName]: [stream, 'pipe'], uid: -1}));241 t.true(stream.destroyed);242};243244test('Input streams are canceled on early process exit', testStreamEarlyExit, noopReadable(), 'stdin');245test('Output streams are canceled on early process exit', testStreamEarlyExit, noopWritable(), 'stdout');Improve stream cleanup and termination logic (#775)ehmickybefore adoption5aa90bd
line 246why, one step back246Improve tests related to passing Node.js streams (#788)ehmickybefore adoption81f0b2d
lines 247–249why, one step back247const testInputDuplexStream = async (t, index) => {248 const stream = new PassThrough();249 stream.end(foobarString);Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
line 250why, one step back250 const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, [stream, new Uint8Array()]));Improve tests related to passing Node.js streams (#788)ehmickybefore adoption81f0b2d
lines 251–268why, one step back251 t.is(stdout, foobarString);252};253254test('Can pass Duplex streams to stdin', testInputDuplexStream, 0);255test('Can pass Duplex streams to input stdio[*]', testInputDuplexStream, 3);256257const testOutputDuplexStream = async (t, index) => {258 const stream = new PassThrough();259 const [output] = await Promise.all([260 text(stream),261 execa('noop-fd.js', [`${index}`], getStdio(index, [stream, 'pipe'])),262 ]);263 t.is(output, foobarString);264};265266test('Can pass Duplex streams to stdout', testOutputDuplexStream, 1);267test('Can pass Duplex streams to stderr', testOutputDuplexStream, 2);268test('Can pass Duplex streams to output stdio[*]', testOutputDuplexStream, 3);