Skip to content
Myr

transform.js

This file

Path

test/stdio/transform.js

204 lines · 7.8 KiB · JavaScript

Last changed by

Split files (#837)ehmickybefore adoption672c2d7

History of this file

Take it

Split files (#837)ehmickybefore adoption672c2d7

lines 1–204why, one step back
1import {Buffer} from 'node:buffer';
2import {once} from 'node:events';3import {setTimeout, scheduler} from 'node:timers/promises';4import {getDefaultHighWaterMark} from 'node:stream';5import test from 'ava';6import {getStreamAsArray} from 'get-stream';7import {execa} from '../../index.js';8import {foobarString} from '../helpers/input.js';9import {10 noopGenerator,11 getOutputsGenerator,12 getOutputGenerator,13 infiniteGenerator,14 outputObjectGenerator,15 convertTransformToFinal,16 noYieldGenerator,17} from '../helpers/generator.js';18import {setFixtureDir} from '../helpers/fixtures-dir.js';1920setFixtureDir();2122const testGeneratorFinal = async (t, fixtureName) => {23 const {stdout} = await execa(fixtureName, {stdout: convertTransformToFinal(getOutputGenerator(foobarString), true)});24 t.is(stdout, foobarString);25};2627test('Generators "final" can be used', testGeneratorFinal, 'noop.js');28test('Generators "final" is used even on empty streams', testGeneratorFinal, 'empty.js');2930const repeatCount = getDefaultHighWaterMark() * 3;3132const writerGenerator = function * () {33 for (let index = 0; index < repeatCount; index += 1) {34 yield '\n';35 }36};3738const getLengthGenerator = function * (t, chunk) {39 t.is(chunk.length, 1);40 yield chunk;41};4243const testHighWaterMark = async (t, passThrough, binary, objectMode) => {44 const {stdout} = await execa('noop.js', {45 stdout: [46 ...(objectMode ? [outputObjectGenerator] : []),47 writerGenerator,48 ...(passThrough ? [noopGenerator(false, binary)] : []),49 {transform: getLengthGenerator.bind(undefined, t), binary: true, objectMode: true},50 ],51 });52 t.is(stdout.length, repeatCount);53 t.true(stdout.every(chunk => chunk.toString() === '\n'));54};5556test('Synchronous yields are not buffered, no passThrough', testHighWaterMark, false, false, false);57test('Synchronous yields are not buffered, line-wise passThrough', testHighWaterMark, true, false, false);58test('Synchronous yields are not buffered, binary passThrough', testHighWaterMark, true, true, false);59test('Synchronous yields are not buffered, objectMode as input but not output', testHighWaterMark, false, false, true);6061const testNoYield = async (t, objectMode, final, output) => {62 const {stdout} = await execa('noop.js', {stdout: convertTransformToFinal(noYieldGenerator(objectMode), final)});63 t.deepEqual(stdout, output);64};6566test('Generator can filter "transform" by not calling yield', testNoYield, false, false, '');67test('Generator can filter "transform" by not calling yield, objectMode', testNoYield, true, false, []);68test('Generator can filter "final" by not calling yield', testNoYield, false, false, '');69test('Generator can filter "final" by not calling yield, objectMode', testNoYield, true, false, []);7071const prefix = '> ';72const suffix = ' <';7374const multipleYieldGenerator = async function * (line = foobarString) {75 yield prefix;76 await scheduler.yield();77 yield line;78 await scheduler.yield();79 yield suffix;80};8182const testMultipleYields = async (t, final) => {83 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: convertTransformToFinal(multipleYieldGenerator, final)});84 t.is(stdout, `${prefix}${foobarString}${suffix}`);85};8687test('Generator can yield "transform" multiple times at different moments', testMultipleYields, false);88test('Generator can yield "final" multiple times at different moments', testMultipleYields, true);8990const partsPerChunk = 4;91const chunksPerCall = 10;92const callCount = 5;93const fullString = '\n'.repeat(getDefaultHighWaterMark(false) / partsPerChunk);9495const yieldFullStrings = function * () {96 yield * Array.from({length: partsPerChunk * chunksPerCall}).fill(fullString);97};9899const manyYieldGenerator = async function * () {100 for (let index = 0; index < callCount; index += 1) {101 yield * yieldFullStrings();102 // eslint-disable-next-line no-await-in-loop103 await scheduler.yield();104 }105};106107const testManyYields = async (t, final) => {108 const childProcess = execa('noop.js', {stdout: convertTransformToFinal(manyYieldGenerator, final), buffer: false});109 const [chunks] = await Promise.all([getStreamAsArray(childProcess.stdout), childProcess]);110 const expectedChunk = Buffer.alloc(getDefaultHighWaterMark(false) * chunksPerCall).fill('\n');111 t.deepEqual(chunks, Array.from({length: callCount}).fill(expectedChunk));112};113114test('Generator "transform" yields are sent right away', testManyYields, false);115test('Generator "final" yields are sent right away', testManyYields, true);116117const maxBuffer = 10;118119test('Generators take "maxBuffer" into account', async t => {120 const bigString = '.'.repeat(maxBuffer);121 const {stdout} = await execa('noop.js', {maxBuffer, stdout: getOutputGenerator(bigString, false)});122 t.is(stdout, bigString);123124 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputGenerator(`${bigString}.`, false)}));125});126127test('Generators take "maxBuffer" into account, objectMode', async t => {128 const bigArray = Array.from({length: maxBuffer}).fill('.');129 const {stdout} = await execa('noop.js', {maxBuffer, stdout: getOutputsGenerator(bigArray, true)});130 t.is(stdout.length, maxBuffer);131132 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputsGenerator([...bigArray, ''], true)}));133});134135const timeoutGenerator = async function * (timeout) {136 await setTimeout(timeout);137 yield foobarString;138};139140const testAsyncGenerators = async (t, final) => {141 const {stdout} = await execa('noop.js', {142 maxBuffer,143 stdout: convertTransformToFinal(timeoutGenerator.bind(undefined, 1e2), final),144 });145 t.is(stdout, foobarString);146};147148test('Generators "transform" is awaited on success', testAsyncGenerators, false);149test('Generators "final" is awaited on success', testAsyncGenerators, true);150151// eslint-disable-next-line require-yield152const throwingGenerator = function * () {153 throw new Error('Generator error');154};155156const GENERATOR_ERROR_REGEXP = /Generator error/;157158const testThrowingGenerator = async (t, final) => {159 await t.throwsAsync(160 execa('noop-fd.js', ['1', foobarString], {stdout: convertTransformToFinal(throwingGenerator, final)}),161 {message: GENERATOR_ERROR_REGEXP},162 );163};164165test('Generators "transform" errors make process fail', testThrowingGenerator, false);166test('Generators "final" errors make process fail', testThrowingGenerator, true);167168test('Generators errors make process fail even when other output generators do not throw', async t => {169 await t.throwsAsync(170 execa('noop-fd.js', ['1', foobarString], {stdout: [noopGenerator(false), throwingGenerator, noopGenerator(false)]}),171 {message: GENERATOR_ERROR_REGEXP},172 );173});174175test('Generators errors make process fail even when other input generators do not throw', async t => {176 const childProcess = execa('stdin-fd.js', ['0'], {stdin: [noopGenerator(false), throwingGenerator, noopGenerator(false)]});177 childProcess.stdin.write('foobar\n');178 await t.throwsAsync(childProcess, {message: GENERATOR_ERROR_REGEXP});179});180181const testGeneratorCancel = async (t, error) => {182 const childProcess = execa('noop.js', {stdout: infiniteGenerator});183 await once(childProcess.stdout, 'data');184 childProcess.stdout.destroy(error);185 await (error === undefined ? t.notThrowsAsync(childProcess) : t.throwsAsync(childProcess));186};187188test('Running generators are canceled on process abort', testGeneratorCancel, undefined);189test('Running generators are canceled on process error', testGeneratorCancel, new Error('test'));190191const testGeneratorDestroy = async (t, transform) => {192 const childProcess = execa('forever.js', {stdout: transform});193 const error = new Error('test');194 childProcess.stdout.destroy(error);195 childProcess.kill();196 t.is(await t.throwsAsync(childProcess), error);197};198199test('Generators are destroyed on process error, sync', testGeneratorDestroy, noopGenerator(false));200test('Generators are destroyed on process error, async', testGeneratorDestroy, infiniteGenerator);201202test('Generators are destroyed on early process exit', async t => {203 await t.throwsAsync(execa('noop.js', {stdout: infiniteGenerator, uid: -1}));204});