Skip to content
Myr

Improve tests related to passing Node.js streams (#788)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 932fb26 · Browse files · 1 file+68 added−30 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+68 added−30 removed

  • tests:+68 added−30 removed

1 file+68 added−30 removed

modified:test/stdio/node-stream.js+68 added-30 removed
1import {once} from 'node:events';2import {createReadStream, createWriteStream} from 'node:fs';3import {readFile, writeFile, rm} from 'node:fs/promises';4import {Readable, Writable, PassThrough} from 'node:stream';4import {Readable, Writable, Duplex, PassThrough} from 'node:stream';5import {text} from 'node:stream/consumers';6import {setImmediate} from 'node:timers/promises';7import {callbackify} from 'node:util';8import test from 'ava';9import tempfile from 'tempfile';10import {execa, execaSync} from '../../index.js';11import {setFixtureDir} from '../helpers/fixtures-dir.js';12import {getStdio} from '../helpers/stdio.js';13import {foobarString} from '../helpers/input.js';1415setFixtureDir();1615const createNoFileReadable = value => {16 const stream = new PassThrough();17 stream.write(value);18 stream.end();19 return stream;20};17const noopReadable = () => new Readable({read() {}});18const noopWritable = () => new Writable({write() {}});19const noopDuplex = () => new Duplex({read() {}, write() {}});20const simpleReadable = () => Readable.from([foobarString]);2122const testNoFileStreamSync = async (t, index, StreamClass) => {22const testNoFileStreamSync = async (t, index, stream) => {23 t.throws(() => {24 execaSync('empty.js', getStdio(index, new StreamClass()));24 execaSync('empty.js', getStdio(index, stream));25 }, {code: 'ERR_INVALID_ARG_VALUE'});26};2728test('stdin cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 0, Readable);29test('stdout cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 1, Writable);30test('stderr cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 2, Writable);31test('stdio[*] cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 3, Readable);32test('stdio[*] cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 3, Writable);28test('stdin cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 0, noopReadable());29test('stdin cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 0, noopDuplex());30test('stdout cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 1, noopWritable());31test('stdout cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 1, noopDuplex());32test('stderr cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 2, noopWritable());33test('stderr cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 2, noopDuplex());34test('stdio[*] cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 3, noopReadable());35test('stdio[*] cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 3, noopWritable());36test('stdio[*] cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 3, noopDuplex());3738test('input can be a Node.js Readable without a file descriptor', async t => {35 const {stdout} = await execa('stdin.js', {input: createNoFileReadable('foobar')});36 t.is(stdout, 'foobar');39 const {stdout} = await execa('stdin.js', {input: simpleReadable()});40 t.is(stdout, foobarString);41});4243test('input cannot be a Node.js Readable without a file descriptor - sync', t => {44 t.throws(() => {41 execaSync('empty.js', {input: createNoFileReadable('foobar')});45 execaSync('empty.js', {input: simpleReadable()});46 }, {message: 'The `input` option cannot be a Node.js stream in sync mode.'});47});4845const testNoFileStream = async (t, index, StreamClass) => {46 await t.throwsAsync(execa('empty.js', getStdio(index, new StreamClass())), {code: 'ERR_INVALID_ARG_VALUE'});49const testNoFileStream = async (t, index, stream) => {50 await t.throwsAsync(execa('empty.js', getStdio(index, stream)), {code: 'ERR_INVALID_ARG_VALUE'});51};5249test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 0, Readable);50test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 1, Writable);51test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 2, Writable);52test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, Readable);53test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, Writable);53test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 0, noopReadable());54test('stdin cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 0, noopDuplex());55test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 1, noopWritable());56test('stdout cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 1, noopDuplex());57test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 2, noopWritable());58test('stderr cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 2, noopDuplex());59test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, noopReadable());60test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, noopWritable());61test('stdio[*] cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 3, noopDuplex());6263const testFileReadable = async (t, index, execaMethod) => {64 const filePath = tempfile();65 await writeFile(filePath, 'foobar');
73 unmodified lines
139 t.true(timedOut);140 t.true(waitedForDestroy);141});142135const noopReadable = () => new Readable({read() {}});136const noopWritable = () => new Writable({write() {}});137143const testStreamEarlyExit = async (t, stream, streamName) => {144 await t.throwsAsync(execa('noop.js', {[streamName]: [stream, 'pipe'], uid: -1}));145 t.true(stream.destroyed);146};147148test('Input streams are canceled on early process exit', testStreamEarlyExit, noopReadable(), 'stdin');149test('Output streams are canceled on early process exit', testStreamEarlyExit, noopWritable(), 'stdout');150151const testInputDuplexStream = async (t, index) => {152 const stream = new PassThrough();153 stream.end(foobarString);154 const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, [stream, 'pipe']));155 t.is(stdout, foobarString);156};157158test('Can pass Duplex streams to stdin', testInputDuplexStream, 0);159test('Can pass Duplex streams to input stdio[*]', testInputDuplexStream, 3);160161const testOutputDuplexStream = async (t, index) => {162 const stream = new PassThrough();163 const [output] = await Promise.all([164 text(stream),165 execa('noop-fd.js', [`${index}`], getStdio(index, [stream, 'pipe'])),166 ]);167 t.is(output, foobarString);168};169170test('Can pass Duplex streams to stdout', testOutputDuplexStream, 1);171test('Can pass Duplex streams to stderr', testOutputDuplexStream, 2);172test('Can pass Duplex streams to output stdio[*]', testOutputDuplexStream, 3);173174test('Handles output streams ends', async t => {175 const stream = noopWritable();176 stream.end();177 await t.throwsAsync(
10 unmodified lines
188 );189};190191test('Handles input streams aborts', testStreamAbort, noopReadable(), 'stdin');192test('Handles input Duplex streams aborts', testStreamAbort, noopDuplex(), 'stdin');193test('Handles output streams aborts', testStreamAbort, noopWritable(), 'stdout');194test('Handles output Duplex streams aborts', testStreamAbort, noopDuplex(), 'stdout');195196const testStreamError = async (t, stream, streamName) => {197 const error = new Error('test');198 stream.destroy(error);
3 unmodified lines
202 );203};204205test('Handles input streams errors', testStreamError, noopReadable(), 'stdin');206test('Handles input Duplex streams errors', testStreamError, noopDuplex(), 'stdin');207test('Handles output streams errors', testStreamError, noopWritable(), 'stdout');208test('Handles output Duplex streams errors', testStreamError, noopDuplex(), 'stdout');209178test('Handles childProcess.stdin end', async t => {179 const stream = noopReadable();210const testChildStreamEnd = async (t, stream) => {211 const childProcess = execa('forever.js', {stdin: [stream, 'pipe']});212 childProcess.stdin.end();213 await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});214 t.true(stream.destroyed);184});215};216217test('Handles childProcess.stdin end', testChildStreamEnd, noopReadable());218test('Handles childProcess.stdin Duplex end', testChildStreamEnd, noopDuplex());219220const testChildStreamAbort = async (t, stream, streamName) => {221 const childProcess = execa('forever.js', {[streamName]: [stream, 'pipe']});222 childProcess[streamName].destroy();223 await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});224 t.true(stream.destroyed);225};226227test('Handles childProcess.stdin aborts', testChildStreamAbort, noopReadable(), 'stdin');228test('Handles childProcess.stdin Duplex aborts', testChildStreamAbort, noopDuplex(), 'stdin');229test('Handles childProcess.stdout aborts', testChildStreamAbort, noopWritable(), 'stdout');230test('Handles childProcess.stdout Duplex aborts', testChildStreamAbort, noopDuplex(), 'stdout');231232const testChildStreamError = async (t, stream, streamName) => {233 const childProcess = execa('forever.js', {[streamName]: [stream, 'pipe']});234 const error = new Error('test');
2 unmodified lines
237 t.true(stream.destroyed);238};239240test('Handles childProcess.stdin errors', testChildStreamError, noopReadable(), 'stdin');241test('Handles childProcess.stdin Duplex errors', testChildStreamError, noopDuplex(), 'stdin');242test('Handles childProcess.stdout errors', testChildStreamError, noopWritable(), 'stdout');243test('Handles childProcess.stdout Duplex errors', testChildStreamError, noopDuplex(), 'stdout');