Skip to content
Myr

encoding-final.js

This file

Path

test/stdio/encoding-final.js

168 lines · 11.2 KiB · JavaScript

Last changed by

Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4

History of this file

Take it

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

lines 1–9why, 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';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';

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

line 10why, one step back
10import {outputObjectGenerator, getChunksGenerator, addNoopGenerator} from '../helpers/generator.js';

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

lines 11–21why, one step back
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) {

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

line 22why, one step back
22 const subprocess = execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding, buffer: false});

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

line 23why, one step back
23 const getStreamMethod = encoding === 'buffer' ? getStreamAsBuffer : getStream;

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

line 24why, one step back
24 const result = await getStreamMethod(subprocess.stdio[fdNumber]);

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

line 25why, one step back
25 compareValues(t, result, encoding);

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

line 26why, one step back
26 await subprocess;

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

lines 27–100why, one step back
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);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', '..'];100

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

line 101why, one step back
101const testMultibyteCharacters = async (t, objectMode, addNoopTransform) => {

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

lines 102–105why, one step back
102 const {stdout} = await execa('noop.js', {
103 stdout: addNoopGenerator(getChunksGenerator(foobarArray, objectMode, true), addNoopTransform),104 encoding: 'base64',105 });

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

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

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

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

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

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

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

line 112why, one step back
112

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

lines 113–116why, one step back
113test('Handle multibyte characters', testMultibyteCharacters, false, false);
114test('Handle multibyte characters, noop transform', testMultibyteCharacters, false, true);115test('Handle multibyte characters, with objectMode', testMultibyteCharacters, true, false);116test('Handle multibyte characters, with objectMode, noop transform', testMultibyteCharacters, true, true);

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

line 117why, one step back
117

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

lines 118–119why, one step back
118const testObjectMode = async (t, addNoopTransform) => {
119 const {stdout} = await execa('noop.js', {stdout: addNoopGenerator(outputObjectGenerator, addNoopTransform), encoding: 'base64'});

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

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

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

lines 121–124why, one step back
121};
122123test('Other encodings work with transforms that return objects', testObjectMode, false);124test('Other encodings work with transforms that return objects, noop transform', testObjectMode, true);

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

lines 125–168why, one step back
125
126const testIgnoredEncoding = async (t, stdoutOption, isUndefined, options) => {127 const {stdout} = await execa('empty.js', {stdout: stdoutOption, ...options});128 t.is(stdout === undefined, isUndefined);129};130131const base64Options = {encoding: 'base64'};132const linesOptions = {lines: true};133test('Is ignored with other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, base64Options);134test('Is ignored with other encodings and ["ignore"]', testIgnoredEncoding, ['ignore'], true, base64Options);135test('Is ignored with other encodings and "ipc"', testIgnoredEncoding, 'ipc', true, base64Options);136test('Is ignored with other encodings and ["ipc"]', testIgnoredEncoding, ['ipc'], true, base64Options);137test('Is ignored with other encodings and "inherit"', testIgnoredEncoding, 'inherit', true, base64Options);138test('Is ignored with other encodings and ["inherit"]', testIgnoredEncoding, ['inherit'], true, base64Options);139test('Is ignored with other encodings and 1', testIgnoredEncoding, 1, true, base64Options);140test('Is ignored with other encodings and [1]', testIgnoredEncoding, [1], true, base64Options);141test('Is ignored with other encodings and process.stdout', testIgnoredEncoding, process.stdout, true, base64Options);142test('Is ignored with other encodings and [process.stdout]', testIgnoredEncoding, [process.stdout], true, base64Options);143test('Is not ignored with other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, base64Options);144test('Is not ignored with other encodings and ["pipe"]', testIgnoredEncoding, ['pipe'], false, base64Options);145test('Is not ignored with other encodings and "overlapped"', testIgnoredEncoding, 'overlapped', false, base64Options);146test('Is not ignored with other encodings and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false, base64Options);147test('Is not ignored with other encodings and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, base64Options);148test('Is not ignored with other encodings and undefined', testIgnoredEncoding, undefined, false, base64Options);149test('Is not ignored with other encodings and null', testIgnoredEncoding, null, false, base64Options);150test('Is ignored with "lines: true" and "ignore"', testIgnoredEncoding, 'ignore', true, linesOptions);151test('Is ignored with "lines: true" and ["ignore"]', testIgnoredEncoding, ['ignore'], true, linesOptions);152test('Is ignored with "lines: true" and "ipc"', testIgnoredEncoding, 'ipc', true, linesOptions);153test('Is ignored with "lines: true" and ["ipc"]', testIgnoredEncoding, ['ipc'], true, linesOptions);154test('Is ignored with "lines: true" and "inherit"', testIgnoredEncoding, 'inherit', true, linesOptions);155test('Is ignored with "lines: true" and ["inherit"]', testIgnoredEncoding, ['inherit'], true, linesOptions);156test('Is ignored with "lines: true" and 1', testIgnoredEncoding, 1, true, linesOptions);157test('Is ignored with "lines: true" and [1]', testIgnoredEncoding, [1], true, linesOptions);158test('Is ignored with "lines: true" and process.stdout', testIgnoredEncoding, process.stdout, true, linesOptions);159test('Is ignored with "lines: true" and [process.stdout]', testIgnoredEncoding, [process.stdout], true, linesOptions);160test('Is not ignored with "lines: true" and "pipe"', testIgnoredEncoding, 'pipe', false, linesOptions);161test('Is not ignored with "lines: true" and ["pipe"]', testIgnoredEncoding, ['pipe'], false, linesOptions);162test('Is not ignored with "lines: true" and "overlapped"', testIgnoredEncoding, 'overlapped', false, linesOptions);163test('Is not ignored with "lines: true" and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false, linesOptions);164test('Is not ignored with "lines: true" and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, linesOptions);165test('Is not ignored with "lines: true" and undefined', testIgnoredEncoding, undefined, false, linesOptions);166test('Is not ignored with "lines: true" and null', testIgnoredEncoding, null, false, linesOptions);167test('Is ignored with "lines: true", other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, {...base64Options, ...linesOptions});168test('Is not ignored with "lines: true", other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, {...base64Options, ...linesOptions});