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
Provenance
- Commit
81f0b2d- Tree
89d80d9- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
932fb26
1 file changed+68 added−30 removed
modified:test/stdio/node-stream.js+68 added-30 removed
1 import {once} from 'node:events';2 import {createReadStream, createWriteStream} from 'node:fs';3 import {readFile, writeFile, rm} from 'node:fs/promises';4-import {Readable, Writable, PassThrough} from 'node:stream';4+import {Readable, Writable, Duplex, PassThrough} from 'node:stream';5+import {text} from 'node:stream/consumers';6 import {setImmediate} from 'node:timers/promises';7 import {callbackify} from 'node:util';8 import test from 'ava';9 import tempfile from 'tempfile';10 import {execa, execaSync} from '../../index.js';11 import {setFixtureDir} from '../helpers/fixtures-dir.js';12 import {getStdio} from '../helpers/stdio.js';13+import {foobarString} from '../helpers/input.js';14 15 setFixtureDir();16 15-const createNoFileReadable = value => {16- const stream = new PassThrough();17- stream.write(value);18- stream.end();19- return stream;20-};17+const noopReadable = () => new Readable({read() {}});18+const noopWritable = () => new Writable({write() {}});19+const noopDuplex = () => new Duplex({read() {}, write() {}});20+const simpleReadable = () => Readable.from([foobarString]);21 22-const testNoFileStreamSync = async (t, index, StreamClass) => {22+const 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 };27 28-test('stdin cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 0, Readable);29-test('stdout cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 1, Writable);30-test('stderr cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 2, Writable);31-test('stdio[*] cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 3, Readable);32-test('stdio[*] cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 3, Writable);28+test('stdin cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 0, noopReadable());29+test('stdin cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 0, noopDuplex());30+test('stdout cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 1, noopWritable());31+test('stdout cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 1, noopDuplex());32+test('stderr cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 2, noopWritable());33+test('stderr cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 2, noopDuplex());34+test('stdio[*] cannot be a Node.js Readable without a file descriptor - sync', testNoFileStreamSync, 3, noopReadable());35+test('stdio[*] cannot be a Node.js Writable without a file descriptor - sync', testNoFileStreamSync, 3, noopWritable());36+test('stdio[*] cannot be a Node.js Duplex without a file descriptor - sync', testNoFileStreamSync, 3, noopDuplex());37 38 test('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 });42 43 test('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 });48 45-const testNoFileStream = async (t, index, StreamClass) => {46- await t.throwsAsync(execa('empty.js', getStdio(index, new StreamClass())), {code: 'ERR_INVALID_ARG_VALUE'});49+const testNoFileStream = async (t, index, stream) => {50+ await t.throwsAsync(execa('empty.js', getStdio(index, stream)), {code: 'ERR_INVALID_ARG_VALUE'});51 };52 49-test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 0, Readable);50-test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 1, Writable);51-test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 2, Writable);52-test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, Readable);53-test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, Writable);53+test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 0, noopReadable());54+test('stdin cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 0, noopDuplex());55+test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 1, noopWritable());56+test('stdout cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 1, noopDuplex());57+test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 2, noopWritable());58+test('stderr cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 2, noopDuplex());59+test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, noopReadable());60+test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, noopWritable());61+test('stdio[*] cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 3, noopDuplex());62 63 const testFileReadable = async (t, index, execaMethod) => {64 const filePath = tempfile();65 await writeFile(filePath, 'foobar');73 unmodified lines139 t.true(timedOut);140 t.true(waitedForDestroy);141 });142 135-const noopReadable = () => new Readable({read() {}});136-const noopWritable = () => new Writable({write() {}});137-143 const testStreamEarlyExit = async (t, stream, streamName) => {144 await t.throwsAsync(execa('noop.js', {[streamName]: [stream, 'pipe'], uid: -1}));145 t.true(stream.destroyed);146 };147 148 test('Input streams are canceled on early process exit', testStreamEarlyExit, noopReadable(), 'stdin');149 test('Output streams are canceled on early process exit', testStreamEarlyExit, noopWritable(), 'stdout');150 151+const 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+};157+158+test('Can pass Duplex streams to stdin', testInputDuplexStream, 0);159+test('Can pass Duplex streams to input stdio[*]', testInputDuplexStream, 3);160+161+const 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+};169+170+test('Can pass Duplex streams to stdout', testOutputDuplexStream, 1);171+test('Can pass Duplex streams to stderr', testOutputDuplexStream, 2);172+test('Can pass Duplex streams to output stdio[*]', testOutputDuplexStream, 3);173+174 test('Handles output streams ends', async t => {175 const stream = noopWritable();176 stream.end();177 await t.throwsAsync(10 unmodified lines188 );189 };190 191 test('Handles input streams aborts', testStreamAbort, noopReadable(), 'stdin');192+test('Handles input Duplex streams aborts', testStreamAbort, noopDuplex(), 'stdin');193 test('Handles output streams aborts', testStreamAbort, noopWritable(), 'stdout');194+test('Handles output Duplex streams aborts', testStreamAbort, noopDuplex(), 'stdout');195 196 const testStreamError = async (t, stream, streamName) => {197 const error = new Error('test');198 stream.destroy(error);3 unmodified lines202 );203 };204 205 test('Handles input streams errors', testStreamError, noopReadable(), 'stdin');206+test('Handles input Duplex streams errors', testStreamError, noopDuplex(), 'stdin');207 test('Handles output streams errors', testStreamError, noopWritable(), 'stdout');208+test('Handles output Duplex streams errors', testStreamError, noopDuplex(), 'stdout');209 178-test('Handles childProcess.stdin end', async t => {179- const stream = noopReadable();210+const 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+};216 217+test('Handles childProcess.stdin end', testChildStreamEnd, noopReadable());218+test('Handles childProcess.stdin Duplex end', testChildStreamEnd, noopDuplex());219+220 const 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 };226 227 test('Handles childProcess.stdin aborts', testChildStreamAbort, noopReadable(), 'stdin');228+test('Handles childProcess.stdin Duplex aborts', testChildStreamAbort, noopDuplex(), 'stdin');229 test('Handles childProcess.stdout aborts', testChildStreamAbort, noopWritable(), 'stdout');230+test('Handles childProcess.stdout Duplex aborts', testChildStreamAbort, noopDuplex(), 'stdout');231 232 const testChildStreamError = async (t, stream, streamName) => {233 const childProcess = execa('forever.js', {[streamName]: [stream, 'pipe']});234 const error = new Error('test');2 unmodified lines237 t.true(stream.destroyed);238 };239 240 test('Handles childProcess.stdin errors', testChildStreamError, noopReadable(), 'stdin');241+test('Handles childProcess.stdin Duplex errors', testChildStreamError, noopDuplex(), 'stdin');242 test('Handles childProcess.stdout errors', testChildStreamError, noopWritable(), 'stdout');243+test('Handles childProcess.stdout Duplex errors', testChildStreamError, noopDuplex(), 'stdout');