Skip to content
Myr

encoding-transform.js

This file

Path

test/transform/encoding-transform.js

308 lines · 26.7 KiB · JavaScript

Last changed by

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–3why, one step back
1import {Buffer} from 'node:buffer';
2import test from 'ava';3import {execa, execaSync} from '../../index.js';

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 4why, one step back
4import {setFixtureDirectory} from '../helpers/fixtures-directory.js';

Rename files (#977)ehmickybefore adoption4b2e316

line 5why, one step back
5import {getStdio} from '../helpers/stdio.js';

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 6–9why, one step back
6import {
7 foobarString,8 foobarUint8Array,9 foobarBuffer,

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 10why, one step back
10 foobarUtf16Uint8Array,

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 11–12why, one step back
11 foobarObject,
12} from '../helpers/input.js';

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

line 13why, one step back
13import {noopGenerator, getOutputGenerator, uppercaseGenerator} from '../helpers/generator.js';

Rename files (#977)ehmickybefore adoption4b2e316

line 14why, one step back
14

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 15why, one step back
15setFixtureDirectory();

Rename files (#977)ehmickybefore adoption4b2e316

lines 16–95why, one step back
16
17const getTypeofGenerator = lines => (objectMode, binary) => ({18 * transform(line) {19 lines.push(Object.prototype.toString.call(line));20 yield '';21 },22 objectMode,23 binary,24});2526const assertTypeofChunk = (t, lines, expectedType) => {27 t.deepEqual(lines, [`[object ${expectedType}]`]);28};2930// eslint-disable-next-line max-params31const testGeneratorFirstEncoding = async (t, input, encoding, expectedType, objectMode, binary) => {32 const lines = [];33 const subprocess = execa('stdin.js', {stdin: getTypeofGenerator(lines)(objectMode, binary), encoding});34 subprocess.stdin.end(input);35 await subprocess;36 assertTypeofChunk(t, lines, expectedType);37};3839test('First generator argument is string with default encoding, with string writes', testGeneratorFirstEncoding, foobarString, 'utf8', 'String', false, undefined);40test('First generator argument is string with default encoding, with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'utf8', 'String', false, undefined);41test('First generator argument is string with default encoding, with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'utf8', 'String', false, undefined);42test('First generator argument is string with default encoding, with string writes, "binary: false"', testGeneratorFirstEncoding, foobarString, 'utf8', 'String', false, false);43test('First generator argument is Uint8Array with default encoding, with string writes, "binary: true"', testGeneratorFirstEncoding, foobarString, 'utf8', 'Uint8Array', false, true);44test('First generator argument is string with encoding "utf16le", with string writes', testGeneratorFirstEncoding, foobarString, 'utf16le', 'String', false, undefined);45test('First generator argument is string with encoding "utf16le", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'utf16le', 'String', false, undefined);46test('First generator argument is string with encoding "utf16le", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'utf16le', 'String', false, undefined);47test('First generator argument is string with encoding "utf16le", with string writes, "binary: false"', testGeneratorFirstEncoding, foobarString, 'utf16le', 'String', false, false);48test('First generator argument is Uint8Array with encoding "utf16le", with string writes, "binary: true"', testGeneratorFirstEncoding, foobarString, 'utf16le', 'Uint8Array', false, true);49test('First generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorFirstEncoding, foobarString, 'buffer', 'Uint8Array', false, undefined);50test('First generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'buffer', 'Uint8Array', false, undefined);51test('First generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'buffer', 'Uint8Array', false, undefined);52test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: false"', testGeneratorFirstEncoding, foobarString, 'buffer', 'Uint8Array', false, false);53test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: true"', testGeneratorFirstEncoding, foobarString, 'buffer', 'Uint8Array', false, true);54test('First generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorFirstEncoding, foobarString, 'hex', 'Uint8Array', false, undefined);55test('First generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'hex', 'Uint8Array', false, undefined);56test('First generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'hex', 'Uint8Array', false, undefined);57test('First generator argument is Uint8Array with encoding "hex", with string writes, "binary: false"', testGeneratorFirstEncoding, foobarString, 'hex', 'Uint8Array', false, false);58test('First generator argument is Uint8Array with encoding "hex", with string writes, "binary: true"', testGeneratorFirstEncoding, foobarString, 'hex', 'Uint8Array', false, true);59test('First generator argument can be string with objectMode', testGeneratorFirstEncoding, foobarString, 'utf8', 'String', true, undefined);60test('First generator argument can be string with objectMode, "binary: false"', testGeneratorFirstEncoding, foobarString, 'utf8', 'String', true, false);61test('First generator argument can be string with objectMode, "binary: true"', testGeneratorFirstEncoding, foobarString, 'utf8', 'String', true, true);62test('First generator argument can be objects with objectMode', testGeneratorFirstEncoding, foobarObject, 'utf8', 'Object', true, undefined);63test('First generator argument can be objects with objectMode, "binary: false"', testGeneratorFirstEncoding, foobarObject, 'utf8', 'Object', true, false);64test('First generator argument can be objects with objectMode, "binary: true"', testGeneratorFirstEncoding, foobarObject, 'utf8', 'Object', true, true);6566// eslint-disable-next-line max-params67const testGeneratorFirstEncodingSync = (t, input, encoding, expectedType, objectMode, binary) => {68 const lines = [];69 execaSync('stdin.js', {stdin: [[input], getTypeofGenerator(lines)(objectMode, binary)], encoding});70 assertTypeofChunk(t, lines, expectedType);71};7273test('First generator argument is string with default encoding, with string writes, sync', testGeneratorFirstEncodingSync, foobarString, 'utf8', 'String', false, undefined);74test('First generator argument is string with default encoding, with Uint8Array writes, sync', testGeneratorFirstEncodingSync, foobarUint8Array, 'utf8', 'String', false, undefined);75test('First generator argument is string with default encoding, with string writes, "binary: false", sync', testGeneratorFirstEncodingSync, foobarString, 'utf8', 'String', false, false);76test('First generator argument is Uint8Array with default encoding, with string writes, "binary: true", sync', testGeneratorFirstEncodingSync, foobarString, 'utf8', 'Uint8Array', false, true);77test('First generator argument is string with encoding "utf16le", with string writes, sync', testGeneratorFirstEncodingSync, foobarString, 'utf16le', 'String', false, undefined);78test('First generator argument is string with encoding "utf16le", with Uint8Array writes, sync', testGeneratorFirstEncodingSync, foobarUint8Array, 'utf16le', 'String', false, undefined);79test('First generator argument is string with encoding "utf16le", with string writes, "binary: false", sync', testGeneratorFirstEncodingSync, foobarString, 'utf16le', 'String', false, false);80test('First generator argument is Uint8Array with encoding "utf16le", with string writes, "binary: true", sync', testGeneratorFirstEncodingSync, foobarString, 'utf16le', 'Uint8Array', false, true);81test('First generator argument is Uint8Array with encoding "buffer", with string writes, sync', testGeneratorFirstEncodingSync, foobarString, 'buffer', 'Uint8Array', false, undefined);82test('First generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, sync', testGeneratorFirstEncodingSync, foobarUint8Array, 'buffer', 'Uint8Array', false, undefined);83test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: false", sync', testGeneratorFirstEncodingSync, foobarString, 'buffer', 'Uint8Array', false, false);84test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: true", sync', testGeneratorFirstEncodingSync, foobarString, 'buffer', 'Uint8Array', false, true);85test('First generator argument is Uint8Array with encoding "hex", with string writes, sync', testGeneratorFirstEncodingSync, foobarString, 'hex', 'Uint8Array', false, undefined);86test('First generator argument is Uint8Array with encoding "hex", with Uint8Array writes, sync', testGeneratorFirstEncodingSync, foobarUint8Array, 'hex', 'Uint8Array', false, undefined);87test('First generator argument is Uint8Array with encoding "hex", with string writes, "binary: false", sync', testGeneratorFirstEncodingSync, foobarString, 'hex', 'Uint8Array', false, false);88test('First generator argument is Uint8Array with encoding "hex", with string writes, "binary: true", sync', testGeneratorFirstEncodingSync, foobarString, 'hex', 'Uint8Array', false, true);89test('First generator argument can be string with objectMode, sync', testGeneratorFirstEncodingSync, foobarString, 'utf8', 'String', true, undefined);90test('First generator argument can be string with objectMode, "binary: false", sync', testGeneratorFirstEncodingSync, foobarString, 'utf8', 'String', true, false);91test('First generator argument can be string with objectMode, "binary: true", sync', testGeneratorFirstEncodingSync, foobarString, 'utf8', 'String', true, true);92test('First generator argument can be objects with objectMode, sync', testGeneratorFirstEncodingSync, foobarObject, 'utf8', 'Object', true, undefined);93test('First generator argument can be objects with objectMode, "binary: false", sync', testGeneratorFirstEncodingSync, foobarObject, 'utf8', 'Object', true, false);94test('First generator argument can be objects with objectMode, "binary: true", sync', testGeneratorFirstEncodingSync, foobarObject, 'utf8', 'Object', true, true);95

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 96–127why, one step back
96/*
97The `encoding` option also determines how binary `stdin` input is decoded before being passed to a transform.98Unlike the tests above, this checks the chunk's value, not only its type.99This must be the same with both asynchronous and synchronous methods.100*/101const getChunkGenerator = chunks => ({102 * transform(chunk) {103 chunks.push(chunk);104 yield chunk;105 },106});107108const testStdinEncodingValue = async (t, encoding) => {109 const chunks = [];110 const subprocess = execa('stdin.js', {stdin: getChunkGenerator(chunks), encoding});111 subprocess.stdin.end(foobarUtf16Uint8Array);112 await subprocess;113 t.deepEqual(chunks, [Buffer.from(foobarUtf16Uint8Array).toString(encoding)]);114};115116test('First generator argument is decoded with encoding "utf8"', testStdinEncodingValue, 'utf8');117test('First generator argument is decoded with encoding "utf16le"', testStdinEncodingValue, 'utf16le');118119const testStdinEncodingValueSync = (t, encoding) => {120 const chunks = [];121 execaSync('stdin.js', {stdin: [[foobarUtf16Uint8Array], getChunkGenerator(chunks)], encoding});122 t.deepEqual(chunks, [Buffer.from(foobarUtf16Uint8Array).toString(encoding)]);123};124125test('First generator argument is decoded with encoding "utf8", sync', testStdinEncodingValueSync, 'utf8');126test('First generator argument is decoded with encoding "utf16le", sync', testStdinEncodingValueSync, 'utf16le');127

Rename files (#977)ehmickybefore adoption4b2e316

lines 128–210why, one step back
128const testEncodingIgnored = async (t, encoding) => {
129 const input = Buffer.from(foobarString).toString(encoding);130 const subprocess = execa('stdin.js', {stdin: noopGenerator(true)});131 subprocess.stdin.end(input, encoding);132 const {stdout} = await subprocess;133 t.is(stdout, input);134};135136test('Write call encoding "utf8" is ignored with objectMode', testEncodingIgnored, 'utf8');137test('Write call encoding "utf16le" is ignored with objectMode', testEncodingIgnored, 'utf16le');138test('Write call encoding "hex" is ignored with objectMode', testEncodingIgnored, 'hex');139test('Write call encoding "base64" is ignored with objectMode', testEncodingIgnored, 'base64');140141// eslint-disable-next-line max-params142const testGeneratorNextEncoding = async (t, input, encoding, firstObjectMode, secondObjectMode, expectedType, execaMethod) => {143 const lines = [];144 await execaMethod('noop.js', ['other'], {145 stdout: [146 getOutputGenerator(input)(firstObjectMode),147 getTypeofGenerator(lines)(secondObjectMode),148 ],149 encoding,150 });151 assertTypeofChunk(t, lines, expectedType);152};153154test('Next generator argument is string with default encoding, with string writes', testGeneratorNextEncoding, foobarString, 'utf8', false, false, 'String', execa);155test('Next generator argument is string with default encoding, with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'utf8', true, false, 'String', execa);156test('Next generator argument is string with default encoding, with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'utf8', true, true, 'String', execa);157test('Next generator argument is string with default encoding, with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'utf8', false, false, 'String', execa);158test('Next generator argument is Uint8Array with default encoding, with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, false, 'Uint8Array', execa);159test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, true, 'Uint8Array', execa);160test('Next generator argument is string with encoding "utf16le", with string writes', testGeneratorNextEncoding, foobarString, 'utf16le', false, false, 'String', execa);161test('Next generator argument is string with encoding "utf16le",, with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'utf16le', true, false, 'String', execa);162test('Next generator argument is string with encoding "utf16le",, with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'utf16le', true, true, 'String', execa);163test('Next generator argument is string with encoding "utf16le",, with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'utf16le', false, false, 'String', execa);164test('Next generator argument is Uint8Array with encoding "utf16le",, with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'utf16le', true, false, 'Uint8Array', execa);165test('Next generator argument is string with encoding "utf16le",, with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'utf16le', true, true, 'Uint8Array', execa);166test('Next generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorNextEncoding, foobarString, 'buffer', false, false, 'Uint8Array', execa);167test('Next generator argument is string with encoding "buffer", with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'buffer', true, false, 'String', execa);168test('Next generator argument is string with encoding "buffer", with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'buffer', true, true, 'String', execa);169test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'buffer', false, false, 'Uint8Array', execa);170test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, false, 'Uint8Array', execa);171test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, true, 'Uint8Array', execa);172test('Next generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'Uint8Array', execa);173test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'Uint8Array', execa);174test('Next generator argument is object with default encoding, with object writes, objectMode first', testGeneratorNextEncoding, foobarObject, 'utf8', true, false, 'Object', execa);175test('Next generator argument is object with default encoding, with object writes, objectMode both', testGeneratorNextEncoding, foobarObject, 'utf8', true, true, 'Object', execa);176test('Next generator argument is string with default encoding, with string writes, sync', testGeneratorNextEncoding, foobarString, 'utf8', false, false, 'String', execaSync);177test('Next generator argument is string with default encoding, with string writes, objectMode first, sync', testGeneratorNextEncoding, foobarString, 'utf8', true, false, 'String', execaSync);178test('Next generator argument is string with default encoding, with string writes, objectMode both, sync', testGeneratorNextEncoding, foobarString, 'utf8', true, true, 'String', execaSync);179test('Next generator argument is string with default encoding, with Uint8Array writes, sync', testGeneratorNextEncoding, foobarUint8Array, 'utf8', false, false, 'String', execaSync);180test('Next generator argument is Uint8Array with default encoding, with Uint8Array writes, objectMode first, sync', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, false, 'Uint8Array', execaSync);181test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode both, sync', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, true, 'Uint8Array', execaSync);182test('Next generator argument is string with encoding "utf16le", with string writes, sync', testGeneratorNextEncoding, foobarString, 'utf16le', false, false, 'String', execaSync);183test('Next generator argument is string with encoding "utf16le",, with string writes, objectMode first, sync', testGeneratorNextEncoding, foobarString, 'utf16le', true, false, 'String', execaSync);184test('Next generator argument is string with encoding "utf16le",, with string writes, objectMode both, sync', testGeneratorNextEncoding, foobarString, 'utf16le', true, true, 'String', execaSync);185test('Next generator argument is string with encoding "utf16le",, with Uint8Array writes, sync', testGeneratorNextEncoding, foobarUint8Array, 'utf16le', false, false, 'String', execaSync);186test('Next generator argument is Uint8Array with encoding "utf16le",, with Uint8Array writes, objectMode first, sync', testGeneratorNextEncoding, foobarUint8Array, 'utf16le', true, false, 'Uint8Array', execaSync);187test('Next generator argument is string with encoding "utf16le",, with Uint8Array writes, objectMode both, sync', testGeneratorNextEncoding, foobarUint8Array, 'utf16le', true, true, 'Uint8Array', execaSync);188test('Next generator argument is Uint8Array with encoding "buffer", with string writes, sync', testGeneratorNextEncoding, foobarString, 'buffer', false, false, 'Uint8Array', execaSync);189test('Next generator argument is string with encoding "buffer", with string writes, objectMode first, sync', testGeneratorNextEncoding, foobarString, 'buffer', true, false, 'String', execaSync);190test('Next generator argument is string with encoding "buffer", with string writes, objectMode both, sync', testGeneratorNextEncoding, foobarString, 'buffer', true, true, 'String', execaSync);191test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, sync', testGeneratorNextEncoding, foobarUint8Array, 'buffer', false, false, 'Uint8Array', execaSync);192test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first, sync', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, false, 'Uint8Array', execaSync);193test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both, sync', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, true, 'Uint8Array', execaSync);194test('Next generator argument is Uint8Array with encoding "hex", with string writes, sync', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'Uint8Array', execaSync);195test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes, sync', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'Uint8Array', execaSync);196test('Next generator argument is object with default encoding, with object writes, objectMode first, sync', testGeneratorNextEncoding, foobarObject, 'utf8', true, false, 'Object', execaSync);197test('Next generator argument is object with default encoding, with object writes, objectMode both, sync', testGeneratorNextEncoding, foobarObject, 'utf8', true, true, 'Object', execaSync);198199const testFirstOutputGeneratorArgument = async (t, fdNumber, execaMethod) => {200 const lines = [];201 await execaMethod('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, getTypeofGenerator(lines)(true)));202 assertTypeofChunk(t, lines, 'String');203};204205test('The first generator with result.stdout does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 1, execa);206test('The first generator with result.stderr does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 2, execa);207test('The first generator with result.stdio[*] does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 3, execa);208test('The first generator with result.stdout does not receive an object argument even in objectMode, sync', testFirstOutputGeneratorArgument, 1, execaSync);209test('The first generator with result.stderr does not receive an object argument even in objectMode, sync', testFirstOutputGeneratorArgument, 2, execaSync);210test('The first generator with result.stdio[*] does not receive an object argument even in objectMode, sync', testFirstOutputGeneratorArgument, 3, execaSync);

Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3

lines 211–308why, one step back
211
212// When the `encoding` option is binary, the whole pipeline is byte-oriented: a transform yielding a213// string has it encoded as UTF-8, then those bytes are serialized using the `encoding` option.214// This is the only coherent rule, since a yielded string like 'foobar' is neither valid hex nor valid base64.215// A non-ASCII string is used, so UTF-8 and the binary encoding differ.216const nonAsciiString = 'ré😀';217const nonAsciiBuffer = Buffer.from(nonAsciiString, 'utf8');218219const testYieldStringBinaryEncoding = async (t, encoding, execaMethod) => {220 const {stdout} = await execaMethod('noop.js', {221 stdout: getOutputGenerator(nonAsciiString)(false, true),222 encoding,223 });224 t.is(stdout, nonAsciiBuffer.toString(encoding));225};226227test('Transforms yielding a string are UTF-8 encoded, latin1', testYieldStringBinaryEncoding, 'latin1', execa);228test('Transforms yielding a string are UTF-8 encoded, latin1, sync', testYieldStringBinaryEncoding, 'latin1', execaSync);229test('Transforms yielding a string are UTF-8 encoded, hex', testYieldStringBinaryEncoding, 'hex', execa);230test('Transforms yielding a string are UTF-8 encoded, hex, sync', testYieldStringBinaryEncoding, 'hex', execaSync);231test('Transforms yielding a string are UTF-8 encoded, base64', testYieldStringBinaryEncoding, 'base64', execa);232test('Transforms yielding a string are UTF-8 encoded, base64, sync', testYieldStringBinaryEncoding, 'base64', execaSync);233234// A transform yielding the bytes it received must round-trip them unchanged235const testYieldBytesBinaryEncoding = async (t, execaMethod) => {236 const {stdout} = await execaMethod('noop-fd.js', ['1', nonAsciiString], {237 stdout: noopGenerator(false, true),238 encoding: 'latin1',239 });240 t.is(stdout, nonAsciiBuffer.toString('latin1'));241};242243test('Transforms yielding Uint8Array keep the bytes unchanged', testYieldBytesBinaryEncoding, execa);244test('Transforms yielding Uint8Array keep the bytes unchanged, sync', testYieldBytesBinaryEncoding, execaSync);245246/*247With a text encoding, a transform receives text decoded with it, so a string it yields must be encoded back with it.248Otherwise that string would be encoded as UTF-8, then decoded with the `encoding` option, which corrupts it.249*/250const nonAsciiUtf16Uint8Array = new Uint8Array(Buffer.from(nonAsciiString, 'utf16le'));251252const testYieldStringTextEncoding = async (t, transform, expectedOutput, execaMethod) => {253 const {stdout} = await execaMethod('noop.js', {254 stdout: [getOutputGenerator(nonAsciiUtf16Uint8Array)(false, true), transform],255 encoding: 'utf16le',256 });257 t.is(stdout, expectedOutput);258};259260test('Transforms yielding a string are encoded with encoding "utf16le"', testYieldStringTextEncoding, noopGenerator(), nonAsciiString, execa);261test('Transforms yielding a string are encoded with encoding "utf16le", sync', testYieldStringTextEncoding, noopGenerator(), nonAsciiString, execaSync);262test('Transforms modifying a string are encoded with encoding "utf16le"', testYieldStringTextEncoding, uppercaseGenerator(), nonAsciiString.toUpperCase(), execa);263test('Transforms modifying a string are encoded with encoding "utf16le", sync', testYieldStringTextEncoding, uppercaseGenerator(), nonAsciiString.toUpperCase(), execaSync);264test('Transforms yielding a Uint8Array keep the bytes unchanged with encoding "utf16le"', testYieldStringTextEncoding, getOutputGenerator(nonAsciiUtf16Uint8Array)(false, true), nonAsciiString, execa);265test('Transforms yielding a Uint8Array keep the bytes unchanged with encoding "utf16le", sync', testYieldStringTextEncoding, getOutputGenerator(nonAsciiUtf16Uint8Array)(false, true), nonAsciiString, execaSync);266267// A `stdin` transform also receives text decoded with the `encoding` option, so the same applies in that direction268const testYieldStringTextEncodingInput = async (t, execaMethod) => {269 const {stdout} = await execaMethod('stdin.js', {270 stdin: [[nonAsciiUtf16Uint8Array], noopGenerator()],271 encoding: 'utf16le',272 });273 t.is(stdout, nonAsciiString);274};275276test('Transforms yielding a string are encoded with encoding "utf16le", stdin', testYieldStringTextEncodingInput, execa);277test('Transforms yielding a string are encoded with encoding "utf16le", stdin, sync', testYieldStringTextEncodingInput, execaSync);278279// When the `stdin` value is a string, the transform receives it as text, so it must not be encoded as UTF-8 either280const testStringInputTextEncoding = async (t, execaMethod) => {281 const {stdout} = await execaMethod('stdin.js', {282 stdin: [[nonAsciiString], noopGenerator()],283 encoding: 'utf16le',284 });285 t.is(stdout, nonAsciiString);286};287288test('String input is encoded with encoding "utf16le", stdin', testStringInputTextEncoding, execa);289test('String input is encoded with encoding "utf16le", stdin, sync', testStringInputTextEncoding, execaSync);290291// A transform may also yield `Uint8Array`s with a text encoding. The newline re-added after each stripped line must then be encoded with that same encoding, otherwise the bytes become misaligned.292const utf16leYieldGenerator = {293 * transform(line) {294 yield Buffer.from(line, 'utf16le');295 },296};297298const testYieldUtf16LeLines = async (t, execaMethod) => {299 const {stdout} = await execaMethod('stdin.js', {300 stdin: [['aaa\nbbb\nccc\n'], utf16leYieldGenerator],301 encoding: 'utf16le',302 stripFinalNewline: false,303 });304 t.is(stdout, 'aaa\nbbb\nccc\n');305};306307test('Transforms yielding Uint8Array re-add newlines with encoding "utf16le"', testYieldUtf16LeLines, execa);308test('Transforms yielding Uint8Array re-add newlines with encoding "utf16le", sync', testYieldUtf16LeLines, execaSync);