Add tests related to streams (#622)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent b369efe · Browse files · 1 file·+80 added−5 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+80 added−5 removed
- tests:+80 added−5 removed
Provenance
- Commit
14485c7- Tree
228bb48- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
b369efe
1 file changed+80 added−5 removed
modified:test/stream.js+80 added-5 removed
1 import {Buffer} from 'node:buffer';2 import {exec} from 'node:child_process';3 import process from 'node:process';4+import {once} from 'node:events';5 import fs from 'node:fs';5-import {readFile} from 'node:fs/promises';6+import {readFile, writeFile, rm} from 'node:fs/promises';7 import {relative} from 'node:path';8 import Stream from 'node:stream';9 import {setTimeout} from 'node:timers/promises';10 import {promisify} from 'node:util';182 unmodified lines193 const {originalMessage} = await t.throwsAsync(execa('stdin.js', {stdin: throwingGenerator()}));194 t.is(originalMessage, 'generator error');195 });196 197+const testNoIterableOutput = async (t, optionName) => {198+ await t.throwsAsync(199+ execa('noop.js', {[optionName]: ['foo', 'bar']}),200+ {code: 'ERR_INVALID_ARG_VALUE'},201+ );202+};203+204+test('stdout option cannot be an iterable', testNoIterableOutput, 'stdout');205+test('stderr option cannot be an iterable', testNoIterableOutput, 'stderr');206+207+const testNoIterableOutputSync = (t, optionName) => {208+ t.throws(() => {209+ execaSync('noop.js', {[optionName]: ['foo', 'bar']});210+ }, {code: 'ERR_INVALID_ARG_VALUE'});211+};212+213+test('stdout option cannot be an iterable - sync', testNoIterableOutputSync, 'stdout');214+test('stderr option cannot be an iterable - sync', testNoIterableOutputSync, 'stderr');215+216 const testWritableStreamError = async (t, streamName) => {217 const writableStream = new WritableStream({218 start(controller) {219 controller.error(new Error('foobar'));27 unmodified lines247 const {stdout} = await execa('stdin.js', {input: 'testing12'});248 t.is(stdout, 'testing12');249 });250 231-test('input can be a Node.js Readable', async t => {251+const createNoFileReadable = value => {252 const stream = new Stream.PassThrough();233- stream.write('howdy');253+ stream.write(value);254 stream.end();255+ return stream;256+};257+258+test('input can be a Node.js Readable without a file descriptor', async t => {235- const {stdout} = await execa('stdin.js', {input: stream});236- t.is(stdout, 'howdy');259+ const {stdout} = await execa('stdin.js', {input: createNoFileReadable('foobar')});260+ t.is(stdout, 'foobar');261 });262 263+const testNoFileStream = async (t, optionName, StreamClass) => {264+ await t.throwsAsync(execa('noop.js', {[optionName]: new StreamClass()}), {code: 'ERR_INVALID_ARG_VALUE'});265+};266+267+test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 'stdin', Stream.Readable);268+test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 'stdout', Stream.Writable);269+test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 'stderr', Stream.Writable);270+271+const createFileReadable = async value => {272+ const filePath = tempfile();273+ await writeFile(filePath, value);274+ const stream = fs.createReadStream(filePath);275+ await once(stream, 'open');276+ const cleanup = () => rm(filePath);277+ return {stream, cleanup};278+};279+280+const testFileReadable = async (t, optionName) => {281+ const {stream, cleanup} = await createFileReadable('foobar');282+ try {283+ const {stdout} = await execa('stdin.js', {[optionName]: stream});284+ t.is(stdout, 'foobar');285+ } finally {286+ await cleanup();287+ }288+};289+290+test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'input');291+test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 'stdin');292+293+const createFileWritable = async () => {294+ const filePath = tempfile();295+ const stream = fs.createWriteStream(filePath);296+ await once(stream, 'open');297+ const cleanup = () => rm(filePath);298+ return {stream, filePath, cleanup};299+};300+301+const testFileWritable = async (t, optionName, fixtureName) => {302+ const {stream, filePath, cleanup} = await createFileWritable();303+ try {304+ await execa(fixtureName, ['foobar'], {[optionName]: stream});305+ t.is(await readFile(filePath, 'utf8'), 'foobar\n');306+ } finally {307+ await cleanup();308+ }309+};310+311+test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, 'stdout', 'noop.js');312+test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, 'stderr', 'noop-err.js');313+314 test('input option cannot be a Node.js Readable when stdin is set', t => {315 t.throws(() => {316 execa('stdin.js', {input: new Stream.PassThrough(), stdin: 'ignore'});317 }, {message: /`input` and `stdin` options/});445 unmodified lines