Skip to content
Myr

Improve default value of transforms `objectMode` (#867)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 08f8e5d · Browse files · 5 files+158 added−84 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+158 added−84 removed

  • tests:+151 added−78 removed
  • other:+7 added−6 removed

5 files+158 added−84 removed

modified:lib/stdio/encoding-final.js+3 added-2 removed
modified:lib/stdio/generator.js+4 added-4 removed
modified:test/helpers/generator.js+4 added
1import {setImmediate, setInterval} from 'node:timers/promises';2import {foobarObject} from './input.js';34export const addNoopGenerator = (transform, addNoopTransform) => addNoopTransform5 ? [transform, noopGenerator(undefined, true)]6 : [transform];78export const noopGenerator = (objectMode, binary) => ({9 * transform(line) {10 yield line;11 },
60 unmodified lines
modified:test/stdio/encoding-final.js+19 added-12 removed
5 unmodified lines
6import getStream, {getStreamAsBuffer} from 'get-stream';7import {execa, execaSync} from '../../index.js';8import {setFixtureDir, FIXTURES_DIR} from '../helpers/fixtures-dir.js';9import {fullStdio} from '../helpers/stdio.js';10import {outputObjectGenerator, getChunksGenerator} from '../helpers/generator.js';10import {outputObjectGenerator, getChunksGenerator, addNoopGenerator} from '../helpers/generator.js';11import {foobarObject} from '../helpers/input.js';1213const pExec = promisify(exec);14
120 unmodified lines
135});136137const foobarArray = ['fo', 'ob', 'ar', '..'];138139test('Handle multibyte characters', async t => {140 const {stdout} = await execa('noop.js', {stdout: getChunksGenerator(foobarArray, false), encoding: 'base64'});141 t.is(stdout, btoa(foobarArray.join('')));142});139const testMultibyteCharacters = async (t, objectMode, addNoopTransform) => {140 const {stdout} = await execa('noop.js', {stdout: addNoopGenerator(getChunksGenerator(foobarArray, objectMode), addNoopTransform), encoding: 'base64'});141 if (objectMode) {142 t.deepEqual(stdout, foobarArray.map(chunk => btoa(chunk)));143 } else {144 t.is(stdout, btoa(foobarArray.join('')));145 }146};147144test('Handle multibyte characters, with objectMode', async t => {145 const {stdout} = await execa('noop.js', {stdout: getChunksGenerator(foobarArray, true), encoding: 'base64'});146 t.deepEqual(stdout, foobarArray.map(chunk => btoa(chunk)));147});148test('Handle multibyte characters', testMultibyteCharacters, false, false);149test('Handle multibyte characters, noop transform', testMultibyteCharacters, false, true);150test('Handle multibyte characters, with objectMode', testMultibyteCharacters, true, false);151test('Handle multibyte characters, with objectMode, noop transform', testMultibyteCharacters, true, true);152149test('Other encodings work with transforms that return objects', async t => {150 const {stdout} = await execa('noop.js', {stdout: outputObjectGenerator, encoding: 'base64'});153const testObjectMode = async (t, addNoopTransform) => {154 const {stdout} = await execa('noop.js', {stdout: addNoopGenerator(outputObjectGenerator, addNoopTransform), encoding: 'base64'});155 t.deepEqual(stdout, [foobarObject]);152});156};157158test('Other encodings work with transforms that return objects', testObjectMode, false);159test('Other encodings work with transforms that return objects, noop transform', testObjectMode, true);160161const testIgnoredEncoding = async (t, stdoutOption, isUndefined, options) => {162 const {stdout} = await execa('empty.js', {stdout: stdoutOption, ...options});163 t.is(stdout === undefined, isUndefined);164};
39 unmodified lines
modified:test/stdio/generator.js+128 added-66 removed
5 unmodified lines
6import {execa} from '../../index.js';7import {setFixtureDir} from '../helpers/fixtures-dir.js';8import {getStdio} from '../helpers/stdio.js';9import {foobarString, foobarUint8Array, foobarBuffer, foobarObject, foobarObjectString} from '../helpers/input.js';10import {serializeGenerator, outputObjectGenerator} from '../helpers/generator.js';10import {serializeGenerator, outputObjectGenerator, addNoopGenerator} from '../helpers/generator.js';1112setFixtureDir();1314const textEncoder = new TextEncoder();
9 unmodified lines
24const uppercaseBufferGenerator = function * (line) {25 yield textDecoder.decode(line).toUpperCase();26};2728const getInputObjectMode = objectMode => objectMode29 ? {input: [foobarObject], generator: serializeGenerator, output: foobarObjectString}30 : {input: foobarUint8Array, generator: uppercaseGenerator, output: foobarUppercase};28const getInputObjectMode = (objectMode, addNoopTransform) => objectMode29 ? {30 input: [foobarObject],31 generators: addNoopGenerator(serializeGenerator, addNoopTransform),32 output: foobarObjectString,33 }34 : {35 input: foobarUint8Array,36 generators: addNoopGenerator(uppercaseGenerator, addNoopTransform),37 output: foobarUppercase,38 };3932const getOutputObjectMode = objectMode => objectMode33 ? {generator: outputObjectGenerator, output: [foobarObject], getStreamMethod: getStreamAsArray}34 : {generator: uppercaseGenerator, output: foobarUppercase, getStreamMethod: getStream};40const getOutputObjectMode = (objectMode, addNoopTransform) => objectMode41 ? {42 generators: addNoopGenerator(outputObjectGenerator, addNoopTransform),43 output: [foobarObject],44 getStreamMethod: getStreamAsArray,45 }46 : {47 generators: addNoopGenerator(uppercaseGenerator, addNoopGenerator),48 output: foobarUppercase,49 getStreamMethod: getStream,50 };5136const testGeneratorInput = async (t, fdNumber, objectMode) => {37 const {input, generator, output} = getInputObjectMode(objectMode);38 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [input, generator]));52const testGeneratorInput = async (t, fdNumber, objectMode, addNoopTransform) => {53 const {input, generators, output} = getInputObjectMode(objectMode, addNoopTransform);54 const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [input, ...generators]));55 t.is(stdout, output);56};5742test('Can use generators with result.stdin', testGeneratorInput, 0, false);43test('Can use generators with result.stdio[*] as input', testGeneratorInput, 3, false);44test('Can use generators with result.stdin, objectMode', testGeneratorInput, 0, true);45test('Can use generators with result.stdio[*] as input, objectMode', testGeneratorInput, 3, true);58test('Can use generators with result.stdin', testGeneratorInput, 0, false, false);59test('Can use generators with result.stdio[*] as input', testGeneratorInput, 3, false, false);60test('Can use generators with result.stdin, objectMode', testGeneratorInput, 0, true, false);61test('Can use generators with result.stdio[*] as input, objectMode', testGeneratorInput, 3, true, false);62test('Can use generators with result.stdin, noop transform', testGeneratorInput, 0, false, true);63test('Can use generators with result.stdio[*] as input, noop transform', testGeneratorInput, 3, false, true);64test('Can use generators with result.stdin, objectMode, noop transform', testGeneratorInput, 0, true, true);65test('Can use generators with result.stdio[*] as input, objectMode, noop transform', testGeneratorInput, 3, true, true);6667// eslint-disable-next-line max-params47const testGeneratorInputPipe = async (t, useShortcutProperty, objectMode, input) => {48 const {generator, output} = getInputObjectMode(objectMode);49 const childProcess = execa('stdin-fd.js', ['0'], getStdio(0, generator));68const testGeneratorInputPipe = async (t, useShortcutProperty, objectMode, addNoopTransform, input) => {69 const {generators, output} = getInputObjectMode(objectMode, addNoopTransform);70 const childProcess = execa('stdin-fd.js', ['0'], getStdio(0, generators));71 const stream = useShortcutProperty ? childProcess.stdin : childProcess.stdio[0];72 stream.end(...input);73 const {stdout} = await childProcess;74 t.is(stdout, output);75};7656test('Can use generators with childProcess.stdio[0] and default encoding', testGeneratorInputPipe, false, false, [foobarString, 'utf8']);57test('Can use generators with childProcess.stdin and default encoding', testGeneratorInputPipe, true, false, [foobarString, 'utf8']);58test('Can use generators with childProcess.stdio[0] and encoding "buffer"', testGeneratorInputPipe, false, false, [foobarBuffer, 'buffer']);59test('Can use generators with childProcess.stdin and encoding "buffer"', testGeneratorInputPipe, true, false, [foobarBuffer, 'buffer']);60test('Can use generators with childProcess.stdio[0] and encoding "hex"', testGeneratorInputPipe, false, false, [foobarHex, 'hex']);61test('Can use generators with childProcess.stdin and encoding "hex"', testGeneratorInputPipe, true, false, [foobarHex, 'hex']);62test('Can use generators with childProcess.stdio[0], objectMode', testGeneratorInputPipe, false, true, [foobarObject]);63test('Can use generators with childProcess.stdin, objectMode', testGeneratorInputPipe, true, true, [foobarObject]);77test('Can use generators with childProcess.stdio[0] and default encoding', testGeneratorInputPipe, false, false, false, [foobarString, 'utf8']);78test('Can use generators with childProcess.stdin and default encoding', testGeneratorInputPipe, true, false, false, [foobarString, 'utf8']);79test('Can use generators with childProcess.stdio[0] and encoding "buffer"', testGeneratorInputPipe, false, false, false, [foobarBuffer, 'buffer']);80test('Can use generators with childProcess.stdin and encoding "buffer"', testGeneratorInputPipe, true, false, false, [foobarBuffer, 'buffer']);81test('Can use generators with childProcess.stdio[0] and encoding "hex"', testGeneratorInputPipe, false, false, false, [foobarHex, 'hex']);82test('Can use generators with childProcess.stdin and encoding "hex"', testGeneratorInputPipe, true, false, false, [foobarHex, 'hex']);83test('Can use generators with childProcess.stdio[0], objectMode', testGeneratorInputPipe, false, true, false, [foobarObject]);84test('Can use generators with childProcess.stdin, objectMode', testGeneratorInputPipe, true, true, false, [foobarObject]);85test('Can use generators with childProcess.stdio[0] and default encoding, noop transform', testGeneratorInputPipe, false, false, true, [foobarString, 'utf8']);86test('Can use generators with childProcess.stdin and default encoding, noop transform', testGeneratorInputPipe, true, false, true, [foobarString, 'utf8']);87test('Can use generators with childProcess.stdio[0] and encoding "buffer", noop transform', testGeneratorInputPipe, false, false, true, [foobarBuffer, 'buffer']);88test('Can use generators with childProcess.stdin and encoding "buffer", noop transform', testGeneratorInputPipe, true, false, true, [foobarBuffer, 'buffer']);89test('Can use generators with childProcess.stdio[0] and encoding "hex", noop transform', testGeneratorInputPipe, false, false, true, [foobarHex, 'hex']);90test('Can use generators with childProcess.stdin and encoding "hex", noop transform', testGeneratorInputPipe, true, false, true, [foobarHex, 'hex']);91test('Can use generators with childProcess.stdio[0], objectMode, noop transform', testGeneratorInputPipe, false, true, true, [foobarObject]);92test('Can use generators with childProcess.stdin, objectMode, noop transform', testGeneratorInputPipe, true, true, true, [foobarObject]);9365const testGeneratorStdioInputPipe = async (t, objectMode) => {66 const {input, generator, output} = getInputObjectMode(objectMode);67 const childProcess = execa('stdin-fd.js', ['3'], getStdio(3, [new Uint8Array(), generator]));94const testGeneratorStdioInputPipe = async (t, objectMode, addNoopTransform) => {95 const {input, generators, output} = getInputObjectMode(objectMode, addNoopTransform);96 const childProcess = execa('stdin-fd.js', ['3'], getStdio(3, [new Uint8Array(), ...generators]));97 childProcess.stdio[3].write(Array.isArray(input) ? input[0] : input);98 const {stdout} = await childProcess;99 t.is(stdout, output);100};10173test('Can use generators with childProcess.stdio[*] as input', testGeneratorStdioInputPipe, false);74test('Can use generators with childProcess.stdio[*] as input, objectMode', testGeneratorStdioInputPipe, true);102test('Can use generators with childProcess.stdio[*] as input', testGeneratorStdioInputPipe, false, false);103test('Can use generators with childProcess.stdio[*] as input, objectMode', testGeneratorStdioInputPipe, true, false);104test('Can use generators with childProcess.stdio[*] as input, noop transform', testGeneratorStdioInputPipe, false, true);105test('Can use generators with childProcess.stdio[*] as input, objectMode, noop transform', testGeneratorStdioInputPipe, true, true);106107// eslint-disable-next-line max-params77const testGeneratorOutput = async (t, fdNumber, reject, useShortcutProperty, objectMode) => {78 const {generator, output} = getOutputObjectMode(objectMode);108const testGeneratorOutput = async (t, fdNumber, reject, useShortcutProperty, objectMode, addNoopTransform) => {109 const {generators, output} = getOutputObjectMode(objectMode, addNoopTransform);110 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';80 const {stdout, stderr, stdio} = await execa(fixtureName, [`${fdNumber}`, foobarString], {...getStdio(fdNumber, generator), reject});111 const {stdout, stderr, stdio} = await execa(fixtureName, [`${fdNumber}`, foobarString], {...getStdio(fdNumber, generators), reject});112 const result = useShortcutProperty ? [stdout, stderr][fdNumber - 1] : stdio[fdNumber];113 t.deepEqual(result, output);114};11585test('Can use generators with result.stdio[1]', testGeneratorOutput, 1, true, false, false);86test('Can use generators with result.stdout', testGeneratorOutput, 1, true, true, false);87test('Can use generators with result.stdio[2]', testGeneratorOutput, 2, true, false, false);88test('Can use generators with result.stderr', testGeneratorOutput, 2, true, true, false);89test('Can use generators with result.stdio[*] as output', testGeneratorOutput, 3, true, false, false);90test('Can use generators with error.stdio[1]', testGeneratorOutput, 1, false, false, false);91test('Can use generators with error.stdout', testGeneratorOutput, 1, false, true, false);92test('Can use generators with error.stdio[2]', testGeneratorOutput, 2, false, false, false);93test('Can use generators with error.stderr', testGeneratorOutput, 2, false, true, false);94test('Can use generators with error.stdio[*] as output', testGeneratorOutput, 3, false, false, false);95test('Can use generators with result.stdio[1], objectMode', testGeneratorOutput, 1, true, false, true);96test('Can use generators with result.stdout, objectMode', testGeneratorOutput, 1, true, true, true);97test('Can use generators with result.stdio[2], objectMode', testGeneratorOutput, 2, true, false, true);98test('Can use generators with result.stderr, objectMode', testGeneratorOutput, 2, true, true, true);99test('Can use generators with result.stdio[*] as output, objectMode', testGeneratorOutput, 3, true, false, true);100test('Can use generators with error.stdio[1], objectMode', testGeneratorOutput, 1, false, false, true);101test('Can use generators with error.stdout, objectMode', testGeneratorOutput, 1, false, true, true);102test('Can use generators with error.stdio[2], objectMode', testGeneratorOutput, 2, false, false, true);103test('Can use generators with error.stderr, objectMode', testGeneratorOutput, 2, false, true, true);104test('Can use generators with error.stdio[*] as output, objectMode', testGeneratorOutput, 3, false, false, true);116test('Can use generators with result.stdio[1]', testGeneratorOutput, 1, true, false, false, false);117test('Can use generators with result.stdout', testGeneratorOutput, 1, true, true, false, false);118test('Can use generators with result.stdio[2]', testGeneratorOutput, 2, true, false, false, false);119test('Can use generators with result.stderr', testGeneratorOutput, 2, true, true, false, false);120test('Can use generators with result.stdio[*] as output', testGeneratorOutput, 3, true, false, false, false);121test('Can use generators with error.stdio[1]', testGeneratorOutput, 1, false, false, false, false);122test('Can use generators with error.stdout', testGeneratorOutput, 1, false, true, false, false);123test('Can use generators with error.stdio[2]', testGeneratorOutput, 2, false, false, false, false);124test('Can use generators with error.stderr', testGeneratorOutput, 2, false, true, false, false);125test('Can use generators with error.stdio[*] as output', testGeneratorOutput, 3, false, false, false, false);126test('Can use generators with result.stdio[1], objectMode', testGeneratorOutput, 1, true, false, true, false);127test('Can use generators with result.stdout, objectMode', testGeneratorOutput, 1, true, true, true, false);128test('Can use generators with result.stdio[2], objectMode', testGeneratorOutput, 2, true, false, true, false);129test('Can use generators with result.stderr, objectMode', testGeneratorOutput, 2, true, true, true, false);130test('Can use generators with result.stdio[*] as output, objectMode', testGeneratorOutput, 3, true, false, true, false);131test('Can use generators with error.stdio[1], objectMode', testGeneratorOutput, 1, false, false, true, false);132test('Can use generators with error.stdout, objectMode', testGeneratorOutput, 1, false, true, true, false);133test('Can use generators with error.stdio[2], objectMode', testGeneratorOutput, 2, false, false, true, false);134test('Can use generators with error.stderr, objectMode', testGeneratorOutput, 2, false, true, true, false);135test('Can use generators with error.stdio[*] as output, objectMode', testGeneratorOutput, 3, false, false, true, false);136test('Can use generators with result.stdio[1], noop transform', testGeneratorOutput, 1, true, false, false, true);137test('Can use generators with result.stdout, noop transform', testGeneratorOutput, 1, true, true, false, true);138test('Can use generators with result.stdio[2], noop transform', testGeneratorOutput, 2, true, false, false, true);139test('Can use generators with result.stderr, noop transform', testGeneratorOutput, 2, true, true, false, true);140test('Can use generators with result.stdio[*] as output, noop transform', testGeneratorOutput, 3, true, false, false, true);141test('Can use generators with error.stdio[1], noop transform', testGeneratorOutput, 1, false, false, false, true);142test('Can use generators with error.stdout, noop transform', testGeneratorOutput, 1, false, true, false, true);143test('Can use generators with error.stdio[2], noop transform', testGeneratorOutput, 2, false, false, false, true);144test('Can use generators with error.stderr, noop transform', testGeneratorOutput, 2, false, true, false, true);145test('Can use generators with error.stdio[*] as output, noop transform', testGeneratorOutput, 3, false, false, false, true);146test('Can use generators with result.stdio[1], objectMode, noop transform', testGeneratorOutput, 1, true, false, true, true);147test('Can use generators with result.stdout, objectMode, noop transform', testGeneratorOutput, 1, true, true, true, true);148test('Can use generators with result.stdio[2], objectMode, noop transform', testGeneratorOutput, 2, true, false, true, true);149test('Can use generators with result.stderr, objectMode, noop transform', testGeneratorOutput, 2, true, true, true, true);150test('Can use generators with result.stdio[*] as output, objectMode, noop transform', testGeneratorOutput, 3, true, false, true, true);151test('Can use generators with error.stdio[1], objectMode, noop transform', testGeneratorOutput, 1, false, false, true, true);152test('Can use generators with error.stdout, objectMode, noop transform', testGeneratorOutput, 1, false, true, true, true);153test('Can use generators with error.stdio[2], objectMode, noop transform', testGeneratorOutput, 2, false, false, true, true);154test('Can use generators with error.stderr, objectMode, noop transform', testGeneratorOutput, 2, false, true, true, true);155test('Can use generators with error.stdio[*] as output, objectMode, noop transform', testGeneratorOutput, 3, false, false, true, true);156157// eslint-disable-next-line max-params106const testGeneratorOutputPipe = async (t, fdNumber, useShortcutProperty, objectMode) => {107 const {generator, output, getStreamMethod} = getOutputObjectMode(objectMode);108 const childProcess = execa('noop-fd.js', [`${fdNumber}`, foobarString], {...getStdio(fdNumber, generator), buffer: false});158const testGeneratorOutputPipe = async (t, fdNumber, useShortcutProperty, objectMode, addNoopTransform) => {159 const {generators, output, getStreamMethod} = getOutputObjectMode(objectMode, addNoopTransform);160 const childProcess = execa('noop-fd.js', [`${fdNumber}`, foobarString], {...getStdio(fdNumber, generators), buffer: false});161 const stream = useShortcutProperty ? [childProcess.stdout, childProcess.stderr][fdNumber - 1] : childProcess.stdio[fdNumber];162 const [result] = await Promise.all([getStreamMethod(stream), childProcess]);163 t.deepEqual(result, output);164};165114test('Can use generators with childProcess.stdio[1]', testGeneratorOutputPipe, 1, false, false);115test('Can use generators with childProcess.stdout', testGeneratorOutputPipe, 1, true, false);116test('Can use generators with childProcess.stdio[2]', testGeneratorOutputPipe, 2, false, false);117test('Can use generators with childProcess.stderr', testGeneratorOutputPipe, 2, true, false);118test('Can use generators with childProcess.stdio[*] as output', testGeneratorOutputPipe, 3, false, false);119test('Can use generators with childProcess.stdio[1], objectMode', testGeneratorOutputPipe, 1, false, true);120test('Can use generators with childProcess.stdout, objectMode', testGeneratorOutputPipe, 1, true, true);121test('Can use generators with childProcess.stdio[2], objectMode', testGeneratorOutputPipe, 2, false, true);122test('Can use generators with childProcess.stderr, objectMode', testGeneratorOutputPipe, 2, true, true);123test('Can use generators with childProcess.stdio[*] as output, objectMode', testGeneratorOutputPipe, 3, false, true);166test('Can use generators with childProcess.stdio[1]', testGeneratorOutputPipe, 1, false, false, false);167test('Can use generators with childProcess.stdout', testGeneratorOutputPipe, 1, true, false, false);168test('Can use generators with childProcess.stdio[2]', testGeneratorOutputPipe, 2, false, false, false);169test('Can use generators with childProcess.stderr', testGeneratorOutputPipe, 2, true, false, false);170test('Can use generators with childProcess.stdio[*] as output', testGeneratorOutputPipe, 3, false, false, false);171test('Can use generators with childProcess.stdio[1], objectMode', testGeneratorOutputPipe, 1, false, true, false);172test('Can use generators with childProcess.stdout, objectMode', testGeneratorOutputPipe, 1, true, true, false);173test('Can use generators with childProcess.stdio[2], objectMode', testGeneratorOutputPipe, 2, false, true, false);174test('Can use generators with childProcess.stderr, objectMode', testGeneratorOutputPipe, 2, true, true, false);175test('Can use generators with childProcess.stdio[*] as output, objectMode', testGeneratorOutputPipe, 3, false, true, false);176test('Can use generators with childProcess.stdio[1], noop transform', testGeneratorOutputPipe, 1, false, false, true);177test('Can use generators with childProcess.stdout, noop transform', testGeneratorOutputPipe, 1, true, false, true);178test('Can use generators with childProcess.stdio[2], noop transform', testGeneratorOutputPipe, 2, false, false, true);179test('Can use generators with childProcess.stderr, noop transform', testGeneratorOutputPipe, 2, true, false, true);180test('Can use generators with childProcess.stdio[*] as output, noop transform', testGeneratorOutputPipe, 3, false, false, true);181test('Can use generators with childProcess.stdio[1], objectMode, noop transform', testGeneratorOutputPipe, 1, false, true, true);182test('Can use generators with childProcess.stdout, objectMode, noop transform', testGeneratorOutputPipe, 1, true, true, true);183test('Can use generators with childProcess.stdio[2], objectMode, noop transform', testGeneratorOutputPipe, 2, false, true, true);184test('Can use generators with childProcess.stderr, objectMode, noop transform', testGeneratorOutputPipe, 2, true, true, true);185test('Can use generators with childProcess.stdio[*] as output, objectMode, noop transform', testGeneratorOutputPipe, 3, false, true, true);186187const getAllStdioOption = (stdioOption, encoding, objectMode) => {188 if (stdioOption) {189 return 'pipe';
160 unmodified lines