Skip to content
Myr

Rename 2 files with wrong file names (#865)

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

authored by ehmicky · committed by GitHub · parent 2ef9e86 · Browse files · 10 files+491 added−491 removed
This commit

State

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

Diff stats

10 files+491 added−491 removed

  • tests:+384 added−384 removed
  • other:+107 added−107 removed

10 files+491 added−491 removed

deleted:lib/stdio/encoding-end.js-49 removed
added:lib/stdio/encoding-final.js+55 added
deleted:lib/stdio/encoding-start.js-55 removed
added:lib/stdio/encoding-transform.js+49 added
modified:lib/stdio/generator.js+2 added-2 removed
1import {generatorsToTransform} from './transform.js';2import {getEncodingStartGenerator} from './encoding-end.js';2import {getEncodingTransformGenerator} from './encoding-transform.js';3import {getLinesGenerator} from './lines.js';4import {pipeStreams} from './pipeline.js';5import {isGeneratorOptions, isAsyncGenerator} from './type.js';6import {getValidateTransformReturn} from './validate.js';
65 unmodified lines
72 encoding,73 optionName,74}) => {75 const generators = [76 getEncodingStartGenerator(encoding),76 getEncodingTransformGenerator(encoding),77 getLinesGenerator(encoding, binary),78 {transform, final},79 {transform: getValidateTransformReturn(readableObjectMode, optionName)},80 ].filter(Boolean);
23 unmodified lines
modified:lib/stdio/handle.js+1 added-1 removed
2 unmodified lines
3import {normalizeStdio} from './option.js';4import {handleNativeStream} from './native.js';5import {handleInputOptions} from './input.js';6import {handleStreamsLines} from './lines.js';7import {handleStreamsEncoding} from './encoding-start.js';7import {handleStreamsEncoding} from './encoding-final.js';8import {normalizeGenerators} from './generator.js';9import {forwardStdio} from './forward.js';1011// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync mode
85 unmodified lines
deleted:test/stdio/encoding-end.js-196 removed
1import {Buffer} from 'node:buffer';2import {exec} from 'node:child_process';3import process from 'node:process';4import {promisify} from 'node:util';5import test from 'ava';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';11import {foobarObject} from '../helpers/input.js';1213const pExec = promisify(exec);1415setFixtureDir();1617const checkEncoding = async (t, encoding, fdNumber, execaMethod) => {18 const {stdio} = await execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding});19 compareValues(t, stdio[fdNumber], encoding);2021 if (execaMethod !== execaSync) {22 const childProcess = execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding, buffer: false});23 const getStreamMethod = encoding === 'buffer' ? getStreamAsBuffer : getStream;24 const result = await getStreamMethod(childProcess.stdio[fdNumber]);25 compareValues(t, result, encoding);26 await childProcess;27 }2829 if (fdNumber === 3) {30 return;31 }3233 const {stdout, stderr} = await pExec(`node noop-fd.js ${fdNumber} ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});34 compareValues(t, fdNumber === 1 ? stdout : stderr, encoding);35};3637const compareValues = (t, value, encoding) => {38 if (encoding === 'buffer') {39 t.true(ArrayBuffer.isView(value));40 t.true(BUFFER_TO_ENCODE.equals(value));41 } else {42 t.is(value, BUFFER_TO_ENCODE.toString(encoding));43 }44};4546// This string gives different outputs with each encoding type47const STRING_TO_ENCODE = '\u1000.';48const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);4950/* eslint-disable unicorn/text-encoding-identifier-case */51test('can pass encoding "buffer" to stdout', checkEncoding, 'buffer', 1, execa);52test('can pass encoding "utf8" to stdout', checkEncoding, 'utf8', 1, execa);53test('can pass encoding "utf-8" to stdout', checkEncoding, 'utf-8', 1, execa);54test('can pass encoding "utf16le" to stdout', checkEncoding, 'utf16le', 1, execa);55test('can pass encoding "utf-16le" to stdout', checkEncoding, 'utf-16le', 1, execa);56test('can pass encoding "ucs2" to stdout', checkEncoding, 'ucs2', 1, execa);57test('can pass encoding "ucs-2" to stdout', checkEncoding, 'ucs-2', 1, execa);58test('can pass encoding "latin1" to stdout', checkEncoding, 'latin1', 1, execa);59test('can pass encoding "binary" to stdout', checkEncoding, 'binary', 1, execa);60test('can pass encoding "ascii" to stdout', checkEncoding, 'ascii', 1, execa);61test('can pass encoding "hex" to stdout', checkEncoding, 'hex', 1, execa);62test('can pass encoding "base64" to stdout', checkEncoding, 'base64', 1, execa);63test('can pass encoding "base64url" to stdout', checkEncoding, 'base64url', 1, execa);64test('can pass encoding "buffer" to stderr', checkEncoding, 'buffer', 2, execa);65test('can pass encoding "utf8" to stderr', checkEncoding, 'utf8', 2, execa);66test('can pass encoding "utf-8" to stderr', checkEncoding, 'utf-8', 2, execa);67test('can pass encoding "utf16le" to stderr', checkEncoding, 'utf16le', 2, execa);68test('can pass encoding "utf-16le" to stderr', checkEncoding, 'utf-16le', 2, execa);69test('can pass encoding "ucs2" to stderr', checkEncoding, 'ucs2', 2, execa);70test('can pass encoding "ucs-2" to stderr', checkEncoding, 'ucs-2', 2, execa);71test('can pass encoding "latin1" to stderr', checkEncoding, 'latin1', 2, execa);72test('can pass encoding "binary" to stderr', checkEncoding, 'binary', 2, execa);73test('can pass encoding "ascii" to stderr', checkEncoding, 'ascii', 2, execa);74test('can pass encoding "hex" to stderr', checkEncoding, 'hex', 2, execa);75test('can pass encoding "base64" to stderr', checkEncoding, 'base64', 2, execa);76test('can pass encoding "base64url" to stderr', checkEncoding, 'base64url', 2, execa);77test('can pass encoding "buffer" to stdio[*]', checkEncoding, 'buffer', 3, execa);78test('can pass encoding "utf8" to stdio[*]', checkEncoding, 'utf8', 3, execa);79test('can pass encoding "utf-8" to stdio[*]', checkEncoding, 'utf-8', 3, execa);80test('can pass encoding "utf16le" to stdio[*]', checkEncoding, 'utf16le', 3, execa);81test('can pass encoding "utf-16le" to stdio[*]', checkEncoding, 'utf-16le', 3, execa);82test('can pass encoding "ucs2" to stdio[*]', checkEncoding, 'ucs2', 3, execa);83test('can pass encoding "ucs-2" to stdio[*]', checkEncoding, 'ucs-2', 3, execa);84test('can pass encoding "latin1" to stdio[*]', checkEncoding, 'latin1', 3, execa);85test('can pass encoding "binary" to stdio[*]', checkEncoding, 'binary', 3, execa);86test('can pass encoding "ascii" to stdio[*]', checkEncoding, 'ascii', 3, execa);87test('can pass encoding "hex" to stdio[*]', checkEncoding, 'hex', 3, execa);88test('can pass encoding "base64" to stdio[*]', checkEncoding, 'base64', 3, execa);89test('can pass encoding "base64url" to stdio[*]', checkEncoding, 'base64url', 3, execa);90test('can pass encoding "buffer" to stdout - sync', checkEncoding, 'buffer', 1, execaSync);91test('can pass encoding "utf8" to stdout - sync', checkEncoding, 'utf8', 1, execaSync);92test('can pass encoding "utf-8" to stdout - sync', checkEncoding, 'utf-8', 1, execaSync);93test('can pass encoding "utf16le" to stdout - sync', checkEncoding, 'utf16le', 1, execaSync);94test('can pass encoding "utf-16le" to stdout - sync', checkEncoding, 'utf-16le', 1, execaSync);95test('can pass encoding "ucs2" to stdout - sync', checkEncoding, 'ucs2', 1, execaSync);96test('can pass encoding "ucs-2" to stdout - sync', checkEncoding, 'ucs-2', 1, execaSync);97test('can pass encoding "latin1" to stdout - sync', checkEncoding, 'latin1', 1, execaSync);98test('can pass encoding "binary" to stdout - sync', checkEncoding, 'binary', 1, execaSync);99test('can pass encoding "ascii" to stdout - sync', checkEncoding, 'ascii', 1, execaSync);100test('can pass encoding "hex" to stdout - sync', checkEncoding, 'hex', 1, execaSync);101test('can pass encoding "base64" to stdout - sync', checkEncoding, 'base64', 1, execaSync);102test('can pass encoding "base64url" to stdout - sync', checkEncoding, 'base64url', 1, execaSync);103test('can pass encoding "buffer" to stderr - sync', checkEncoding, 'buffer', 2, execaSync);104test('can pass encoding "utf8" to stderr - sync', checkEncoding, 'utf8', 2, execaSync);105test('can pass encoding "utf-8" to stderr - sync', checkEncoding, 'utf-8', 2, execaSync);106test('can pass encoding "utf16le" to stderr - sync', checkEncoding, 'utf16le', 2, execaSync);107test('can pass encoding "utf-16le" to stderr - sync', checkEncoding, 'utf-16le', 2, execaSync);108test('can pass encoding "ucs2" to stderr - sync', checkEncoding, 'ucs2', 2, execaSync);109test('can pass encoding "ucs-2" to stderr - sync', checkEncoding, 'ucs-2', 2, execaSync);110test('can pass encoding "latin1" to stderr - sync', checkEncoding, 'latin1', 2, execaSync);111test('can pass encoding "binary" to stderr - sync', checkEncoding, 'binary', 2, execaSync);112test('can pass encoding "ascii" to stderr - sync', checkEncoding, 'ascii', 2, execaSync);113test('can pass encoding "hex" to stderr - sync', checkEncoding, 'hex', 2, execaSync);114test('can pass encoding "base64" to stderr - sync', checkEncoding, 'base64', 2, execaSync);115test('can pass encoding "base64url" to stderr - sync', checkEncoding, 'base64url', 2, execaSync);116test('can pass encoding "buffer" to stdio[*] - sync', checkEncoding, 'buffer', 3, execaSync);117test('can pass encoding "utf8" to stdio[*] - sync', checkEncoding, 'utf8', 3, execaSync);118test('can pass encoding "utf-8" to stdio[*] - sync', checkEncoding, 'utf-8', 3, execaSync);119test('can pass encoding "utf16le" to stdio[*] - sync', checkEncoding, 'utf16le', 3, execaSync);120test('can pass encoding "utf-16le" to stdio[*] - sync', checkEncoding, 'utf-16le', 3, execaSync);121test('can pass encoding "ucs2" to stdio[*] - sync', checkEncoding, 'ucs2', 3, execaSync);122test('can pass encoding "ucs-2" to stdio[*] - sync', checkEncoding, 'ucs-2', 3, execaSync);123test('can pass encoding "latin1" to stdio[*] - sync', checkEncoding, 'latin1', 3, execaSync);124test('can pass encoding "binary" to stdio[*] - sync', checkEncoding, 'binary', 3, execaSync);125test('can pass encoding "ascii" to stdio[*] - sync', checkEncoding, 'ascii', 3, execaSync);126test('can pass encoding "hex" to stdio[*] - sync', checkEncoding, 'hex', 3, execaSync);127test('can pass encoding "base64" to stdio[*] - sync', checkEncoding, 'base64', 3, execaSync);128test('can pass encoding "base64url" to stdio[*] - sync', checkEncoding, 'base64url', 3, execaSync);129/* eslint-enable unicorn/text-encoding-identifier-case */130131test('validate unknown encodings', t => {132 t.throws(() => {133 execa('noop.js', {encoding: 'unknownEncoding'});134 }, {code: 'ERR_UNKNOWN_ENCODING'});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});143144test('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});148149test('Other encodings work with transforms that return objects', async t => {150 const {stdout} = await execa('noop.js', {stdout: outputObjectGenerator, encoding: 'base64'});151 t.deepEqual(stdout, [foobarObject]);152});153154const testIgnoredEncoding = async (t, stdoutOption, isUndefined, options) => {155 const {stdout} = await execa('empty.js', {stdout: stdoutOption, ...options});156 t.is(stdout === undefined, isUndefined);157};158159const base64Options = {encoding: 'base64'};160const linesOptions = {lines: true};161test('Is ignored with other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, base64Options);162test('Is ignored with other encodings and ["ignore"]', testIgnoredEncoding, ['ignore'], true, base64Options);163test('Is ignored with other encodings and "ipc"', testIgnoredEncoding, 'ipc', true, base64Options);164test('Is ignored with other encodings and ["ipc"]', testIgnoredEncoding, ['ipc'], true, base64Options);165test('Is ignored with other encodings and "inherit"', testIgnoredEncoding, 'inherit', true, base64Options);166test('Is ignored with other encodings and ["inherit"]', testIgnoredEncoding, ['inherit'], true, base64Options);167test('Is ignored with other encodings and 1', testIgnoredEncoding, 1, true, base64Options);168test('Is ignored with other encodings and [1]', testIgnoredEncoding, [1], true, base64Options);169test('Is ignored with other encodings and process.stdout', testIgnoredEncoding, process.stdout, true, base64Options);170test('Is ignored with other encodings and [process.stdout]', testIgnoredEncoding, [process.stdout], true, base64Options);171test('Is not ignored with other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, base64Options);172test('Is not ignored with other encodings and ["pipe"]', testIgnoredEncoding, ['pipe'], false, base64Options);173test('Is not ignored with other encodings and "overlapped"', testIgnoredEncoding, 'overlapped', false, base64Options);174test('Is not ignored with other encodings and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false, base64Options);175test('Is not ignored with other encodings and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, base64Options);176test('Is not ignored with other encodings and undefined', testIgnoredEncoding, undefined, false, base64Options);177test('Is not ignored with other encodings and null', testIgnoredEncoding, null, false, base64Options);178test('Is ignored with "lines: true" and "ignore"', testIgnoredEncoding, 'ignore', true, linesOptions);179test('Is ignored with "lines: true" and ["ignore"]', testIgnoredEncoding, ['ignore'], true, linesOptions);180test('Is ignored with "lines: true" and "ipc"', testIgnoredEncoding, 'ipc', true, linesOptions);181test('Is ignored with "lines: true" and ["ipc"]', testIgnoredEncoding, ['ipc'], true, linesOptions);182test('Is ignored with "lines: true" and "inherit"', testIgnoredEncoding, 'inherit', true, linesOptions);183test('Is ignored with "lines: true" and ["inherit"]', testIgnoredEncoding, ['inherit'], true, linesOptions);184test('Is ignored with "lines: true" and 1', testIgnoredEncoding, 1, true, linesOptions);185test('Is ignored with "lines: true" and [1]', testIgnoredEncoding, [1], true, linesOptions);186test('Is ignored with "lines: true" and process.stdout', testIgnoredEncoding, process.stdout, true, linesOptions);187test('Is ignored with "lines: true" and [process.stdout]', testIgnoredEncoding, [process.stdout], true, linesOptions);188test('Is not ignored with "lines: true" and "pipe"', testIgnoredEncoding, 'pipe', false, linesOptions);189test('Is not ignored with "lines: true" and ["pipe"]', testIgnoredEncoding, ['pipe'], false, linesOptions);190test('Is not ignored with "lines: true" and "overlapped"', testIgnoredEncoding, 'overlapped', false, linesOptions);191test('Is not ignored with "lines: true" and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false, linesOptions);192test('Is not ignored with "lines: true" and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, linesOptions);193test('Is not ignored with "lines: true" and undefined', testIgnoredEncoding, undefined, false, linesOptions);194test('Is not ignored with "lines: true" and null', testIgnoredEncoding, null, false, linesOptions);195test('Is ignored with "lines: true", other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, {...base64Options, ...linesOptions});196test('Is not ignored with "lines: true", other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, {...base64Options, ...linesOptions});
added:test/stdio/encoding-final.js+196 added
1import {Buffer} from 'node:buffer';2import {exec} from 'node:child_process';3import process from 'node:process';4import {promisify} from 'node:util';5import test from 'ava';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';11import {foobarObject} from '../helpers/input.js';1213const pExec = promisify(exec);1415setFixtureDir();1617const checkEncoding = async (t, encoding, fdNumber, execaMethod) => {18 const {stdio} = await execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding});19 compareValues(t, stdio[fdNumber], encoding);2021 if (execaMethod !== execaSync) {22 const childProcess = execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding, buffer: false});23 const getStreamMethod = encoding === 'buffer' ? getStreamAsBuffer : getStream;24 const result = await getStreamMethod(childProcess.stdio[fdNumber]);25 compareValues(t, result, encoding);26 await childProcess;27 }2829 if (fdNumber === 3) {30 return;31 }3233 const {stdout, stderr} = await pExec(`node noop-fd.js ${fdNumber} ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});34 compareValues(t, fdNumber === 1 ? stdout : stderr, encoding);35};3637const compareValues = (t, value, encoding) => {38 if (encoding === 'buffer') {39 t.true(ArrayBuffer.isView(value));40 t.true(BUFFER_TO_ENCODE.equals(value));41 } else {42 t.is(value, BUFFER_TO_ENCODE.toString(encoding));43 }44};4546// This string gives different outputs with each encoding type47const STRING_TO_ENCODE = '\u1000.';48const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);4950/* eslint-disable unicorn/text-encoding-identifier-case */51test('can pass encoding "buffer" to stdout', checkEncoding, 'buffer', 1, execa);52test('can pass encoding "utf8" to stdout', checkEncoding, 'utf8', 1, execa);53test('can pass encoding "utf-8" to stdout', checkEncoding, 'utf-8', 1, execa);54test('can pass encoding "utf16le" to stdout', checkEncoding, 'utf16le', 1, execa);55test('can pass encoding "utf-16le" to stdout', checkEncoding, 'utf-16le', 1, execa);56test('can pass encoding "ucs2" to stdout', checkEncoding, 'ucs2', 1, execa);57test('can pass encoding "ucs-2" to stdout', checkEncoding, 'ucs-2', 1, execa);58test('can pass encoding "latin1" to stdout', checkEncoding, 'latin1', 1, execa);59test('can pass encoding "binary" to stdout', checkEncoding, 'binary', 1, execa);60test('can pass encoding "ascii" to stdout', checkEncoding, 'ascii', 1, execa);61test('can pass encoding "hex" to stdout', checkEncoding, 'hex', 1, execa);62test('can pass encoding "base64" to stdout', checkEncoding, 'base64', 1, execa);63test('can pass encoding "base64url" to stdout', checkEncoding, 'base64url', 1, execa);64test('can pass encoding "buffer" to stderr', checkEncoding, 'buffer', 2, execa);65test('can pass encoding "utf8" to stderr', checkEncoding, 'utf8', 2, execa);66test('can pass encoding "utf-8" to stderr', checkEncoding, 'utf-8', 2, execa);67test('can pass encoding "utf16le" to stderr', checkEncoding, 'utf16le', 2, execa);68test('can pass encoding "utf-16le" to stderr', checkEncoding, 'utf-16le', 2, execa);69test('can pass encoding "ucs2" to stderr', checkEncoding, 'ucs2', 2, execa);70test('can pass encoding "ucs-2" to stderr', checkEncoding, 'ucs-2', 2, execa);71test('can pass encoding "latin1" to stderr', checkEncoding, 'latin1', 2, execa);72test('can pass encoding "binary" to stderr', checkEncoding, 'binary', 2, execa);73test('can pass encoding "ascii" to stderr', checkEncoding, 'ascii', 2, execa);74test('can pass encoding "hex" to stderr', checkEncoding, 'hex', 2, execa);75test('can pass encoding "base64" to stderr', checkEncoding, 'base64', 2, execa);76test('can pass encoding "base64url" to stderr', checkEncoding, 'base64url', 2, execa);77test('can pass encoding "buffer" to stdio[*]', checkEncoding, 'buffer', 3, execa);78test('can pass encoding "utf8" to stdio[*]', checkEncoding, 'utf8', 3, execa);79test('can pass encoding "utf-8" to stdio[*]', checkEncoding, 'utf-8', 3, execa);80test('can pass encoding "utf16le" to stdio[*]', checkEncoding, 'utf16le', 3, execa);81test('can pass encoding "utf-16le" to stdio[*]', checkEncoding, 'utf-16le', 3, execa);82test('can pass encoding "ucs2" to stdio[*]', checkEncoding, 'ucs2', 3, execa);83test('can pass encoding "ucs-2" to stdio[*]', checkEncoding, 'ucs-2', 3, execa);84test('can pass encoding "latin1" to stdio[*]', checkEncoding, 'latin1', 3, execa);85test('can pass encoding "binary" to stdio[*]', checkEncoding, 'binary', 3, execa);86test('can pass encoding "ascii" to stdio[*]', checkEncoding, 'ascii', 3, execa);87test('can pass encoding "hex" to stdio[*]', checkEncoding, 'hex', 3, execa);88test('can pass encoding "base64" to stdio[*]', checkEncoding, 'base64', 3, execa);89test('can pass encoding "base64url" to stdio[*]', checkEncoding, 'base64url', 3, execa);90test('can pass encoding "buffer" to stdout - sync', checkEncoding, 'buffer', 1, execaSync);91test('can pass encoding "utf8" to stdout - sync', checkEncoding, 'utf8', 1, execaSync);92test('can pass encoding "utf-8" to stdout - sync', checkEncoding, 'utf-8', 1, execaSync);93test('can pass encoding "utf16le" to stdout - sync', checkEncoding, 'utf16le', 1, execaSync);94test('can pass encoding "utf-16le" to stdout - sync', checkEncoding, 'utf-16le', 1, execaSync);95test('can pass encoding "ucs2" to stdout - sync', checkEncoding, 'ucs2', 1, execaSync);96test('can pass encoding "ucs-2" to stdout - sync', checkEncoding, 'ucs-2', 1, execaSync);97test('can pass encoding "latin1" to stdout - sync', checkEncoding, 'latin1', 1, execaSync);98test('can pass encoding "binary" to stdout - sync', checkEncoding, 'binary', 1, execaSync);99test('can pass encoding "ascii" to stdout - sync', checkEncoding, 'ascii', 1, execaSync);100test('can pass encoding "hex" to stdout - sync', checkEncoding, 'hex', 1, execaSync);101test('can pass encoding "base64" to stdout - sync', checkEncoding, 'base64', 1, execaSync);102test('can pass encoding "base64url" to stdout - sync', checkEncoding, 'base64url', 1, execaSync);103test('can pass encoding "buffer" to stderr - sync', checkEncoding, 'buffer', 2, execaSync);104test('can pass encoding "utf8" to stderr - sync', checkEncoding, 'utf8', 2, execaSync);105test('can pass encoding "utf-8" to stderr - sync', checkEncoding, 'utf-8', 2, execaSync);106test('can pass encoding "utf16le" to stderr - sync', checkEncoding, 'utf16le', 2, execaSync);107test('can pass encoding "utf-16le" to stderr - sync', checkEncoding, 'utf-16le', 2, execaSync);108test('can pass encoding "ucs2" to stderr - sync', checkEncoding, 'ucs2', 2, execaSync);109test('can pass encoding "ucs-2" to stderr - sync', checkEncoding, 'ucs-2', 2, execaSync);110test('can pass encoding "latin1" to stderr - sync', checkEncoding, 'latin1', 2, execaSync);111test('can pass encoding "binary" to stderr - sync', checkEncoding, 'binary', 2, execaSync);112test('can pass encoding "ascii" to stderr - sync', checkEncoding, 'ascii', 2, execaSync);113test('can pass encoding "hex" to stderr - sync', checkEncoding, 'hex', 2, execaSync);114test('can pass encoding "base64" to stderr - sync', checkEncoding, 'base64', 2, execaSync);115test('can pass encoding "base64url" to stderr - sync', checkEncoding, 'base64url', 2, execaSync);116test('can pass encoding "buffer" to stdio[*] - sync', checkEncoding, 'buffer', 3, execaSync);117test('can pass encoding "utf8" to stdio[*] - sync', checkEncoding, 'utf8', 3, execaSync);118test('can pass encoding "utf-8" to stdio[*] - sync', checkEncoding, 'utf-8', 3, execaSync);119test('can pass encoding "utf16le" to stdio[*] - sync', checkEncoding, 'utf16le', 3, execaSync);120test('can pass encoding "utf-16le" to stdio[*] - sync', checkEncoding, 'utf-16le', 3, execaSync);121test('can pass encoding "ucs2" to stdio[*] - sync', checkEncoding, 'ucs2', 3, execaSync);122test('can pass encoding "ucs-2" to stdio[*] - sync', checkEncoding, 'ucs-2', 3, execaSync);123test('can pass encoding "latin1" to stdio[*] - sync', checkEncoding, 'latin1', 3, execaSync);124test('can pass encoding "binary" to stdio[*] - sync', checkEncoding, 'binary', 3, execaSync);125test('can pass encoding "ascii" to stdio[*] - sync', checkEncoding, 'ascii', 3, execaSync);126test('can pass encoding "hex" to stdio[*] - sync', checkEncoding, 'hex', 3, execaSync);127test('can pass encoding "base64" to stdio[*] - sync', checkEncoding, 'base64', 3, execaSync);128test('can pass encoding "base64url" to stdio[*] - sync', checkEncoding, 'base64url', 3, execaSync);129/* eslint-enable unicorn/text-encoding-identifier-case */130131test('validate unknown encodings', t => {132 t.throws(() => {133 execa('noop.js', {encoding: 'unknownEncoding'});134 }, {code: 'ERR_UNKNOWN_ENCODING'});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});143144test('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});148149test('Other encodings work with transforms that return objects', async t => {150 const {stdout} = await execa('noop.js', {stdout: outputObjectGenerator, encoding: 'base64'});151 t.deepEqual(stdout, [foobarObject]);152});153154const testIgnoredEncoding = async (t, stdoutOption, isUndefined, options) => {155 const {stdout} = await execa('empty.js', {stdout: stdoutOption, ...options});156 t.is(stdout === undefined, isUndefined);157};158159const base64Options = {encoding: 'base64'};160const linesOptions = {lines: true};161test('Is ignored with other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, base64Options);162test('Is ignored with other encodings and ["ignore"]', testIgnoredEncoding, ['ignore'], true, base64Options);163test('Is ignored with other encodings and "ipc"', testIgnoredEncoding, 'ipc', true, base64Options);164test('Is ignored with other encodings and ["ipc"]', testIgnoredEncoding, ['ipc'], true, base64Options);165test('Is ignored with other encodings and "inherit"', testIgnoredEncoding, 'inherit', true, base64Options);166test('Is ignored with other encodings and ["inherit"]', testIgnoredEncoding, ['inherit'], true, base64Options);167test('Is ignored with other encodings and 1', testIgnoredEncoding, 1, true, base64Options);168test('Is ignored with other encodings and [1]', testIgnoredEncoding, [1], true, base64Options);169test('Is ignored with other encodings and process.stdout', testIgnoredEncoding, process.stdout, true, base64Options);170test('Is ignored with other encodings and [process.stdout]', testIgnoredEncoding, [process.stdout], true, base64Options);171test('Is not ignored with other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, base64Options);172test('Is not ignored with other encodings and ["pipe"]', testIgnoredEncoding, ['pipe'], false, base64Options);173test('Is not ignored with other encodings and "overlapped"', testIgnoredEncoding, 'overlapped', false, base64Options);174test('Is not ignored with other encodings and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false, base64Options);175test('Is not ignored with other encodings and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, base64Options);176test('Is not ignored with other encodings and undefined', testIgnoredEncoding, undefined, false, base64Options);177test('Is not ignored with other encodings and null', testIgnoredEncoding, null, false, base64Options);178test('Is ignored with "lines: true" and "ignore"', testIgnoredEncoding, 'ignore', true, linesOptions);179test('Is ignored with "lines: true" and ["ignore"]', testIgnoredEncoding, ['ignore'], true, linesOptions);180test('Is ignored with "lines: true" and "ipc"', testIgnoredEncoding, 'ipc', true, linesOptions);181test('Is ignored with "lines: true" and ["ipc"]', testIgnoredEncoding, ['ipc'], true, linesOptions);182test('Is ignored with "lines: true" and "inherit"', testIgnoredEncoding, 'inherit', true, linesOptions);183test('Is ignored with "lines: true" and ["inherit"]', testIgnoredEncoding, ['inherit'], true, linesOptions);184test('Is ignored with "lines: true" and 1', testIgnoredEncoding, 1, true, linesOptions);185test('Is ignored with "lines: true" and [1]', testIgnoredEncoding, [1], true, linesOptions);186test('Is ignored with "lines: true" and process.stdout', testIgnoredEncoding, process.stdout, true, linesOptions);187test('Is ignored with "lines: true" and [process.stdout]', testIgnoredEncoding, [process.stdout], true, linesOptions);188test('Is not ignored with "lines: true" and "pipe"', testIgnoredEncoding, 'pipe', false, linesOptions);189test('Is not ignored with "lines: true" and ["pipe"]', testIgnoredEncoding, ['pipe'], false, linesOptions);190test('Is not ignored with "lines: true" and "overlapped"', testIgnoredEncoding, 'overlapped', false, linesOptions);191test('Is not ignored with "lines: true" and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false, linesOptions);192test('Is not ignored with "lines: true" and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, linesOptions);193test('Is not ignored with "lines: true" and undefined', testIgnoredEncoding, undefined, false, linesOptions);194test('Is not ignored with "lines: true" and null', testIgnoredEncoding, null, false, linesOptions);195test('Is ignored with "lines: true", other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, {...base64Options, ...linesOptions});196test('Is not ignored with "lines: true", other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, {...base64Options, ...linesOptions});
deleted:test/stdio/encoding-start.js-188 removed
1import {Buffer} from 'node:buffer';2import {scheduler} from 'node:timers/promises';3import test from 'ava';4import {execa} from '../../index.js';5import {setFixtureDir} from '../helpers/fixtures-dir.js';6import {getStdio} from '../helpers/stdio.js';7import {foobarString, foobarUint8Array, foobarBuffer, foobarObject} from '../helpers/input.js';8import {noopGenerator, getOutputGenerator, convertTransformToFinal} from '../helpers/generator.js';910setFixtureDir();1112const textEncoder = new TextEncoder();1314const getTypeofGenerator = objectMode => ({15 * transform(line) {16 yield Object.prototype.toString.call(line);17 },18 objectMode,19});2021// eslint-disable-next-line max-params22const testGeneratorFirstEncoding = async (t, input, encoding, output, objectMode) => {23 const childProcess = execa('stdin.js', {stdin: getTypeofGenerator(objectMode), encoding});24 childProcess.stdin.end(input);25 const {stdout} = await childProcess;26 const result = Buffer.from(stdout, encoding).toString();27 t.is(result, output);28};2930test('First generator argument is string with default encoding, with string writes', testGeneratorFirstEncoding, foobarString, 'utf8', '[object String]', false);31test('First generator argument is string with default encoding, with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'utf8', '[object String]', false);32test('First generator argument is string with default encoding, with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'utf8', '[object String]', false);33test('First generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorFirstEncoding, foobarString, 'buffer', '[object Uint8Array]', false);34test('First generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'buffer', '[object Uint8Array]', false);35test('First generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'buffer', '[object Uint8Array]', false);36test('First generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorFirstEncoding, foobarString, 'hex', '[object String]', false);37test('First generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'hex', '[object String]', false);38test('First generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'hex', '[object String]', false);39test('First generator argument can be string with objectMode', testGeneratorFirstEncoding, foobarString, 'utf8', '[object String]', true);40test('First generator argument can be objects with objectMode', testGeneratorFirstEncoding, foobarObject, 'utf8', '[object Object]', true);4142const testEncodingIgnored = async (t, encoding) => {43 const input = Buffer.from(foobarString).toString(encoding);44 const childProcess = execa('stdin.js', {stdin: noopGenerator(true)});45 childProcess.stdin.end(input, encoding);46 const {stdout} = await childProcess;47 t.is(stdout, input);48};4950test('Write call encoding "utf8" is ignored with objectMode', testEncodingIgnored, 'utf8');51test('Write call encoding "utf16le" is ignored with objectMode', testEncodingIgnored, 'utf16le');52test('Write call encoding "hex" is ignored with objectMode', testEncodingIgnored, 'hex');53test('Write call encoding "base64" is ignored with objectMode', testEncodingIgnored, 'base64');5455// eslint-disable-next-line max-params56const testGeneratorNextEncoding = async (t, input, encoding, firstObjectMode, secondObjectMode, expectedType) => {57 const {stdout} = await execa('noop.js', ['other'], {58 stdout: [59 getOutputGenerator(input, firstObjectMode),60 getTypeofGenerator(secondObjectMode),61 ],62 encoding,63 });64 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;65 const output = Buffer.from(typeofChunk, encoding === 'buffer' ? undefined : encoding).toString();66 t.is(output, `[object ${expectedType}]`);67};6869test('Next generator argument is string with default encoding, with string writes', testGeneratorNextEncoding, foobarString, 'utf8', false, false, 'String');70test('Next generator argument is string with default encoding, with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'utf8', true, false, 'String');71test('Next generator argument is string with default encoding, with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'utf8', true, true, 'String');72test('Next generator argument is string with default encoding, with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'utf8', false, false, 'String');73test('Next generator argument is string with default encoding, with Buffer writes, objectMode first', testGeneratorNextEncoding, foobarBuffer, 'utf8', true, false, 'String');74test('Next generator argument is string with default encoding, with Buffer writes, objectMode both', testGeneratorNextEncoding, foobarBuffer, 'utf8', true, true, 'String');75test('Next generator argument is string with default encoding, with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'utf8', false, false, 'String');76test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, false, 'String');77test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, true, 'String');78test('Next generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorNextEncoding, foobarString, 'buffer', false, false, 'Uint8Array');79test('Next generator argument is Uint8Array with encoding "buffer", with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'buffer', true, false, 'Uint8Array');80test('Next generator argument is Uint8Array with encoding "buffer", with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'buffer', true, true, 'Uint8Array');81test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'buffer', false, false, 'Uint8Array');82test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes, objectMode first', testGeneratorNextEncoding, foobarBuffer, 'buffer', true, false, 'Uint8Array');83test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes, objectMode both', testGeneratorNextEncoding, foobarBuffer, 'buffer', true, true, 'Uint8Array');84test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'buffer', false, false, 'Uint8Array');85test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, false, 'Uint8Array');86test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, true, 'Uint8Array');87test('Next generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'String');88test('Next generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'hex', false, false, 'String');89test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'String');90test('Next generator argument is object with default encoding, with object writes, objectMode first', testGeneratorNextEncoding, foobarObject, 'utf8', true, false, 'Object');91test('Next generator argument is object with default encoding, with object writes, objectMode both', testGeneratorNextEncoding, foobarObject, 'utf8', true, true, 'Object');9293const testFirstOutputGeneratorArgument = async (t, fdNumber) => {94 const {stdio} = await execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, getTypeofGenerator(true)));95 t.deepEqual(stdio[fdNumber], ['[object String]']);96};9798test('The first generator with result.stdout does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 1);99test('The first generator with result.stderr does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 2);100test('The first generator with result.stdio[*] does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 3);101102// eslint-disable-next-line max-params103const testGeneratorReturnType = async (t, input, encoding, reject, objectMode, final) => {104 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';105 const {stdout} = await execa(fixtureName, ['1', 'other'], {106 stdout: convertTransformToFinal(getOutputGenerator(input, objectMode), final),107 encoding,108 reject,109 });110 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;111 const output = Buffer.from(typeofChunk, encoding === 'buffer' ? undefined : encoding).toString();112 t.is(output, foobarString);113};114115test('Generator can return string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, false);116test('Generator can return Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, false);117test('Generator can return string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, false);118test('Generator can return Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, false);119test('Generator can return string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, false);120test('Generator can return Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, false);121test('Generator can return string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, false);122test('Generator can return Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, false);123test('Generator can return string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, false);124test('Generator can return Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, false);125test('Generator can return string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, false);126test('Generator can return Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, false);127test('Generator can return string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, false);128test('Generator can return Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, false);129test('Generator can return string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, false);130test('Generator can return Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, false);131test('Generator can return string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, false);132test('Generator can return Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, false);133test('Generator can return string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, false);134test('Generator can return Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, false);135test('Generator can return string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, false);136test('Generator can return Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, false);137test('Generator can return string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, false);138test('Generator can return Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, false);139test('Generator can return final string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, true);140test('Generator can return final Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, true);141test('Generator can return final string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, true);142test('Generator can return final Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, true);143test('Generator can return final string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, true);144test('Generator can return final Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, true);145test('Generator can return final string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, true);146test('Generator can return final Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, true);147test('Generator can return final string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, true);148test('Generator can return final Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, true);149test('Generator can return final string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, true);150test('Generator can return final Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, true);151test('Generator can return final string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, true);152test('Generator can return final Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, true);153test('Generator can return final string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, true);154test('Generator can return final Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, true);155test('Generator can return final string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, true);156test('Generator can return final Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, true);157test('Generator can return final string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, true);158test('Generator can return final Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, true);159test('Generator can return final string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, true);160test('Generator can return final Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, true);161test('Generator can return final string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, true);162test('Generator can return final Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, true);163164const multibyteChar = '\u{1F984}';165const multibyteString = `${multibyteChar}${multibyteChar}`;166const multibyteUint8Array = textEncoder.encode(multibyteString);167const breakingLength = multibyteUint8Array.length * 0.75;168const brokenSymbol = '\uFFFD';169170const testMultibyte = async (t, objectMode) => {171 const childProcess = execa('stdin.js', {stdin: noopGenerator(objectMode)});172 childProcess.stdin.write(multibyteUint8Array.slice(0, breakingLength));173 await scheduler.yield();174 childProcess.stdin.end(multibyteUint8Array.slice(breakingLength));175 const {stdout} = await childProcess;176 t.is(stdout, multibyteString);177};178179test('Generator handles multibyte characters with Uint8Array', testMultibyte, false);180test('Generator handles multibyte characters with Uint8Array, objectMode', testMultibyte, true);181182const testMultibytePartial = async (t, objectMode) => {183 const {stdout} = await execa('stdin.js', {stdin: [multibyteUint8Array.slice(0, breakingLength), noopGenerator(objectMode)]});184 t.is(stdout, `${multibyteChar}${brokenSymbol}`);185};186187test('Generator handles partial multibyte characters with Uint8Array', testMultibytePartial, false);188test('Generator handles partial multibyte characters with Uint8Array, objectMode', testMultibytePartial, true);
added:test/stdio/encoding-transform.js+188 added
1import {Buffer} from 'node:buffer';2import {scheduler} from 'node:timers/promises';3import test from 'ava';4import {execa} from '../../index.js';5import {setFixtureDir} from '../helpers/fixtures-dir.js';6import {getStdio} from '../helpers/stdio.js';7import {foobarString, foobarUint8Array, foobarBuffer, foobarObject} from '../helpers/input.js';8import {noopGenerator, getOutputGenerator, convertTransformToFinal} from '../helpers/generator.js';910setFixtureDir();1112const textEncoder = new TextEncoder();1314const getTypeofGenerator = objectMode => ({15 * transform(line) {16 yield Object.prototype.toString.call(line);17 },18 objectMode,19});2021// eslint-disable-next-line max-params22const testGeneratorFirstEncoding = async (t, input, encoding, output, objectMode) => {23 const childProcess = execa('stdin.js', {stdin: getTypeofGenerator(objectMode), encoding});24 childProcess.stdin.end(input);25 const {stdout} = await childProcess;26 const result = Buffer.from(stdout, encoding).toString();27 t.is(result, output);28};2930test('First generator argument is string with default encoding, with string writes', testGeneratorFirstEncoding, foobarString, 'utf8', '[object String]', false);31test('First generator argument is string with default encoding, with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'utf8', '[object String]', false);32test('First generator argument is string with default encoding, with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'utf8', '[object String]', false);33test('First generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorFirstEncoding, foobarString, 'buffer', '[object Uint8Array]', false);34test('First generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'buffer', '[object Uint8Array]', false);35test('First generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'buffer', '[object Uint8Array]', false);36test('First generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorFirstEncoding, foobarString, 'hex', '[object String]', false);37test('First generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'hex', '[object String]', false);38test('First generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'hex', '[object String]', false);39test('First generator argument can be string with objectMode', testGeneratorFirstEncoding, foobarString, 'utf8', '[object String]', true);40test('First generator argument can be objects with objectMode', testGeneratorFirstEncoding, foobarObject, 'utf8', '[object Object]', true);4142const testEncodingIgnored = async (t, encoding) => {43 const input = Buffer.from(foobarString).toString(encoding);44 const childProcess = execa('stdin.js', {stdin: noopGenerator(true)});45 childProcess.stdin.end(input, encoding);46 const {stdout} = await childProcess;47 t.is(stdout, input);48};4950test('Write call encoding "utf8" is ignored with objectMode', testEncodingIgnored, 'utf8');51test('Write call encoding "utf16le" is ignored with objectMode', testEncodingIgnored, 'utf16le');52test('Write call encoding "hex" is ignored with objectMode', testEncodingIgnored, 'hex');53test('Write call encoding "base64" is ignored with objectMode', testEncodingIgnored, 'base64');5455// eslint-disable-next-line max-params56const testGeneratorNextEncoding = async (t, input, encoding, firstObjectMode, secondObjectMode, expectedType) => {57 const {stdout} = await execa('noop.js', ['other'], {58 stdout: [59 getOutputGenerator(input, firstObjectMode),60 getTypeofGenerator(secondObjectMode),61 ],62 encoding,63 });64 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;65 const output = Buffer.from(typeofChunk, encoding === 'buffer' ? undefined : encoding).toString();66 t.is(output, `[object ${expectedType}]`);67};6869test('Next generator argument is string with default encoding, with string writes', testGeneratorNextEncoding, foobarString, 'utf8', false, false, 'String');70test('Next generator argument is string with default encoding, with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'utf8', true, false, 'String');71test('Next generator argument is string with default encoding, with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'utf8', true, true, 'String');72test('Next generator argument is string with default encoding, with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'utf8', false, false, 'String');73test('Next generator argument is string with default encoding, with Buffer writes, objectMode first', testGeneratorNextEncoding, foobarBuffer, 'utf8', true, false, 'String');74test('Next generator argument is string with default encoding, with Buffer writes, objectMode both', testGeneratorNextEncoding, foobarBuffer, 'utf8', true, true, 'String');75test('Next generator argument is string with default encoding, with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'utf8', false, false, 'String');76test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, false, 'String');77test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, true, 'String');78test('Next generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorNextEncoding, foobarString, 'buffer', false, false, 'Uint8Array');79test('Next generator argument is Uint8Array with encoding "buffer", with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'buffer', true, false, 'Uint8Array');80test('Next generator argument is Uint8Array with encoding "buffer", with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'buffer', true, true, 'Uint8Array');81test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'buffer', false, false, 'Uint8Array');82test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes, objectMode first', testGeneratorNextEncoding, foobarBuffer, 'buffer', true, false, 'Uint8Array');83test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes, objectMode both', testGeneratorNextEncoding, foobarBuffer, 'buffer', true, true, 'Uint8Array');84test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'buffer', false, false, 'Uint8Array');85test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, false, 'Uint8Array');86test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, true, 'Uint8Array');87test('Next generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'String');88test('Next generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'hex', false, false, 'String');89test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'String');90test('Next generator argument is object with default encoding, with object writes, objectMode first', testGeneratorNextEncoding, foobarObject, 'utf8', true, false, 'Object');91test('Next generator argument is object with default encoding, with object writes, objectMode both', testGeneratorNextEncoding, foobarObject, 'utf8', true, true, 'Object');9293const testFirstOutputGeneratorArgument = async (t, fdNumber) => {94 const {stdio} = await execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, getTypeofGenerator(true)));95 t.deepEqual(stdio[fdNumber], ['[object String]']);96};9798test('The first generator with result.stdout does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 1);99test('The first generator with result.stderr does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 2);100test('The first generator with result.stdio[*] does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 3);101102// eslint-disable-next-line max-params103const testGeneratorReturnType = async (t, input, encoding, reject, objectMode, final) => {104 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';105 const {stdout} = await execa(fixtureName, ['1', 'other'], {106 stdout: convertTransformToFinal(getOutputGenerator(input, objectMode), final),107 encoding,108 reject,109 });110 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;111 const output = Buffer.from(typeofChunk, encoding === 'buffer' ? undefined : encoding).toString();112 t.is(output, foobarString);113};114115test('Generator can return string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, false);116test('Generator can return Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, false);117test('Generator can return string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, false);118test('Generator can return Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, false);119test('Generator can return string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, false);120test('Generator can return Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, false);121test('Generator can return string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, false);122test('Generator can return Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, false);123test('Generator can return string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, false);124test('Generator can return Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, false);125test('Generator can return string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, false);126test('Generator can return Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, false);127test('Generator can return string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, false);128test('Generator can return Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, false);129test('Generator can return string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, false);130test('Generator can return Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, false);131test('Generator can return string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, false);132test('Generator can return Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, false);133test('Generator can return string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, false);134test('Generator can return Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, false);135test('Generator can return string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, false);136test('Generator can return Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, false);137test('Generator can return string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, false);138test('Generator can return Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, false);139test('Generator can return final string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, true);140test('Generator can return final Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, true);141test('Generator can return final string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, true);142test('Generator can return final Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, true);143test('Generator can return final string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, true);144test('Generator can return final Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, true);145test('Generator can return final string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, true);146test('Generator can return final Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, true);147test('Generator can return final string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, true);148test('Generator can return final Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, true);149test('Generator can return final string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, true);150test('Generator can return final Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, true);151test('Generator can return final string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, true);152test('Generator can return final Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, true);153test('Generator can return final string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, true);154test('Generator can return final Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, true);155test('Generator can return final string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, true);156test('Generator can return final Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, true);157test('Generator can return final string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, true);158test('Generator can return final Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, true);159test('Generator can return final string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, true);160test('Generator can return final Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, true);161test('Generator can return final string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, true);162test('Generator can return final Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, true);163164const multibyteChar = '\u{1F984}';165const multibyteString = `${multibyteChar}${multibyteChar}`;166const multibyteUint8Array = textEncoder.encode(multibyteString);167const breakingLength = multibyteUint8Array.length * 0.75;168const brokenSymbol = '\uFFFD';169170const testMultibyte = async (t, objectMode) => {171 const childProcess = execa('stdin.js', {stdin: noopGenerator(objectMode)});172 childProcess.stdin.write(multibyteUint8Array.slice(0, breakingLength));173 await scheduler.yield();174 childProcess.stdin.end(multibyteUint8Array.slice(breakingLength));175 const {stdout} = await childProcess;176 t.is(stdout, multibyteString);177};178179test('Generator handles multibyte characters with Uint8Array', testMultibyte, false);180test('Generator handles multibyte characters with Uint8Array, objectMode', testMultibyte, true);181182const testMultibytePartial = async (t, objectMode) => {183 const {stdout} = await execa('stdin.js', {stdin: [multibyteUint8Array.slice(0, breakingLength), noopGenerator(objectMode)]});184 t.is(stdout, `${multibyteChar}${brokenSymbol}`);185};186187test('Generator handles partial multibyte characters with Uint8Array', testMultibytePartial, false);188test('Generator handles partial multibyte characters with Uint8Array, objectMode', testMultibytePartial, true);