encoding-final.js
This file
Path
test/transform/encoding-final.js
129 lines · 8.6 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 2–4why, one step back2import {Buffer} from 'node:buffer';3import {exec} from 'node:child_process';4import {promisify} from 'node:util';TweaksSindre Sorhusbefore adoption698a1b8
line 5why, one step back5import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 6–15why, one step back6import getStream from 'get-stream';7import {execa, execaSync} from '../../index.js';8import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';9import {fullStdio} from '../helpers/stdio.js';10import {foobarString, foobarUint8Array, foobarHex} from '../helpers/input.js';1112const pExec = promisify(exec);1314setFixtureDirectory();15TweaksSindre Sorhusbefore adoption698a1b8
line 16why, one step back16const checkEncoding = async (encoding, fdNumber, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 17why, one step back17 const {stdio} = await execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding});TweaksSindre Sorhusbefore adoption698a1b8
line 18why, one step back18 compareValues(stdio[fdNumber], encoding);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 19–22why, one step back1920 if (execaMethod !== execaSync) {21 const subprocess = execaMethod('noop-fd.js', [`${fdNumber}`, STRING_TO_ENCODE], {...fullStdio, encoding});22 const result = await getStream(subprocess.stdio[fdNumber]);TweaksSindre Sorhusbefore adoption698a1b8
line 23why, one step back23 compareValues(result, 'utf8');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 24–31why, one step back24 await subprocess;25 }2627 if (fdNumber === 3) {28 return;29 }3031 const {stdout, stderr} = await pExec(`node noop-fd.js ${fdNumber} ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIRECTORY});TweaksSindre Sorhusbefore adoption698a1b8
line 32why, one step back32 compareValues(fdNumber === 1 ? stdout : stderr, encoding);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 33–34why, one step back33};34TweaksSindre Sorhusbefore adoption698a1b8
line 35why, one step back35const compareValues = (value, encoding) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 36why, one step back36 if (encoding === 'buffer') {TweaksSindre Sorhusbefore adoption698a1b8
lines 37–38why, one step back37 assert.ok(ArrayBuffer.isView(value));38 assert.ok(BUFFER_TO_ENCODE.equals(value));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 39why, one step back39 } else {TweaksSindre Sorhusbefore adoption698a1b8
line 40why, one step back40 assert.equal(value, BUFFER_TO_ENCODE.toString(encoding));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 41–44why, one step back41 }42};4344// This string gives different outputs with each encoding typeRequire Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 45why, one step back45const STRING_TO_ENCODE = '\u{1000}.';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 46–47why, one step back46const BUFFER_TO_ENCODE = Buffer.from(STRING_TO_ENCODE);47TweaksSindre Sorhusbefore adoption698a1b8
lines 48–95why, one step back48test('can pass encoding "buffer" to stdout', () => checkEncoding('buffer', 1, execa));49test('can pass encoding "utf8" to stdout', () => checkEncoding('utf8', 1, execa));50test('can pass encoding "utf16le" to stdout', () => checkEncoding('utf16le', 1, execa));51test('can pass encoding "latin1" to stdout', () => checkEncoding('latin1', 1, execa));52test('can pass encoding "ascii" to stdout', () => checkEncoding('ascii', 1, execa));53test('can pass encoding "hex" to stdout', () => checkEncoding('hex', 1, execa));54test('can pass encoding "base64" to stdout', () => checkEncoding('base64', 1, execa));55test('can pass encoding "base64url" to stdout', () => checkEncoding('base64url', 1, execa));56test('can pass encoding "buffer" to stderr', () => checkEncoding('buffer', 2, execa));57test('can pass encoding "utf8" to stderr', () => checkEncoding('utf8', 2, execa));58test('can pass encoding "utf16le" to stderr', () => checkEncoding('utf16le', 2, execa));59test('can pass encoding "latin1" to stderr', () => checkEncoding('latin1', 2, execa));60test('can pass encoding "ascii" to stderr', () => checkEncoding('ascii', 2, execa));61test('can pass encoding "hex" to stderr', () => checkEncoding('hex', 2, execa));62test('can pass encoding "base64" to stderr', () => checkEncoding('base64', 2, execa));63test('can pass encoding "base64url" to stderr', () => checkEncoding('base64url', 2, execa));64test('can pass encoding "buffer" to stdio[*]', () => checkEncoding('buffer', 3, execa));65test('can pass encoding "utf8" to stdio[*]', () => checkEncoding('utf8', 3, execa));66test('can pass encoding "utf16le" to stdio[*]', () => checkEncoding('utf16le', 3, execa));67test('can pass encoding "latin1" to stdio[*]', () => checkEncoding('latin1', 3, execa));68test('can pass encoding "ascii" to stdio[*]', () => checkEncoding('ascii', 3, execa));69test('can pass encoding "hex" to stdio[*]', () => checkEncoding('hex', 3, execa));70test('can pass encoding "base64" to stdio[*]', () => checkEncoding('base64', 3, execa));71test('can pass encoding "base64url" to stdio[*]', () => checkEncoding('base64url', 3, execa));72test('can pass encoding "buffer" to stdout - sync', () => checkEncoding('buffer', 1, execaSync));73test('can pass encoding "utf8" to stdout - sync', () => checkEncoding('utf8', 1, execaSync));74test('can pass encoding "utf16le" to stdout - sync', () => checkEncoding('utf16le', 1, execaSync));75test('can pass encoding "latin1" to stdout - sync', () => checkEncoding('latin1', 1, execaSync));76test('can pass encoding "ascii" to stdout - sync', () => checkEncoding('ascii', 1, execaSync));77test('can pass encoding "hex" to stdout - sync', () => checkEncoding('hex', 1, execaSync));78test('can pass encoding "base64" to stdout - sync', () => checkEncoding('base64', 1, execaSync));79test('can pass encoding "base64url" to stdout - sync', () => checkEncoding('base64url', 1, execaSync));80test('can pass encoding "buffer" to stderr - sync', () => checkEncoding('buffer', 2, execaSync));81test('can pass encoding "utf8" to stderr - sync', () => checkEncoding('utf8', 2, execaSync));82test('can pass encoding "utf16le" to stderr - sync', () => checkEncoding('utf16le', 2, execaSync));83test('can pass encoding "latin1" to stderr - sync', () => checkEncoding('latin1', 2, execaSync));84test('can pass encoding "ascii" to stderr - sync', () => checkEncoding('ascii', 2, execaSync));85test('can pass encoding "hex" to stderr - sync', () => checkEncoding('hex', 2, execaSync));86test('can pass encoding "base64" to stderr - sync', () => checkEncoding('base64', 2, execaSync));87test('can pass encoding "base64url" to stderr - sync', () => checkEncoding('base64url', 2, execaSync));88test('can pass encoding "buffer" to stdio[*] - sync', () => checkEncoding('buffer', 3, execaSync));89test('can pass encoding "utf8" to stdio[*] - sync', () => checkEncoding('utf8', 3, execaSync));90test('can pass encoding "utf16le" to stdio[*] - sync', () => checkEncoding('utf16le', 3, execaSync));91test('can pass encoding "latin1" to stdio[*] - sync', () => checkEncoding('latin1', 3, execaSync));92test('can pass encoding "ascii" to stdio[*] - sync', () => checkEncoding('ascii', 3, execaSync));93test('can pass encoding "hex" to stdio[*] - sync', () => checkEncoding('hex', 3, execaSync));94test('can pass encoding "base64" to stdio[*] - sync', () => checkEncoding('base64', 3, execaSync));95test('can pass encoding "base64url" to stdio[*] - sync', () => checkEncoding('base64url', 3, execaSync));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 96why, one step back96TweaksSindre Sorhusbefore adoption698a1b8
line 97why, one step back97const testEncodingInput = async (input, expectedStdout, encoding, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 98why, one step back98 const {stdout} = await execaMethod('stdin.js', {input, encoding});TweaksSindre Sorhusbefore adoption698a1b8
line 99why, one step back99 assert.deepEqual(stdout, expectedStdout);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 100–101why, one step back100};101TweaksSindre Sorhusbefore adoption698a1b8
lines 102–113why, one step back102test('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));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 114why, one step back114TweaksSindre Sorhusbefore adoption698a1b8
line 115why, one step back115const testSubprocessEncoding = encoding => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 116why, one step back116 const subprocess = execa('empty.js', {...fullStdio, encoding});TweaksSindre Sorhusbefore adoption698a1b8
lines 117–119why, one step back117 assert.equal(subprocess.stdout.readableEncoding, null);118 assert.equal(subprocess.stderr.readableEncoding, null);119 assert.equal(subprocess.stdio[3].readableEncoding, null);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 120–121why, one step back120};121TweaksSindre Sorhusbefore adoption698a1b8
lines 122–129why, one step back122test('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'));