Skip to content
Myr

transform.js

This file

Path

test/stdio/transform.js

224 lines · 8.6 KiB · JavaScript

Last changed by

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

History of this file

Take it

Split files (#837)ehmickybefore adoption672c2d7

lines 1–9why, one step back
1import {Buffer} from 'node:buffer';
2import {once} from 'node:events';3import {setTimeout, scheduler} from 'node:timers/promises';4import test from 'ava';5import {getStreamAsArray} from 'get-stream';6import {execa} from '../../index.js';7import {foobarString} from '../helpers/input.js';8import {9 noopGenerator,

Allow using a `final` function without a `transform` (#916)ehmickybefore adoptionf8af153

lines 10–11why, one step back
10 identityGenerator,
11 identityAsyncGenerator,

Split files (#837)ehmickybefore adoption672c2d7

lines 12–17why, one step back
12 getOutputsGenerator,
13 getOutputGenerator,14 infiniteGenerator,15 outputObjectGenerator,16 convertTransformToFinal,17 noYieldGenerator,

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 18–19why, one step back
18 throwingGenerator,
19 GENERATOR_ERROR_REGEXP,

Split files (#837)ehmickybefore adoption672c2d7

line 20why, one step back
20} from '../helpers/generator.js';

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 21why, one step back
21import {defaultHighWaterMark} from '../helpers/stream.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 22–32why, one step back
22import {setFixtureDir} from '../helpers/fixtures-dir.js';
2324setFixtureDir();2526const testGeneratorFinal = async (t, fixtureName) => {27 const {stdout} = await execa(fixtureName, {stdout: convertTransformToFinal(getOutputGenerator(foobarString), true)});28 t.is(stdout, foobarString);29};3031test('Generators "final" can be used', testGeneratorFinal, 'noop.js');32test('Generators "final" is used even on empty streams', testGeneratorFinal, 'empty.js');

Allow using a `final` function without a `transform` (#916)ehmickybefore adoptionf8af153

lines 33–34why, one step back
33
34const testFinalAlone = async (t, final) => {

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 35why, one step back
35 const {stdout} = await execa('noop-fd.js', ['1', '.'], {stdout: {final: final(foobarString), binary: true}});

Allow using a `final` function without a `transform` (#916)ehmickybefore adoptionf8af153

lines 36–48why, one step back
36 t.is(stdout, `.${foobarString}`);
37};3839test('Generators "final" can be used without "transform"', testFinalAlone, identityGenerator);40test('Generators "final" can be used without "transform", async', testFinalAlone, identityAsyncGenerator);4142const testFinalNoOutput = async (t, final) => {43 const {stdout} = await execa('empty.js', {stdout: {final: final(foobarString)}});44 t.is(stdout, foobarString);45};4647test('Generators "final" can be used without "transform" nor output', testFinalNoOutput, identityGenerator);48test('Generators "final" can be used without "transform" nor output, async', testFinalNoOutput, identityAsyncGenerator);

Split files (#837)ehmickybefore adoption672c2d7

line 49why, one step back
49

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 50why, one step back
50const repeatCount = defaultHighWaterMark * 3;

Split files (#837)ehmickybefore adoption672c2d7

lines 51–68why, one step back
51
52const writerGenerator = function * () {53 for (let index = 0; index < repeatCount; index += 1) {54 yield '\n';55 }56};5758const getLengthGenerator = function * (t, chunk) {59 t.is(chunk.length, 1);60 yield chunk;61};6263const testHighWaterMark = async (t, passThrough, binary, objectMode) => {64 const {stdout} = await execa('noop.js', {65 stdout: [66 ...(objectMode ? [outputObjectGenerator] : []),67 writerGenerator,68 ...(passThrough ? [noopGenerator(false, binary)] : []),

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 69why, one step back
69 {transform: getLengthGenerator.bind(undefined, t), preserveNewlines: true, objectMode: true},

Split files (#837)ehmickybefore adoption672c2d7

lines 70–72why, one step back
70 ],
71 });72 t.is(stdout.length, repeatCount);

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 73why, one step back
73 t.true(stdout.every(chunk => chunk === '\n'));

Split files (#837)ehmickybefore adoption672c2d7

lines 74–103why, one step back
74};
7576test('Synchronous yields are not buffered, no passThrough', testHighWaterMark, false, false, false);77test('Synchronous yields are not buffered, line-wise passThrough', testHighWaterMark, true, false, false);78test('Synchronous yields are not buffered, binary passThrough', testHighWaterMark, true, true, false);79test('Synchronous yields are not buffered, objectMode as input but not output', testHighWaterMark, false, false, true);8081const testNoYield = async (t, objectMode, final, output) => {82 const {stdout} = await execa('noop.js', {stdout: convertTransformToFinal(noYieldGenerator(objectMode), final)});83 t.deepEqual(stdout, output);84};8586test('Generator can filter "transform" by not calling yield', testNoYield, false, false, '');87test('Generator can filter "transform" by not calling yield, objectMode', testNoYield, true, false, []);88test('Generator can filter "final" by not calling yield', testNoYield, false, false, '');89test('Generator can filter "final" by not calling yield, objectMode', testNoYield, true, false, []);9091const prefix = '> ';92const suffix = ' <';9394const multipleYieldGenerator = async function * (line = foobarString) {95 yield prefix;96 await scheduler.yield();97 yield line;98 await scheduler.yield();99 yield suffix;100};101102const testMultipleYields = async (t, final) => {103 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: convertTransformToFinal(multipleYieldGenerator, final)});

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 104why, one step back
104 t.is(stdout, `${prefix}\n${foobarString}\n${suffix}`);

Split files (#837)ehmickybefore adoption672c2d7

lines 105–112why, one step back
105};
106107test('Generator can yield "transform" multiple times at different moments', testMultipleYields, false);108test('Generator can yield "final" multiple times at different moments', testMultipleYields, true);109110const partsPerChunk = 4;111const chunksPerCall = 10;112const callCount = 5;

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 113why, one step back
113const fullString = '\n'.repeat(defaultHighWaterMark / partsPerChunk);

Split files (#837)ehmickybefore adoption672c2d7

lines 114–127why, one step back
114
115const yieldFullStrings = function * () {116 yield * Array.from({length: partsPerChunk * chunksPerCall}).fill(fullString);117};118119const manyYieldGenerator = async function * () {120 for (let index = 0; index < callCount; index += 1) {121 yield * yieldFullStrings();122 // eslint-disable-next-line no-await-in-loop123 await scheduler.yield();124 }125};126127const testManyYields = async (t, final) => {

Upgrade `get-stream` (#908)ehmickybefore adoption3ecd447

lines 128–132why, one step back
128 const subprocess = execa('noop.js', {stdout: convertTransformToFinal(manyYieldGenerator, final), stripFinalNewline: false});
129 const [chunks, {stdout}] = await Promise.all([getStreamAsArray(subprocess.stdout), subprocess]);130 const expectedChunk = Buffer.from(fullString);131 t.deepEqual(chunks, Array.from({length: callCount * partsPerChunk * chunksPerCall}).fill(expectedChunk));132 t.is(chunks.join(''), stdout);

Split files (#837)ehmickybefore adoption672c2d7

lines 133–141why, one step back
133};
134135test('Generator "transform" yields are sent right away', testManyYields, false);136test('Generator "final" yields are sent right away', testManyYields, true);137138const maxBuffer = 10;139140test('Generators take "maxBuffer" into account', async t => {141 const bigString = '.'.repeat(maxBuffer);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 142–145why, one step back
142 const {stdout} = await execa('noop.js', {
143 maxBuffer,144 stdout: getOutputGenerator(bigString, false, true),145 });

Split files (#837)ehmickybefore adoption672c2d7

lines 146–152why, one step back
146 t.is(stdout, bigString);
147148 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputGenerator(`${bigString}.`, false)}));149});150151test('Generators take "maxBuffer" into account, objectMode', async t => {152 const bigArray = Array.from({length: maxBuffer}).fill('.');

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 153–156why, one step back
153 const {stdout} = await execa('noop.js', {
154 maxBuffer,155 stdout: getOutputsGenerator(bigArray, true, true),156 });

Split files (#837)ehmickybefore adoption672c2d7

lines 157–184why, one step back
157 t.is(stdout.length, maxBuffer);
158159 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputsGenerator([...bigArray, ''], true)}));160});161162const timeoutGenerator = async function * (timeout) {163 await setTimeout(timeout);164 yield foobarString;165};166167const testAsyncGenerators = async (t, final) => {168 const {stdout} = await execa('noop.js', {169 maxBuffer,170 stdout: convertTransformToFinal(timeoutGenerator.bind(undefined, 1e2), final),171 });172 t.is(stdout, foobarString);173};174175test('Generators "transform" is awaited on success', testAsyncGenerators, false);176test('Generators "final" is awaited on success', testAsyncGenerators, true);177178const testThrowingGenerator = async (t, final) => {179 await t.throwsAsync(180 execa('noop-fd.js', ['1', foobarString], {stdout: convertTransformToFinal(throwingGenerator, final)}),181 {message: GENERATOR_ERROR_REGEXP},182 );183};184

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 185–186why, one step back
185test('Generators "transform" errors make subprocess fail', testThrowingGenerator, false);
186test('Generators "final" errors make subprocess fail', testThrowingGenerator, true);

Split files (#837)ehmickybefore adoption672c2d7

line 187why, one step back
187

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 188why, one step back
188test('Generators errors make subprocess fail even when other output generators do not throw', async t => {

Split files (#837)ehmickybefore adoption672c2d7

lines 189–194why, one step back
189 await t.throwsAsync(
190 execa('noop-fd.js', ['1', foobarString], {stdout: [noopGenerator(false), throwingGenerator, noopGenerator(false)]}),191 {message: GENERATOR_ERROR_REGEXP},192 );193});194

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 195–198why, one step back
195test('Generators errors make subprocess fail even when other input generators do not throw', async t => {
196 const subprocess = execa('stdin-fd.js', ['0'], {stdin: [noopGenerator(false), throwingGenerator, noopGenerator(false)]});197 subprocess.stdin.write('foobar\n');198 await t.throwsAsync(subprocess, {message: GENERATOR_ERROR_REGEXP});

Split files (#837)ehmickybefore adoption672c2d7

lines 199–201why, one step back
199});
200201const testGeneratorCancel = async (t, error) => {

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 202–205why, one step back
202 const subprocess = execa('noop.js', {stdout: infiniteGenerator});
203 await once(subprocess.stdout, 'data');204 subprocess.stdout.destroy(error);205 await (error === undefined ? t.notThrowsAsync(subprocess) : t.throwsAsync(subprocess));

Split files (#837)ehmickybefore adoption672c2d7

lines 206–207why, one step back
206};
207

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 208–209why, one step back
208test('Running generators are canceled on subprocess abort', testGeneratorCancel, undefined);
209test('Running generators are canceled on subprocess error', testGeneratorCancel, new Error('test'));

Split files (#837)ehmickybefore adoption672c2d7

lines 210–211why, one step back
210
211const testGeneratorDestroy = async (t, transform) => {

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 212why, one step back
212 const subprocess = execa('forever.js', {stdout: transform});

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 213–214why, one step back
213 const cause = new Error('test');
214 subprocess.stdout.destroy(cause);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 215why, one step back
215 subprocess.kill();

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

line 216why, one step back
216 t.like(await t.throwsAsync(subprocess), {cause});

Split files (#837)ehmickybefore adoption672c2d7

lines 217–218why, one step back
217};
218

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 219–220why, one step back
219test('Generators are destroyed on subprocess error, sync', testGeneratorDestroy, noopGenerator(false));
220test('Generators are destroyed on subprocess error, async', testGeneratorDestroy, infiniteGenerator);

Split files (#837)ehmickybefore adoption672c2d7

line 221why, one step back
221

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 222why, one step back
222test('Generators are destroyed on early subprocess exit', async t => {

Split files (#837)ehmickybefore adoption672c2d7

lines 223–224why, one step back
223 await t.throwsAsync(execa('noop.js', {stdout: infiniteGenerator, uid: -1}));
224});