encoding-transform.js
This file
Path
test/transform/encoding-transform.js
297 lines · 27.5 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298import assert from 'node:assert/strict';import {Buffer} from 'node:buffer';import test from 'node:test';import {execa, execaSync} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {getStdio} from '../helpers/stdio.js';import { foobarString, foobarUint8Array, foobarBuffer, foobarObject,} from '../helpers/input.js';import {noopGenerator, getOutputGenerator, uppercaseGenerator} from '../helpers/generator.js';
setFixtureDirectory();
const getTypeofGenerator = lines => (objectMode, binary) => ({ * transform(line) { lines.push(Object.prototype.toString.call(line)); yield ''; }, objectMode, binary,});
const assertTypeofChunk = (lines, expectedType) => { assert.deepEqual(lines, [`[object ${expectedType}]`]);};
// eslint-disable-next-line max-paramsconst testGeneratorFirstEncoding = async (input, encoding, expectedType, objectMode, binary) => { const lines = []; const subprocess = execa('stdin.js', {stdin: getTypeofGenerator(lines)(objectMode, binary), encoding}); subprocess.stdin.end(input); await subprocess; assertTypeofChunk(lines, expectedType);};
test('First generator argument is string with default encoding, with string writes', () => testGeneratorFirstEncoding(foobarString, 'utf8', 'String', false, undefined));test('First generator argument is string with default encoding, with Buffer writes', () => testGeneratorFirstEncoding(foobarBuffer, 'utf8', 'String', false, undefined));test('First generator argument is string with default encoding, with Uint8Array writes', () => testGeneratorFirstEncoding(foobarUint8Array, 'utf8', 'String', false, undefined));test('First generator argument is string with default encoding, with string writes, "binary: false"', () => testGeneratorFirstEncoding(foobarString, 'utf8', 'String', false, false));test('First generator argument is Uint8Array with default encoding, with string writes, "binary: true"', () => testGeneratorFirstEncoding(foobarString, 'utf8', 'Uint8Array', false, true));test('First generator argument is string with encoding "utf16le", with string writes', () => testGeneratorFirstEncoding(foobarString, 'utf16le', 'String', false, undefined));test('First generator argument is string with encoding "utf16le", with Buffer writes', () => testGeneratorFirstEncoding(foobarBuffer, 'utf16le', 'String', false, undefined));test('First generator argument is string with encoding "utf16le", with Uint8Array writes', () => testGeneratorFirstEncoding(foobarUint8Array, 'utf16le', 'String', false, undefined));test('First generator argument is string with encoding "utf16le", with string writes, "binary: false"', () => testGeneratorFirstEncoding(foobarString, 'utf16le', 'String', false, false));test('First generator argument is Uint8Array with encoding "utf16le", with string writes, "binary: true"', () => testGeneratorFirstEncoding(foobarString, 'utf16le', 'Uint8Array', false, true));test('First generator argument is Uint8Array with encoding "buffer", with string writes', () => testGeneratorFirstEncoding(foobarString, 'buffer', 'Uint8Array', false, undefined));test('First generator argument is Uint8Array with encoding "buffer", with Buffer writes', () => testGeneratorFirstEncoding(foobarBuffer, 'buffer', 'Uint8Array', false, undefined));test('First generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', () => testGeneratorFirstEncoding(foobarUint8Array, 'buffer', 'Uint8Array', false, undefined));test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: false"', () => testGeneratorFirstEncoding(foobarString, 'buffer', 'Uint8Array', false, false));test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: true"', () => testGeneratorFirstEncoding(foobarString, 'buffer', 'Uint8Array', false, true));test('First generator argument is Uint8Array with encoding "hex", with string writes', () => testGeneratorFirstEncoding(foobarString, 'hex', 'Uint8Array', false, undefined));test('First generator argument is Uint8Array with encoding "hex", with Buffer writes', () => testGeneratorFirstEncoding(foobarBuffer, 'hex', 'Uint8Array', false, undefined));test('First generator argument is Uint8Array with encoding "hex", with Uint8Array writes', () => testGeneratorFirstEncoding(foobarUint8Array, 'hex', 'Uint8Array', false, undefined));test('First generator argument is Uint8Array with encoding "hex", with string writes, "binary: false"', () => testGeneratorFirstEncoding(foobarString, 'hex', 'Uint8Array', false, false));test('First generator argument is Uint8Array with encoding "hex", with string writes, "binary: true"', () => testGeneratorFirstEncoding(foobarString, 'hex', 'Uint8Array', false, true));test('First generator argument can be string with objectMode', () => testGeneratorFirstEncoding(foobarString, 'utf8', 'String', true, undefined));test('First generator argument can be string with objectMode, "binary: false"', () => testGeneratorFirstEncoding(foobarString, 'utf8', 'String', true, false));test('First generator argument can be string with objectMode, "binary: true"', () => testGeneratorFirstEncoding(foobarString, 'utf8', 'String', true, true));test('First generator argument can be objects with objectMode', () => testGeneratorFirstEncoding(foobarObject, 'utf8', 'Object', true, undefined));test('First generator argument can be objects with objectMode, "binary: false"', () => testGeneratorFirstEncoding(foobarObject, 'utf8', 'Object', true, false));test('First generator argument can be objects with objectMode, "binary: true"', () => testGeneratorFirstEncoding(foobarObject, 'utf8', 'Object', true, true));
// eslint-disable-next-line max-paramsconst testGeneratorFirstEncodingSync = (input, encoding, expectedType, objectMode, binary) => { const lines = []; execaSync('stdin.js', {stdin: [[input], getTypeofGenerator(lines)(objectMode, binary)], encoding}); assertTypeofChunk(lines, expectedType);};
test('First generator argument is string with default encoding, with string writes, sync', () => testGeneratorFirstEncodingSync(foobarString, 'utf8', 'String', false, undefined));test('First generator argument is string with default encoding, with Uint8Array writes, sync', () => testGeneratorFirstEncodingSync(foobarUint8Array, 'utf8', 'String', false, undefined));test('First generator argument is string with default encoding, with string writes, "binary: false", sync', () => testGeneratorFirstEncodingSync(foobarString, 'utf8', 'String', false, false));test('First generator argument is Uint8Array with default encoding, with string writes, "binary: true", sync', () => testGeneratorFirstEncodingSync(foobarString, 'utf8', 'Uint8Array', false, true));test('First generator argument is string with encoding "utf16le", with string writes, sync', () => testGeneratorFirstEncodingSync(foobarString, 'utf16le', 'String', false, undefined));test('First generator argument is string with encoding "utf16le", with Uint8Array writes, sync', () => testGeneratorFirstEncodingSync(foobarUint8Array, 'utf16le', 'String', false, undefined));test('First generator argument is string with encoding "utf16le", with string writes, "binary: false", sync', () => testGeneratorFirstEncodingSync(foobarString, 'utf16le', 'String', false, false));test('First generator argument is Uint8Array with encoding "utf16le", with string writes, "binary: true", sync', () => testGeneratorFirstEncodingSync(foobarString, 'utf16le', 'Uint8Array', false, true));test('First generator argument is Uint8Array with encoding "buffer", with string writes, sync', () => testGeneratorFirstEncodingSync(foobarString, 'buffer', 'Uint8Array', false, undefined));test('First generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, sync', () => testGeneratorFirstEncodingSync(foobarUint8Array, 'buffer', 'Uint8Array', false, undefined));test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: false", sync', () => testGeneratorFirstEncodingSync(foobarString, 'buffer', 'Uint8Array', false, false));test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: true", sync', () => testGeneratorFirstEncodingSync(foobarString, 'buffer', 'Uint8Array', false, true));test('First generator argument is Uint8Array with encoding "hex", with string writes, sync', () => testGeneratorFirstEncodingSync(foobarString, 'hex', 'Uint8Array', false, undefined));test('First generator argument is Uint8Array with encoding "hex", with Uint8Array writes, sync', () => testGeneratorFirstEncodingSync(foobarUint8Array, 'hex', 'Uint8Array', false, undefined));test('First generator argument is Uint8Array with encoding "hex", with string writes, "binary: false", sync', () => testGeneratorFirstEncodingSync(foobarString, 'hex', 'Uint8Array', false, false));test('First generator argument is Uint8Array with encoding "hex", with string writes, "binary: true", sync', () => testGeneratorFirstEncodingSync(foobarString, 'hex', 'Uint8Array', false, true));test('First generator argument can be string with objectMode, sync', () => testGeneratorFirstEncodingSync(foobarString, 'utf8', 'String', true, undefined));test('First generator argument can be string with objectMode, "binary: false", sync', () => testGeneratorFirstEncodingSync(foobarString, 'utf8', 'String', true, false));test('First generator argument can be string with objectMode, "binary: true", sync', () => testGeneratorFirstEncodingSync(foobarString, 'utf8', 'String', true, true));test('First generator argument can be objects with objectMode, sync', () => testGeneratorFirstEncodingSync(foobarObject, 'utf8', 'Object', true, undefined));test('First generator argument can be objects with objectMode, "binary: false", sync', () => testGeneratorFirstEncodingSync(foobarObject, 'utf8', 'Object', true, false));test('First generator argument can be objects with objectMode, "binary: true", sync', () => testGeneratorFirstEncodingSync(foobarObject, 'utf8', 'Object', true, true));
/*The `encoding` option only applies to the output, so binary `stdin` input is decoded with UTF-8 before being passed to a transform, whichever text encoding that option uses.Unlike the tests above, this checks the chunk's value, not only its type.This must be the same with both asynchronous and synchronous methods.*/const getChunkGenerator = chunks => ({ * transform(chunk) { chunks.push(chunk); yield chunk; },});
const testStdinEncodingValue = async encoding => { const chunks = []; const subprocess = execa('stdin.js', {stdin: getChunkGenerator(chunks), encoding}); subprocess.stdin.end(foobarUint8Array); await subprocess; assert.deepEqual(chunks, [foobarString]);};
test('First generator argument is decoded with UTF-8 with encoding "utf8", stdin', () => testStdinEncodingValue('utf8'));test('First generator argument is decoded with UTF-8 with encoding "utf16le", stdin', () => testStdinEncodingValue('utf16le'));
const testStdinEncodingValueSync = encoding => { const chunks = []; execaSync('stdin.js', {stdin: [[foobarUint8Array], getChunkGenerator(chunks)], encoding}); assert.deepEqual(chunks, [foobarString]);};
test('First generator argument is decoded with UTF-8 with encoding "utf8", stdin, sync', () => testStdinEncodingValueSync('utf8'));test('First generator argument is decoded with UTF-8 with encoding "utf16le", stdin, sync', () => testStdinEncodingValueSync('utf16le'));
const testEncodingIgnored = async encoding => { const input = Buffer.from(foobarString).toString(encoding); const subprocess = execa('stdin.js', {stdin: noopGenerator(true)}); subprocess.stdin.end(input, encoding); const {stdout} = await subprocess; assert.equal(stdout, input);};
test('Write call encoding "utf8" is ignored with objectMode', () => testEncodingIgnored('utf8'));test('Write call encoding "utf16le" is ignored with objectMode', () => testEncodingIgnored('utf16le'));test('Write call encoding "hex" is ignored with objectMode', () => testEncodingIgnored('hex'));test('Write call encoding "base64" is ignored with objectMode', () => testEncodingIgnored('base64'));
// eslint-disable-next-line max-paramsconst testGeneratorNextEncoding = async (input, encoding, firstObjectMode, secondObjectMode, expectedType, execaMethod) => { const lines = []; await execaMethod('noop.js', ['other'], { stdout: [ getOutputGenerator(input)(firstObjectMode), getTypeofGenerator(lines)(secondObjectMode), ], encoding, }); assertTypeofChunk(lines, expectedType);};
test('Next generator argument is string with default encoding, with string writes', () => testGeneratorNextEncoding(foobarString, 'utf8', false, false, 'String', execa));test('Next generator argument is string with default encoding, with string writes, objectMode first', () => testGeneratorNextEncoding(foobarString, 'utf8', true, false, 'String', execa));test('Next generator argument is string with default encoding, with string writes, objectMode both', () => testGeneratorNextEncoding(foobarString, 'utf8', true, true, 'String', execa));test('Next generator argument is string with default encoding, with Uint8Array writes', () => testGeneratorNextEncoding(foobarUint8Array, 'utf8', false, false, 'String', execa));test('Next generator argument is Uint8Array with default encoding, with Uint8Array writes, objectMode first', () => testGeneratorNextEncoding(foobarUint8Array, 'utf8', true, false, 'Uint8Array', execa));test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode both', () => testGeneratorNextEncoding(foobarUint8Array, 'utf8', true, true, 'Uint8Array', execa));test('Next generator argument is string with encoding "utf16le", with string writes', () => testGeneratorNextEncoding(foobarString, 'utf16le', false, false, 'String', execa));test('Next generator argument is string with encoding "utf16le",, with string writes, objectMode first', () => testGeneratorNextEncoding(foobarString, 'utf16le', true, false, 'String', execa));test('Next generator argument is string with encoding "utf16le",, with string writes, objectMode both', () => testGeneratorNextEncoding(foobarString, 'utf16le', true, true, 'String', execa));test('Next generator argument is string with encoding "utf16le",, with Uint8Array writes', () => testGeneratorNextEncoding(foobarUint8Array, 'utf16le', false, false, 'String', execa));test('Next generator argument is Uint8Array with encoding "utf16le",, with Uint8Array writes, objectMode first', () => testGeneratorNextEncoding(foobarUint8Array, 'utf16le', true, false, 'Uint8Array', execa));test('Next generator argument is string with encoding "utf16le",, with Uint8Array writes, objectMode both', () => testGeneratorNextEncoding(foobarUint8Array, 'utf16le', true, true, 'Uint8Array', execa));test('Next generator argument is Uint8Array with encoding "buffer", with string writes', () => testGeneratorNextEncoding(foobarString, 'buffer', false, false, 'Uint8Array', execa));test('Next generator argument is string with encoding "buffer", with string writes, objectMode first', () => testGeneratorNextEncoding(foobarString, 'buffer', true, false, 'String', execa));test('Next generator argument is string with encoding "buffer", with string writes, objectMode both', () => testGeneratorNextEncoding(foobarString, 'buffer', true, true, 'String', execa));test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', () => testGeneratorNextEncoding(foobarUint8Array, 'buffer', false, false, 'Uint8Array', execa));test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first', () => testGeneratorNextEncoding(foobarUint8Array, 'buffer', true, false, 'Uint8Array', execa));test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both', () => testGeneratorNextEncoding(foobarUint8Array, 'buffer', true, true, 'Uint8Array', execa));test('Next generator argument is Uint8Array with encoding "hex", with string writes', () => testGeneratorNextEncoding(foobarString, 'hex', false, false, 'Uint8Array', execa));test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', () => testGeneratorNextEncoding(foobarUint8Array, 'hex', false, false, 'Uint8Array', execa));test('Next generator argument is object with default encoding, with object writes, objectMode first', () => testGeneratorNextEncoding(foobarObject, 'utf8', true, false, 'Object', execa));test('Next generator argument is object with default encoding, with object writes, objectMode both', () => testGeneratorNextEncoding(foobarObject, 'utf8', true, true, 'Object', execa));test('Next generator argument is string with default encoding, with string writes, sync', () => testGeneratorNextEncoding(foobarString, 'utf8', false, false, 'String', execaSync));test('Next generator argument is string with default encoding, with string writes, objectMode first, sync', () => testGeneratorNextEncoding(foobarString, 'utf8', true, false, 'String', execaSync));test('Next generator argument is string with default encoding, with string writes, objectMode both, sync', () => testGeneratorNextEncoding(foobarString, 'utf8', true, true, 'String', execaSync));test('Next generator argument is string with default encoding, with Uint8Array writes, sync', () => testGeneratorNextEncoding(foobarUint8Array, 'utf8', false, false, 'String', execaSync));test('Next generator argument is Uint8Array with default encoding, with Uint8Array writes, objectMode first, sync', () => testGeneratorNextEncoding(foobarUint8Array, 'utf8', true, false, 'Uint8Array', execaSync));test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode both, sync', () => testGeneratorNextEncoding(foobarUint8Array, 'utf8', true, true, 'Uint8Array', execaSync));test('Next generator argument is string with encoding "utf16le", with string writes, sync', () => testGeneratorNextEncoding(foobarString, 'utf16le', false, false, 'String', execaSync));test('Next generator argument is string with encoding "utf16le",, with string writes, objectMode first, sync', () => testGeneratorNextEncoding(foobarString, 'utf16le', true, false, 'String', execaSync));test('Next generator argument is string with encoding "utf16le",, with string writes, objectMode both, sync', () => testGeneratorNextEncoding(foobarString, 'utf16le', true, true, 'String', execaSync));test('Next generator argument is string with encoding "utf16le",, with Uint8Array writes, sync', () => testGeneratorNextEncoding(foobarUint8Array, 'utf16le', false, false, 'String', execaSync));test('Next generator argument is Uint8Array with encoding "utf16le",, with Uint8Array writes, objectMode first, sync', () => testGeneratorNextEncoding(foobarUint8Array, 'utf16le', true, false, 'Uint8Array', execaSync));test('Next generator argument is string with encoding "utf16le",, with Uint8Array writes, objectMode both, sync', () => testGeneratorNextEncoding(foobarUint8Array, 'utf16le', true, true, 'Uint8Array', execaSync));test('Next generator argument is Uint8Array with encoding "buffer", with string writes, sync', () => testGeneratorNextEncoding(foobarString, 'buffer', false, false, 'Uint8Array', execaSync));test('Next generator argument is string with encoding "buffer", with string writes, objectMode first, sync', () => testGeneratorNextEncoding(foobarString, 'buffer', true, false, 'String', execaSync));test('Next generator argument is string with encoding "buffer", with string writes, objectMode both, sync', () => testGeneratorNextEncoding(foobarString, 'buffer', true, true, 'String', execaSync));test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, sync', () => testGeneratorNextEncoding(foobarUint8Array, 'buffer', false, false, 'Uint8Array', execaSync));test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first, sync', () => testGeneratorNextEncoding(foobarUint8Array, 'buffer', true, false, 'Uint8Array', execaSync));test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both, sync', () => testGeneratorNextEncoding(foobarUint8Array, 'buffer', true, true, 'Uint8Array', execaSync));test('Next generator argument is Uint8Array with encoding "hex", with string writes, sync', () => testGeneratorNextEncoding(foobarString, 'hex', false, false, 'Uint8Array', execaSync));test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes, sync', () => testGeneratorNextEncoding(foobarUint8Array, 'hex', false, false, 'Uint8Array', execaSync));test('Next generator argument is object with default encoding, with object writes, objectMode first, sync', () => testGeneratorNextEncoding(foobarObject, 'utf8', true, false, 'Object', execaSync));test('Next generator argument is object with default encoding, with object writes, objectMode both, sync', () => testGeneratorNextEncoding(foobarObject, 'utf8', true, true, 'Object', execaSync));
const testFirstOutputGeneratorArgument = async (fdNumber, execaMethod) => { const lines = []; await execaMethod('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, getTypeofGenerator(lines)(true))); assertTypeofChunk(lines, 'String');};
test('The first generator with result.stdout does not receive an object argument even in objectMode', () => testFirstOutputGeneratorArgument(1, execa));test('The first generator with result.stderr does not receive an object argument even in objectMode', () => testFirstOutputGeneratorArgument(2, execa));test('The first generator with result.stdio[*] does not receive an object argument even in objectMode', () => testFirstOutputGeneratorArgument(3, execa));test('The first generator with result.stdout does not receive an object argument even in objectMode, sync', () => testFirstOutputGeneratorArgument(1, execaSync));test('The first generator with result.stderr does not receive an object argument even in objectMode, sync', () => testFirstOutputGeneratorArgument(2, execaSync));test('The first generator with result.stdio[*] does not receive an object argument even in objectMode, sync', () => testFirstOutputGeneratorArgument(3, execaSync));
// When the `encoding` option is binary, the whole pipeline is byte-oriented: a transform yielding a// string has it encoded as UTF-8, then those bytes are serialized using the `encoding` option.// This is the only coherent rule, since a yielded string like 'foobar' is neither valid hex nor valid base64.// A non-ASCII string is used, so UTF-8 and the binary encoding differ.const nonAsciiString = 'ré😀';const nonAsciiBuffer = Buffer.from(nonAsciiString, 'utf8');
const testYieldStringBinaryEncoding = async (encoding, execaMethod) => { const {stdout} = await execaMethod('noop.js', { stdout: getOutputGenerator(nonAsciiString)(false, true), encoding, }); assert.equal(stdout, nonAsciiBuffer.toString(encoding));};
test('Transforms yielding a string are UTF-8 encoded, latin1', () => testYieldStringBinaryEncoding('latin1', execa));test('Transforms yielding a string are UTF-8 encoded, latin1, sync', () => testYieldStringBinaryEncoding('latin1', execaSync));test('Transforms yielding a string are UTF-8 encoded, hex', () => testYieldStringBinaryEncoding('hex', execa));test('Transforms yielding a string are UTF-8 encoded, hex, sync', () => testYieldStringBinaryEncoding('hex', execaSync));test('Transforms yielding a string are UTF-8 encoded, base64', () => testYieldStringBinaryEncoding('base64', execa));test('Transforms yielding a string are UTF-8 encoded, base64, sync', () => testYieldStringBinaryEncoding('base64', execaSync));
// A transform yielding the bytes it received must round-trip them unchangedconst testYieldBytesBinaryEncoding = async execaMethod => { const {stdout} = await execaMethod('noop-fd.js', ['1', nonAsciiString], { stdout: noopGenerator(false, true), encoding: 'latin1', }); assert.equal(stdout, nonAsciiBuffer.toString('latin1'));};
test('Transforms yielding Uint8Array keep the bytes unchanged', () => testYieldBytesBinaryEncoding(execa));test('Transforms yielding Uint8Array keep the bytes unchanged, sync', () => testYieldBytesBinaryEncoding(execaSync));
/*With a text encoding, a transform receives text decoded with it, so a string it yields must be encoded back with it.Otherwise that string would be encoded as UTF-8, then decoded with the `encoding` option, which corrupts it.*/const nonAsciiUtf16Uint8Array = new Uint8Array(Buffer.from(nonAsciiString, 'utf16le'));
const testYieldStringTextEncoding = async (transform, expectedOutput, execaMethod) => { const {stdout} = await execaMethod('noop.js', { stdout: [getOutputGenerator(nonAsciiUtf16Uint8Array)(false, true), transform], encoding: 'utf16le', }); assert.equal(stdout, expectedOutput);};
test('Transforms yielding a string are encoded with encoding "utf16le"', () => testYieldStringTextEncoding(noopGenerator(), nonAsciiString, execa));test('Transforms yielding a string are encoded with encoding "utf16le", sync', () => testYieldStringTextEncoding(noopGenerator(), nonAsciiString, execaSync));test('Transforms modifying a string are encoded with encoding "utf16le"', () => testYieldStringTextEncoding(uppercaseGenerator(), nonAsciiString.toUpperCase(), execa));test('Transforms modifying a string are encoded with encoding "utf16le", sync', () => testYieldStringTextEncoding(uppercaseGenerator(), nonAsciiString.toUpperCase(), execaSync));test('Transforms yielding a Uint8Array keep the bytes unchanged with encoding "utf16le"', () => testYieldStringTextEncoding(getOutputGenerator(nonAsciiUtf16Uint8Array)(false, true), nonAsciiString, execa));test('Transforms yielding a Uint8Array keep the bytes unchanged with encoding "utf16le", sync', () => testYieldStringTextEncoding(getOutputGenerator(nonAsciiUtf16Uint8Array)(false, true), nonAsciiString, execaSync));
// With the `binary` transform option, a string is encoded with UTF-8, including when it is passed to the next transformconst testYieldStringBinaryNextTransform = async execaMethod => { const {stdout} = await execaMethod('noop-fd.js', ['1', foobarString], { stdout: [getOutputGenerator(foobarString)(false, true), noopGenerator()], encoding: 'utf16le', }); assert.equal(stdout, Buffer.from(foobarString).toString('utf16le'));};
test('Transforms with "binary: true" yielding a string to the next transform are encoded with UTF-8 with encoding "utf16le"', () => testYieldStringBinaryNextTransform(execa));test('Transforms with "binary: true" yielding a string to the next transform are encoded with UTF-8 with encoding "utf16le", sync', () => testYieldStringBinaryNextTransform(execaSync));
/*A `stdin` transform receives and yields text encoded with UTF-8, since the `encoding` option only applies to the output.Since the subprocess echoes its input, its output is those UTF-8 bytes decoded as UTF-16.*/const unicornsString = '🦄🦄.';
const testStdinTransformUtf8 = async (input, execaMethod) => { const {stdout} = await execaMethod('stdin.js', { stdin: [[input], noopGenerator()], encoding: 'utf16le', stripFinalNewline: false, }); assert.equal(stdout, Buffer.from(`${Buffer.from(input).toString()}\n`).toString('utf16le'));};
test('Transforms yielding a string are encoded with UTF-8 with encoding "utf16le", stdin', () => testStdinTransformUtf8(new Uint8Array(Buffer.from(unicornsString)), execa));test('Transforms yielding a string are encoded with UTF-8 with encoding "utf16le", stdin, sync', () => testStdinTransformUtf8(new Uint8Array(Buffer.from(unicornsString)), execaSync));test('String input is encoded with UTF-8 with encoding "utf16le", stdin', () => testStdinTransformUtf8(unicornsString, execa));test('String input is encoded with UTF-8 with encoding "utf16le", stdin, sync', () => testStdinTransformUtf8(unicornsString, execaSync));