encoding.js
This file
Path
test/stdio/encoding.js
173 lines · 11.1 KiB · JavaScript
Last changed by
Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169
History of this fileTake it
Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 1–2why, one step back1import {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 back3import process from 'node:process';Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 4–9why, one step back4import {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 back10import {outputObjectGenerator, getChunksGenerator} from '../helpers/generator.js';11import {foobarObject} from '../helpers/input.js';Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
lines 12–136why, one step back1213const 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 */130131test('validate unknown encodings', async t => {132 await t.throwsAsync(execa('noop.js', {encoding: 'unknownEncoding'}), {code: 'ERR_UNKNOWN_ENCODING'});133});134135const foobarArray = ['fo', 'ob', 'ar', '..'];136Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6
lines 137–140why, one step back137test('Handle multibyte characters', async t => {138 const {stdout} = await execa('noop.js', {stdout: getChunksGenerator(foobarArray, false), encoding: 'base64'});139 t.is(stdout, btoa(foobarArray.join('')));140});Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 141why, one step back141Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6
lines 142–145why, one step back142test('Handle multibyte characters, with objectMode', async t => {143 const {stdout} = await execa('noop.js', {stdout: getChunksGenerator(foobarArray, true), encoding: 'base64'});144 t.deepEqual(stdout, foobarArray.map(chunk => btoa(chunk)));145});Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 146why, one step back146Fix using both `objectMode` and `encoding` options (#736)ehmickybefore adoption9f205f6
lines 147–150why, one step back147test('Other encodings work with transforms that return objects', async t => {148 const {stdout} = await execa('noop.js', {stdout: outputObjectGenerator, encoding: 'base64'});149 t.deepEqual(stdout, [foobarObject]);150});Fix `encoding` option not working with `ignore` and `inherit` (#738)ehmickybefore adoption61ed169
lines 151–173why, one step back151152const testIgnoredEncoding = async (t, stdoutOption, isUndefined) => {153 const {stdout} = await execa('empty.js', {stdout: stdoutOption, encoding: 'base64'});154 t.is(stdout === undefined, isUndefined);155};156157test('Is ignored with other encodings and "ignore"', testIgnoredEncoding, 'ignore', true);158test('Is ignored with other encodings and ["ignore"]', testIgnoredEncoding, ['ignore'], true);159test('Is ignored with other encodings and "ipc"', testIgnoredEncoding, 'ipc', true);160test('Is ignored with other encodings and ["ipc"]', testIgnoredEncoding, ['ipc'], true);161test('Is ignored with other encodings and "inherit"', testIgnoredEncoding, 'inherit', true);162test('Is ignored with other encodings and ["inherit"]', testIgnoredEncoding, ['inherit'], true);163test('Is ignored with other encodings and 1', testIgnoredEncoding, 1, true);164test('Is ignored with other encodings and [1]', testIgnoredEncoding, [1], true);165test('Is ignored with other encodings and process.stdout', testIgnoredEncoding, process.stdout, true);166test('Is ignored with other encodings and [process.stdout]', testIgnoredEncoding, [process.stdout], true);167test('Is not ignored with other encodings and "pipe"', testIgnoredEncoding, 'pipe', false);168test('Is not ignored with other encodings and ["pipe"]', testIgnoredEncoding, ['pipe'], false);169test('Is not ignored with other encodings and "overlapped"', testIgnoredEncoding, 'overlapped', false);170test('Is not ignored with other encodings and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false);171test('Is not ignored with other encodings and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false);172test('Is not ignored with other encodings and undefined', testIgnoredEncoding, undefined, false);173test('Is not ignored with other encodings and null', testIgnoredEncoding, null, false);