loop.js
This file
Path
test/convert/loop.js
172 lines · 11.8 KiB · JavaScript
Last changed by
Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
History of this fileTake it
Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 1–6why, one step back1import {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 back7 assertIterableChunks,Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 8–13why, one step back8 assertStreamChunks,9 assertSubprocessOutput,10 getReadableSubprocess,11 getReadWriteSubprocess,12} from '../helpers/convert.js';13import {Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 14why, one step back14 stringToUint8Arrays,Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 15–17why, one step back15 simpleFull,16 simpleChunks,17 simpleChunksBuffer,Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 18why, one step back18 simpleChunksUint8Array,Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 19–22why, one step back19 simpleLines,20 noNewlinesFull,21 complexFull,22 singleComplexBuffer,Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 23why, one step back23 singleComplexUint8Array,Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 24–44why, one step back24 complexChunks,25 complexChunksEnd,26} from '../helpers/lines.js';27import {outputObjectGenerator, getOutputGenerator} from '../helpers/generator.js';28import {foobarString, foobarObject} from '../helpers/input.js';29import {multibyteChar, multibyteUint8Array, breakingLength, brokenSymbol} from '../helpers/encoding.js';3031setFixtureDir();3233const foobarObjectChunks = [foobarObject, foobarObject, foobarObject];3435const getSubprocess = (methodName, output, options) => {36 if (methodName !== 'duplex') {37 return getReadableSubprocess(output, options);38 }3940 const subprocess = getReadWriteSubprocess(options);41 subprocess.stdin.end(output);42 return subprocess;43};44Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 45–49why, one step back45const assertChunks = async (t, streamOrIterable, expectedChunks, methodName) => {46 const assertMethod = methodName === 'iterable' ? assertIterableChunks : assertStreamChunks;47 await assertMethod(t, streamOrIterable, expectedChunks);48};49Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
line 50why, one step back50// eslint-disable-next-line max-paramsMake subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 51–53why, one step back51const testText = async (t, expectedChunks, methodName, binary, preserveNewlines, isUint8Array) => {52 const options = isUint8Array ? {encoding: 'buffer'} : {};53 const subprocess = getSubprocess(methodName, complexFull, options);Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 54–55why, one step back54 const stream = subprocess[methodName]({binary, preserveNewlines});55Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 56–57why, one step back56 await assertChunks(t, stream, expectedChunks, methodName);57 await assertSubprocessOutput(t, subprocess, stringToUint8Arrays(complexFull, isUint8Array));Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 58–59why, one step back58};59Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 60why, one step back60test('.iterable() can use "binary: true"', testText, [singleComplexUint8Array], 'iterable', true, undefined, false);Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 61why, one step back61test('.iterable() can use "binary: undefined"', testText, complexChunks, 'iterable', undefined, undefined, false);Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 62why, one step back62test('.iterable() can use "binary: undefined" + "encoding: buffer"', testText, [singleComplexUint8Array], 'iterable', undefined, undefined, true);Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 63why, one step back63test('.iterable() can use "binary: false"', testText, complexChunks, 'iterable', false, undefined, false);Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 64why, one step back64test('.iterable() can use "binary: false" + "encoding: buffer"', testText, [singleComplexUint8Array], 'iterable', false, undefined, true);Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 65–70why, one step back65test('.iterable() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'iterable', false, true, false);66test('.iterable() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'iterable', false, false, false);67test('.readable() can use "binary: true"', testText, singleComplexBuffer, 'readable', true, undefined, false);68test('.readable() can use "binary: undefined"', testText, singleComplexBuffer, 'readable', undefined, undefined, false);69test('.readable() can use "binary: undefined" + "encoding: buffer"', testText, singleComplexBuffer, 'readable', undefined, undefined, true);70test('.readable() can use "binary: false"', testText, complexChunksEnd, 'readable', false, undefined, false);Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 71why, one step back71test('.readable() can use "binary: false" + "encoding: buffer"', testText, singleComplexBuffer, 'readable', false, undefined, true);Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 72–77why, one step back72test('.readable() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'readable', false, true, false);73test('.readable() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'readable', false, false, false);74test('.duplex() can use "binary: true"', testText, singleComplexBuffer, 'duplex', true, undefined, false);75test('.duplex() can use "binary: undefined"', testText, singleComplexBuffer, 'duplex', undefined, undefined, false);76test('.duplex() can use "binary: undefined" + "encoding: "buffer"', testText, singleComplexBuffer, 'duplex', undefined, undefined, true);77test('.duplex() can use "binary: false"', testText, complexChunksEnd, 'duplex', false, undefined, false);Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 78why, one step back78test('.duplex() can use "binary: false" + "encoding: buffer"', testText, singleComplexBuffer, 'duplex', false, undefined, true);Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 79–80why, one step back79test('.duplex() can use "binary: false" + "preserveNewlines: true"', testText, complexChunksEnd, 'duplex', false, true, false);80test('.duplex() can use "binary: false" + "preserveNewlines: false"', testText, complexChunks, 'duplex', false, false, false);Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 81–108why, one step back8182const testTextOutput = async (t, expectedOutput, methodName, preserveNewlines) => {83 const subprocess = getSubprocess(methodName, complexFull);84 const stream = subprocess[methodName]({binary: false, preserveNewlines});8586 await assertStreamOutput(t, stream, expectedOutput);87 await assertSubprocessOutput(t, subprocess, complexFull);88};8990test('.readable() "binary: false" keeps output as is', testTextOutput, complexFull, 'readable', undefined);91test('.readable() "binary: false" + "preserveNewlines: true" keeps output as is', testTextOutput, complexFull, 'readable', true);92test('.readable() "binary: false" + "preserveNewlines: false" removes all newlines', testTextOutput, noNewlinesFull, 'readable', false);93test('.duplex() "binary: false" keeps output as is', testTextOutput, complexFull, 'duplex', undefined);94test('.duplex() "binary: false" + "preserveNewlines: true" keeps output as is', testTextOutput, complexFull, 'duplex', true);95test('.duplex() "binary: false" + "preserveNewlines: false" removes all newlines', testTextOutput, noNewlinesFull, 'duplex', false);9697// eslint-disable-next-line max-params98const testObjectMode = async (t, expectedChunks, methodName, encoding, initialObjectMode, finalObjectMode, binary, options) => {99 const subprocess = getSubprocess(methodName, simpleFull, options);100 if (encoding !== null) {101 subprocess.stdout.setEncoding(encoding);102 }103104 t.is(subprocess.stdout.readableEncoding, encoding);105 t.is(subprocess.stdout.readableObjectMode, initialObjectMode);106 t.is(subprocess.stdout.readableHighWaterMark, getDefaultHighWaterMark(initialObjectMode));107108 const stream = subprocess[methodName]({binary, preserveNewlines: true});Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 109–115why, one step back109110 if (methodName !== 'iterable') {111 t.is(stream.readableEncoding, encoding);112 t.is(stream.readableObjectMode, finalObjectMode);113 t.is(stream.readableHighWaterMark, getDefaultHighWaterMark(finalObjectMode));114 }115Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 116–119why, one step back116 t.is(subprocess.stdout.readableEncoding, encoding);117 t.is(subprocess.stdout.readableObjectMode, initialObjectMode);118 t.is(subprocess.stdout.readableHighWaterMark, getDefaultHighWaterMark(initialObjectMode));119Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 120why, one step back120 await assertChunks(t, stream, expectedChunks, methodName);Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 121–123why, one step back121 await subprocess;122};123Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 124why, one step back124test('.iterable() uses Uint8Arrays with "binary: true"', testObjectMode, simpleChunksUint8Array, 'iterable', null, false, false, true);Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 125–129why, one step back125test('.iterable() uses strings with "binary: true" and .setEncoding("utf8")', testObjectMode, simpleChunks, 'iterable', 'utf8', false, false, true);126test('.iterable() uses strings with "binary: true" and "encoding: buffer"', testObjectMode, simpleChunks, 'iterable', 'utf8', false, false, true, {encoding: 'buffer'});127test('.iterable() uses strings in objectMode with "binary: true" and object transforms', testObjectMode, foobarObjectChunks, 'iterable', null, true, true, true, {stdout: outputObjectGenerator});128test('.iterable() uses strings in objectMode with "binary: false"', testObjectMode, simpleLines, 'iterable', null, false, true, false);129test('.iterable() uses strings in objectMode with "binary: false" and .setEncoding("utf8")', testObjectMode, simpleLines, 'iterable', 'utf8', false, true, false);Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 130why, one step back130test('.iterable() uses strings in objectMode with "binary: false" and "encoding: buffer"', testObjectMode, simpleChunks, 'iterable', 'utf8', false, true, false, {encoding: 'buffer'});Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 131why, one step back131test('.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 132–137why, one step back132test('.readable() uses Buffers with "binary: true"', testObjectMode, simpleChunksBuffer, 'readable', null, false, false, true);133test('.readable() uses strings with "binary: true" and .setEncoding("utf8")', testObjectMode, simpleChunks, 'readable', 'utf8', false, false, true);134test('.readable() uses strings with "binary: true" and "encoding: buffer"', testObjectMode, simpleChunks, 'readable', 'utf8', false, false, true, {encoding: 'buffer'});135test('.readable() uses strings in objectMode with "binary: true" and object transforms', testObjectMode, foobarObjectChunks, 'readable', null, true, true, true, {stdout: outputObjectGenerator});136test('.readable() uses strings in objectMode with "binary: false"', testObjectMode, simpleLines, 'readable', null, false, true, false);137test('.readable() uses strings in objectMode with "binary: false" and .setEncoding("utf8")', testObjectMode, simpleLines, 'readable', 'utf8', false, true, false);Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 138why, one step back138test('.readable() uses strings in objectMode with "binary: false" and "encoding: buffer"', testObjectMode, simpleChunks, 'readable', 'utf8', false, false, false, {encoding: 'buffer'});Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 139–145why, one step back139test('.readable() uses strings in objectMode with "binary: false" and object transforms', testObjectMode, foobarObjectChunks, 'readable', null, true, true, false, {stdout: outputObjectGenerator});140test('.duplex() uses Buffers with "binary: true"', testObjectMode, simpleChunksBuffer, 'duplex', null, false, false, true);141test('.duplex() uses strings with "binary: true" and .setEncoding("utf8")', testObjectMode, simpleChunks, 'duplex', 'utf8', false, false, true);142test('.duplex() uses strings with "binary: true" and "encoding: buffer"', testObjectMode, simpleChunks, 'duplex', 'utf8', false, false, true, {encoding: 'buffer'});143test('.duplex() uses strings in objectMode with "binary: true" and object transforms', testObjectMode, foobarObjectChunks, 'duplex', null, true, true, true, {stdout: outputObjectGenerator});144test('.duplex() uses strings in objectMode with "binary: false"', testObjectMode, simpleLines, 'duplex', null, false, true, false);145test('.duplex() uses strings in objectMode with "binary: false" and .setEncoding("utf8")', testObjectMode, simpleLines, 'duplex', 'utf8', false, true, false);Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd
line 146why, one step back146test('.duplex() uses strings in objectMode with "binary: false" and "encoding: buffer"', testObjectMode, simpleChunks, 'duplex', 'utf8', false, false, false, {encoding: 'buffer'});Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 147–151why, one step back147test('.duplex() uses strings in objectMode with "binary: false" and object transforms', testObjectMode, foobarObjectChunks, 'duplex', null, true, true, false, {stdout: outputObjectGenerator});148149const testObjectSplit = async (t, methodName) => {150 const subprocess = getSubprocess(methodName, foobarString, {stdout: getOutputGenerator(simpleFull, true)});151 const stream = subprocess[methodName]({binary: false});Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 152why, one step back152 await assertChunks(t, stream, [simpleFull], methodName);Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 153–155why, one step back153 await subprocess;154};155Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 156why, one step back156test('.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 157–162why, one step back157test('.readable() "binary: false" does not split lines of strings produced by object transforms', testObjectSplit, 'readable');158test('.duplex() "binary: false" does not split lines of strings produced by object transforms', testObjectSplit, 'duplex');159160const testMultibyteCharacters = async (t, methodName) => {161 const subprocess = getReadWriteSubprocess();162 const stream = subprocess[methodName]({binary: false});Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 163why, one step back163 const assertPromise = assertChunks(t, stream, [`${multibyteChar}${brokenSymbol}`], methodName);Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 164–169why, one step back164 subprocess.stdin.write(multibyteUint8Array.slice(0, breakingLength));165 await once(subprocess.stdout, 'data');166 subprocess.stdin.end();167 await assertPromise;168};169Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
line 170why, one step back170test('.iterable() "binary: false" handles partial multibyte characters', testMultibyteCharacters, 'iterable');Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 171–172why, one step back171test('.readable() "binary: false" handles partial multibyte characters', testMultibyteCharacters, 'readable');172test('.duplex() "binary: false" handles partial multibyte characters', testMultibyteCharacters, 'duplex');