Skip to content
Myr

encoding-final.js

This file

Path

test/transform/encoding-final.js

129 lines · 8.2 KiB · JavaScript

Last changed by

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–6why, one step back
1import {Buffer} from 'node:buffer';
2import {exec} from 'node:child_process';3import {promisify} from 'node:util';4import test from 'ava';5import getStream from 'get-stream';6import {execa, execaSync} from '../../index.js';

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 7why, one step back
7import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';

Rename files (#977)ehmickybefore adoption4b2e316

lines 8–12why, one step back
8import {fullStdio} from '../helpers/stdio.js';
9import {foobarString, foobarUint8Array, foobarHex} from '../helpers/input.js';1011const pExec = promisify(exec);12

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 13why, one step back
13setFixtureDirectory();

Rename files (#977)ehmickybefore adoption4b2e316

lines 14–29why, one step back
14
15const checkEncoding = async (t, encoding, fdNumber, execaMethod) => {16 const {stdio} = await execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding});17 compareValues(t, stdio[fdNumber], encoding);1819 if (execaMethod !== execaSync) {20 const subprocess = execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding});21 const result = await getStream(subprocess.stdio[fdNumber]);22 compareValues(t, result, 'utf8');23 await subprocess;24 }2526 if (fdNumber === 3) {27 return;28 }29

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 30why, one step back
30 const {stdout, stderr} = await pExec(`node noop-fd.js ${fdNumber} ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIRECTORY});

Rename files (#977)ehmickybefore adoption4b2e316

lines 31–43why, one step back
31 compareValues(t, fdNumber === 1 ? stdout : stderr, encoding);
32};3334const compareValues = (t, value, encoding) => {35 if (encoding === 'buffer') {36 t.true(ArrayBuffer.isView(value));37 t.true(BUFFER_TO_ENCODE.equals(value));38 } else {39 t.is(value, BUFFER_TO_ENCODE.toString(encoding));40 }41};4243// This string gives different outputs with each encoding type

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

line 44why, one step back
44const STRING_TO_ENCODE = '\u{1000}.';

Rename files (#977)ehmickybefore adoption4b2e316

lines 45–129why, one step back
45const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);
4647test('can pass encoding "buffer" to stdout', checkEncoding, 'buffer', 1, execa);48test('can pass encoding "utf8" to stdout', checkEncoding, 'utf8', 1, execa);49test('can pass encoding "utf16le" to stdout', checkEncoding, 'utf16le', 1, execa);50test('can pass encoding "latin1" to stdout', checkEncoding, 'latin1', 1, execa);51test('can pass encoding "ascii" to stdout', checkEncoding, 'ascii', 1, execa);52test('can pass encoding "hex" to stdout', checkEncoding, 'hex', 1, execa);53test('can pass encoding "base64" to stdout', checkEncoding, 'base64', 1, execa);54test('can pass encoding "base64url" to stdout', checkEncoding, 'base64url', 1, execa);55test('can pass encoding "buffer" to stderr', checkEncoding, 'buffer', 2, execa);56test('can pass encoding "utf8" to stderr', checkEncoding, 'utf8', 2, execa);57test('can pass encoding "utf16le" to stderr', checkEncoding, 'utf16le', 2, execa);58test('can pass encoding "latin1" to stderr', checkEncoding, 'latin1', 2, execa);59test('can pass encoding "ascii" to stderr', checkEncoding, 'ascii', 2, execa);60test('can pass encoding "hex" to stderr', checkEncoding, 'hex', 2, execa);61test('can pass encoding "base64" to stderr', checkEncoding, 'base64', 2, execa);62test('can pass encoding "base64url" to stderr', checkEncoding, 'base64url', 2, execa);63test('can pass encoding "buffer" to stdio[*]', checkEncoding, 'buffer', 3, execa);64test('can pass encoding "utf8" to stdio[*]', checkEncoding, 'utf8', 3, execa);65test('can pass encoding "utf16le" to stdio[*]', checkEncoding, 'utf16le', 3, execa);66test('can pass encoding "latin1" to stdio[*]', checkEncoding, 'latin1', 3, execa);67test('can pass encoding "ascii" to stdio[*]', checkEncoding, 'ascii', 3, execa);68test('can pass encoding "hex" to stdio[*]', checkEncoding, 'hex', 3, execa);69test('can pass encoding "base64" to stdio[*]', checkEncoding, 'base64', 3, execa);70test('can pass encoding "base64url" to stdio[*]', checkEncoding, 'base64url', 3, execa);71test('can pass encoding "buffer" to stdout - sync', checkEncoding, 'buffer', 1, execaSync);72test('can pass encoding "utf8" to stdout - sync', checkEncoding, 'utf8', 1, execaSync);73test('can pass encoding "utf16le" to stdout - sync', checkEncoding, 'utf16le', 1, execaSync);74test('can pass encoding "latin1" to stdout - sync', checkEncoding, 'latin1', 1, execaSync);75test('can pass encoding "ascii" to stdout - sync', checkEncoding, 'ascii', 1, execaSync);76test('can pass encoding "hex" to stdout - sync', checkEncoding, 'hex', 1, execaSync);77test('can pass encoding "base64" to stdout - sync', checkEncoding, 'base64', 1, execaSync);78test('can pass encoding "base64url" to stdout - sync', checkEncoding, 'base64url', 1, execaSync);79test('can pass encoding "buffer" to stderr - sync', checkEncoding, 'buffer', 2, execaSync);80test('can pass encoding "utf8" to stderr - sync', checkEncoding, 'utf8', 2, execaSync);81test('can pass encoding "utf16le" to stderr - sync', checkEncoding, 'utf16le', 2, execaSync);82test('can pass encoding "latin1" to stderr - sync', checkEncoding, 'latin1', 2, execaSync);83test('can pass encoding "ascii" to stderr - sync', checkEncoding, 'ascii', 2, execaSync);84test('can pass encoding "hex" to stderr - sync', checkEncoding, 'hex', 2, execaSync);85test('can pass encoding "base64" to stderr - sync', checkEncoding, 'base64', 2, execaSync);86test('can pass encoding "base64url" to stderr - sync', checkEncoding, 'base64url', 2, execaSync);87test('can pass encoding "buffer" to stdio[*] - sync', checkEncoding, 'buffer', 3, execaSync);88test('can pass encoding "utf8" to stdio[*] - sync', checkEncoding, 'utf8', 3, execaSync);89test('can pass encoding "utf16le" to stdio[*] - sync', checkEncoding, 'utf16le', 3, execaSync);90test('can pass encoding "latin1" to stdio[*] - sync', checkEncoding, 'latin1', 3, execaSync);91test('can pass encoding "ascii" to stdio[*] - sync', checkEncoding, 'ascii', 3, execaSync);92test('can pass encoding "hex" to stdio[*] - sync', checkEncoding, 'hex', 3, execaSync);93test('can pass encoding "base64" to stdio[*] - sync', checkEncoding, 'base64', 3, execaSync);94test('can pass encoding "base64url" to stdio[*] - sync', checkEncoding, 'base64url', 3, execaSync);9596// eslint-disable-next-line max-params97const testEncodingInput = async (t, input, expectedStdout, encoding, execaMethod) => {98 const {stdout} = await execaMethod('stdin.js', {input, encoding});99 t.deepEqual(stdout, expectedStdout);100};101102test('Can use string input', testEncodingInput, foobarString, foobarString, 'utf8', execa);103test('Can use Uint8Array input', testEncodingInput, foobarUint8Array, foobarString, 'utf8', execa);104test('Can use string input, encoding "buffer"', testEncodingInput, foobarString, foobarUint8Array, 'buffer', execa);105test('Can use Uint8Array input, encoding "buffer"', testEncodingInput, foobarUint8Array, foobarUint8Array, 'buffer', execa);106test('Can use string input, encoding "hex"', testEncodingInput, foobarString, foobarHex, 'hex', execa);107test('Can use Uint8Array input, encoding "hex"', testEncodingInput, foobarUint8Array, foobarHex, 'hex', execa);108test('Can use string input, sync', testEncodingInput, foobarString, foobarString, 'utf8', execaSync);109test('Can use Uint8Array input, sync', testEncodingInput, foobarUint8Array, foobarString, 'utf8', execaSync);110test('Can use string input, encoding "buffer", sync', testEncodingInput, foobarString, foobarUint8Array, 'buffer', execaSync);111test('Can use Uint8Array input, encoding "buffer", sync', testEncodingInput, foobarUint8Array, foobarUint8Array, 'buffer', execaSync);112test('Can use string input, encoding "hex", sync', testEncodingInput, foobarString, foobarHex, 'hex', execaSync);113test('Can use Uint8Array input, encoding "hex", sync', testEncodingInput, foobarUint8Array, foobarHex, 'hex', execaSync);114115const testSubprocessEncoding = (t, encoding) => {116 const subprocess = execa('empty.js', {...fullStdio, encoding});117 t.is(subprocess.stdout.readableEncoding, null);118 t.is(subprocess.stderr.readableEncoding, null);119 t.is(subprocess.stdio[3].readableEncoding, null);120};121122test('Does not modify subprocess.std* encoding, "utf8"', testSubprocessEncoding, 'utf8');123test('Does not modify subprocess.std* encoding, "utf16le"', testSubprocessEncoding, 'utf16le');124test('Does not modify subprocess.std* encoding, "buffer"', testSubprocessEncoding, 'buffer');125test('Does not modify subprocess.std* encoding, "hex"', testSubprocessEncoding, 'hex');126test('Does not modify subprocess.std* encoding, "base64"', testSubprocessEncoding, 'base64');127test('Does not modify subprocess.std* encoding, "base64url"', testSubprocessEncoding, 'base64url');128test('Does not modify subprocess.std* encoding, "latin1"', testSubprocessEncoding, 'latin1');129test('Does not modify subprocess.std* encoding, "ascii"', testSubprocessEncoding, 'ascii');