node-stream-native.js
This file
Path
test/stdio/node-stream-native.js
185 lines · 9.4 KiB · JavaScript
Last changed by
Upgrade xo (#980)ehmickybefore adoptionccfa412
History of this fileTake it
Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 1–6why, one step back1import {once} from 'node:events';2import {createReadStream, createWriteStream} from 'node:fs';3import {readFile, writeFile, rm} from 'node:fs/promises';4import test from 'ava';5import tempfile from 'tempfile';6import {execa, execaSync} from '../../index.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
line 7why, one step back7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 8–9why, one step back8import {getStdio} from '../helpers/stdio.js';9import {foobarString} from '../helpers/input.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 10–15why, one step back10import {11 noopReadable,12 noopWritable,13 noopDuplex,14 simpleReadable,15} from '../helpers/stream.js';Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
line 16why, one step back16Upgrade xo (#980)ehmickybefore adoptionccfa412
line 17why, one step back17setFixtureDirectory();Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 18–185why, one step back1819const testNoFileStreamSync = async (t, fdNumber, stream) => {20 t.throws(() => {21 execaSync('empty.js', getStdio(fdNumber, stream));22 }, {code: 'ERR_INVALID_ARG_VALUE'});23};2425test('stdin cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 0, noopReadable());26test('stdin cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 0, noopDuplex());27test('stdout cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 1, noopWritable());28test('stdout cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 1, noopDuplex());29test('stderr cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 2, noopWritable());30test('stderr cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 2, noopDuplex());31test('stdio[*] cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 3, noopReadable());32test('stdio[*] cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 3, noopWritable());33test('stdio[*] cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 3, noopDuplex());3435test('input can be a Node.js Readable without a file descriptor', async t => {36 const {stdout} = await execa('stdin.js', {input: simpleReadable()});37 t.is(stdout, foobarString);38});3940test('input cannot be a Node.js Readable without a file descriptor - sync', t => {41 t.throws(() => {42 execaSync('empty.js', {input: simpleReadable()});43 }, {message: 'The `input` option cannot be a Node.js stream with synchronous methods.'});44});4546const testNoFileStream = async (t, fdNumber, stream) => {47 await t.throwsAsync(execa('empty.js', getStdio(fdNumber, stream)), {code: 'ERR_INVALID_ARG_VALUE'});48};4950test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 0, noopReadable());51test('stdin cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 0, noopDuplex());52test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 1, noopWritable());53test('stdout cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 1, noopDuplex());54test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 2, noopWritable());55test('stderr cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 2, noopDuplex());56test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, noopReadable());57test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, noopWritable());58test('stdio[*] cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 3, noopDuplex());5960const createFileReadStream = async () => {61 const filePath = tempfile();62 await writeFile(filePath, 'foobar');63 const stream = createReadStream(filePath);64 await once(stream, 'open');65 return {stream, filePath};66};6768const createFileWriteStream = async () => {69 const filePath = tempfile();70 const stream = createWriteStream(filePath);71 await once(stream, 'open');72 return {stream, filePath};73};7475const assertFileStreamError = async (t, subprocess, stream, filePath) => {76 const cause = new Error('test');77 stream.destroy(cause);7879 const error = await t.throwsAsync(subprocess);80 t.is(error.cause, cause);81 t.is(error.exitCode, 0);82 t.is(error.signal, undefined);8384 await rm(filePath);85};8687const testFileReadable = async (t, fdNumber, execaMethod) => {88 const {stream, filePath} = await createFileReadStream();8990 const fdNumberString = fdNumber === 'input' ? '0' : `${fdNumber}`;91 const {stdout} = await execaMethod('stdin-fd.js', [fdNumberString], getStdio(fdNumber, stream));92 t.is(stdout, 'foobar');9394 await rm(filePath);95};9697test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'input', execa);98test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 0, execa);99test('stdio[*] can be a Node.js Readable with a file descriptor', testFileReadable, 3, execa);100test('stdin can be a Node.js Readable with a file descriptor - sync', testFileReadable, 0, execaSync);101test('stdio[*] can be a Node.js Readable with a file descriptor - sync', testFileReadable, 3, execaSync);102103const testFileReadableError = async (t, fdNumber) => {104 const {stream, filePath} = await createFileReadStream();105106 const fdNumberString = fdNumber === 'input' ? '0' : `${fdNumber}`;107 const subprocess = execa('stdin-fd.js', [fdNumberString], getStdio(fdNumber, stream));108109 await assertFileStreamError(t, subprocess, stream, filePath);110};111112test.serial('input handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 'input');113test.serial('stdin handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 0);114test.serial('stdio[*] handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 3);115116const testFileReadableOpen = async (t, fdNumber, useSingle, execaMethod) => {117 const {stream, filePath} = await createFileReadStream();118 t.deepEqual(stream.eventNames(), []);119120 const stdioOption = useSingle ? stream : [stream, 'pipe'];121 await execaMethod('empty.js', getStdio(fdNumber, stdioOption));122123 t.is(stream.readable, useSingle && fdNumber !== 'input');124 t.deepEqual(stream.eventNames(), []);125126 await rm(filePath);127};128129test('input closes a Node.js Readable with a file descriptor', testFileReadableOpen, 'input', true, execa);130test('stdin leaves open a single Node.js Readable with a file descriptor', testFileReadableOpen, 0, true, execa);131test('stdin closes a combined Node.js Readable with a file descriptor', testFileReadableOpen, 0, false, execa);132test('stdio[*] leaves open a single Node.js Readable with a file descriptor', testFileReadableOpen, 3, true, execa);133test('stdin leaves open a single Node.js Readable with a file descriptor - sync', testFileReadableOpen, 0, true, execaSync);134test('stdio[*] leaves open a single Node.js Readable with a file descriptor - sync', testFileReadableOpen, 3, true, execaSync);135136const testFileWritable = async (t, fdNumber, execaMethod) => {137 const {stream, filePath} = await createFileWriteStream();138139 await execaMethod('noop-fd.js', [`${fdNumber}`, 'foobar'], getStdio(fdNumber, stream));140 t.is(await readFile(filePath, 'utf8'), 'foobar');141142 await rm(filePath);143};144145test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, 1, execa);146test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, 2, execa);147test('stdio[*] can be a Node.js Writable with a file descriptor', testFileWritable, 3, execa);148test('stdout can be a Node.js Writable with a file descriptor - sync', testFileWritable, 1, execaSync);149test('stderr can be a Node.js Writable with a file descriptor - sync', testFileWritable, 2, execaSync);150test('stdio[*] can be a Node.js Writable with a file descriptor - sync', testFileWritable, 3, execaSync);151152const testFileWritableError = async (t, fdNumber) => {153 const {stream, filePath} = await createFileWriteStream();154155 const subprocess = execa('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, stream));156157 await assertFileStreamError(t, subprocess, stream, filePath);158};159160test.serial('stdout handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 1);161test.serial('stderr handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 2);162test.serial('stdio[*] handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 3);163164const testFileWritableOpen = async (t, fdNumber, useSingle, execaMethod) => {165 const {stream, filePath} = await createFileWriteStream();166 t.deepEqual(stream.eventNames(), []);167168 const stdioOption = useSingle ? stream : [stream, 'pipe'];169 await execaMethod('empty.js', getStdio(fdNumber, stdioOption));170171 t.is(stream.writable, useSingle);172 t.deepEqual(stream.eventNames(), []);173174 await rm(filePath);175};176177test('stdout leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 1, true, execa);178test('stdout closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 1, false, execa);179test('stderr leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 2, true, execa);180test('stderr closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 2, false, execa);181test('stdio[*] leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 3, true, execa);182test('stdio[*] closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 3, false, execa);183test('stdout leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 1, true, execaSync);184test('stderr leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 2, true, execaSync);185test('stdio[*] leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 3, true, execaSync);