node-stream-custom.js
This file
Path
test/stdio/node-stream-custom.js
160 lines · 6.5 KiB · JavaScript
Last changed by
Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
History of this fileTake it
Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 1–9why, one step back1import {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 back10import {setFixtureDirectory} from '../helpers/fixtures-directory.js';Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 11–13why, one step back11import {getStdio} from '../helpers/stdio.js';12import {foobarString} from '../helpers/input.js';13import {noopReadable, noopWritable} from '../helpers/stream.js';Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 14why, one step back14import {getEarlyErrorSubprocess, expectedEarlyError} from '../helpers/early-error.js';Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
line 15why, one step back15Upgrade xo (#980)ehmickybefore adoptionccfa412
line 16why, one step back16setFixtureDirectory();Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 17–90why, one step back1718const testLazyFileReadable = async (t, fdNumber) => {19 const filePath = tempfile();20 await writeFile(filePath, 'foobar');21 const stream = createReadStream(filePath);2223 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, 'pipe']));24 t.is(stdout, 'foobar');2526 await rm(filePath);27};2829test('stdin can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 0);30test('stdio[*] can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 3);3132const testLazyFileReadableSync = (t, fdNumber) => {33 t.throws(() => {34 execaSync('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [noopReadable(), 'pipe']));35 }, {message: /cannot both be an array and include a stream/});36};3738test('stdin cannot be [Readable, "pipe"] without a file descriptor, sync', testLazyFileReadableSync, 0);39test('stdio[*] cannot be [Readable, "pipe"] without a file descriptor, sync', testLazyFileReadableSync, 3);4041const testLazyFileWritable = async (t, fdNumber) => {42 const filePath = tempfile();43 const stream = createWriteStream(filePath);4445 await execa('noop-fd.js', [`${fdNumber}`, 'foobar'], getStdio(fdNumber, [stream, 'pipe']));46 t.is(await readFile(filePath, 'utf8'), 'foobar');4748 await rm(filePath);49};5051test('stdout can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 1);52test('stderr can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 2);53test('stdio[*] can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, 3);5455const testLazyFileWritableSync = (t, fdNumber) => {56 t.throws(() => {57 execaSync('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, [noopWritable(), 'pipe']));58 }, {message: /cannot both be an array and include a stream/});59};6061test('stdout cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 1);62test('stderr cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 2);63test('stdio[*] cannot be [Writable, "pipe"] without a file descriptor, sync', testLazyFileWritableSync, 3);6465test('Waits for custom streams destroy on subprocess errors', async t => {66 let waitedForDestroy = false;67 const stream = new Writable({68 destroy: callbackify(async error => {69 await setImmediate();70 waitedForDestroy = true;71 return error;72 }),73 });74 const {timedOut} = await t.throwsAsync(execa('forever.js', {stdout: [stream, 'pipe'], timeout: 1}));75 t.true(timedOut);76 t.true(waitedForDestroy);77});7879test('Handles custom streams destroy errors on subprocess success', async t => {80 const cause = new Error('test');81 const stream = new Writable({82 destroy(destroyError, done) {83 done(destroyError ?? cause);84 },85 });86 const error = await t.throwsAsync(execa('empty.js', {stdout: [stream, 'pipe']}));87 t.is(error.cause, cause);88});8990const testStreamEarlyExit = async (t, stream, streamName) => {Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 91–92why, one step back91 const error = await t.throwsAsync(getEarlyErrorSubprocess({[streamName]: [stream, 'pipe']}));92 t.like(error, expectedEarlyError);Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
lines 93–160why, one step back93 t.true(stream.destroyed);94};9596test('Input streams are canceled on early subprocess exit', testStreamEarlyExit, noopReadable(), 'stdin');97test('Output streams are canceled on early subprocess exit', testStreamEarlyExit, noopWritable(), 'stdout');9899const testInputDuplexStream = async (t, fdNumber) => {100 const stream = new PassThrough();101 stream.end(foobarString);102 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, new Uint8Array()]));103 t.is(stdout, foobarString);104};105106test('Can pass Duplex streams to stdin', testInputDuplexStream, 0);107test('Can pass Duplex streams to input stdio[*]', testInputDuplexStream, 3);108109const testOutputDuplexStream = async (t, fdNumber) => {110 const stream = new PassThrough();111 const [output] = await Promise.all([112 text(stream),113 execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, 'pipe'])),114 ]);115 t.is(output, foobarString);116};117118test('Can pass Duplex streams to stdout', testOutputDuplexStream, 1);119test('Can pass Duplex streams to stderr', testOutputDuplexStream, 2);120test('Can pass Duplex streams to output stdio[*]', testOutputDuplexStream, 3);121122const testInputStreamAbort = async (t, fdNumber) => {123 const stream = new PassThrough();124 stream.destroy();125126 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, new Uint8Array()]));127 await subprocess;128 t.true(subprocess.stdio[fdNumber].writableEnded);129};130131test('subprocess.stdin is ended when an input stream aborts', testInputStreamAbort, 0);132test('subprocess.stdio[*] is ended when an input stream aborts', testInputStreamAbort, 3);133134const testInputStreamError = async (t, fdNumber) => {135 const stream = new PassThrough();136 const cause = new Error(foobarString);137 stream.destroy(cause);138139 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, new Uint8Array()]));140 t.like(await t.throwsAsync(subprocess), {cause});141 t.true(subprocess.stdio[fdNumber].writableEnded);142};143144test('subprocess.stdin is ended when an input stream errors', testInputStreamError, 0);145test('subprocess.stdio[*] is ended when an input stream errors', testInputStreamError, 3);146147const testOutputStreamError = async (t, fdNumber) => {148 const stream = new PassThrough();149 const cause = new Error(foobarString);150 stream.destroy(cause);151152 const subprocess = execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, 'pipe']));153 t.like(await t.throwsAsync(subprocess), {cause});154 t.true(subprocess.stdio[fdNumber].readableAborted);155 t.is(subprocess.stdio[fdNumber].errored, null);156};157158test('subprocess.stdout is aborted when an output stream errors', testOutputStreamError, 1);159test('subprocess.stderr is aborted when an output stream errors', testOutputStreamError, 2);160test('subprocess.stdio[*] is aborted when an output stream errors', testOutputStreamError, 3);