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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239import assert from 'node:assert/strict';import {once} from 'node:events';import {setImmediate} from 'node:timers/promises';import test from 'node:test';import {assertThrows, assertRejects} from '../helpers/assert.js';import {execa, execaSync, ExecaSyncError} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {getStdio} from '../helpers/stdio.js';import { foobarString, foobarObject, foobarObjectString, foobarArray,} from '../helpers/input.js';import { noopGenerator, serializeGenerator, infiniteGenerator, throwingGenerator,} from '../helpers/generator.js';
const stringGenerator = function * () { yield * foobarArray;};
const textEncoder = new TextEncoder();const binaryFoo = textEncoder.encode('foo');const binaryBar = textEncoder.encode('bar');const binaryArray = [binaryFoo, binaryBar];
const binaryGenerator = function * () { yield * binaryArray;};
const mixedArray = [foobarArray[0], binaryArray[1]];
const mixedGenerator = function * () { yield * mixedArray;};
const asyncGenerator = async function * () { await setImmediate(); yield * foobarArray;};
setFixtureDirectory();
const testIterable = async (stdioOption, fdNumber, execaMethod) => { const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, stdioOption)); assert.equal(stdout, 'foobar');};
const testIpcDataIterable = async (fdNumber, execaMethod) => { const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [['ipc']])); assert.equal(stdout, 'ipc');};
test('stdin option can be an array of strings', () => testIterable([foobarArray], 0, execa));test('stdin option can be an array of strings - sync', () => testIterable([foobarArray], 0, execaSync));test('stdio[*] option can be an array of strings', () => testIterable([foobarArray], 3, execa));test('stdin option can be an array with "ipc" as input data', () => testIpcDataIterable(0, execa));test('stdin option can be an array with "ipc" as input data - sync', () => testIpcDataIterable(0, execaSync));test('stdio[*] option can be an array with "ipc" as input data', () => testIpcDataIterable(3, execa));test('stdin option can be an array of Uint8Arrays', () => testIterable([binaryArray], 0, execa));test('stdin option can be an array of Uint8Arrays - sync', () => testIterable([binaryArray], 0, execaSync));test('stdio[*] option can be an array of Uint8Arrays', () => testIterable([binaryArray], 3, execa));test('stdin option can be an iterable of strings', () => testIterable(stringGenerator(), 0, execa));test('stdin option can be an iterable of strings - sync', () => testIterable(stringGenerator(), 0, execaSync));test('stdio[*] option can be an iterable of strings', () => testIterable(stringGenerator(), 3, execa));test('stdin option can be an iterable of Uint8Arrays', () => testIterable(binaryGenerator(), 0, execa));test('stdin option can be an iterable of Uint8Arrays - sync', () => testIterable(binaryGenerator(), 0, execaSync));test('stdio[*] option can be an iterable of Uint8Arrays', () => testIterable(binaryGenerator(), 3, execa));test('stdin option can be an iterable of strings + Uint8Arrays', () => testIterable(mixedGenerator(), 0, execa));test('stdin option can be an iterable of strings + Uint8Arrays - sync', () => testIterable(mixedGenerator(), 0, execaSync));test('stdio[*] option can be an iterable of strings + Uint8Arrays', () => testIterable(mixedGenerator(), 3, execa));test('stdin option can be an async iterable', () => testIterable(asyncGenerator(), 0, execa));test('stdio[*] option can be an async iterable', () => testIterable(asyncGenerator(), 3, execa));
const foobarObjectGenerator = function * () { yield foobarObject;};
const foobarAsyncObjectGenerator = async function * () { yield foobarObject;};
const testObjectIterable = async (stdioOption, fdNumber, execaMethod) => { const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stdioOption, serializeGenerator(true)])); assert.equal(stdout, foobarObjectString);};
test('stdin option can be an array of objects', () => testObjectIterable([foobarObject], 0, execa));test('stdio[*] option can be an array of objects', () => testObjectIterable([foobarObject], 3, execa));test('stdin option can be an iterable of objects', () => testObjectIterable(foobarObjectGenerator(), 0, execa));test('stdio[*] option can be an iterable of objects', () => testObjectIterable(foobarObjectGenerator(), 3, execa));test('stdin option can be an async iterable of objects', () => testObjectIterable(foobarAsyncObjectGenerator(), 0, execa));test('stdio[*] option can be an async iterable of objects', () => testObjectIterable(foobarAsyncObjectGenerator(), 3, execa));test('stdin option can be an array of objects - sync', () => testObjectIterable([foobarObject], 0, execaSync));test('stdin option can be an iterable of objects - sync', () => testObjectIterable(foobarObjectGenerator(), 0, execaSync));
// Writing those objects to `subprocess.stdin` fails, which must not crash the current processconst testIterableNoGenerators = async (stdioOption, fdNumber) => { await assertRejects(execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, stdioOption)), {message: /must be used to serialize/});};
test('stdin option cannot be an array of objects without generators', () => testIterableNoGenerators([[foobarObject]], 0));test('stdin option cannot be an array of numbers without generators', () => testIterableNoGenerators([[0]], 0));test('stdio[*] option cannot be an array of objects without generators', () => testIterableNoGenerators([[foobarObject]], 3));test('stdin option cannot be an iterable of objects without generators', () => testIterableNoGenerators(foobarObjectGenerator(), 0));test('stdin option cannot be an async iterable of objects without generators', () => testIterableNoGenerators(foobarAsyncObjectGenerator(), 0));test('stdin option cannot be an array of strings and objects without generators', () => testIterableNoGenerators([[foobarString, foobarObject]], 0));
const testIterableNoGeneratorsSync = (stdioOption, fdNumber) => { assertThrows(() => { execaSync('empty.js', getStdio(fdNumber, stdioOption)); }, {message: /must be used to serialize/});};
test('stdin option cannot be an array of objects without generators - sync', () => testIterableNoGeneratorsSync([[foobarObject]], 0));test('stdin option cannot be an iterable of objects without generators - sync', () => testIterableNoGeneratorsSync(foobarObjectGenerator(), 0));
const testIterableNoSerializeSync = (stdioOption, fdNumber) => { assertThrows(() => { execaSync('empty.js', getStdio(fdNumber, [stdioOption, noopGenerator()])); }, {message: /The `stdin` option's transform must use "objectMode: true" to receive as input: object/});};
test('stdin option cannot be an array of objects without serializing - sync', () => testIterableNoSerializeSync([foobarObject], 0));test('stdin option cannot be an iterable of objects without serializing - sync', () => testIterableNoSerializeSync(foobarObjectGenerator(), 0));
const testIterableSync = (stdioOption, fdNumber) => { assertThrows(() => { execaSync('empty.js', getStdio(fdNumber, stdioOption)); }, {message: /an iterable with synchronous methods/});};
test('stdio[*] option cannot be an array of strings - sync', () => testIterableSync([foobarArray], 3));test('stdio[*] option cannot be an array of Uint8Arrays - sync', () => testIterableSync([binaryArray], 3));test('stdio[*] option cannot be an array of objects - sync', () => testIterableSync([[foobarObject]], 3));test('stdio[*] option cannot be an iterable of strings - sync', () => testIterableSync(stringGenerator(), 3));test('stdio[*] option cannot be an iterable of Uint8Arrays - sync', () => testIterableSync(binaryGenerator(), 3));test('stdio[*] option cannot be an iterable of objects - sync', () => testIterableSync(foobarObjectGenerator(), 3));test('stdio[*] option cannot be multiple iterables - sync', () => testIterableSync([stringGenerator(), stringGenerator()], 3));
const testAsyncIterableSync = (stdioOption, fdNumber) => { assertThrows(() => { execaSync('empty.js', getStdio(fdNumber, stdioOption)); }, {message: /an async iterable with synchronous method/});};
test('stdin option cannot be an async iterable - sync', () => testAsyncIterableSync(asyncGenerator(), 0));test('stdio[*] option cannot be an async iterable - sync', () => testAsyncIterableSync(asyncGenerator(), 3));test('stdin option cannot be an async iterable of objects - sync', () => testAsyncIterableSync(foobarAsyncObjectGenerator(), 0));test('stdio[*] option cannot be an async iterable of objects - sync', () => testAsyncIterableSync(foobarAsyncObjectGenerator(), 3));
const testIterableError = async fdNumber => { const cause = new Error(foobarString); const error = await assertRejects(execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, throwingGenerator(cause)().transform()))); assert.equal(error.cause, cause);};
test('stdin option handles errors in iterables', () => testIterableError(0));test('stdio[*] option handles errors in iterables', () => testIterableError(3));
// Like with asynchronous methods, the iterable's error is reported as the subprocess errortest('stdin option handles errors in iterables - sync', () => { const cause = new Error(foobarString); const error = assertThrows(() => { execaSync('stdin-fd.js', ['0'], getStdio(0, throwingGenerator(cause)().transform())); }); assert.ok(error instanceof ExecaSyncError); assert.equal(error.cause, cause); assert.equal(error.stdout, '');});
test('stdin option handles errors in iterables, reject: false - sync', () => { const cause = new Error(foobarString); const {failed, cause: errorCause} = execaSync('stdin-fd.js', ['0'], {...getStdio(0, throwingGenerator(cause)().transform()), reject: false}); assert.equal(failed, true); assert.equal(errorCause, cause);});
test('stdio[*] option cannot be an iterable - sync', () => { const error = assertThrows(() => { execaSync('stdin-fd.js', ['3'], getStdio(3, [[foobarString]])); }, {message: /Only the `stdin` option, not `stdio\[3\]`, can be an iterable with synchronous methods/}); assert.ok(error instanceof ExecaSyncError);});
const testNoIterableOutput = (stdioOption, fdNumber, execaMethod) => { assertThrows(() => { execaMethod('empty.js', getStdio(fdNumber, stdioOption)); }, {message: /cannot be an iterable/});};
test('stdout option cannot be an array of strings', () => testNoIterableOutput([foobarArray], 1, execa));test('stderr option cannot be an array of strings', () => testNoIterableOutput([foobarArray], 2, execa));test('stdout option cannot be an array of strings - sync', () => testNoIterableOutput([foobarArray], 1, execaSync));test('stderr option cannot be an array of strings - sync', () => testNoIterableOutput([foobarArray], 2, execaSync));test('stdout option cannot be an iterable', () => testNoIterableOutput(stringGenerator(), 1, execa));test('stderr option cannot be an iterable', () => testNoIterableOutput(stringGenerator(), 2, execa));test('stdout option cannot be an iterable - sync', () => testNoIterableOutput(stringGenerator(), 1, execaSync));test('stderr option cannot be an iterable - sync', () => testNoIterableOutput(stringGenerator(), 2, execaSync));
test('stdin option can be an infinite iterable', async () => { const iterable = infiniteGenerator().transform(); const subprocess = execa('stdin.js', getStdio(0, iterable)); await once(subprocess.stdout, 'data'); subprocess.kill(); const {stdout} = await assertRejects(subprocess); assert.ok(stdout.startsWith('foo')); assert.deepEqual(await iterable.next(), {value: undefined, done: true});});
const testMultipleIterable = async (stdioOption, fdNumber, execaMethod) => { const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, stdioOption)); const expectedOutputs = [ `${foobarArray[0]}${foobarArray[1]}${foobarArray[0]}${foobarArray[1]}`, `${foobarArray[0]}${foobarArray[0]}${foobarArray[1]}${foobarArray[1]}`, ]; assert.ok(expectedOutputs.includes(stdout));};
test('stdin option can be multiple iterables', () => testMultipleIterable([stringGenerator(), stringGenerator()], 0, execa));test('stdio[*] option can be multiple iterables', () => testMultipleIterable([stringGenerator(), stringGenerator()], 3, execa));test('stdin option can be multiple iterables - sync', () => testMultipleIterable([stringGenerator(), stringGenerator()], 0, execaSync));test('stdin option can be multiple mixed iterables', () => testMultipleIterable([stringGenerator(), binaryGenerator()], 0, execa));test('stdio[*] option can be multiple mixed iterables', () => testMultipleIterable([stringGenerator(), binaryGenerator()], 3, execa));test('stdin option can be multiple mixed iterables - sync', () => testMultipleIterable([stringGenerator(), binaryGenerator()], 0, execaSync));test('stdin option can be sync/async mixed iterables', () => testMultipleIterable([stringGenerator(), asyncGenerator()], 0, execa));test('stdio[*] option can be sync/async mixed iterables', () => testMultipleIterable([stringGenerator(), asyncGenerator()], 3, execa));
test('stdin option iterable is canceled on subprocess error', async () => { const iterable = infiniteGenerator().transform(); await assertRejects(execa('stdin.js', {stdin: iterable, timeout: 1}), {message: /timed out/}); // eslint-disable-next-line no-empty for await (const _ of iterable) {}});