Skip to content
Myr

node-stream-custom.js

This file

Path

test/stdio/node-stream-custom.js

158 lines · 6.3 KiB · JavaScript

Last changed by

Upgrade xo (#980)ehmickybefore adoptionccfa412

History of this file

Take it

Split files into smaller ones (#976)ehmickybefore adoptiond7c6853

lines 1–9why, one step back
1import {createReadStream, createWriteStream} from 'node:fs';
2import {readFile, writeFile, rm} from 'node:fs/promises';3import {Writable, PassThrough} from 'node:stream';4import {text} from 'node:stream/consumers';5import {setImmediate} from 'node:timers/promises';6import {callbackify} from 'node:util';7import test from 'ava';8import tempfile from 'tempfile';9import {execa, execaSync} from '../../index.js';

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Split files into smaller ones (#976)ehmickybefore adoptiond7c6853

lines 11–14why, one step back
11import {getStdio} from '../helpers/stdio.js';
12import {foobarString} from '../helpers/input.js';13import {noopReadable, noopWritable} from '../helpers/stream.js';14

Upgrade xo (#980)ehmickybefore adoptionccfa412

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

Split files into smaller ones (#976)ehmickybefore adoptiond7c6853

lines 16–158why, one step back
16
17const testLazyFileReadable = async (t, fdNumber) => {18 const filePath = tempfile();19 await writeFile(filePath, 'foobar');20 const stream = createReadStream(filePath);2122 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, 'pipe']));23 t.is(stdout, 'foobar');2425 await rm(filePath);26};2728test('stdin can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 0);29test('stdio[*] can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 3);3031const testLazyFileReadableSync = (t, fdNumber) => {32 t.throws(() => {33 execaSync('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [noopReadable(), 'pipe']));34 }, {message: /cannot both be an array and include a stream/});35};3637test('stdin cannot be [Readable, "pipe"] without a file descriptor, sync', testLazyFileReadableSync, 0);38test('stdio[*] cannot be [Readable, "pipe"] without a file descriptor, sync', testLazyFileReadableSync, 3);3940const testLazyFileWritable = async (t, fdNumber) => {41 const filePath = tempfile();42 const stream = createWriteStream(filePath);4344 await execa('noop-fd.js', [`${fdNumber}`, 'foobar'], getStdio(fdNumber, [stream, 'pipe']));45 t.is(await readFile(filePath, 'utf8'), 'foobar');4647 await rm(filePath);48};4950test('stdout can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 1);51test('stderr can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 2);52test('stdio[*] can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 3);5354const testLazyFileWritableSync = (t, fdNumber) => {55 t.throws(() => {56 execaSync('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, [noopWritable(), 'pipe']));57 }, {message: /cannot both be an array and include a stream/});58};5960test('stdout cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 1);61test('stderr cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 2);62test('stdio[*] cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 3);6364test('Waits for custom streams destroy on subprocess errors', async t => {65 let waitedForDestroy = false;66 const stream = new Writable({67 destroy: callbackify(async error => {68 await setImmediate();69 waitedForDestroy = true;70 return error;71 }),72 });73 const {timedOut} = await t.throwsAsync(execa('forever.js', {stdout: [stream, 'pipe'], timeout: 1}));74 t.true(timedOut);75 t.true(waitedForDestroy);76});7778test('Handles custom streams destroy errors on subprocess success', async t => {79 const cause = new Error('test');80 const stream = new Writable({81 destroy(destroyError, done) {82 done(destroyError ?? cause);83 },84 });85 const error = await t.throwsAsync(execa('empty.js', {stdout: [stream, 'pipe']}));86 t.is(error.cause, cause);87});8889const testStreamEarlyExit = async (t, stream, streamName) => {90 await t.throwsAsync(execa('noop.js', {[streamName]: [stream, 'pipe'], uid: -1}));91 t.true(stream.destroyed);92};9394test('Input streams are canceled on early subprocess exit', testStreamEarlyExit, noopReadable(), 'stdin');95test('Output streams are canceled on early subprocess exit', testStreamEarlyExit, noopWritable(), 'stdout');9697const testInputDuplexStream = async (t, fdNumber) => {98 const stream = new PassThrough();99 stream.end(foobarString);100 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, new Uint8Array()]));101 t.is(stdout, foobarString);102};103104test('Can pass Duplex streams to stdin', testInputDuplexStream, 0);105test('Can pass Duplex streams to input stdio[*]', testInputDuplexStream, 3);106107const testOutputDuplexStream = async (t, fdNumber) => {108 const stream = new PassThrough();109 const [output] = await Promise.all([110 text(stream),111 execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, 'pipe'])),112 ]);113 t.is(output, foobarString);114};115116test('Can pass Duplex streams to stdout', testOutputDuplexStream, 1);117test('Can pass Duplex streams to stderr', testOutputDuplexStream, 2);118test('Can pass Duplex streams to output stdio[*]', testOutputDuplexStream, 3);119120const testInputStreamAbort = async (t, fdNumber) => {121 const stream = new PassThrough();122 stream.destroy();123124 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, new Uint8Array()]));125 await subprocess;126 t.true(subprocess.stdio[fdNumber].writableEnded);127};128129test('subprocess.stdin is ended when an input stream aborts', testInputStreamAbort, 0);130test('subprocess.stdio[*] is ended when an input stream aborts', testInputStreamAbort, 3);131132const testInputStreamError = async (t, fdNumber) => {133 const stream = new PassThrough();134 const cause = new Error(foobarString);135 stream.destroy(cause);136137 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, new Uint8Array()]));138 t.like(await t.throwsAsync(subprocess), {cause});139 t.true(subprocess.stdio[fdNumber].writableEnded);140};141142test('subprocess.stdin is ended when an input stream errors', testInputStreamError, 0);143test('subprocess.stdio[*] is ended when an input stream errors', testInputStreamError, 3);144145const testOutputStreamError = async (t, fdNumber) => {146 const stream = new PassThrough();147 const cause = new Error(foobarString);148 stream.destroy(cause);149150 const subprocess = execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, 'pipe']));151 t.like(await t.throwsAsync(subprocess), {cause});152 t.true(subprocess.stdio[fdNumber].readableAborted);153 t.is(subprocess.stdio[fdNumber].errored, null);154};155156test('subprocess.stdout is aborted when an output stream errors', testOutputStreamError, 1);157test('subprocess.stderr is aborted when an output stream errors', testOutputStreamError, 2);158test('subprocess.stdio[*] is aborted when an output stream errors', testOutputStreamError, 3);