encoding-final.js
This file
Path
test/stdio/encoding-final.js
223 lines · 17.0 KiB · JavaScript
Last changed by
Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac
History of this fileTake it
Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 1–9why, one step back1import {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';Refactor tests (#934)ehmickybefore adoption52c4bde
line 10why, one step back10import {outputObjectGenerator, getOutputsGenerator, addNoopGenerator} from '../helpers/generator.js';Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 11why, one step back11import {foobarString, foobarUint8Array, foobarObject, foobarHex} from '../helpers/input.js';Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 12–21why, one step back1213const 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) {Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac
line 22why, one step back22 const subprocess = execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding});Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 23why, one step back23 const getStreamMethod = encoding === 'buffer' ? getStreamAsBuffer : getStream;Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 24why, one step back24 const result = await getStreamMethod(subprocess.stdio[fdNumber]);Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 25why, one step back25 compareValues(t, result, encoding);Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 26why, one step back26 await subprocess;Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 27–100why, one step back27 }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);4950test('can pass encoding "buffer" to stdout', checkEncoding, 'buffer', 1, execa);51test('can pass encoding "utf8" to stdout', checkEncoding, 'utf8', 1, execa);52test('can pass encoding "utf16le" to stdout', checkEncoding, 'utf16le', 1, execa);53test('can pass encoding "latin1" to stdout', checkEncoding, 'latin1', 1, execa);54test('can pass encoding "ascii" to stdout', checkEncoding, 'ascii', 1, execa);55test('can pass encoding "hex" to stdout', checkEncoding, 'hex', 1, execa);56test('can pass encoding "base64" to stdout', checkEncoding, 'base64', 1, execa);57test('can pass encoding "base64url" to stdout', checkEncoding, 'base64url', 1, execa);58test('can pass encoding "buffer" to stderr', checkEncoding, 'buffer', 2, execa);59test('can pass encoding "utf8" to stderr', checkEncoding, 'utf8', 2, execa);60test('can pass encoding "utf16le" to stderr', checkEncoding, 'utf16le', 2, execa);61test('can pass encoding "latin1" to stderr', checkEncoding, 'latin1', 2, execa);62test('can pass encoding "ascii" to stderr', checkEncoding, 'ascii', 2, execa);63test('can pass encoding "hex" to stderr', checkEncoding, 'hex', 2, execa);64test('can pass encoding "base64" to stderr', checkEncoding, 'base64', 2, execa);65test('can pass encoding "base64url" to stderr', checkEncoding, 'base64url', 2, execa);66test('can pass encoding "buffer" to stdio[*]', checkEncoding, 'buffer', 3, execa);67test('can pass encoding "utf8" to stdio[*]', checkEncoding, 'utf8', 3, execa);68test('can pass encoding "utf16le" to stdio[*]', checkEncoding, 'utf16le', 3, execa);69test('can pass encoding "latin1" to stdio[*]', checkEncoding, 'latin1', 3, execa);70test('can pass encoding "ascii" to stdio[*]', checkEncoding, 'ascii', 3, execa);71test('can pass encoding "hex" to stdio[*]', checkEncoding, 'hex', 3, execa);72test('can pass encoding "base64" to stdio[*]', checkEncoding, 'base64', 3, execa);73test('can pass encoding "base64url" to stdio[*]', checkEncoding, 'base64url', 3, execa);74test('can pass encoding "buffer" to stdout - sync', checkEncoding, 'buffer', 1, execaSync);75test('can pass encoding "utf8" to stdout - sync', checkEncoding, 'utf8', 1, execaSync);76test('can pass encoding "utf16le" to stdout - sync', checkEncoding, 'utf16le', 1, execaSync);77test('can pass encoding "latin1" to stdout - sync', checkEncoding, 'latin1', 1, execaSync);78test('can pass encoding "ascii" to stdout - sync', checkEncoding, 'ascii', 1, execaSync);79test('can pass encoding "hex" to stdout - sync', checkEncoding, 'hex', 1, execaSync);80test('can pass encoding "base64" to stdout - sync', checkEncoding, 'base64', 1, execaSync);81test('can pass encoding "base64url" to stdout - sync', checkEncoding, 'base64url', 1, execaSync);82test('can pass encoding "buffer" to stderr - sync', checkEncoding, 'buffer', 2, execaSync);83test('can pass encoding "utf8" to stderr - sync', checkEncoding, 'utf8', 2, execaSync);84test('can pass encoding "utf16le" to stderr - sync', checkEncoding, 'utf16le', 2, execaSync);85test('can pass encoding "latin1" to stderr - sync', checkEncoding, 'latin1', 2, execaSync);86test('can pass encoding "ascii" to stderr - sync', checkEncoding, 'ascii', 2, execaSync);87test('can pass encoding "hex" to stderr - sync', checkEncoding, 'hex', 2, execaSync);88test('can pass encoding "base64" to stderr - sync', checkEncoding, 'base64', 2, execaSync);89test('can pass encoding "base64url" to stderr - sync', checkEncoding, 'base64url', 2, execaSync);90test('can pass encoding "buffer" to stdio[*] - sync', checkEncoding, 'buffer', 3, execaSync);91test('can pass encoding "utf8" to stdio[*] - sync', checkEncoding, 'utf8', 3, execaSync);92test('can pass encoding "utf16le" to stdio[*] - sync', checkEncoding, 'utf16le', 3, execaSync);93test('can pass encoding "latin1" to stdio[*] - sync', checkEncoding, 'latin1', 3, execaSync);94test('can pass encoding "ascii" to stdio[*] - sync', checkEncoding, 'ascii', 3, execaSync);95test('can pass encoding "hex" to stdio[*] - sync', checkEncoding, 'hex', 3, execaSync);96test('can pass encoding "base64" to stdio[*] - sync', checkEncoding, 'base64', 3, execaSync);97test('can pass encoding "base64url" to stdio[*] - sync', checkEncoding, 'base64url', 3, execaSync);9899const foobarArray = ['fo', 'ob', 'ar', '..'];100Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 101–102why, one step back101const testMultibyteCharacters = async (t, objectMode, addNoopTransform, execaMethod) => {102 const {stdout} = await execaMethod('noop.js', {Refactor tests (#934)ehmickybefore adoption52c4bde
line 103why, one step back103 stdout: addNoopGenerator(getOutputsGenerator(foobarArray)(objectMode, true), addNoopTransform, objectMode),Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 104–105why, one step back104 encoding: 'base64',105 });Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b
line 106why, one step back106 if (objectMode) {Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
line 107why, one step back107 t.deepEqual(stdout, foobarArray);Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b
lines 108–111why, one step back108 } else {109 t.is(stdout, btoa(foobarArray.join('')));110 }111};Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 112why, one step back112Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 113–120why, one step back113test('Handle multibyte characters', testMultibyteCharacters, false, false, execa);114test('Handle multibyte characters, noop transform', testMultibyteCharacters, false, true, execa);115test('Handle multibyte characters, with objectMode', testMultibyteCharacters, true, false, execa);116test('Handle multibyte characters, with objectMode, noop transform', testMultibyteCharacters, true, true, execa);117test('Handle multibyte characters, sync', testMultibyteCharacters, false, false, execaSync);118test('Handle multibyte characters, noop transform, sync', testMultibyteCharacters, false, true, execaSync);119test('Handle multibyte characters, with objectMode, sync', testMultibyteCharacters, true, false, execaSync);120test('Handle multibyte characters, with objectMode, noop transform, sync', testMultibyteCharacters, true, true, execaSync);Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 121why, one step back121Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 122–126why, one step back122const testObjectMode = async (t, addNoopTransform, execaMethod) => {123 const {stdout} = await execaMethod('noop.js', {124 stdout: addNoopGenerator(outputObjectGenerator(), addNoopTransform, true),125 encoding: 'base64',126 });Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 127why, one step back127 t.deepEqual(stdout, [foobarObject]);Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b
lines 128–129why, one step back128};129Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 130–133why, one step back130test('Other encodings work with transforms that return objects', testObjectMode, false, execa);131test('Other encodings work with transforms that return objects, noop transform', testObjectMode, true, execa);132test('Other encodings work with transforms that return objects, sync', testObjectMode, false, execaSync);133test('Other encodings work with transforms that return objects, noop transform, sync', testObjectMode, true, execaSync);Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 134why, one step back134Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 135–137why, one step back135// eslint-disable-next-line max-params136const testIgnoredEncoding = async (t, stdoutOption, isUndefined, options, execaMethod) => {137 const {stdout} = await execaMethod('empty.js', {stdout: stdoutOption, ...options});Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 138–142why, one step back138 t.is(stdout === undefined, isUndefined);139};140141const base64Options = {encoding: 'base64'};142const linesOptions = {lines: true};Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 143–178why, one step back143test('Is ignored with other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, base64Options, execa);144test('Is ignored with other encodings and ["ignore"]', testIgnoredEncoding, ['ignore'], true, base64Options, execa);145test('Is ignored with other encodings and "ipc"', testIgnoredEncoding, 'ipc', true, base64Options, execa);146test('Is ignored with other encodings and ["ipc"]', testIgnoredEncoding, ['ipc'], true, base64Options, execa);147test('Is ignored with other encodings and "inherit"', testIgnoredEncoding, 'inherit', true, base64Options, execa);148test('Is ignored with other encodings and ["inherit"]', testIgnoredEncoding, ['inherit'], true, base64Options, execa);149test('Is ignored with other encodings and 1', testIgnoredEncoding, 1, true, base64Options, execa);150test('Is ignored with other encodings and [1]', testIgnoredEncoding, [1], true, base64Options, execa);151test('Is ignored with other encodings and process.stdout', testIgnoredEncoding, process.stdout, true, base64Options, execa);152test('Is ignored with other encodings and [process.stdout]', testIgnoredEncoding, [process.stdout], true, base64Options, execa);153test('Is not ignored with other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, base64Options, execa);154test('Is not ignored with other encodings and ["pipe"]', testIgnoredEncoding, ['pipe'], false, base64Options, execa);155test('Is not ignored with other encodings and "overlapped"', testIgnoredEncoding, 'overlapped', false, base64Options, execa);156test('Is not ignored with other encodings and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false, base64Options, execa);157test('Is not ignored with other encodings and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, base64Options, execa);158test('Is not ignored with other encodings and undefined', testIgnoredEncoding, undefined, false, base64Options, execa);159test('Is not ignored with other encodings and null', testIgnoredEncoding, null, false, base64Options, execa);160test('Is ignored with "lines: true" and "ignore"', testIgnoredEncoding, 'ignore', true, linesOptions, execa);161test('Is ignored with "lines: true" and ["ignore"]', testIgnoredEncoding, ['ignore'], true, linesOptions, execa);162test('Is ignored with "lines: true" and "ipc"', testIgnoredEncoding, 'ipc', true, linesOptions, execa);163test('Is ignored with "lines: true" and ["ipc"]', testIgnoredEncoding, ['ipc'], true, linesOptions, execa);164test('Is ignored with "lines: true" and "inherit"', testIgnoredEncoding, 'inherit', true, linesOptions, execa);165test('Is ignored with "lines: true" and ["inherit"]', testIgnoredEncoding, ['inherit'], true, linesOptions, execa);166test('Is ignored with "lines: true" and 1', testIgnoredEncoding, 1, true, linesOptions, execa);167test('Is ignored with "lines: true" and [1]', testIgnoredEncoding, [1], true, linesOptions, execa);168test('Is ignored with "lines: true" and process.stdout', testIgnoredEncoding, process.stdout, true, linesOptions, execa);169test('Is ignored with "lines: true" and [process.stdout]', testIgnoredEncoding, [process.stdout], true, linesOptions, execa);170test('Is not ignored with "lines: true" and "pipe"', testIgnoredEncoding, 'pipe', false, linesOptions, execa);171test('Is not ignored with "lines: true" and ["pipe"]', testIgnoredEncoding, ['pipe'], false, linesOptions, execa);172test('Is not ignored with "lines: true" and "overlapped"', testIgnoredEncoding, 'overlapped', false, linesOptions, execa);173test('Is not ignored with "lines: true" and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false, linesOptions, execa);174test('Is not ignored with "lines: true" and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, linesOptions, execa);175test('Is not ignored with "lines: true" and undefined', testIgnoredEncoding, undefined, false, linesOptions, execa);176test('Is not ignored with "lines: true" and null', testIgnoredEncoding, null, false, linesOptions, execa);177test('Is ignored with "lines: true", other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, {...base64Options, ...linesOptions}, execa);178test('Is not ignored with "lines: true", other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, {...base64Options, ...linesOptions}, execa);Add support for `lines` option with `execaSync()` (#951)ehmickybefore adoption88bff1f
lines 179–204why, one step back179test('Is ignored with other encodings and "ignore", sync', testIgnoredEncoding, 'ignore', true, base64Options, execaSync);180test('Is ignored with other encodings and ["ignore"], sync', testIgnoredEncoding, ['ignore'], true, base64Options, execaSync);181test('Is ignored with other encodings and "inherit", sync', testIgnoredEncoding, 'inherit', true, base64Options, execaSync);182test('Is ignored with other encodings and ["inherit"], sync', testIgnoredEncoding, ['inherit'], true, base64Options, execaSync);183test('Is ignored with other encodings and 1, sync', testIgnoredEncoding, 1, true, base64Options, execaSync);184test('Is ignored with other encodings and [1], sync', testIgnoredEncoding, [1], true, base64Options, execaSync);185test('Is ignored with other encodings and process.stdout, sync', testIgnoredEncoding, process.stdout, true, base64Options, execaSync);186test('Is ignored with other encodings and [process.stdout], sync', testIgnoredEncoding, [process.stdout], true, base64Options, execaSync);187test('Is not ignored with other encodings and "pipe", sync', testIgnoredEncoding, 'pipe', false, base64Options, execaSync);188test('Is not ignored with other encodings and ["pipe"], sync', testIgnoredEncoding, ['pipe'], false, base64Options, execaSync);189test('Is not ignored with other encodings and undefined, sync', testIgnoredEncoding, undefined, false, base64Options, execaSync);190test('Is not ignored with other encodings and null, sync', testIgnoredEncoding, null, false, base64Options, execaSync);191test('Is ignored with "lines: true" and "ignore", sync', testIgnoredEncoding, 'ignore', true, linesOptions, execaSync);192test('Is ignored with "lines: true" and ["ignore"], sync', testIgnoredEncoding, ['ignore'], true, linesOptions, execaSync);193test('Is ignored with "lines: true" and "inherit", sync', testIgnoredEncoding, 'inherit', true, linesOptions, execaSync);194test('Is ignored with "lines: true" and ["inherit"], sync', testIgnoredEncoding, ['inherit'], true, linesOptions, execaSync);195test('Is ignored with "lines: true" and 1, sync', testIgnoredEncoding, 1, true, linesOptions, execaSync);196test('Is ignored with "lines: true" and [1], sync', testIgnoredEncoding, [1], true, linesOptions, execaSync);197test('Is ignored with "lines: true" and process.stdout, sync', testIgnoredEncoding, process.stdout, true, linesOptions, execaSync);198test('Is ignored with "lines: true" and [process.stdout], sync', testIgnoredEncoding, [process.stdout], true, linesOptions, execaSync);199test('Is not ignored with "lines: true" and "pipe", sync', testIgnoredEncoding, 'pipe', false, linesOptions, execaSync);200test('Is not ignored with "lines: true" and ["pipe"], sync', testIgnoredEncoding, ['pipe'], false, linesOptions, execaSync);201test('Is not ignored with "lines: true" and undefined, sync', testIgnoredEncoding, undefined, false, linesOptions, execaSync);202test('Is not ignored with "lines: true" and null, sync', testIgnoredEncoding, null, false, linesOptions, execaSync);203test('Is ignored with "lines: true", other encodings and "ignore", sync', testIgnoredEncoding, 'ignore', true, {...base64Options, ...linesOptions}, execaSync);204test('Is not ignored with "lines: true", other encodings and "pipe", sync', testIgnoredEncoding, 'pipe', false, {...base64Options, ...linesOptions}, execaSync);Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 205–223why, one step back205206// eslint-disable-next-line max-params207const testEncodingInput = async (t, input, expectedStdout, encoding, execaMethod) => {208 const {stdout} = await execaMethod('stdin.js', {input, encoding});209 t.deepEqual(stdout, expectedStdout);210};211212test('Can use string input', testEncodingInput, foobarString, foobarString, 'utf8', execa);213test('Can use Uint8Array input', testEncodingInput, foobarUint8Array, foobarString, 'utf8', execa);214test('Can use string input, encoding "buffer"', testEncodingInput, foobarString, foobarUint8Array, 'buffer', execa);215test('Can use Uint8Array input, encoding "buffer"', testEncodingInput, foobarUint8Array, foobarUint8Array, 'buffer', execa);216test('Can use string input, encoding "hex"', testEncodingInput, foobarString, foobarHex, 'hex', execa);217test('Can use Uint8Array input, encoding "hex"', testEncodingInput, foobarUint8Array, foobarHex, 'hex', execa);218test('Can use string input, sync', testEncodingInput, foobarString, foobarString, 'utf8', execaSync);219test('Can use Uint8Array input, sync', testEncodingInput, foobarUint8Array, foobarString, 'utf8', execaSync);220test('Can use string input, encoding "buffer", sync', testEncodingInput, foobarString, foobarUint8Array, 'buffer', execaSync);221test('Can use Uint8Array input, encoding "buffer", sync', testEncodingInput, foobarUint8Array, foobarUint8Array, 'buffer', execaSync);222test('Can use string input, encoding "hex", sync', testEncodingInput, foobarString, foobarHex, 'hex', execaSync);223test('Can use Uint8Array input, encoding "hex", sync', testEncodingInput, foobarUint8Array, foobarHex, 'hex', execaSync);