Skip to content
Myr

loop.js

This file

Path

test/convert/loop.js

183 lines · 13.0 KiB · JavaScript

Last changed by

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

History of this file

Take it

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 1–6why, one step back
1import {once} from 'node:events';
2import {getDefaultHighWaterMark} from 'node:stream';3import test from 'ava';4import {setFixtureDir} from '../helpers/fixtures-dir.js';5import {6 assertStreamOutput,

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 7why, one step back
7 assertIterableChunks,

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 8–13why, one step back
8 assertStreamChunks,
9 assertSubprocessOutput,10 getReadableSubprocess,11 getReadWriteSubprocess,12} from '../helpers/convert.js';13import {

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 14why, one step back
14 stringToUint8Arrays,

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 15–17why, one step back
15 simpleFull,
16 simpleChunks,17 simpleChunksBuffer,

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 18why, one step back
18 simpleChunksUint8Array,

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 19–22why, one step back
19 simpleLines,
20 noNewlinesFull,21 complexFull,22 singleComplexBuffer,

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 23why, one step back
23 singleComplexUint8Array,

Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf

lines 24–26why, one step back
24 singleComplexHex,
25 singleComplexHexBuffer,26 singleComplexHexUint8Array,

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 27–47why, one step back
27 complexChunks,
28 complexChunksEnd,29} from '../helpers/lines.js';30import {outputObjectGenerator, getOutputGenerator} from '../helpers/generator.js';31import {foobarString, foobarObject} from '../helpers/input.js';32import {multibyteChar, multibyteUint8Array, breakingLength, brokenSymbol} from '../helpers/encoding.js';3334setFixtureDir();3536const foobarObjectChunks = [foobarObject, foobarObject, foobarObject];3738const getSubprocess = (methodName, output, options) => {39 if (methodName !== 'duplex') {40 return getReadableSubprocess(output, options);41 }4243 const subprocess = getReadWriteSubprocess(options);44 subprocess.stdin.end(output);45 return subprocess;46};47

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

lines 48–52why, one step back
48const assertChunks = async (t, streamOrIterable, expectedChunks, methodName) => {
49 const assertMethod = methodName === 'iterable' ? assertIterableChunks : assertStreamChunks;50 await assertMethod(t, streamOrIterable, expectedChunks);51};52

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

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

Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf

lines 54–55why, one step back
54const testText = async (t, expectedChunks, methodName, binary, preserveNewlines, encoding) => {
55 const subprocess = getSubprocess(methodName, complexFull, {encoding});

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 56–57why, one step back
56 const stream = subprocess[methodName]({binary, preserveNewlines});
57

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 58why, one step back
58 await assertChunks(t, stream, expectedChunks, methodName);

Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf

lines 59–62why, one step back
59 const expectedOutput = encoding === 'hex'
60 ? singleComplexHex61 : stringToUint8Arrays(complexFull, encoding === 'buffer');62 await assertSubprocessOutput(t, subprocess, expectedOutput);

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 63–64why, one step back
63};
64

Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf

lines 65–91why, one step back
65test('.iterable() can use "binary: true"', testText, [singleComplexUint8Array], 'iterable', true, undefined, 'utf8');
66test('.iterable() can use "binary: undefined"', testText, complexChunks, 'iterable', undefined, undefined, 'utf8');67test('.iterable() can use "binary: undefined" + "encoding: buffer"', testText, [singleComplexUint8Array], 'iterable', undefined, undefined, 'buffer');68test('.iterable() can use "binary: undefined" + "encoding: hex"', testText, [singleComplexHexUint8Array], 'iterable', undefined, undefined, 'hex');69test('.iterable() can use "binary: false"', testText, complexChunks, 'iterable', false, undefined, 'utf8');70test('.iterable() can use "binary: false" + "encoding: buffer"', testText, [singleComplexUint8Array], 'iterable', false, undefined, 'buffer');71test('.iterable() can use "binary: false" + "encoding: hex"', testText, [singleComplexHexUint8Array], 'iterable', false, undefined, 'hex');72test('.iterable() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'iterable', false, true, 'utf8');73test('.iterable() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'iterable', false, false, 'utf8');74test('.readable() can use "binary: true"', testText, singleComplexBuffer, 'readable', true, undefined, 'utf8');75test('.readable() can use "binary: undefined"', testText, singleComplexBuffer, 'readable', undefined, undefined, 'utf8');76test('.readable() can use "binary: undefined" + "encoding: buffer"', testText, singleComplexBuffer, 'readable', undefined, undefined, 'buffer');77test('.readable() can use "binary: undefined" + "encoding: hex"', testText, [singleComplexHexBuffer], 'readable', undefined, undefined, 'hex');78test('.readable() can use "binary: false"', testText, complexChunksEnd, 'readable', false, undefined, 'utf8');79test('.readable() can use "binary: false" + "encoding: buffer"', testText, singleComplexBuffer, 'readable', false, undefined, 'buffer');80test('.readable() can use "binary: false" + "encoding: hex"', testText, [singleComplexHexBuffer], 'readable', false, undefined, 'hex');81test('.readable() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'readable', false, true, 'utf8');82test('.readable() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'readable', false, false, 'utf8');83test('.duplex() can use "binary: true"', testText, singleComplexBuffer, 'duplex', true, undefined, 'utf8');84test('.duplex() can use "binary: undefined"', testText, singleComplexBuffer, 'duplex', undefined, undefined, 'utf8');85test('.duplex() can use "binary: undefined" + "encoding: "buffer"', testText, singleComplexBuffer, 'duplex', undefined, undefined, 'buffer');86test('.duplex() can use "binary: undefined" + "encoding: "hex"', testText, [singleComplexHexBuffer], 'duplex', undefined, undefined, 'hex');87test('.duplex() can use "binary: false"', testText, complexChunksEnd, 'duplex', false, undefined, 'utf8');88test('.duplex() can use "binary: false" + "encoding: buffer"', testText, singleComplexBuffer, 'duplex', false, undefined, 'buffer');89test('.duplex() can use "binary: false" + "encoding: hex"', testText, [singleComplexHexBuffer], 'duplex', false, undefined, 'hex');90test('.duplex() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'duplex', false, true, 'utf8');91test('.duplex() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'duplex', false, false, 'utf8');

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 92–119why, one step back
92
93const testTextOutput = async (t, expectedOutput, methodName, preserveNewlines) => {94 const subprocess = getSubprocess(methodName, complexFull);95 const stream = subprocess[methodName]({binary: false, preserveNewlines});9697 await assertStreamOutput(t, stream, expectedOutput);98 await assertSubprocessOutput(t, subprocess, complexFull);99};100101test('.readable() "binary: false" keeps output as is', testTextOutput, complexFull, 'readable', undefined);102test('.readable() "binary: false" + "preserveNewlines: true" keeps output as is', testTextOutput, complexFull, 'readable', true);103test('.readable() "binary: false" + "preserveNewlines: false" removes all newlines', testTextOutput, noNewlinesFull, 'readable', false);104test('.duplex() "binary: false" keeps output as is', testTextOutput, complexFull, 'duplex', undefined);105test('.duplex() "binary: false" + "preserveNewlines: true" keeps output as is', testTextOutput, complexFull, 'duplex', true);106test('.duplex() "binary: false" + "preserveNewlines: false" removes all newlines', testTextOutput, noNewlinesFull, 'duplex', false);107108// eslint-disable-next-line max-params109const testObjectMode = async (t, expectedChunks, methodName, encoding, initialObjectMode, finalObjectMode, binary, options) => {110 const subprocess = getSubprocess(methodName, simpleFull, options);111 if (encoding !== null) {112 subprocess.stdout.setEncoding(encoding);113 }114115 t.is(subprocess.stdout.readableEncoding, encoding);116 t.is(subprocess.stdout.readableObjectMode, initialObjectMode);117 t.is(subprocess.stdout.readableHighWaterMark, getDefaultHighWaterMark(initialObjectMode));118119 const stream = subprocess[methodName]({binary, preserveNewlines: true});

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

lines 120–126why, one step back
120
121 if (methodName !== 'iterable') {122 t.is(stream.readableEncoding, encoding);123 t.is(stream.readableObjectMode, finalObjectMode);124 t.is(stream.readableHighWaterMark, getDefaultHighWaterMark(finalObjectMode));125 }126

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 127–130why, one step back
127 t.is(subprocess.stdout.readableEncoding, encoding);
128 t.is(subprocess.stdout.readableObjectMode, initialObjectMode);129 t.is(subprocess.stdout.readableHighWaterMark, getDefaultHighWaterMark(initialObjectMode));130

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 131why, one step back
131 await assertChunks(t, stream, expectedChunks, methodName);

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 132–134why, one step back
132 await subprocess;
133};134

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

line 135why, one step back
135test('.iterable() uses Uint8Arrays with "binary: true"', testObjectMode, simpleChunksUint8Array, 'iterable', null, false, false, true);

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

lines 136–137why, one step back
136test('.iterable() uses Uint8Arrays with "binary: true" and .setEncoding("utf8")', testObjectMode, simpleChunksUint8Array, 'iterable', 'utf8', false, false, true);
137test('.iterable() uses Uint8Arrays with "binary: true", .setEncoding("utf8") and "encoding: buffer"', testObjectMode, simpleChunksUint8Array, 'iterable', 'utf8', false, false, true, {encoding: 'buffer'});

Refactor tests (#934)ehmickybefore adoption52c4bde

line 138why, one step back
138test('.iterable() uses strings in objectMode with "binary: true" and object transforms', testObjectMode, foobarObjectChunks, 'iterable', null, true, true, true, {stdout: outputObjectGenerator()});

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

lines 139–140why, one step back
139test('.iterable() uses strings in objectMode with "binary: false"', testObjectMode, simpleLines, 'iterable', null, false, true, false);
140test('.iterable() uses strings in objectMode with "binary: false" and .setEncoding("utf8")', testObjectMode, simpleLines, 'iterable', 'utf8', false, true, false);

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

line 141why, one step back
141test('.iterable() uses Uint8Arrays in objectMode with "binary: false", .setEncoding("utf8") and "encoding: buffer"', testObjectMode, simpleChunksUint8Array, 'iterable', 'utf8', false, true, false, {encoding: 'buffer'});

Refactor tests (#934)ehmickybefore adoption52c4bde

line 142why, one step back
142test('.iterable() uses strings in objectMode with "binary: false" and object transforms', testObjectMode, foobarObjectChunks, 'iterable', null, true, true, false, {stdout: outputObjectGenerator()});

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 143–144why, one step back
143test('.readable() uses Buffers with "binary: true"', testObjectMode, simpleChunksBuffer, 'readable', null, false, false, true);
144test('.readable() uses strings with "binary: true" and .setEncoding("utf8")', testObjectMode, simpleChunks, 'readable', 'utf8', false, false, true);

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

line 145why, one step back
145test('.readable() uses strings with "binary: true", .setEncoding("utf8") and "encoding: buffer"', testObjectMode, simpleChunks, 'readable', 'utf8', false, false, true, {encoding: 'buffer'});

Refactor tests (#934)ehmickybefore adoption52c4bde

line 146why, one step back
146test('.readable() uses strings in objectMode with "binary: true" and object transforms', testObjectMode, foobarObjectChunks, 'readable', null, true, true, true, {stdout: outputObjectGenerator()});

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 147–148why, one step back
147test('.readable() uses strings in objectMode with "binary: false"', testObjectMode, simpleLines, 'readable', null, false, true, false);
148test('.readable() uses strings in objectMode with "binary: false" and .setEncoding("utf8")', testObjectMode, simpleLines, 'readable', 'utf8', false, true, false);

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

line 149why, one step back
149test('.readable() uses strings in objectMode with "binary: false", .setEncoding("utf8") and "encoding: buffer"', testObjectMode, simpleChunks, 'readable', 'utf8', false, false, false, {encoding: 'buffer'});

Refactor tests (#934)ehmickybefore adoption52c4bde

line 150why, one step back
150test('.readable() uses strings in objectMode with "binary: false" and object transforms', testObjectMode, foobarObjectChunks, 'readable', null, true, true, false, {stdout: outputObjectGenerator()});

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 151–152why, one step back
151test('.duplex() uses Buffers with "binary: true"', testObjectMode, simpleChunksBuffer, 'duplex', null, false, false, true);
152test('.duplex() uses strings with "binary: true" and .setEncoding("utf8")', testObjectMode, simpleChunks, 'duplex', 'utf8', false, false, true);

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

line 153why, one step back
153test('.duplex() uses strings with "binary: true", .setEncoding("utf8") and "encoding: buffer"', testObjectMode, simpleChunks, 'duplex', 'utf8', false, false, true, {encoding: 'buffer'});

Refactor tests (#934)ehmickybefore adoption52c4bde

line 154why, one step back
154test('.duplex() uses strings in objectMode with "binary: true" and object transforms', testObjectMode, foobarObjectChunks, 'duplex', null, true, true, true, {stdout: outputObjectGenerator()});

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 155–156why, one step back
155test('.duplex() uses strings in objectMode with "binary: false"', testObjectMode, simpleLines, 'duplex', null, false, true, false);
156test('.duplex() uses strings in objectMode with "binary: false" and .setEncoding("utf8")', testObjectMode, simpleLines, 'duplex', 'utf8', false, true, false);

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

line 157why, one step back
157test('.duplex() uses strings in objectMode with "binary: false", .setEncoding("utf8") and "encoding: buffer"', testObjectMode, simpleChunks, 'duplex', 'utf8', false, false, false, {encoding: 'buffer'});

Refactor tests (#934)ehmickybefore adoption52c4bde

line 158why, one step back
158test('.duplex() uses strings in objectMode with "binary: false" and object transforms', testObjectMode, foobarObjectChunks, 'duplex', null, true, true, false, {stdout: outputObjectGenerator()});

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 159–160why, one step back
159
160const testObjectSplit = async (t, methodName) => {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 161why, one step back
161 const subprocess = getSubprocess(methodName, foobarString, {stdout: getOutputGenerator(simpleFull)(true)});

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 162why, one step back
162 const stream = subprocess[methodName]({binary: false});

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 163why, one step back
163 await assertChunks(t, stream, [simpleFull], methodName);

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 164–166why, one step back
164 await subprocess;
165};166

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 167why, one step back
167test('.iterable() "binary: false" does not split lines of strings produced by object transforms', testObjectSplit, 'iterable');

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 168–173why, one step back
168test('.readable() "binary: false" does not split lines of strings produced by object transforms', testObjectSplit, 'readable');
169test('.duplex() "binary: false" does not split lines of strings produced by object transforms', testObjectSplit, 'duplex');170171const testMultibyteCharacters = async (t, methodName) => {172 const subprocess = getReadWriteSubprocess();173 const stream = subprocess[methodName]({binary: false});

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 174why, one step back
174 const assertPromise = assertChunks(t, stream, [`${multibyteChar}${brokenSymbol}`], methodName);

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 175–180why, one step back
175 subprocess.stdin.write(multibyteUint8Array.slice(0, breakingLength));
176 await once(subprocess.stdout, 'data');177 subprocess.stdin.end();178 await assertPromise;179};180

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 181why, one step back
181test('.iterable() "binary: false" handles partial multibyte characters', testMultibyteCharacters, 'iterable');

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 182–183why, one step back
182test('.readable() "binary: false" handles partial multibyte characters', testMultibyteCharacters, 'readable');
183test('.duplex() "binary: false" handles partial multibyte characters', testMultibyteCharacters, 'duplex');