Skip to content
Myr

encoding-final.js

This file

Path

test/stdio/encoding-final.js

238 lines · 17.9 KiB · JavaScript

Last changed by

Improve `encoding` option (#958)ehmickybefore adoption507b09c

History of this file

Take it

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 1–5why, one step back
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';

Improve `encoding` option (#958)ehmickybefore adoption507b09c

line 6why, one step back
6import getStream from 'get-stream';

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 7–9why, one step back
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 back
10import {outputObjectGenerator, getOutputsGenerator, addNoopGenerator} from '../helpers/generator.js';

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

line 11why, one step back
11import {foobarString, foobarUint8Array, foobarObject, foobarHex} from '../helpers/input.js';

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 12–21why, one step back
12
13const 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 back
22 const subprocess = execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding});

Improve `encoding` option (#958)ehmickybefore adoption507b09c

lines 23–24why, one step back
23 const result = await getStream(subprocess.stdio[fdNumber]);
24 compareValues(t, result, 'utf8');

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 25why, one step back
25 await subprocess;

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 26–99why, one step back
26 }
2728 if (fdNumber === 3) {29 return;30 }3132 const {stdout, stderr} = await pExec(`node noop-fd.js ${fdNumber} ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});33 compareValues(t, fdNumber === 1 ? stdout : stderr, encoding);34};3536const compareValues = (t, value, encoding) => {37 if (encoding === 'buffer') {38 t.true(ArrayBuffer.isView(value));39 t.true(BUFFER_TO_ENCODE.equals(value));40 } else {41 t.is(value, BUFFER_TO_ENCODE.toString(encoding));42 }43};4445// This string gives different outputs with each encoding type46const STRING_TO_ENCODE = '\u1000.';47const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);4849test('can pass encoding "buffer" to stdout', checkEncoding, 'buffer', 1, execa);50test('can pass encoding "utf8" to stdout', checkEncoding, 'utf8', 1, execa);51test('can pass encoding "utf16le" to stdout', checkEncoding, 'utf16le', 1, execa);52test('can pass encoding "latin1" to stdout', checkEncoding, 'latin1', 1, execa);53test('can pass encoding "ascii" to stdout', checkEncoding, 'ascii', 1, execa);54test('can pass encoding "hex" to stdout', checkEncoding, 'hex', 1, execa);55test('can pass encoding "base64" to stdout', checkEncoding, 'base64', 1, execa);56test('can pass encoding "base64url" to stdout', checkEncoding, 'base64url', 1, execa);57test('can pass encoding "buffer" to stderr', checkEncoding, 'buffer', 2, execa);58test('can pass encoding "utf8" to stderr', checkEncoding, 'utf8', 2, execa);59test('can pass encoding "utf16le" to stderr', checkEncoding, 'utf16le', 2, execa);60test('can pass encoding "latin1" to stderr', checkEncoding, 'latin1', 2, execa);61test('can pass encoding "ascii" to stderr', checkEncoding, 'ascii', 2, execa);62test('can pass encoding "hex" to stderr', checkEncoding, 'hex', 2, execa);63test('can pass encoding "base64" to stderr', checkEncoding, 'base64', 2, execa);64test('can pass encoding "base64url" to stderr', checkEncoding, 'base64url', 2, execa);65test('can pass encoding "buffer" to stdio[*]', checkEncoding, 'buffer', 3, execa);66test('can pass encoding "utf8" to stdio[*]', checkEncoding, 'utf8', 3, execa);67test('can pass encoding "utf16le" to stdio[*]', checkEncoding, 'utf16le', 3, execa);68test('can pass encoding "latin1" to stdio[*]', checkEncoding, 'latin1', 3, execa);69test('can pass encoding "ascii" to stdio[*]', checkEncoding, 'ascii', 3, execa);70test('can pass encoding "hex" to stdio[*]', checkEncoding, 'hex', 3, execa);71test('can pass encoding "base64" to stdio[*]', checkEncoding, 'base64', 3, execa);72test('can pass encoding "base64url" to stdio[*]', checkEncoding, 'base64url', 3, execa);73test('can pass encoding "buffer" to stdout - sync', checkEncoding, 'buffer', 1, execaSync);74test('can pass encoding "utf8" to stdout - sync', checkEncoding, 'utf8', 1, execaSync);75test('can pass encoding "utf16le" to stdout - sync', checkEncoding, 'utf16le', 1, execaSync);76test('can pass encoding "latin1" to stdout - sync', checkEncoding, 'latin1', 1, execaSync);77test('can pass encoding "ascii" to stdout - sync', checkEncoding, 'ascii', 1, execaSync);78test('can pass encoding "hex" to stdout - sync', checkEncoding, 'hex', 1, execaSync);79test('can pass encoding "base64" to stdout - sync', checkEncoding, 'base64', 1, execaSync);80test('can pass encoding "base64url" to stdout - sync', checkEncoding, 'base64url', 1, execaSync);81test('can pass encoding "buffer" to stderr - sync', checkEncoding, 'buffer', 2, execaSync);82test('can pass encoding "utf8" to stderr - sync', checkEncoding, 'utf8', 2, execaSync);83test('can pass encoding "utf16le" to stderr - sync', checkEncoding, 'utf16le', 2, execaSync);84test('can pass encoding "latin1" to stderr - sync', checkEncoding, 'latin1', 2, execaSync);85test('can pass encoding "ascii" to stderr - sync', checkEncoding, 'ascii', 2, execaSync);86test('can pass encoding "hex" to stderr - sync', checkEncoding, 'hex', 2, execaSync);87test('can pass encoding "base64" to stderr - sync', checkEncoding, 'base64', 2, execaSync);88test('can pass encoding "base64url" to stderr - sync', checkEncoding, 'base64url', 2, execaSync);89test('can pass encoding "buffer" to stdio[*] - sync', checkEncoding, 'buffer', 3, execaSync);90test('can pass encoding "utf8" to stdio[*] - sync', checkEncoding, 'utf8', 3, execaSync);91test('can pass encoding "utf16le" to stdio[*] - sync', checkEncoding, 'utf16le', 3, execaSync);92test('can pass encoding "latin1" to stdio[*] - sync', checkEncoding, 'latin1', 3, execaSync);93test('can pass encoding "ascii" to stdio[*] - sync', checkEncoding, 'ascii', 3, execaSync);94test('can pass encoding "hex" to stdio[*] - sync', checkEncoding, 'hex', 3, execaSync);95test('can pass encoding "base64" to stdio[*] - sync', checkEncoding, 'base64', 3, execaSync);96test('can pass encoding "base64url" to stdio[*] - sync', checkEncoding, 'base64url', 3, execaSync);9798const foobarArray = ['fo', 'ob', 'ar', '..'];99

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

lines 100–101why, one step back
100const testMultibyteCharacters = async (t, objectMode, addNoopTransform, execaMethod) => {
101 const {stdout} = await execaMethod('noop.js', {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 102why, one step back
102 stdout: addNoopGenerator(getOutputsGenerator(foobarArray)(objectMode, true), addNoopTransform, objectMode),

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 103–104why, one step back
103 encoding: 'base64',
104 });

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

line 105why, one step back
105 if (objectMode) {

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

line 106why, one step back
106 t.deepEqual(stdout, foobarArray);

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

lines 107–110why, one step back
107 } else {
108 t.is(stdout, btoa(foobarArray.join('')));109 }110};

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

line 111why, one step back
111

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

lines 112–119why, one step back
112test('Handle multibyte characters', testMultibyteCharacters, false, false, execa);
113test('Handle multibyte characters, noop transform', testMultibyteCharacters, false, true, execa);114test('Handle multibyte characters, with objectMode', testMultibyteCharacters, true, false, execa);115test('Handle multibyte characters, with objectMode, noop transform', testMultibyteCharacters, true, true, execa);116test('Handle multibyte characters, sync', testMultibyteCharacters, false, false, execaSync);117test('Handle multibyte characters, noop transform, sync', testMultibyteCharacters, false, true, execaSync);118test('Handle multibyte characters, with objectMode, sync', testMultibyteCharacters, true, false, execaSync);119test('Handle multibyte characters, with objectMode, noop transform, sync', testMultibyteCharacters, true, true, execaSync);

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

line 120why, one step back
120

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

lines 121–125why, one step back
121const testObjectMode = async (t, addNoopTransform, execaMethod) => {
122 const {stdout} = await execaMethod('noop.js', {123 stdout: addNoopGenerator(outputObjectGenerator(), addNoopTransform, true),124 encoding: 'base64',125 });

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

line 126why, one step back
126 t.deepEqual(stdout, [foobarObject]);

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

lines 127–128why, one step back
127};
128

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

lines 129–132why, one step back
129test('Other encodings work with transforms that return objects', testObjectMode, false, execa);
130test('Other encodings work with transforms that return objects, noop transform', testObjectMode, true, execa);131test('Other encodings work with transforms that return objects, sync', testObjectMode, false, execaSync);132test('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 133why, one step back
133

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

lines 134–136why, one step back
134// eslint-disable-next-line max-params
135const testIgnoredEncoding = async (t, stdoutOption, isUndefined, options, execaMethod) => {136 const {stdout} = await execaMethod('empty.js', {stdout: stdoutOption, ...options});

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 137–141why, one step back
137 t.is(stdout === undefined, isUndefined);
138};139140const base64Options = {encoding: 'base64'};141const linesOptions = {lines: true};

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

lines 142–177why, one step back
142test('Is ignored with other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, base64Options, execa);
143test('Is ignored with other encodings and ["ignore"]', testIgnoredEncoding, ['ignore'], true, base64Options, execa);144test('Is ignored with other encodings and "ipc"', testIgnoredEncoding, 'ipc', true, base64Options, execa);145test('Is ignored with other encodings and ["ipc"]', testIgnoredEncoding, ['ipc'], true, base64Options, execa);146test('Is ignored with other encodings and "inherit"', testIgnoredEncoding, 'inherit', true, base64Options, execa);147test('Is ignored with other encodings and ["inherit"]', testIgnoredEncoding, ['inherit'], true, base64Options, execa);148test('Is ignored with other encodings and 1', testIgnoredEncoding, 1, true, base64Options, execa);149test('Is ignored with other encodings and [1]', testIgnoredEncoding, [1], true, base64Options, execa);150test('Is ignored with other encodings and process.stdout', testIgnoredEncoding, process.stdout, true, base64Options, execa);151test('Is ignored with other encodings and [process.stdout]', testIgnoredEncoding, [process.stdout], true, base64Options, execa);152test('Is not ignored with other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, base64Options, execa);153test('Is not ignored with other encodings and ["pipe"]', testIgnoredEncoding, ['pipe'], false, base64Options, execa);154test('Is not ignored with other encodings and "overlapped"', testIgnoredEncoding, 'overlapped', 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 ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, base64Options, execa);157test('Is not ignored with other encodings and undefined', testIgnoredEncoding, undefined, false, base64Options, execa);158test('Is not ignored with other encodings and null', testIgnoredEncoding, null, false, base64Options, execa);159test('Is ignored with "lines: true" and "ignore"', testIgnoredEncoding, 'ignore', true, linesOptions, execa);160test('Is ignored with "lines: true" and ["ignore"]', testIgnoredEncoding, ['ignore'], true, linesOptions, execa);161test('Is ignored with "lines: true" and "ipc"', testIgnoredEncoding, 'ipc', true, linesOptions, execa);162test('Is ignored with "lines: true" and ["ipc"]', testIgnoredEncoding, ['ipc'], true, linesOptions, execa);163test('Is ignored with "lines: true" and "inherit"', testIgnoredEncoding, 'inherit', true, linesOptions, execa);164test('Is ignored with "lines: true" and ["inherit"]', testIgnoredEncoding, ['inherit'], true, linesOptions, execa);165test('Is ignored with "lines: true" and 1', testIgnoredEncoding, 1, true, linesOptions, execa);166test('Is ignored with "lines: true" and [1]', testIgnoredEncoding, [1], true, linesOptions, execa);167test('Is ignored with "lines: true" and process.stdout', testIgnoredEncoding, process.stdout, true, linesOptions, execa);168test('Is ignored with "lines: true" and [process.stdout]', testIgnoredEncoding, [process.stdout], true, linesOptions, execa);169test('Is not ignored with "lines: true" and "pipe"', testIgnoredEncoding, 'pipe', false, linesOptions, execa);170test('Is not ignored with "lines: true" and ["pipe"]', testIgnoredEncoding, ['pipe'], false, linesOptions, execa);171test('Is not ignored with "lines: true" and "overlapped"', testIgnoredEncoding, 'overlapped', 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 ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, linesOptions, execa);174test('Is not ignored with "lines: true" and undefined', testIgnoredEncoding, undefined, false, linesOptions, execa);175test('Is not ignored with "lines: true" and null', testIgnoredEncoding, null, false, linesOptions, execa);176test('Is ignored with "lines: true", other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, {...base64Options, ...linesOptions}, execa);177test('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 178–203why, one step back
178test('Is ignored with other encodings and "ignore", sync', testIgnoredEncoding, 'ignore', true, base64Options, execaSync);
179test('Is ignored with other encodings and ["ignore"], sync', testIgnoredEncoding, ['ignore'], true, base64Options, execaSync);180test('Is ignored with other encodings and "inherit", sync', testIgnoredEncoding, 'inherit', true, base64Options, execaSync);181test('Is ignored with other encodings and ["inherit"], sync', testIgnoredEncoding, ['inherit'], true, base64Options, execaSync);182test('Is ignored with other encodings and 1, sync', testIgnoredEncoding, 1, true, base64Options, execaSync);183test('Is ignored with other encodings and [1], sync', testIgnoredEncoding, [1], true, base64Options, execaSync);184test('Is ignored with other encodings and process.stdout, sync', testIgnoredEncoding, process.stdout, true, base64Options, execaSync);185test('Is ignored with other encodings and [process.stdout], sync', testIgnoredEncoding, [process.stdout], true, base64Options, execaSync);186test('Is not ignored with other encodings and "pipe", sync', testIgnoredEncoding, 'pipe', false, 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 undefined, sync', testIgnoredEncoding, undefined, false, base64Options, execaSync);189test('Is not ignored with other encodings and null, sync', testIgnoredEncoding, null, false, base64Options, execaSync);190test('Is ignored with "lines: true" and "ignore", sync', testIgnoredEncoding, 'ignore', true, linesOptions, execaSync);191test('Is ignored with "lines: true" and ["ignore"], sync', testIgnoredEncoding, ['ignore'], true, linesOptions, execaSync);192test('Is ignored with "lines: true" and "inherit", sync', testIgnoredEncoding, 'inherit', true, linesOptions, execaSync);193test('Is ignored with "lines: true" and ["inherit"], sync', testIgnoredEncoding, ['inherit'], true, linesOptions, execaSync);194test('Is ignored with "lines: true" and 1, sync', testIgnoredEncoding, 1, true, linesOptions, execaSync);195test('Is ignored with "lines: true" and [1], sync', testIgnoredEncoding, [1], true, linesOptions, execaSync);196test('Is ignored with "lines: true" and process.stdout, sync', testIgnoredEncoding, process.stdout, true, linesOptions, execaSync);197test('Is ignored with "lines: true" and [process.stdout], sync', testIgnoredEncoding, [process.stdout], true, linesOptions, execaSync);198test('Is not ignored with "lines: true" and "pipe", sync', testIgnoredEncoding, 'pipe', false, 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 undefined, sync', testIgnoredEncoding, undefined, false, linesOptions, execaSync);201test('Is not ignored with "lines: true" and null, sync', testIgnoredEncoding, null, false, linesOptions, execaSync);202test('Is ignored with "lines: true", other encodings and "ignore", sync', testIgnoredEncoding, 'ignore', true, {...base64Options, ...linesOptions}, execaSync);203test('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 204–222why, one step back
204
205// eslint-disable-next-line max-params206const testEncodingInput = async (t, input, expectedStdout, encoding, execaMethod) => {207 const {stdout} = await execaMethod('stdin.js', {input, encoding});208 t.deepEqual(stdout, expectedStdout);209};210211test('Can use string input', testEncodingInput, foobarString, foobarString, 'utf8', execa);212test('Can use Uint8Array input', testEncodingInput, foobarUint8Array, foobarString, 'utf8', execa);213test('Can use string input, encoding "buffer"', testEncodingInput, foobarString, foobarUint8Array, 'buffer', execa);214test('Can use Uint8Array input, encoding "buffer"', testEncodingInput, foobarUint8Array, foobarUint8Array, 'buffer', execa);215test('Can use string input, encoding "hex"', testEncodingInput, foobarString, foobarHex, 'hex', execa);216test('Can use Uint8Array input, encoding "hex"', testEncodingInput, foobarUint8Array, foobarHex, 'hex', execa);217test('Can use string input, sync', testEncodingInput, foobarString, foobarString, 'utf8', execaSync);218test('Can use Uint8Array input, sync', testEncodingInput, foobarUint8Array, foobarString, 'utf8', execaSync);219test('Can use string input, encoding "buffer", sync', testEncodingInput, foobarString, foobarUint8Array, 'buffer', execaSync);220test('Can use Uint8Array input, encoding "buffer", sync', testEncodingInput, foobarUint8Array, foobarUint8Array, 'buffer', execaSync);221test('Can use string input, encoding "hex", sync', testEncodingInput, foobarString, foobarHex, 'hex', execaSync);222test('Can use Uint8Array input, encoding "hex", sync', testEncodingInput, foobarUint8Array, foobarHex, 'hex', execaSync);

Improve `encoding` option (#958)ehmickybefore adoption507b09c

lines 223–238why, one step back
223
224const testSubprocessEncoding = (t, encoding) => {225 const subprocess = execa('empty.js', {...fullStdio, encoding});226 t.is(subprocess.stdout.readableEncoding, null);227 t.is(subprocess.stderr.readableEncoding, null);228 t.is(subprocess.stdio[3].readableEncoding, null);229};230231test('Does not modify subprocess.std* encoding, "utf8"', testSubprocessEncoding, 'utf8');232test('Does not modify subprocess.std* encoding, "utf16le"', testSubprocessEncoding, 'utf16le');233test('Does not modify subprocess.std* encoding, "buffer"', testSubprocessEncoding, 'buffer');234test('Does not modify subprocess.std* encoding, "hex"', testSubprocessEncoding, 'hex');235test('Does not modify subprocess.std* encoding, "base64"', testSubprocessEncoding, 'base64');236test('Does not modify subprocess.std* encoding, "base64url"', testSubprocessEncoding, 'base64url');237test('Does not modify subprocess.std* encoding, "latin1"', testSubprocessEncoding, 'latin1');238test('Does not modify subprocess.std* encoding, "ascii"', testSubprocessEncoding, 'ascii');