encoding-transform.js
This file
Path
test/stdio/encoding-transform.js
187 lines · 15.8 KiB · JavaScript
Last changed by
Strip newlines in transforms (#919)ehmickybefore adoption26e9150
History of this fileTake it
Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 1–13why, one step back1import {Buffer} from 'node:buffer';2import {scheduler} from 'node:timers/promises';3import test from 'ava';4import {execa} from '../../index.js';5import {setFixtureDir} from '../helpers/fixtures-dir.js';6import {getStdio} from '../helpers/stdio.js';7import {foobarString, foobarUint8Array, foobarBuffer, foobarObject} from '../helpers/input.js';8import {noopGenerator, getOutputGenerator, convertTransformToFinal} from '../helpers/generator.js';910setFixtureDir();1112const textEncoder = new TextEncoder();13Strip newlines in transforms (#919)ehmickybefore adoption26e9150
line 14why, one step back14const getTypeofGenerator = (objectMode, binary) => ({Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 15–18why, one step back15 * transform(line) {16 yield Object.prototype.toString.call(line);17 },18 objectMode,Strip newlines in transforms (#919)ehmickybefore adoption26e9150
line 19why, one step back19 binary,Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 20–23why, one step back20});2122// eslint-disable-next-line max-params23const testGeneratorFirstEncoding = async (t, input, encoding, output, objectMode) => {Strip newlines in transforms (#919)ehmickybefore adoption26e9150
line 24why, one step back24 const subprocess = execa('stdin.js', {stdin: getTypeofGenerator(objectMode, true), encoding});Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 25–26why, one step back25 subprocess.stdin.end(input);26 const {stdout} = await subprocess;Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 27–44why, one step back27 const result = Buffer.from(stdout, encoding).toString();28 t.is(result, output);29};3031test('First generator argument is string with default encoding, with string writes', testGeneratorFirstEncoding, foobarString, 'utf8', '[object String]', false);32test('First generator argument is string with default encoding, with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'utf8', '[object String]', false);33test('First generator argument is string with default encoding, with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'utf8', '[object String]', false);34test('First generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorFirstEncoding, foobarString, 'buffer', '[object Uint8Array]', false);35test('First generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'buffer', '[object Uint8Array]', false);36test('First generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'buffer', '[object Uint8Array]', false);37test('First generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorFirstEncoding, foobarString, 'hex', '[object String]', false);38test('First generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'hex', '[object String]', false);39test('First generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'hex', '[object String]', false);40test('First generator argument can be string with objectMode', testGeneratorFirstEncoding, foobarString, 'utf8', '[object String]', true);41test('First generator argument can be objects with objectMode', testGeneratorFirstEncoding, foobarObject, 'utf8', '[object Object]', true);4243const testEncodingIgnored = async (t, encoding) => {44 const input = Buffer.from(foobarString).toString(encoding);Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 45–47why, one step back45 const subprocess = execa('stdin.js', {stdin: noopGenerator(true)});46 subprocess.stdin.end(input, encoding);47 const {stdout} = await subprocess;Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 48–60why, one step back48 t.is(stdout, input);49};5051test('Write call encoding "utf8" is ignored with objectMode', testEncodingIgnored, 'utf8');52test('Write call encoding "utf16le" is ignored with objectMode', testEncodingIgnored, 'utf16le');53test('Write call encoding "hex" is ignored with objectMode', testEncodingIgnored, 'hex');54test('Write call encoding "base64" is ignored with objectMode', testEncodingIgnored, 'base64');5556// eslint-disable-next-line max-params57const testGeneratorNextEncoding = async (t, input, encoding, firstObjectMode, secondObjectMode, expectedType) => {58 const {stdout} = await execa('noop.js', ['other'], {59 stdout: [60 getOutputGenerator(input, firstObjectMode),Strip newlines in transforms (#919)ehmickybefore adoption26e9150
line 61why, one step back61 getTypeofGenerator(secondObjectMode, true),Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 62–73why, one step back62 ],63 encoding,64 });65 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;66 const output = Buffer.from(typeofChunk, encoding === 'buffer' ? undefined : encoding).toString();67 t.is(output, `[object ${expectedType}]`);68};6970test('Next generator argument is string with default encoding, with string writes', testGeneratorNextEncoding, foobarString, 'utf8', false, false, 'String');71test('Next generator argument is string with default encoding, with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'utf8', true, false, 'String');72test('Next generator argument is string with default encoding, with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'utf8', true, true, 'String');73test('Next generator argument is string with default encoding, with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'utf8', false, false, 'String');Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 74–75why, one step back74test('Next generator argument is Uint8Array with default encoding, with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, false, 'Uint8Array');75test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, true, 'Uint8Array');Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 76why, one step back76test('Next generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorNextEncoding, foobarString, 'buffer', false, false, 'Uint8Array');Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 77–78why, one step back77test('Next generator argument is string with encoding "buffer", with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'buffer', true, false, 'String');78test('Next generator argument is string with encoding "buffer", with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'buffer', true, true, 'String');Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 79–98why, one step back79test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'buffer', false, false, 'Uint8Array');80test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, false, 'Uint8Array');81test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, true, 'Uint8Array');82test('Next generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'String');83test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'String');84test('Next generator argument is object with default encoding, with object writes, objectMode first', testGeneratorNextEncoding, foobarObject, 'utf8', true, false, 'Object');85test('Next generator argument is object with default encoding, with object writes, objectMode both', testGeneratorNextEncoding, foobarObject, 'utf8', true, true, 'Object');8687const testFirstOutputGeneratorArgument = async (t, fdNumber) => {88 const {stdio} = await execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, getTypeofGenerator(true)));89 t.deepEqual(stdio[fdNumber], ['[object String]']);90};9192test('The first generator with result.stdout does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 1);93test('The first generator with result.stderr does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 2);94test('The first generator with result.stdio[*] does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 3);9596// eslint-disable-next-line max-params97const testGeneratorReturnType = async (t, input, encoding, reject, objectMode, final) => {98 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';Strip newlines in transforms (#919)ehmickybefore adoption26e9150
lines 99–100why, one step back99 const {stdout} = await execa(fixtureName, ['1', foobarString], {100 stdout: convertTransformToFinal(getOutputGenerator(input, objectMode, true), final),Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 101–104why, one step back101 encoding,102 reject,103 });104 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
line 105why, one step back105 const output = Buffer.from(typeofChunk, encoding === 'buffer' || objectMode ? undefined : encoding).toString();Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 106–164why, one step back106 t.is(output, foobarString);107};108109test('Generator can return string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, false);110test('Generator can return Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, false);111test('Generator can return string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, false);112test('Generator can return Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, false);113test('Generator can return string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, false);114test('Generator can return Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, false);115test('Generator can return string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, false);116test('Generator can return Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, false);117test('Generator can return string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, false);118test('Generator can return Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, false);119test('Generator can return string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, false);120test('Generator can return Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, false);121test('Generator can return string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, false);122test('Generator can return Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, false);123test('Generator can return string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, false);124test('Generator can return Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, false);125test('Generator can return string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, false);126test('Generator can return Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, false);127test('Generator can return string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, false);128test('Generator can return Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, false);129test('Generator can return string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, false);130test('Generator can return Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, false);131test('Generator can return string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, false);132test('Generator can return Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, false);133test('Generator can return final string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, true);134test('Generator can return final Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, true);135test('Generator can return final string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, true);136test('Generator can return final Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, true);137test('Generator can return final string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, true);138test('Generator can return final Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, true);139test('Generator can return final string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, true);140test('Generator can return final Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, true);141test('Generator can return final string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, true);142test('Generator can return final Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, true);143test('Generator can return final string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, true);144test('Generator can return final Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, true);145test('Generator can return final string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, true);146test('Generator can return final Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, true);147test('Generator can return final string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, true);148test('Generator can return final Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, true);149test('Generator can return final string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, true);150test('Generator can return final Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, true);151test('Generator can return final string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, true);152test('Generator can return final Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, true);153test('Generator can return final string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, true);154test('Generator can return final Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, true);155test('Generator can return final string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, true);156test('Generator can return final Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, true);157158const multibyteChar = '\u{1F984}';159const multibyteString = `${multibyteChar}${multibyteChar}`;160const multibyteUint8Array = textEncoder.encode(multibyteString);161const breakingLength = multibyteUint8Array.length * 0.75;162const brokenSymbol = '\uFFFD';163164const testMultibyte = async (t, objectMode) => {Strip newlines in transforms (#919)ehmickybefore adoption26e9150
line 165why, one step back165 const subprocess = execa('stdin.js', {stdin: noopGenerator(objectMode, true)});Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 166why, one step back166 subprocess.stdin.write(multibyteUint8Array.slice(0, breakingLength));Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 167why, one step back167 await scheduler.yield();Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 168–169why, one step back168 subprocess.stdin.end(multibyteUint8Array.slice(breakingLength));169 const {stdout} = await subprocess;Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 170–176why, one step back170 t.is(stdout, multibyteString);171};172173test('Generator handles multibyte characters with Uint8Array', testMultibyte, false);174test('Generator handles multibyte characters with Uint8Array, objectMode', testMultibyte, true);175176const testMultibytePartial = async (t, objectMode) => {Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 177–179why, one step back177 const {stdout} = await execa('stdin.js', {178 stdin: [179 [multibyteUint8Array.slice(0, breakingLength)],Strip newlines in transforms (#919)ehmickybefore adoption26e9150
line 180why, one step back180 noopGenerator(objectMode, true),Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 181–182why, one step back181 ],182 });Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 183–187why, one step back183 t.is(stdout, `${multibyteChar}${brokenSymbol}`);184};185186test('Generator handles partial multibyte characters with Uint8Array', testMultibytePartial, false);187test('Generator handles partial multibyte characters with Uint8Array, objectMode', testMultibytePartial, true);