Skip to content
Myr

transform.js

This file

Path

test/stdio/transform.js

268 lines · 13.2 KiB · JavaScript

Last changed by

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

History of this file

Take it

Split files (#837)ehmickybefore adoption672c2d7

lines 1–2why, one step back
1import {Buffer} from 'node:buffer';
2import {once} from 'node:events';

Refactor tests (#934)ehmickybefore adoption52c4bde

line 3why, one step back
3import {scheduler} from 'node:timers/promises';

Split files (#837)ehmickybefore adoption672c2d7

lines 4–5why, one step back
4import test from 'ava';
5import {getStreamAsArray} from 'get-stream';

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 6why, one step back
6import {execa, execaSync} from '../../index.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 7–9why, one step back
7import {foobarString} from '../helpers/input.js';
8import {9 noopGenerator,

Refactor tests (#934)ehmickybefore adoption52c4bde

line 10why, one step back
10 getOutputAsyncGenerator,

Split files (#837)ehmickybefore adoption672c2d7

lines 11–13why, one step back
11 getOutputGenerator,
12 infiniteGenerator,13 outputObjectGenerator,

Refactor tests (#934)ehmickybefore adoption52c4bde

lines 14–16why, one step back
14 convertTransformToFinal,
15 prefix,16 suffix,

Split files (#837)ehmickybefore adoption672c2d7

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

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 18why, one step back
18import {generatorsMap} from '../helpers/map.js';

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

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

Split files (#837)ehmickybefore adoption672c2d7

lines 20–23why, one step back
20import {setFixtureDir} from '../helpers/fixtures-dir.js';
2122setFixtureDir();23

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 24–25why, one step back
24const testGeneratorFinal = async (t, fixtureName, execaMethod) => {
25 const {stdout} = await execaMethod(fixtureName, {stdout: convertTransformToFinal(getOutputGenerator(foobarString)(), true)});

Split files (#837)ehmickybefore adoption672c2d7

lines 26–28why, one step back
26 t.is(stdout, foobarString);
27};28

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 29–32why, one step back
29test('Generators "final" can be used', testGeneratorFinal, 'noop.js', execa);
30test('Generators "final" is used even on empty streams', testGeneratorFinal, 'empty.js', execa);31test('Generators "final" can be used, sync', testGeneratorFinal, 'noop.js', execaSync);32test('Generators "final" is used even on empty streams, sync', testGeneratorFinal, 'empty.js', execaSync);

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

line 33why, one step back
33

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 34–35why, one step back
34const testFinalAlone = async (t, final, execaMethod) => {
35 const {stdout} = await execaMethod('noop-fd.js', ['1', '.'], {stdout: {final: final(foobarString)().transform, binary: true}});

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

lines 36–38why, one step back
36 t.is(stdout, `.${foobarString}`);
37};38

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 39–41why, one step back
39test('Generators "final" can be used without "transform"', testFinalAlone, getOutputGenerator, execa);
40test('Generators "final" can be used without "transform", sync', testFinalAlone, getOutputGenerator, execaSync);41test('Generators "final" can be used without "transform", async', testFinalAlone, getOutputAsyncGenerator, execa);

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

line 42why, one step back
42

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 43–44why, one step back
43const testFinalNoOutput = async (t, final, execaMethod) => {
44 const {stdout} = await execaMethod('empty.js', {stdout: {final: final(foobarString)().transform}});

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

lines 45–47why, one step back
45 t.is(stdout, foobarString);
46};47

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 48–50why, one step back
48test('Generators "final" can be used without "transform" nor output', testFinalNoOutput, getOutputGenerator, execa);
49test('Generators "final" can be used without "transform" nor output, sync', testFinalNoOutput, getOutputGenerator, execaSync);50test('Generators "final" can be used without "transform" nor output, async', testFinalNoOutput, getOutputAsyncGenerator, execa);

Split files (#837)ehmickybefore adoption672c2d7

line 51why, one step back
51

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

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

Split files (#837)ehmickybefore adoption672c2d7

lines 53–64why, one step back
53
54const writerGenerator = function * () {55 for (let index = 0; index < repeatCount; index += 1) {56 yield '\n';57 }58};5960const getLengthGenerator = function * (t, chunk) {61 t.is(chunk.length, 1);62 yield chunk;63};64

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 65–67why, one step back
65// eslint-disable-next-line max-params
66const testHighWaterMark = async (t, passThrough, binary, objectMode, execaMethod) => {67 const {stdout} = await execaMethod('noop.js', {

Split files (#837)ehmickybefore adoption672c2d7

line 68why, one step back
68 stdout: [

Refactor tests (#934)ehmickybefore adoption52c4bde

line 69why, one step back
69 ...(objectMode ? [outputObjectGenerator()] : []),

Split files (#837)ehmickybefore adoption672c2d7

lines 70–71why, one step back
70 writerGenerator,
71 ...(passThrough ? [noopGenerator(false, binary)] : []),

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

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

Split files (#837)ehmickybefore adoption672c2d7

lines 73–75why, one step back
73 ],
74 });75 t.is(stdout.length, repeatCount);

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

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

Split files (#837)ehmickybefore adoption672c2d7

lines 77–78why, one step back
77};
78

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 79–86why, one step back
79test('Synchronous yields are not buffered, no passThrough', testHighWaterMark, false, false, false, execa);
80test('Synchronous yields are not buffered, line-wise passThrough', testHighWaterMark, true, false, false, execa);81test('Synchronous yields are not buffered, binary passThrough', testHighWaterMark, true, true, false, execa);82test('Synchronous yields are not buffered, objectMode as input but not output', testHighWaterMark, false, false, true, execa);83test('Synchronous yields are not buffered, no passThrough, sync', testHighWaterMark, false, false, false, execaSync);84test('Synchronous yields are not buffered, line-wise passThrough, sync', testHighWaterMark, true, false, false, execaSync);85test('Synchronous yields are not buffered, binary passThrough, sync', testHighWaterMark, true, true, false, execaSync);86test('Synchronous yields are not buffered, objectMode as input but not output, sync', testHighWaterMark, false, false, true, execaSync);

Split files (#837)ehmickybefore adoption672c2d7

line 87why, one step back
87

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 88why, one step back
88// eslint-disable-next-line max-params

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 89–90why, one step back
89const testNoYield = async (t, type, objectMode, final, output, execaMethod) => {
90 const {stdout} = await execaMethod('noop.js', {stdout: convertTransformToFinal(generatorsMap[type].noYield(objectMode), final)});

Split files (#837)ehmickybefore adoption672c2d7

lines 91–93why, one step back
91 t.deepEqual(stdout, output);
92};93

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 94–105why, one step back
94test('Generator can filter "transform" by not calling yield', testNoYield, 'generator', false, false, '', execa);
95test('Generator can filter "transform" by not calling yield, objectMode', testNoYield, 'generator', true, false, [], execa);96test('Generator can filter "final" by not calling yield', testNoYield, 'generator', false, true, '', execa);97test('Generator can filter "final" by not calling yield, objectMode', testNoYield, 'generator', true, true, [], execa);98test('Generator can filter "transform" by not calling yield, sync', testNoYield, 'generator', false, false, '', execaSync);99test('Generator can filter "transform" by not calling yield, objectMode, sync', testNoYield, 'generator', true, false, [], execaSync);100test('Generator can filter "final" by not calling yield, sync', testNoYield, 'generator', false, true, '', execaSync);101test('Generator can filter "final" by not calling yield, objectMode, sync', testNoYield, 'generator', true, true, [], execaSync);102test('Duplex can filter by not calling push', testNoYield, 'duplex', false, false, '', execa);103test('Duplex can filter by not calling push, objectMode', testNoYield, 'duplex', true, false, [], execa);104test('WebTransform can filter by not calling push', testNoYield, 'webTransform', false, false, '', execa);105test('WebTransform can filter by not calling push, objectMode', testNoYield, 'webTransform', true, false, [], execa);

Split files (#837)ehmickybefore adoption672c2d7

line 106why, one step back
106

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 107–110why, one step back
107const testMultipleYields = async (t, type, final, binary) => {
108 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: convertTransformToFinal(generatorsMap[type].multipleYield(), final)});109 const newline = binary ? '' : '\n';110 t.is(stdout, `${prefix}${newline}${foobarString}${newline}${suffix}`);

Split files (#837)ehmickybefore adoption672c2d7

lines 111–112why, one step back
111};
112

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 113–115why, one step back
113test('Generator can yield "transform" multiple times at different moments', testMultipleYields, 'generator', false, false);
114test('Generator can yield "final" multiple times at different moments', testMultipleYields, 'generator', true, false);115test('Duplex can push multiple times at different moments', testMultipleYields, 'duplex', false, true);

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

line 116why, one step back
116test('WebTransform can push multiple times at different moments', testMultipleYields, 'webTransform', false, true);

Split files (#837)ehmickybefore adoption672c2d7

lines 117–120why, one step back
117
118const partsPerChunk = 4;119const chunksPerCall = 10;120const callCount = 5;

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

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

Split files (#837)ehmickybefore adoption672c2d7

lines 122–135why, one step back
122
123const yieldFullStrings = function * () {124 yield * Array.from({length: partsPerChunk * chunksPerCall}).fill(fullString);125};126127const manyYieldGenerator = async function * () {128 for (let index = 0; index < callCount; index += 1) {129 yield * yieldFullStrings();130 // eslint-disable-next-line no-await-in-loop131 await scheduler.yield();132 }133};134135const testManyYields = async (t, final) => {

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

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

Split files (#837)ehmickybefore adoption672c2d7

lines 141–147why, one step back
141};
142143test('Generator "transform" yields are sent right away', testManyYields, false);144test('Generator "final" yields are sent right away', testManyYields, true);145146const maxBuffer = 10;147

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 148why, one step back
148const testMaxBuffer = async (t, type) => {

Split files (#837)ehmickybefore adoption672c2d7

line 149why, one step back
149 const bigString = '.'.repeat(maxBuffer);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 150–151why, one step back
150 const {stdout} = await execa('noop.js', {
151 maxBuffer,

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 152why, one step back
152 stdout: generatorsMap[type].getOutput(bigString)(false, true),

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 153why, one step back
153 });

Split files (#837)ehmickybefore adoption672c2d7

lines 154–155why, one step back
154 t.is(stdout, bigString);
155

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 156–157why, one step back
156 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: generatorsMap[type].getOutput(`${bigString}.`)(false)}));
157};

Split files (#837)ehmickybefore adoption672c2d7

line 158why, one step back
158

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 159–160why, one step back
159test('Generators take "maxBuffer" into account', testMaxBuffer, 'generator');
160test('Duplexes take "maxBuffer" into account', testMaxBuffer, 'duplex');

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

line 161why, one step back
161test('WebTransforms take "maxBuffer" into account', testMaxBuffer, 'webTransform');

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 162–163why, one step back
162
163const testMaxBufferObject = async (t, type) => {

Split files (#837)ehmickybefore adoption672c2d7

line 164why, one step back
164 const bigArray = Array.from({length: maxBuffer}).fill('.');

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 165–166why, one step back
165 const {stdout} = await execa('noop.js', {
166 maxBuffer,

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 167why, one step back
167 stdout: generatorsMap[type].getOutputs(bigArray)(true, true),

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 168why, one step back
168 });

Split files (#837)ehmickybefore adoption672c2d7

lines 169–170why, one step back
169 t.is(stdout.length, maxBuffer);
170

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 171–172why, one step back
171 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: generatorsMap[type].getOutputs([...bigArray, ''])(true)}));
172};

Split files (#837)ehmickybefore adoption672c2d7

line 173why, one step back
173

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 174–175why, one step back
174test('Generators take "maxBuffer" into account, objectMode', testMaxBufferObject, 'generator');
175test('Duplexes take "maxBuffer" into account, objectMode', testMaxBufferObject, 'duplex');

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

line 176why, one step back
176test('WebTransforms take "maxBuffer" into account, objectMode', testMaxBufferObject, 'webTransform');

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 177–178why, one step back
177
178const testAsyncGenerators = async (t, type, final) => {

Split files (#837)ehmickybefore adoption672c2d7

line 179why, one step back
179 const {stdout} = await execa('noop.js', {

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 180why, one step back
180 stdout: convertTransformToFinal(generatorsMap[type].timeout(1e2)(), final),

Split files (#837)ehmickybefore adoption672c2d7

lines 181–184why, one step back
181 });
182 t.is(stdout, foobarString);183};184

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 185–187why, one step back
185test('Generators "transform" is awaited on success', testAsyncGenerators, 'generator', false);
186test('Generators "final" is awaited on success', testAsyncGenerators, 'generator', true);187test('Duplex is awaited on success', testAsyncGenerators, 'duplex', false);

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

line 188why, one step back
188test('WebTransform is awaited on success', testAsyncGenerators, 'webTransform', false);

Split files (#837)ehmickybefore adoption672c2d7

line 189why, one step back
189

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 190–198why, one step back
190const assertProcessError = async (t, type, execaMethod, getSubprocess) => {
191 const cause = new Error(foobarString);192 const transform = generatorsMap[type].throwing(cause)();193 const error = execaMethod === execa194 ? await t.throwsAsync(getSubprocess(transform))195 : t.throws(() => {196 getSubprocess(transform);197 });198 t.is(error.cause, cause);

Split files (#837)ehmickybefore adoption672c2d7

lines 199–200why, one step back
199};
200

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 201–205why, one step back
201const testThrowingGenerator = async (t, type, final, execaMethod) => {
202 await assertProcessError(t, type, execaMethod, transform => execaMethod('noop.js', {203 stdout: convertTransformToFinal(transform, final),204 }));205};

Split files (#837)ehmickybefore adoption672c2d7

line 206why, one step back
206

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 207–221why, one step back
207test('Generators "transform" errors make subprocess fail', testThrowingGenerator, 'generator', false, execa);
208test('Generators "final" errors make subprocess fail', testThrowingGenerator, 'generator', true, execa);209test('Generators "transform" errors make subprocess fail, sync', testThrowingGenerator, 'generator', false, execaSync);210test('Generators "final" errors make subprocess fail, sync', testThrowingGenerator, 'generator', true, execaSync);211test('Duplexes "transform" errors make subprocess fail', testThrowingGenerator, 'duplex', false, execa);212test('WebTransform "transform" errors make subprocess fail', testThrowingGenerator, 'webTransform', false, execa);213214const testSingleErrorOutput = async (t, type, execaMethod) => {215 await assertProcessError(t, type, execaMethod, transform => execaMethod('noop.js', {216 stdout: [217 generatorsMap[type].noop(false),218 transform,219 generatorsMap[type].noop(false),220 ],221 }));

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 222why, one step back
222};

Split files (#837)ehmickybefore adoption672c2d7

line 223why, one step back
223

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 224–227why, one step back
224test('Generators errors make subprocess fail even when other output generators do not throw', testSingleErrorOutput, 'generator', execa);
225test('Generators errors make subprocess fail even when other output generators do not throw, sync', testSingleErrorOutput, 'generator', execaSync);226test('Duplexes errors make subprocess fail even when other output generators do not throw', testSingleErrorOutput, 'duplex', execa);227test('WebTransform errors make subprocess fail even when other output generators do not throw', testSingleErrorOutput, 'webTransform', execa);

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 228why, one step back
228

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 229–237why, one step back
229const testSingleErrorInput = async (t, type, execaMethod) => {
230 await assertProcessError(t, type, execaMethod, transform => execaMethod('stdin.js', {231 stdin: [232 ['foobar\n'],233 generatorsMap[type].noop(false),234 transform,235 generatorsMap[type].noop(false),236 ],237 }));

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 238–239why, one step back
238};
239

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 240–243why, one step back
240test('Generators errors make subprocess fail even when other input generators do not throw', testSingleErrorInput, 'generator', execa);
241test('Generators errors make subprocess fail even when other input generators do not throw, sync', testSingleErrorInput, 'generator', execaSync);242test('Duplexes errors make subprocess fail even when other input generators do not throw', testSingleErrorInput, 'duplex', execa);243test('WebTransform errors make subprocess fail even when other input generators do not throw', testSingleErrorInput, 'webTransform', execa);

Split files (#837)ehmickybefore adoption672c2d7

lines 244–245why, one step back
244
245const testGeneratorCancel = async (t, error) => {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 246why, one step back
246 const subprocess = execa('noop.js', {stdout: infiniteGenerator()});

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

lines 247–249why, one step back
247 await once(subprocess.stdout, 'data');
248 subprocess.stdout.destroy(error);249 await (error === undefined ? t.notThrowsAsync(subprocess) : t.throwsAsync(subprocess));

Split files (#837)ehmickybefore adoption672c2d7

lines 250–251why, one step back
250};
251

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

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

Split files (#837)ehmickybefore adoption672c2d7

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

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

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

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

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

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

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

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

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

Split files (#837)ehmickybefore adoption672c2d7

lines 261–262why, one step back
261};
262

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

line 263why, one step back
263test('Generators are destroyed on subprocess error, sync', testGeneratorDestroy, noopGenerator(false));

Refactor tests (#934)ehmickybefore adoption52c4bde

line 264why, one step back
264test('Generators are destroyed on subprocess error, async', testGeneratorDestroy, infiniteGenerator());

Split files (#837)ehmickybefore adoption672c2d7

line 265why, one step back
265

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

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

Refactor tests (#934)ehmickybefore adoption52c4bde

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

Split files (#837)ehmickybefore adoption672c2d7

line 268why, one step back
268});