encoding-transform.js
This file
Path
test/stdio/encoding-transform.js
186 lines · 15.8 KiB · JavaScript
Last changed by
Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
History of this fileTake it
Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 1–22why, 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();1314const getTypeofGenerator = objectMode => ({15 * transform(line) {16 yield Object.prototype.toString.call(line);17 },18 objectMode,19});2021// eslint-disable-next-line max-params22const testGeneratorFirstEncoding = async (t, input, encoding, output, objectMode) => {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 23–25why, one step back23 const subprocess = execa('stdin.js', {stdin: getTypeofGenerator(objectMode), encoding});24 subprocess.stdin.end(input);25 const {stdout} = await subprocess;Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 26–43why, one step back26 const result = Buffer.from(stdout, encoding).toString();27 t.is(result, output);28};2930test('First generator argument is string with default encoding, with string writes', testGeneratorFirstEncoding, foobarString, 'utf8', '[object String]', false);31test('First generator argument is string with default encoding, with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'utf8', '[object String]', false);32test('First generator argument is string with default encoding, with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'utf8', '[object String]', false);33test('First generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorFirstEncoding, foobarString, 'buffer', '[object Uint8Array]', false);34test('First generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'buffer', '[object Uint8Array]', false);35test('First generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'buffer', '[object Uint8Array]', false);36test('First generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorFirstEncoding, foobarString, 'hex', '[object String]', false);37test('First generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'hex', '[object String]', false);38test('First generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'hex', '[object String]', false);39test('First generator argument can be string with objectMode', testGeneratorFirstEncoding, foobarString, 'utf8', '[object String]', true);40test('First generator argument can be objects with objectMode', testGeneratorFirstEncoding, foobarObject, 'utf8', '[object Object]', true);4142const testEncodingIgnored = async (t, encoding) => {43 const input = Buffer.from(foobarString).toString(encoding);Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 44–46why, one step back44 const subprocess = execa('stdin.js', {stdin: noopGenerator(true)});45 subprocess.stdin.end(input, encoding);46 const {stdout} = await subprocess;Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 47–72why, one step back47 t.is(stdout, input);48};4950test('Write call encoding "utf8" is ignored with objectMode', testEncodingIgnored, 'utf8');51test('Write call encoding "utf16le" is ignored with objectMode', testEncodingIgnored, 'utf16le');52test('Write call encoding "hex" is ignored with objectMode', testEncodingIgnored, 'hex');53test('Write call encoding "base64" is ignored with objectMode', testEncodingIgnored, 'base64');5455// eslint-disable-next-line max-params56const testGeneratorNextEncoding = async (t, input, encoding, firstObjectMode, secondObjectMode, expectedType) => {57 const {stdout} = await execa('noop.js', ['other'], {58 stdout: [59 getOutputGenerator(input, firstObjectMode),60 getTypeofGenerator(secondObjectMode),61 ],62 encoding,63 });64 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;65 const output = Buffer.from(typeofChunk, encoding === 'buffer' ? undefined : encoding).toString();66 t.is(output, `[object ${expectedType}]`);67};6869test('Next generator argument is string with default encoding, with string writes', testGeneratorNextEncoding, foobarString, 'utf8', false, false, 'String');70test('Next generator argument is string with default encoding, with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'utf8', true, false, 'String');71test('Next generator argument is string with default encoding, with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'utf8', true, true, 'String');72test('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 73–74why, one step back73test('Next generator argument is Uint8Array with default encoding, with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, false, 'Uint8Array');74test('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 75why, one step back75test('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 76–77why, one step back76test('Next generator argument is string with encoding "buffer", with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'buffer', true, false, 'String');77test('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 78–103why, one step back78test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'buffer', false, false, 'Uint8Array');79test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, false, 'Uint8Array');80test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, true, 'Uint8Array');81test('Next generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'String');82test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'String');83test('Next generator argument is object with default encoding, with object writes, objectMode first', testGeneratorNextEncoding, foobarObject, 'utf8', true, false, 'Object');84test('Next generator argument is object with default encoding, with object writes, objectMode both', testGeneratorNextEncoding, foobarObject, 'utf8', true, true, 'Object');8586const testFirstOutputGeneratorArgument = async (t, fdNumber) => {87 const {stdio} = await execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, getTypeofGenerator(true)));88 t.deepEqual(stdio[fdNumber], ['[object String]']);89};9091test('The first generator with result.stdout does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 1);92test('The first generator with result.stderr does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 2);93test('The first generator with result.stdio[*] does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 3);9495// eslint-disable-next-line max-params96const testGeneratorReturnType = async (t, input, encoding, reject, objectMode, final) => {97 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';98 const {stdout} = await execa(fixtureName, ['1', 'other'], {99 stdout: convertTransformToFinal(getOutputGenerator(input, objectMode), final),100 encoding,101 reject,102 });103 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
line 104why, one step back104 const output = Buffer.from(typeofChunk, encoding === 'buffer' || objectMode ? undefined : encoding).toString();Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 105–163why, one step back105 t.is(output, foobarString);106};107108test('Generator can return string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, false);109test('Generator can return Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, false);110test('Generator can return string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, false);111test('Generator can return Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, false);112test('Generator can return string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, false);113test('Generator can return Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, false);114test('Generator can return string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, false);115test('Generator can return Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, false);116test('Generator can return string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, false);117test('Generator can return Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, false);118test('Generator can return string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, false);119test('Generator can return Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, false);120test('Generator can return string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, false);121test('Generator can return Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, false);122test('Generator can return string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, false);123test('Generator can return Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, false);124test('Generator can return string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, false);125test('Generator can return Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, false);126test('Generator can return string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, false);127test('Generator can return Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, false);128test('Generator can return string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, false);129test('Generator can return Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, false);130test('Generator can return string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, false);131test('Generator can return Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, false);132test('Generator can return final string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, true);133test('Generator can return final Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, true);134test('Generator can return final string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, true);135test('Generator can return final Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, true);136test('Generator can return final string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, true);137test('Generator can return final Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, true);138test('Generator can return final string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, true);139test('Generator can return final Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, true);140test('Generator can return final string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, true);141test('Generator can return final Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, true);142test('Generator can return final string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, true);143test('Generator can return final Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, true);144test('Generator can return final string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, true);145test('Generator can return final Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, true);146test('Generator can return final string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, true);147test('Generator can return final Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, true);148test('Generator can return final string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, true);149test('Generator can return final Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, true);150test('Generator can return final string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, true);151test('Generator can return final Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, true);152test('Generator can return final string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, true);153test('Generator can return final Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, true);154test('Generator can return final string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, true);155test('Generator can return final Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, true);156157const multibyteChar = '\u{1F984}';158const multibyteString = `${multibyteChar}${multibyteChar}`;159const multibyteUint8Array = textEncoder.encode(multibyteString);160const breakingLength = multibyteUint8Array.length * 0.75;161const brokenSymbol = '\uFFFD';162163const testMultibyte = async (t, objectMode) => {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 164–165why, one step back164 const subprocess = execa('stdin.js', {stdin: noopGenerator(objectMode)});165 subprocess.stdin.write(multibyteUint8Array.slice(0, breakingLength));Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
line 166why, one step back166 await scheduler.yield();Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 167–168why, one step back167 subprocess.stdin.end(multibyteUint8Array.slice(breakingLength));168 const {stdout} = await subprocess;Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 169–175why, one step back169 t.is(stdout, multibyteString);170};171172test('Generator handles multibyte characters with Uint8Array', testMultibyte, false);173test('Generator handles multibyte characters with Uint8Array, objectMode', testMultibyte, true);174175const testMultibytePartial = async (t, objectMode) => {Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c
lines 176–181why, one step back176 const {stdout} = await execa('stdin.js', {177 stdin: [178 [multibyteUint8Array.slice(0, breakingLength)],179 noopGenerator(objectMode),180 ],181 });Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3
lines 182–186why, one step back182 t.is(stdout, `${multibyteChar}${brokenSymbol}`);183};184185test('Generator handles partial multibyte characters with Uint8Array', testMultibytePartial, false);186test('Generator handles partial multibyte characters with Uint8Array, objectMode', testMultibytePartial, true);