Skip to content
Myr

encoding.js

This file

Path

test/stdio/encoding.js

154 lines · 9.2 KiB · JavaScript

Last changed by

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

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';

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

line 3why, one step back
3import {setImmediate} from 'node:timers/promises';

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

lines 4–134why, 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';1011const pExec = promisify(exec);1213setFixtureDir();1415const checkEncoding = async (t, encoding, index, execaMethod) => {16 const {stdio} = await execaMethod('noop-fd.js', [`${index}`, STRING_TO_ENCODE], {...fullStdio, encoding});17 compareValues(t, stdio[index], encoding);1819 if (execaMethod !== execaSync) {20 const childProcess = execaMethod('noop-fd.js', [`${index}`, STRING_TO_ENCODE], {...fullStdio, encoding, buffer: false});21 const getStreamMethod = encoding === 'buffer' ? getStreamAsBuffer : getStream;22 const result = await getStreamMethod(childProcess.stdio[index]);23 compareValues(t, result, encoding);24 await childProcess;25 }2627 if (index === 3) {28 return;29 }3031 const {stdout, stderr} = await pExec(`node noop-fd.js ${index} ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});32 compareValues(t, index === 1 ? stdout : stderr, encoding);33};3435const compareValues = (t, value, encoding) => {36 if (encoding === 'buffer') {37 t.true(ArrayBuffer.isView(value));38 t.true(BUFFER_TO_ENCODE.equals(value));39 } else {40 t.is(value, BUFFER_TO_ENCODE.toString(encoding));41 }42};4344// This string gives different outputs with each encoding type45const STRING_TO_ENCODE = '\u1000.';46const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);4748/* eslint-disable unicorn/text-encoding-identifier-case */49test('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 "utf-8" to stdout', checkEncoding, 'utf-8', 1, execa);52test('can pass encoding "utf16le" to stdout', checkEncoding, 'utf16le', 1, execa);53test('can pass encoding "utf-16le" to stdout', checkEncoding, 'utf-16le', 1, execa);54test('can pass encoding "ucs2" to stdout', checkEncoding, 'ucs2', 1, execa);55test('can pass encoding "ucs-2" to stdout', checkEncoding, 'ucs-2', 1, execa);56test('can pass encoding "latin1" to stdout', checkEncoding, 'latin1', 1, execa);57test('can pass encoding "binary" to stdout', checkEncoding, 'binary', 1, execa);58test('can pass encoding "ascii" to stdout', checkEncoding, 'ascii', 1, execa);59test('can pass encoding "hex" to stdout', checkEncoding, 'hex', 1, execa);60test('can pass encoding "base64" to stdout', checkEncoding, 'base64', 1, execa);61test('can pass encoding "base64url" to stdout', checkEncoding, 'base64url', 1, execa);62test('can pass encoding "buffer" to stderr', checkEncoding, 'buffer', 2, execa);63test('can pass encoding "utf8" to stderr', checkEncoding, 'utf8', 2, execa);64test('can pass encoding "utf-8" to stderr', checkEncoding, 'utf-8', 2, execa);65test('can pass encoding "utf16le" to stderr', checkEncoding, 'utf16le', 2, execa);66test('can pass encoding "utf-16le" to stderr', checkEncoding, 'utf-16le', 2, execa);67test('can pass encoding "ucs2" to stderr', checkEncoding, 'ucs2', 2, execa);68test('can pass encoding "ucs-2" to stderr', checkEncoding, 'ucs-2', 2, execa);69test('can pass encoding "latin1" to stderr', checkEncoding, 'latin1', 2, execa);70test('can pass encoding "binary" to stderr', checkEncoding, 'binary', 2, execa);71test('can pass encoding "ascii" to stderr', checkEncoding, 'ascii', 2, execa);72test('can pass encoding "hex" to stderr', checkEncoding, 'hex', 2, execa);73test('can pass encoding "base64" to stderr', checkEncoding, 'base64', 2, execa);74test('can pass encoding "base64url" to stderr', checkEncoding, 'base64url', 2, execa);75test('can pass encoding "buffer" to stdio[*]', checkEncoding, 'buffer', 3, execa);76test('can pass encoding "utf8" to stdio[*]', checkEncoding, 'utf8', 3, execa);77test('can pass encoding "utf-8" to stdio[*]', checkEncoding, 'utf-8', 3, execa);78test('can pass encoding "utf16le" to stdio[*]', checkEncoding, 'utf16le', 3, execa);79test('can pass encoding "utf-16le" to stdio[*]', checkEncoding, 'utf-16le', 3, execa);80test('can pass encoding "ucs2" to stdio[*]', checkEncoding, 'ucs2', 3, execa);81test('can pass encoding "ucs-2" to stdio[*]', checkEncoding, 'ucs-2', 3, execa);82test('can pass encoding "latin1" to stdio[*]', checkEncoding, 'latin1', 3, execa);83test('can pass encoding "binary" to stdio[*]', checkEncoding, 'binary', 3, execa);84test('can pass encoding "ascii" to stdio[*]', checkEncoding, 'ascii', 3, execa);85test('can pass encoding "hex" to stdio[*]', checkEncoding, 'hex', 3, execa);86test('can pass encoding "base64" to stdio[*]', checkEncoding, 'base64', 3, execa);87test('can pass encoding "base64url" to stdio[*]', checkEncoding, 'base64url', 3, execa);88test('can pass encoding "buffer" to stdout - sync', checkEncoding, 'buffer', 1, execaSync);89test('can pass encoding "utf8" to stdout - sync', checkEncoding, 'utf8', 1, execaSync);90test('can pass encoding "utf-8" to stdout - sync', checkEncoding, 'utf-8', 1, execaSync);91test('can pass encoding "utf16le" to stdout - sync', checkEncoding, 'utf16le', 1, execaSync);92test('can pass encoding "utf-16le" to stdout - sync', checkEncoding, 'utf-16le', 1, execaSync);93test('can pass encoding "ucs2" to stdout - sync', checkEncoding, 'ucs2', 1, execaSync);94test('can pass encoding "ucs-2" to stdout - sync', checkEncoding, 'ucs-2', 1, execaSync);95test('can pass encoding "latin1" to stdout - sync', checkEncoding, 'latin1', 1, execaSync);96test('can pass encoding "binary" to stdout - sync', checkEncoding, 'binary', 1, execaSync);97test('can pass encoding "ascii" to stdout - sync', checkEncoding, 'ascii', 1, execaSync);98test('can pass encoding "hex" to stdout - sync', checkEncoding, 'hex', 1, execaSync);99test('can pass encoding "base64" to stdout - sync', checkEncoding, 'base64', 1, execaSync);100test('can pass encoding "base64url" to stdout - sync', checkEncoding, 'base64url', 1, execaSync);101test('can pass encoding "buffer" to stderr - sync', checkEncoding, 'buffer', 2, execaSync);102test('can pass encoding "utf8" to stderr - sync', checkEncoding, 'utf8', 2, execaSync);103test('can pass encoding "utf-8" to stderr - sync', checkEncoding, 'utf-8', 2, execaSync);104test('can pass encoding "utf16le" to stderr - sync', checkEncoding, 'utf16le', 2, execaSync);105test('can pass encoding "utf-16le" to stderr - sync', checkEncoding, 'utf-16le', 2, execaSync);106test('can pass encoding "ucs2" to stderr - sync', checkEncoding, 'ucs2', 2, execaSync);107test('can pass encoding "ucs-2" to stderr - sync', checkEncoding, 'ucs-2', 2, execaSync);108test('can pass encoding "latin1" to stderr - sync', checkEncoding, 'latin1', 2, execaSync);109test('can pass encoding "binary" to stderr - sync', checkEncoding, 'binary', 2, execaSync);110test('can pass encoding "ascii" to stderr - sync', checkEncoding, 'ascii', 2, execaSync);111test('can pass encoding "hex" to stderr - sync', checkEncoding, 'hex', 2, execaSync);112test('can pass encoding "base64" to stderr - sync', checkEncoding, 'base64', 2, execaSync);113test('can pass encoding "base64url" to stderr - sync', checkEncoding, 'base64url', 2, execaSync);114test('can pass encoding "buffer" to stdio[*] - sync', checkEncoding, 'buffer', 3, execaSync);115test('can pass encoding "utf8" to stdio[*] - sync', checkEncoding, 'utf8', 3, execaSync);116test('can pass encoding "utf-8" to stdio[*] - sync', checkEncoding, 'utf-8', 3, execaSync);117test('can pass encoding "utf16le" to stdio[*] - sync', checkEncoding, 'utf16le', 3, execaSync);118test('can pass encoding "utf-16le" to stdio[*] - sync', checkEncoding, 'utf-16le', 3, execaSync);119test('can pass encoding "ucs2" to stdio[*] - sync', checkEncoding, 'ucs2', 3, execaSync);120test('can pass encoding "ucs-2" to stdio[*] - sync', checkEncoding, 'ucs-2', 3, execaSync);121test('can pass encoding "latin1" to stdio[*] - sync', checkEncoding, 'latin1', 3, execaSync);122test('can pass encoding "binary" to stdio[*] - sync', checkEncoding, 'binary', 3, execaSync);123test('can pass encoding "ascii" to stdio[*] - sync', checkEncoding, 'ascii', 3, execaSync);124test('can pass encoding "hex" to stdio[*] - sync', checkEncoding, 'hex', 3, execaSync);125test('can pass encoding "base64" to stdio[*] - sync', checkEncoding, 'base64', 3, execaSync);126test('can pass encoding "base64url" to stdio[*] - sync', checkEncoding, 'base64url', 3, execaSync);127/* eslint-enable unicorn/text-encoding-identifier-case */128129test('validate unknown encodings', async t => {130 await t.throwsAsync(execa('noop.js', {encoding: 'unknownEncoding'}), {code: 'ERR_UNKNOWN_ENCODING'});131});132133const foobarArray = ['fo', 'ob', 'ar', '..'];134

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 135why, one step back
135const delayedGenerator = async function * (lines) {

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

line 136why, one step back
136 // eslint-disable-next-line no-unused-vars

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 137why, one step back
137 for await (const line of lines) {

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

line 138why, one step back
138 yield foobarArray[0];

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

line 139why, one step back
139 await setImmediate();

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

line 140why, one step back
140 yield foobarArray[1];

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

line 141why, one step back
141 await setImmediate();

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

line 142why, one step back
142 yield foobarArray[2];

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

line 143why, one step back
143 await setImmediate();

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

lines 144–147why, one step back
144 yield foobarArray[3];
145 }146};147

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 148–154why, one step back
148const testMultiByteCharacter = async (t, objectMode) => {
149 const {stdout} = await execa('noop.js', {stdout: {transform: delayedGenerator, objectMode}, encoding: 'base64'});150 t.is(objectMode ? stdout.join('') : stdout, btoa(foobarArray.join('')));151};152153test('Handle multibyte characters', testMultiByteCharacter, false);154test('Handle multibyte characters, with objectMode', testMultiByteCharacter, true);