iterable.js
This file
Path
test/stdio/iterable.js
238 lines · 13.3 KiB · JavaScript
Last changed by
Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
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–3why, one step back2import {once} from 'node:events';3import {setImmediate} from 'node:timers/promises';TweaksSindre Sorhusbefore adoption698a1b8
lines 4–5why, one step back4import test from 'node:test';5import {assertThrows, assertRejects} from '../helpers/assert.js';Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
line 6why, one step back6import {execa, execaSync, ExecaSyncError} from '../../index.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 7–47why, one step back7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8import {getStdio} from '../helpers/stdio.js';9import {10 foobarString,11 foobarObject,12 foobarObjectString,13 foobarArray,14} from '../helpers/input.js';15import {16 noopGenerator,17 serializeGenerator,18 infiniteGenerator,19 throwingGenerator,20} from '../helpers/generator.js';2122const stringGenerator = function * () {23 yield * foobarArray;24};2526const textEncoder = new TextEncoder();27const binaryFoo = textEncoder.encode('foo');28const binaryBar = textEncoder.encode('bar');29const binaryArray = [binaryFoo, binaryBar];3031const binaryGenerator = function * () {32 yield * binaryArray;33};3435const mixedArray = [foobarArray[0], binaryArray[1]];3637const mixedGenerator = function * () {38 yield * mixedArray;39};4041const asyncGenerator = async function * () {42 await setImmediate();43 yield * foobarArray;44};4546setFixtureDirectory();47TweaksSindre Sorhusbefore adoption698a1b8
line 48why, one step back48const testIterable = async (stdioOption, fdNumber, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 49why, one step back49 const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, stdioOption));TweaksSindre Sorhusbefore adoption698a1b8
line 50why, one step back50 assert.equal(stdout, 'foobar');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 51–52why, one step back51};52TweaksSindre Sorhusbefore adoption698a1b8
line 53why, one step back53const testIpcDataIterable = async (fdNumber, execaMethod) => {Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
line 54why, one step back54 const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [['ipc']]));TweaksSindre Sorhusbefore adoption698a1b8
line 55why, one step back55 assert.equal(stdout, 'ipc');Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
lines 56–57why, one step back56};57TweaksSindre Sorhusbefore adoption698a1b8
lines 58–77why, one step back58test('stdin option can be an array of strings', () => testIterable([foobarArray], 0, execa));59test('stdin option can be an array of strings - sync', () => testIterable([foobarArray], 0, execaSync));60test('stdio[*] option can be an array of strings', () => testIterable([foobarArray], 3, execa));61test('stdin option can be an array with "ipc" as input data', () => testIpcDataIterable(0, execa));62test('stdin option can be an array with "ipc" as input data - sync', () => testIpcDataIterable(0, execaSync));63test('stdio[*] option can be an array with "ipc" as input data', () => testIpcDataIterable(3, execa));64test('stdin option can be an array of Uint8Arrays', () => testIterable([binaryArray], 0, execa));65test('stdin option can be an array of Uint8Arrays - sync', () => testIterable([binaryArray], 0, execaSync));66test('stdio[*] option can be an array of Uint8Arrays', () => testIterable([binaryArray], 3, execa));67test('stdin option can be an iterable of strings', () => testIterable(stringGenerator(), 0, execa));68test('stdin option can be an iterable of strings - sync', () => testIterable(stringGenerator(), 0, execaSync));69test('stdio[*] option can be an iterable of strings', () => testIterable(stringGenerator(), 3, execa));70test('stdin option can be an iterable of Uint8Arrays', () => testIterable(binaryGenerator(), 0, execa));71test('stdin option can be an iterable of Uint8Arrays - sync', () => testIterable(binaryGenerator(), 0, execaSync));72test('stdio[*] option can be an iterable of Uint8Arrays', () => testIterable(binaryGenerator(), 3, execa));73test('stdin option can be an iterable of strings + Uint8Arrays', () => testIterable(mixedGenerator(), 0, execa));74test('stdin option can be an iterable of strings + Uint8Arrays - sync', () => testIterable(mixedGenerator(), 0, execaSync));75test('stdio[*] option can be an iterable of strings + Uint8Arrays', () => testIterable(mixedGenerator(), 3, execa));76test('stdin option can be an async iterable', () => testIterable(asyncGenerator(), 0, execa));77test('stdio[*] option can be an async iterable', () => testIterable(asyncGenerator(), 3, execa));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 78–86why, one step back7879const foobarObjectGenerator = function * () {80 yield foobarObject;81};8283const foobarAsyncObjectGenerator = async function * () {84 yield foobarObject;85};86TweaksSindre Sorhusbefore adoption698a1b8
line 87why, one step back87const testObjectIterable = async (stdioOption, fdNumber, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 88why, one step back88 const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stdioOption, serializeGenerator(true)]));TweaksSindre Sorhusbefore adoption698a1b8
line 89why, one step back89 assert.equal(stdout, foobarObjectString);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 90–91why, one step back90};91TweaksSindre Sorhusbefore adoption698a1b8
lines 92–99why, one step back92test('stdin option can be an array of objects', () => testObjectIterable([foobarObject], 0, execa));93test('stdio[*] option can be an array of objects', () => testObjectIterable([foobarObject], 3, execa));94test('stdin option can be an iterable of objects', () => testObjectIterable(foobarObjectGenerator(), 0, execa));95test('stdio[*] option can be an iterable of objects', () => testObjectIterable(foobarObjectGenerator(), 3, execa));96test('stdin option can be an async iterable of objects', () => testObjectIterable(foobarAsyncObjectGenerator(), 0, execa));97test('stdio[*] option can be an async iterable of objects', () => testObjectIterable(foobarAsyncObjectGenerator(), 3, execa));98test('stdin option can be an array of objects - sync', () => testObjectIterable([foobarObject], 0, execaSync));99test('stdin option can be an iterable of objects - sync', () => testObjectIterable(foobarObjectGenerator(), 0, execaSync));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 100why, one step back100Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 101–112why, one step back101// Writing those objects to `subprocess.stdin` fails, which must not crash the current process102const testIterableNoGenerators = async (stdioOption, fdNumber) => {103 await assertRejects(execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, stdioOption)), {message: /must be used to serialize/});104};105106test('stdin option cannot be an array of objects without generators', () => testIterableNoGenerators([[foobarObject]], 0));107test('stdin option cannot be an array of numbers without generators', () => testIterableNoGenerators([[0]], 0));108test('stdio[*] option cannot be an array of objects without generators', () => testIterableNoGenerators([[foobarObject]], 3));109test('stdin option cannot be an iterable of objects without generators', () => testIterableNoGenerators(foobarObjectGenerator(), 0));110test('stdin option cannot be an async iterable of objects without generators', () => testIterableNoGenerators(foobarAsyncObjectGenerator(), 0));111test('stdin option cannot be an array of strings and objects without generators', () => testIterableNoGenerators([[foobarString, foobarObject]], 0));112TweaksSindre Sorhusbefore adoption698a1b8
lines 113–114why, one step back113const testIterableNoGeneratorsSync = (stdioOption, fdNumber) => {114 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 115–118why, one step back115 execaSync('empty.js', getStdio(fdNumber, stdioOption));116 }, {message: /must be used to serialize/});117};118TweaksSindre Sorhusbefore adoption698a1b8
lines 119–120why, one step back119test('stdin option cannot be an array of objects without generators - sync', () => testIterableNoGeneratorsSync([[foobarObject]], 0));120test('stdin option cannot be an iterable of objects without generators - sync', () => testIterableNoGeneratorsSync(foobarObjectGenerator(), 0));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 121why, one step back121TweaksSindre Sorhusbefore adoption698a1b8
lines 122–123why, one step back122const testIterableNoSerializeSync = (stdioOption, fdNumber) => {123 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 124–127why, one step back124 execaSync('empty.js', getStdio(fdNumber, [stdioOption, noopGenerator()]));125 }, {message: /The `stdin` option's transform must use "objectMode: true" to receive as input: object/});126};127TweaksSindre Sorhusbefore adoption698a1b8
lines 128–129why, one step back128test('stdin option cannot be an array of objects without serializing - sync', () => testIterableNoSerializeSync([foobarObject], 0));129test('stdin option cannot be an iterable of objects without serializing - sync', () => testIterableNoSerializeSync(foobarObjectGenerator(), 0));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 130why, one step back130TweaksSindre Sorhusbefore adoption698a1b8
lines 131–132why, one step back131const testIterableSync = (stdioOption, fdNumber) => {132 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 133–136why, one step back133 execaSync('empty.js', getStdio(fdNumber, stdioOption));134 }, {message: /an iterable with synchronous methods/});135};136TweaksSindre Sorhusbefore adoption698a1b8
lines 137–143why, one step back137test('stdio[*] option cannot be an array of strings - sync', () => testIterableSync([foobarArray], 3));138test('stdio[*] option cannot be an array of Uint8Arrays - sync', () => testIterableSync([binaryArray], 3));139test('stdio[*] option cannot be an array of objects - sync', () => testIterableSync([[foobarObject]], 3));140test('stdio[*] option cannot be an iterable of strings - sync', () => testIterableSync(stringGenerator(), 3));141test('stdio[*] option cannot be an iterable of Uint8Arrays - sync', () => testIterableSync(binaryGenerator(), 3));142test('stdio[*] option cannot be an iterable of objects - sync', () => testIterableSync(foobarObjectGenerator(), 3));143test('stdio[*] option cannot be multiple iterables - sync', () => testIterableSync([stringGenerator(), stringGenerator()], 3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 144why, one step back144TweaksSindre Sorhusbefore adoption698a1b8
lines 145–146why, one step back145const testAsyncIterableSync = (stdioOption, fdNumber) => {146 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 147–150why, one step back147 execaSync('empty.js', getStdio(fdNumber, stdioOption));148 }, {message: /an async iterable with synchronous method/});149};150TweaksSindre Sorhusbefore adoption698a1b8
lines 151–154why, one step back151test('stdin option cannot be an async iterable - sync', () => testAsyncIterableSync(asyncGenerator(), 0));152test('stdio[*] option cannot be an async iterable - sync', () => testAsyncIterableSync(asyncGenerator(), 3));153test('stdin option cannot be an async iterable of objects - sync', () => testAsyncIterableSync(foobarAsyncObjectGenerator(), 0));154test('stdio[*] option cannot be an async iterable of objects - sync', () => testAsyncIterableSync(foobarAsyncObjectGenerator(), 3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 155why, one step back155TweaksSindre Sorhusbefore adoption698a1b8
line 156why, one step back156const testIterableError = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 157why, one step back157 const cause = new Error(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
lines 158–159why, one step back158 const error = await assertRejects(execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, throwingGenerator(cause)().transform())));159 assert.equal(error.cause, cause);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 160–161why, one step back160};161TweaksSindre Sorhusbefore adoption698a1b8
lines 162–163why, one step back162test('stdin option handles errors in iterables', () => testIterableError(0));163test('stdio[*] option handles errors in iterables', () => testIterableError(3));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 164why, one step back164Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 165–166why, one step back165// Like with asynchronous methods, the iterable's error is reported as the subprocess error166test('stdin option handles errors in iterables - sync', () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 167why, one step back167 const cause = new Error(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
line 168why, one step back168 const error = assertThrows(() => {Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
line 169why, one step back169 execaSync('stdin-fd.js', ['0'], getStdio(0, throwingGenerator(cause)().transform()));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 170why, one step back170 });Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 171–174why, one step back171 assert.ok(error instanceof ExecaSyncError);172 assert.equal(error.cause, cause);173 assert.equal(error.stdout, '');174});100% test coverage (#1018)ehmickybefore adoption9f65b86
line 175why, one step back175Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 176–188why, one step back176test('stdin option handles errors in iterables, reject: false - sync', () => {177 const cause = new Error(foobarString);178 const {failed, cause: errorCause} = execaSync('stdin-fd.js', ['0'], {...getStdio(0, throwingGenerator(cause)().transform()), reject: false});179 assert.equal(failed, true);180 assert.equal(errorCause, cause);181});182183test('stdio[*] option cannot be an iterable - sync', () => {184 const error = assertThrows(() => {185 execaSync('stdin-fd.js', ['3'], getStdio(3, [[foobarString]]));186 }, {message: /Only the `stdin` option, not `stdio\[3\]`, can be an iterable with synchronous methods/});187 assert.ok(error instanceof ExecaSyncError);188});100% test coverage (#1018)ehmickybefore adoption9f65b86
line 189why, one step back189TweaksSindre Sorhusbefore adoption698a1b8
lines 190–191why, one step back190const testNoIterableOutput = (stdioOption, fdNumber, execaMethod) => {191 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 192–195why, one step back192 execaMethod('empty.js', getStdio(fdNumber, stdioOption));193 }, {message: /cannot be an iterable/});194};195TweaksSindre Sorhusbefore adoption698a1b8
lines 196–203why, one step back196test('stdout option cannot be an array of strings', () => testNoIterableOutput([foobarArray], 1, execa));197test('stderr option cannot be an array of strings', () => testNoIterableOutput([foobarArray], 2, execa));198test('stdout option cannot be an array of strings - sync', () => testNoIterableOutput([foobarArray], 1, execaSync));199test('stderr option cannot be an array of strings - sync', () => testNoIterableOutput([foobarArray], 2, execaSync));200test('stdout option cannot be an iterable', () => testNoIterableOutput(stringGenerator(), 1, execa));201test('stderr option cannot be an iterable', () => testNoIterableOutput(stringGenerator(), 2, execa));202test('stdout option cannot be an iterable - sync', () => testNoIterableOutput(stringGenerator(), 1, execaSync));203test('stderr option cannot be an iterable - sync', () => testNoIterableOutput(stringGenerator(), 2, execaSync));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 204why, one step back204TweaksSindre Sorhusbefore adoption698a1b8
line 205why, one step back205test('stdin option can be an infinite iterable', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 206–209why, one step back206 const iterable = infiniteGenerator().transform();207 const subprocess = execa('stdin.js', getStdio(0, iterable));208 await once(subprocess.stdout, 'data');209 subprocess.kill();TweaksSindre Sorhusbefore adoption698a1b8
lines 210–212why, one step back210 const {stdout} = await assertRejects(subprocess);211 assert.ok(stdout.startsWith('foo'));212 assert.deepEqual(await iterable.next(), {value: undefined, done: true});100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 213–214why, one step back213});214TweaksSindre Sorhusbefore adoption698a1b8
line 215why, one step back215const testMultipleIterable = async (stdioOption, fdNumber, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 216–220why, one step back216 const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, stdioOption));217 const expectedOutputs = [218 `${foobarArray[0]}${foobarArray[1]}${foobarArray[0]}${foobarArray[1]}`,219 `${foobarArray[0]}${foobarArray[0]}${foobarArray[1]}${foobarArray[1]}`,220 ];TweaksSindre Sorhusbefore adoption698a1b8
line 221why, one step back221 assert.ok(expectedOutputs.includes(stdout));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 222–223why, one step back222};223TweaksSindre Sorhusbefore adoption698a1b8
lines 224–231why, one step back224test('stdin option can be multiple iterables', () => testMultipleIterable([stringGenerator(), stringGenerator()], 0, execa));225test('stdio[*] option can be multiple iterables', () => testMultipleIterable([stringGenerator(), stringGenerator()], 3, execa));226test('stdin option can be multiple iterables - sync', () => testMultipleIterable([stringGenerator(), stringGenerator()], 0, execaSync));227test('stdin option can be multiple mixed iterables', () => testMultipleIterable([stringGenerator(), binaryGenerator()], 0, execa));228test('stdio[*] option can be multiple mixed iterables', () => testMultipleIterable([stringGenerator(), binaryGenerator()], 3, execa));229test('stdin option can be multiple mixed iterables - sync', () => testMultipleIterable([stringGenerator(), binaryGenerator()], 0, execaSync));230test('stdin option can be sync/async mixed iterables', () => testMultipleIterable([stringGenerator(), asyncGenerator()], 0, execa));231test('stdio[*] option can be sync/async mixed iterables', () => testMultipleIterable([stringGenerator(), asyncGenerator()], 3, execa));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 232why, one step back232TweaksSindre Sorhusbefore adoption698a1b8
line 233why, one step back233test('stdin option iterable is canceled on subprocess error', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 234why, one step back234 const iterable = infiniteGenerator().transform();TweaksSindre Sorhusbefore adoption698a1b8
line 235why, one step back235 await assertRejects(execa('stdin.js', {stdin: iterable, timeout: 1}), {message: /timed out/});100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 236–238why, one step back236 // eslint-disable-next-line no-empty237 for await (const _ of iterable) {}238});