Skip to content
Myr

encoding-transform.js

This file

Path

test/stdio/encoding-transform.js

189 lines · 16.7 KiB · JavaScript

Last changed by

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

History of this file

Take it

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 1–8why, one step back
1import {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';

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 9why, one step back
9import {multibyteChar, multibyteString, multibyteUint8Array, breakingLength, brokenSymbol} from '../helpers/encoding.js';

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 10–12why, one step back
10
11setFixtureDir();12

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 13why, one step back
13const getTypeofGenerator = (objectMode, binary) => ({

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 14–17why, one step back
14 * transform(line) {
15 yield Object.prototype.toString.call(line);16 },17 objectMode,

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 18why, one step back
18 binary,

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 19–20why, one step back
19});
20

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 21–25why, one step back
21const assertTypeofChunk = (t, output, encoding, expectedType) => {
22 const typeofChunk = Buffer.from(output, encoding === 'buffer' ? undefined : encoding).toString().trim();23 t.is(typeofChunk, `[object ${expectedType}]`);24};25

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

line 26why, one step back
26// eslint-disable-next-line max-params

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 27–28why, one step back
27const testGeneratorFirstEncoding = async (t, input, encoding, expectedType, objectMode, binary) => {
28 const subprocess = execa('stdin.js', {stdin: getTypeofGenerator(objectMode, binary), encoding});

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 29–30why, one step back
29 subprocess.stdin.end(input);
30 const {stdout} = await subprocess;

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 31why, one step back
31 assertTypeofChunk(t, stdout, encoding, expectedType);

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 32–33why, one step back
32};
33

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 34–50why, one step back
34test('First generator argument is string with default encoding, with string writes', testGeneratorFirstEncoding, foobarString, 'utf8', 'String', false);
35test('First generator argument is string with default encoding, with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'utf8', 'String', false);36test('First generator argument is string with default encoding, with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'utf8', 'String', false);37test('First generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorFirstEncoding, foobarString, 'buffer', 'Uint8Array', false);38test('First generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'buffer', 'Uint8Array', false);39test('First generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'buffer', 'Uint8Array', false);40test('First generator argument is string with encoding "hex", with string writes', testGeneratorFirstEncoding, foobarString, 'hex', 'String', false);41test('First generator argument is string with encoding "hex", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'hex', 'String', false);42test('First generator argument is string with encoding "hex", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'hex', 'String', false);43test('First generator argument can be string with objectMode', testGeneratorFirstEncoding, foobarString, 'utf8', 'String', true);44test('First generator argument can be objects with objectMode', testGeneratorFirstEncoding, foobarObject, 'utf8', 'Object', true);45test('First generator argument is string with default encoding, with string writes, "binary: false"', testGeneratorFirstEncoding, foobarString, 'utf8', 'String', false, false);46test('First generator argument is Uint8Array with default encoding, with string writes, "binary: true"', testGeneratorFirstEncoding, foobarString, 'utf8', 'Uint8Array', false, true);47test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: false"', testGeneratorFirstEncoding, foobarString, 'buffer', 'Uint8Array', false, false);48test('First generator argument is Uint8Array with encoding "buffer", with string writes, "binary: true"', testGeneratorFirstEncoding, foobarString, 'buffer', 'Uint8Array', false, true);49test('First generator argument is string with encoding "hex", with string writes, "binary: false"', testGeneratorFirstEncoding, foobarString, 'hex', 'String', false, false);50test('First generator argument is Uint8Array with encoding "hex", with string writes, "binary: true"', testGeneratorFirstEncoding, foobarString, 'hex', 'Uint8Array', false, true);

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 51–53why, one step back
51
52const testEncodingIgnored = async (t, encoding) => {53 const input = Buffer.from(foobarString).toString(encoding);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 54–56why, one step back
54 const subprocess = execa('stdin.js', {stdin: noopGenerator(true)});
55 subprocess.stdin.end(input, encoding);56 const {stdout} = await subprocess;

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 57–69why, one step back
57 t.is(stdout, input);
58};5960test('Write call encoding "utf8" is ignored with objectMode', testEncodingIgnored, 'utf8');61test('Write call encoding "utf16le" is ignored with objectMode', testEncodingIgnored, 'utf16le');62test('Write call encoding "hex" is ignored with objectMode', testEncodingIgnored, 'hex');63test('Write call encoding "base64" is ignored with objectMode', testEncodingIgnored, 'base64');6465// eslint-disable-next-line max-params66const testGeneratorNextEncoding = async (t, input, encoding, firstObjectMode, secondObjectMode, expectedType) => {67 const {stdout} = await execa('noop.js', ['other'], {68 stdout: [69 getOutputGenerator(input, firstObjectMode),

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 70why, one step back
70 getTypeofGenerator(secondObjectMode),

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 71–73why, one step back
71 ],
72 encoding,73 });

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 74–75why, one step back
74 const output = Array.isArray(stdout) ? stdout[0] : stdout;
75 assertTypeofChunk(t, output, encoding, expectedType);

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 76–81why, one step back
76};
7778test('Next generator argument is string with default encoding, with string writes', testGeneratorNextEncoding, foobarString, 'utf8', false, false, 'String');79test('Next generator argument is string with default encoding, with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'utf8', true, false, 'String');80test('Next generator argument is string with default encoding, with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'utf8', true, true, 'String');81test('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 82–83why, one step back
82test('Next generator argument is Uint8Array with default encoding, with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, false, 'Uint8Array');
83test('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 84why, one step back
84test('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 85–86why, one step back
85test('Next generator argument is string with encoding "buffer", with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'buffer', true, false, 'String');
86test('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 87–106why, one step back
87test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'buffer', false, false, 'Uint8Array');
88test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, false, 'Uint8Array');89test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, true, 'Uint8Array');90test('Next generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'String');91test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'String');92test('Next generator argument is object with default encoding, with object writes, objectMode first', testGeneratorNextEncoding, foobarObject, 'utf8', true, false, 'Object');93test('Next generator argument is object with default encoding, with object writes, objectMode both', testGeneratorNextEncoding, foobarObject, 'utf8', true, true, 'Object');9495const testFirstOutputGeneratorArgument = async (t, fdNumber) => {96 const {stdio} = await execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, getTypeofGenerator(true)));97 t.deepEqual(stdio[fdNumber], ['[object String]']);98};99100test('The first generator with result.stdout does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 1);101test('The first generator with result.stderr does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 2);102test('The first generator with result.stdio[*] does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 3);103104// eslint-disable-next-line max-params105const testGeneratorReturnType = async (t, input, encoding, reject, objectMode, final) => {106 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 107–108why, one step back
107 const {stdout} = await execa(fixtureName, ['1', foobarString], {
108 stdout: convertTransformToFinal(getOutputGenerator(input, objectMode, true), final),

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 109–112why, one step back
109 encoding,
110 reject,111 });112 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

line 113why, one step back
113 const output = Buffer.from(typeofChunk, encoding === 'buffer' || objectMode ? undefined : encoding).toString();

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 114–166why, one step back
114 t.is(output, foobarString);
115};116117test('Generator can return string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, false);118test('Generator can return Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, false);119test('Generator can return string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, false);120test('Generator can return Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, false);121test('Generator can return string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, false);122test('Generator can return Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, false);123test('Generator can return string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, false);124test('Generator can return Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, false);125test('Generator can return string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, false);126test('Generator can return Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, false);127test('Generator can return string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, false);128test('Generator can return Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, false);129test('Generator can return string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, false);130test('Generator can return Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, false);131test('Generator can return string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, false);132test('Generator can return Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, false);133test('Generator can return string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, false);134test('Generator can return Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, false);135test('Generator can return string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, false);136test('Generator can return Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, false);137test('Generator can return string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, false);138test('Generator can return Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, false);139test('Generator can return string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, false);140test('Generator can return Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, false);141test('Generator can return final string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false, true);142test('Generator can return final Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false, true);143test('Generator can return final string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false, true);144test('Generator can return final Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false, true);145test('Generator can return final string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false, true);146test('Generator can return final Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false, true);147test('Generator can return final string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false, true);148test('Generator can return final Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false, true);149test('Generator can return final string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false, true);150test('Generator can return final Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false, true);151test('Generator can return final string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false, true);152test('Generator can return final Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false, true);153test('Generator can return final string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true, true);154test('Generator can return final Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true, true);155test('Generator can return final string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true, true);156test('Generator can return final Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true, true);157test('Generator can return final string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true, true);158test('Generator can return final Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true, true);159test('Generator can return final string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true, true);160test('Generator can return final Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true, true);161test('Generator can return final string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true, true);162test('Generator can return final Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true, true);163test('Generator can return final string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true, true);164test('Generator can return final Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true, true);165166const testMultibyte = async (t, objectMode) => {

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 167why, one step back
167 const subprocess = execa('stdin.js', {stdin: noopGenerator(objectMode, true)});

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 168why, one step back
168 subprocess.stdin.write(multibyteUint8Array.slice(0, breakingLength));

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

line 169why, one step back
169 await scheduler.yield();

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 170–171why, one step back
170 subprocess.stdin.end(multibyteUint8Array.slice(breakingLength));
171 const {stdout} = await subprocess;

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 172–178why, one step back
172 t.is(stdout, multibyteString);
173};174175test('Generator handles multibyte characters with Uint8Array', testMultibyte, false);176test('Generator handles multibyte characters with Uint8Array, objectMode', testMultibyte, true);177178const testMultibytePartial = async (t, objectMode) => {

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 179–181why, one step back
179 const {stdout} = await execa('stdin.js', {
180 stdin: [181 [multibyteUint8Array.slice(0, breakingLength)],

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 182why, one step back
182 noopGenerator(objectMode, true),

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 183–184why, one step back
183 ],
184 });

Rename 2 files with wrong file names (#865)ehmickybefore adoption4bd0fe3

lines 185–189why, one step back
185 t.is(stdout, `${multibyteChar}${brokenSymbol}`);
186};187188test('Generator handles partial multibyte characters with Uint8Array', testMultibytePartial, false);189test('Generator handles partial multibyte characters with Uint8Array, objectMode', testMultibytePartial, true);