Skip to content
Myr

encoding.js

This file

Path

test/stdio/encoding.js

196 lines · 13.5 KiB · JavaScript

Last changed by

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

History of this file

Take it

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

lines 1–2why, one step back
1import {Buffer} from 'node:buffer';
2import {exec} from 'node:child_process';

Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169

line 3why, one step back
3import process from 'node:process';

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

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

Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6

lines 10–11why, one step back
10import {outputObjectGenerator, getChunksGenerator} from '../helpers/generator.js';
11import {foobarObject} from '../helpers/input.js';

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

lines 12–130why, one step back
12
13const pExec = promisify(exec);1415setFixtureDir();1617const checkEncoding = async (t, encoding, index, execaMethod) => {18 const {stdio} = await execaMethod('noop-fd.js', [`${index}`, STRING_TO_ENCODE], {...fullStdio, encoding});19 compareValues(t, stdio[index], encoding);2021 if (execaMethod !== execaSync) {22 const childProcess = execaMethod('noop-fd.js', [`${index}`, STRING_TO_ENCODE], {...fullStdio, encoding, buffer: false});23 const getStreamMethod = encoding === 'buffer' ? getStreamAsBuffer : getStream;24 const result = await getStreamMethod(childProcess.stdio[index]);25 compareValues(t, result, encoding);26 await childProcess;27 }2829 if (index === 3) {30 return;31 }3233 const {stdout, stderr} = await pExec(`node noop-fd.js ${index} ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});34 compareValues(t, index === 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 */130

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 131–134why, one step back
131test('validate unknown encodings', t => {
132 t.throws(() => {133 execa('noop.js', {encoding: 'unknownEncoding'});134 }, {code: 'ERR_UNKNOWN_ENCODING'});

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

lines 135–138why, one step back
135});
136137const foobarArray = ['fo', 'ob', 'ar', '..'];138

Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6

lines 139–142why, one step back
139test('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});

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

line 143why, one step back
143

Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6

lines 144–147why, one step back
144test('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});

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 148why, one step back
148

Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6

lines 149–152why, one step back
149test('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});

Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169

line 153why, one step back
153

Line-wise standard streams (#741)ehmickybefore adoption479d12e

lines 154–155why, one step back
154const testIgnoredEncoding = async (t, stdoutOption, isUndefined, options) => {
155 const {stdout} = await execa('empty.js', {stdout: stdoutOption, ...options});

Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169

lines 156–158why, one step back
156 t.is(stdout === undefined, isUndefined);
157};158

Line-wise standard streams (#741)ehmickybefore adoption479d12e

lines 159–196why, one step back
159const 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});