Skip to content
Myr

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

1 file+80 added−5 removed

modified:test/stream.js+80 added-5 removed
1import {Buffer} from 'node:buffer';2import {exec} from 'node:child_process';3import process from 'node:process';4import {once} from 'node:events';5import fs from 'node:fs';5import {readFile} from 'node:fs/promises';6import {readFile, writeFile, rm} from 'node:fs/promises';7import {relative} from 'node:path';8import Stream from 'node:stream';9import {setTimeout} from 'node:timers/promises';10import {promisify} from 'node:util';
182 unmodified lines
193 const {originalMessage} = await t.throwsAsync(execa('stdin.js', {stdin: throwingGenerator()}));194 t.is(originalMessage, 'generator error');195});196197const testNoIterableOutput = async (t, optionName) => {198 await t.throwsAsync(199 execa('noop.js', {[optionName]: ['foo', 'bar']}),200 {code: 'ERR_INVALID_ARG_VALUE'},201 );202};203204test('stdout option cannot be an iterable', testNoIterableOutput, 'stdout');205test('stderr option cannot be an iterable', testNoIterableOutput, 'stderr');206207const testNoIterableOutputSync = (t, optionName) => {208 t.throws(() => {209 execaSync('noop.js', {[optionName]: ['foo', 'bar']});210 }, {code: 'ERR_INVALID_ARG_VALUE'});211};212213test('stdout option cannot be an iterable - sync', testNoIterableOutputSync, 'stdout');214test('stderr option cannot be an iterable - sync', testNoIterableOutputSync, 'stderr');215216const testWritableStreamError = async (t, streamName) => {217 const writableStream = new WritableStream({218 start(controller) {219 controller.error(new Error('foobar'));
27 unmodified lines
247 const {stdout} = await execa('stdin.js', {input: 'testing12'});248 t.is(stdout, 'testing12');249});250231test('input can be a Node.js Readable', async t => {251const createNoFileReadable = value => {252 const stream = new Stream.PassThrough();233 stream.write('howdy');253 stream.write(value);254 stream.end();255 return stream;256};257258test('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});262263const testNoFileStream = async (t, optionName, StreamClass) => {264 await t.throwsAsync(execa('noop.js', {[optionName]: new StreamClass()}), {code: 'ERR_INVALID_ARG_VALUE'});265};266267test('stdin cannot be a Node.js Readable without a file descriptor', testNoFileStream, 'stdin', Stream.Readable);268test('stdout cannot be a Node.js Writable without a file descriptor', testNoFileStream, 'stdout', Stream.Writable);269test('stderr cannot be a Node.js Writable without a file descriptor', testNoFileStream, 'stderr', Stream.Writable);270271const 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};279280const 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};289290test('input can be a Node.js Readable with a file descriptor', testFileReadable, 'input');291test('stdin can be a Node.js Readable with a file descriptor', testFileReadable, 'stdin');292293const 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};300301const 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};310311test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, 'stdout', 'noop.js');312test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, 'stderr', 'noop-err.js');313314test('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