encoding-final.js
This file
Path
test/stdio/encoding-final.js
203 lines · 14.0 KiB · JavaScript
Last changed by
Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b
History of this fileTake it
Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 1–9why, one step back1import {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 back10import {outputObjectGenerator, getChunksGenerator, addNoopGenerator} from '../helpers/generator.js';Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 11–138why, one step back11import {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) {22 const childProcess = execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding, buffer: false});23 const getStreamMethod = encoding === 'buffer' ? getStreamAsBuffer : getStream;24 const result = await getStreamMethod(childProcess.stdio[fdNumber]);25 compareValues(t, result, encoding);26 await childProcess;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);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', t => {132 t.throws(() => {133 execa('noop.js', {encoding: 'unknownEncoding'});134 }, {code: 'ERR_UNKNOWN_ENCODING'});135});136137const foobarArray = ['fo', 'ob', 'ar', '..'];138Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b
lines 139–146why, one step back139const testMultibyteCharacters = async (t, objectMode, addNoopTransform) => {140 const {stdout} = await execa('noop.js', {stdout: addNoopGenerator(getChunksGenerator(foobarArray, objectMode), addNoopTransform), encoding: 'base64'});141 if (objectMode) {142 t.deepEqual(stdout, foobarArray.map(chunk => btoa(chunk)));143 } else {144 t.is(stdout, btoa(foobarArray.join('')));145 }146};Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 147why, one step back147Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b
lines 148–151why, one step back148test('Handle multibyte characters', testMultibyteCharacters, false, false);149test('Handle multibyte characters, noop transform', testMultibyteCharacters, false, true);150test('Handle multibyte characters, with objectMode', testMultibyteCharacters, true, false);151test('Handle multibyte characters, with objectMode, noop transform', testMultibyteCharacters, true, true);Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 152why, one step back152Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b
lines 153–154why, one step back153const testObjectMode = async (t, addNoopTransform) => {154 const {stdout} = await execa('noop.js', {stdout: addNoopGenerator(outputObjectGenerator, addNoopTransform), encoding: 'base64'});Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 155why, one step back155 t.deepEqual(stdout, [foobarObject]);Improve default value of transforms `objectMode` (#867)ehmickybefore adoption8f51c6b
lines 156–159why, one step back156};157158test('Other encodings work with transforms that return objects', testObjectMode, false);159test('Other encodings work with transforms that return objects, noop transform', testObjectMode, true);Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 160–203why, one step back160161const testIgnoredEncoding = async (t, stdoutOption, isUndefined, options) => {162 const {stdout} = await execa('empty.js', {stdout: stdoutOption, ...options});163 t.is(stdout === undefined, isUndefined);164};165166const base64Options = {encoding: 'base64'};167const linesOptions = {lines: true};168test('Is ignored with other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, base64Options);169test('Is ignored with other encodings and ["ignore"]', testIgnoredEncoding, ['ignore'], true, base64Options);170test('Is ignored with other encodings and "ipc"', testIgnoredEncoding, 'ipc', true, base64Options);171test('Is ignored with other encodings and ["ipc"]', testIgnoredEncoding, ['ipc'], true, base64Options);172test('Is ignored with other encodings and "inherit"', testIgnoredEncoding, 'inherit', true, base64Options);173test('Is ignored with other encodings and ["inherit"]', testIgnoredEncoding, ['inherit'], true, base64Options);174test('Is ignored with other encodings and 1', testIgnoredEncoding, 1, true, base64Options);175test('Is ignored with other encodings and [1]', testIgnoredEncoding, [1], true, base64Options);176test('Is ignored with other encodings and process.stdout', testIgnoredEncoding, process.stdout, true, base64Options);177test('Is ignored with other encodings and [process.stdout]', testIgnoredEncoding, [process.stdout], true, base64Options);178test('Is not ignored with other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, base64Options);179test('Is not ignored with other encodings and ["pipe"]', testIgnoredEncoding, ['pipe'], false, base64Options);180test('Is not ignored with other encodings and "overlapped"', testIgnoredEncoding, 'overlapped', false, base64Options);181test('Is not ignored with other encodings and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false, base64Options);182test('Is not ignored with other encodings and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, base64Options);183test('Is not ignored with other encodings and undefined', testIgnoredEncoding, undefined, false, base64Options);184test('Is not ignored with other encodings and null', testIgnoredEncoding, null, false, base64Options);185test('Is ignored with "lines: true" and "ignore"', testIgnoredEncoding, 'ignore', true, linesOptions);186test('Is ignored with "lines: true" and ["ignore"]', testIgnoredEncoding, ['ignore'], true, linesOptions);187test('Is ignored with "lines: true" and "ipc"', testIgnoredEncoding, 'ipc', true, linesOptions);188test('Is ignored with "lines: true" and ["ipc"]', testIgnoredEncoding, ['ipc'], true, linesOptions);189test('Is ignored with "lines: true" and "inherit"', testIgnoredEncoding, 'inherit', true, linesOptions);190test('Is ignored with "lines: true" and ["inherit"]', testIgnoredEncoding, ['inherit'], true, linesOptions);191test('Is ignored with "lines: true" and 1', testIgnoredEncoding, 1, true, linesOptions);192test('Is ignored with "lines: true" and [1]', testIgnoredEncoding, [1], true, linesOptions);193test('Is ignored with "lines: true" and process.stdout', testIgnoredEncoding, process.stdout, true, linesOptions);194test('Is ignored with "lines: true" and [process.stdout]', testIgnoredEncoding, [process.stdout], true, linesOptions);195test('Is not ignored with "lines: true" and "pipe"', testIgnoredEncoding, 'pipe', false, linesOptions);196test('Is not ignored with "lines: true" and ["pipe"]', testIgnoredEncoding, ['pipe'], false, linesOptions);197test('Is not ignored with "lines: true" and "overlapped"', testIgnoredEncoding, 'overlapped', false, linesOptions);198test('Is not ignored with "lines: true" and ["overlapped"]', testIgnoredEncoding, ['overlapped'], false, linesOptions);199test('Is not ignored with "lines: true" and ["inherit", "pipe"]', testIgnoredEncoding, ['inherit', 'pipe'], false, linesOptions);200test('Is not ignored with "lines: true" and undefined', testIgnoredEncoding, undefined, false, linesOptions);201test('Is not ignored with "lines: true" and null', testIgnoredEncoding, null, false, linesOptions);202test('Is ignored with "lines: true", other encodings and "ignore"', testIgnoredEncoding, 'ignore', true, {...base64Options, ...linesOptions});203test('Is not ignored with "lines: true", other encodings and "pipe"', testIgnoredEncoding, 'pipe', false, {...base64Options, ...linesOptions});