import {Buffer} from 'node:buffer'; import test from 'ava'; import {execa, execaSync} from '../../index.js'; import {setFixtureDirectory} from '../helpers/fixtures-directory.js'; import {getStdio} from '../helpers/stdio.js'; import { foobarString, foobarUint8Array, foobarBuffer, foobarUtf16Uint8Array, 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 = (t, lines, expectedType) => { t.deepEqual(lines, [`[object ${expectedType}]`]); }; // eslint-disable-next-line max-params const testGeneratorFirstEncoding = async (t, 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(t, 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-params const testGeneratorFirstEncodingSync = (t, input, encoding, expectedType, objectMode, binary) => { const lines = []; execaSync('stdin.js', {stdin: [[input], getTypeofGenerator(lines)(objectMode, binary)], encoding}); assertTypeofChunk(t, 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 also determines how binary `stdin` input is decoded before being passed to a transform. 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 (t, encoding) => { const chunks = []; const subprocess = execa('stdin.js', {stdin: getChunkGenerator(chunks), encoding}); subprocess.stdin.end(foobarUtf16Uint8Array); await subprocess; t.deepEqual(chunks, [Buffer.from(foobarUtf16Uint8Array).toString(encoding)]); }; test('First generator argument is decoded with encoding "utf8"', testStdinEncodingValue, 'utf8'); test('First generator argument is decoded with encoding "utf16le"', testStdinEncodingValue, 'utf16le'); const testStdinEncodingValueSync = (t, encoding) => { const chunks = []; execaSync('stdin.js', {stdin: [[foobarUtf16Uint8Array], getChunkGenerator(chunks)], encoding}); t.deepEqual(chunks, [Buffer.from(foobarUtf16Uint8Array).toString(encoding)]); }; test('First generator argument is decoded with encoding "utf8", sync', testStdinEncodingValueSync, 'utf8'); test('First generator argument is decoded with encoding "utf16le", sync', testStdinEncodingValueSync, 'utf16le'); const testEncodingIgnored = async (t, 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; t.is(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-params const testGeneratorNextEncoding = async (t, input, encoding, firstObjectMode, secondObjectMode, expectedType, execaMethod) => { const lines = []; await execaMethod('noop.js', ['other'], { stdout: [ getOutputGenerator(input)(firstObjectMode), getTypeofGenerator(lines)(secondObjectMode), ], encoding, }); assertTypeofChunk(t, 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 (t, fdNumber, execaMethod) => { const lines = []; await execaMethod('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, getTypeofGenerator(lines)(true))); assertTypeofChunk(t, 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 (t, encoding, execaMethod) => { const {stdout} = await execaMethod('noop.js', { stdout: getOutputGenerator(nonAsciiString)(false, true), encoding, }); t.is(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 unchanged const testYieldBytesBinaryEncoding = async (t, execaMethod) => { const {stdout} = await execaMethod('noop-fd.js', ['1', nonAsciiString], { stdout: noopGenerator(false, true), encoding: 'latin1', }); t.is(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 (t, transform, expectedOutput, execaMethod) => { const {stdout} = await execaMethod('noop.js', { stdout: [getOutputGenerator(nonAsciiUtf16Uint8Array)(false, true), transform], encoding: 'utf16le', }); t.is(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); // A `stdin` transform also receives text decoded with the `encoding` option, so the same applies in that direction const testYieldStringTextEncodingInput = async (t, execaMethod) => { const {stdout} = await execaMethod('stdin.js', { stdin: [[nonAsciiUtf16Uint8Array], noopGenerator()], encoding: 'utf16le', }); t.is(stdout, nonAsciiString); }; test('Transforms yielding a string are encoded with encoding "utf16le", stdin', testYieldStringTextEncodingInput, execa); test('Transforms yielding a string are encoded with encoding "utf16le", stdin, sync', testYieldStringTextEncodingInput, execaSync); // When the `stdin` value is a string, the transform receives it as text, so it must not be encoded as UTF-8 either const testStringInputTextEncoding = async (t, execaMethod) => { const {stdout} = await execaMethod('stdin.js', { stdin: [[nonAsciiString], noopGenerator()], encoding: 'utf16le', }); t.is(stdout, nonAsciiString); }; test('String input is encoded with encoding "utf16le", stdin', testStringInputTextEncoding, execa); test('String input is encoded with encoding "utf16le", stdin, sync', testStringInputTextEncoding, execaSync); // 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. const utf16leYieldGenerator = { * transform(line) { yield Buffer.from(line, 'utf16le'); }, }; const testYieldUtf16LeLines = async (t, execaMethod) => { const {stdout} = await execaMethod('stdin.js', { stdin: [['aaa\nbbb\nccc\n'], utf16leYieldGenerator], encoding: 'utf16le', stripFinalNewline: false, }); t.is(stdout, 'aaa\nbbb\nccc\n'); }; test('Transforms yielding Uint8Array re-add newlines with encoding "utf16le"', testYieldUtf16LeLines, execa); test('Transforms yielding Uint8Array re-add newlines with encoding "utf16le", sync', testYieldUtf16LeLines, execaSync);