Skip to content
Myr

iterable.js

This file

Path

test/stdio/iterable.js

139 lines · 6.0 KiB · JavaScript

Last changed by

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

History of this file

Take it

Improve error handling (#647)ehmickybefore adoption5b6d859

line 1why, one step back
1import {once} from 'node:events';

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

line 2why, one step back
2import {setTimeout, setImmediate} from 'node:timers/promises';

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 3–5why, one step back
3import test from 'ava';
4import {execa, execaSync} from '../../index.js';5import {setFixtureDir} from '../helpers/fixtures-dir.js';

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 6why, one step back
6import {getStdio} from '../helpers/stdio.js';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 7–8why, one step back
7import {foobarObject, foobarObjectString} from '../helpers/input.js';
8import {serializeGenerator} from '../helpers/generator.js';

Refactor tests (#628)ehmickybefore adoption30ecc81

line 9why, one step back
9

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 10–25why, one step back
10const stringArray = ['foo', 'bar'];
1112const stringGenerator = function * () {13 yield * stringArray;14};1516const textEncoder = new TextEncoder();17const binaryFoo = textEncoder.encode('foo');18const binaryBar = textEncoder.encode('bar');19const binaryArray = [binaryFoo, binaryBar];2021const binaryGenerator = function * () {22 yield * binaryArray;23};2425const asyncGenerator = async function * () {

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

line 26why, one step back
26 await setImmediate();

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 27–29why, one step back
27 yield * stringArray;
28};29

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 30–31why, one step back
30setFixtureDir();
31

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 32–33why, one step back
32const testIterable = async (t, stdioOption, index) => {
33 const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, stdioOption));

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 34–36why, one step back
34 t.is(stdout, 'foobar');
35};36

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 37–40why, one step back
37test('stdin option can be an array of strings', testIterable, [stringArray], 0);
38test('stdio[*] option can be an array of strings', testIterable, [stringArray], 3);39test('stdin option can be an array of Uint8Arrays', testIterable, [binaryArray], 0);40test('stdio[*] option can be an array of Uint8Arrays', testIterable, [binaryArray], 3);

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 41–46why, one step back
41test('stdin option can be an iterable of strings', testIterable, stringGenerator(), 0);
42test('stdio[*] option can be an iterable of strings', testIterable, stringGenerator(), 3);43test('stdin option can be an iterable of Uint8Arrays', testIterable, binaryGenerator(), 0);44test('stdio[*] option can be an iterable of Uint8Arrays', testIterable, binaryGenerator(), 3);45test('stdin option can be an async iterable', testIterable, asyncGenerator(), 0);46test('stdio[*] option can be an async iterable', testIterable, asyncGenerator(), 3);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 47why, one step back
47

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 48–67why, one step back
48const foobarObjectGenerator = function * () {
49 yield foobarObject;50};5152const foobarAsyncObjectGenerator = function * () {53 yield foobarObject;54};5556const testObjectIterable = async (t, stdioOption, index) => {57 const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, [stdioOption, serializeGenerator]));58 t.is(stdout, foobarObjectString);59};6061test('stdin option can be an array of objects', testObjectIterable, [foobarObject], 0);62test('stdio[*] option can be an array of objects', testObjectIterable, [foobarObject], 3);63test('stdin option can be an iterable of objects', testObjectIterable, foobarObjectGenerator(), 0);64test('stdio[*] option can be an iterable of objects', testObjectIterable, foobarObjectGenerator(), 3);65test('stdin option can be an async iterable of objects', testObjectIterable, foobarAsyncObjectGenerator(), 0);66test('stdio[*] option can be an async iterable of objects', testObjectIterable, foobarAsyncObjectGenerator(), 3);67

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 68why, one step back
68const testIterableSync = (t, stdioOption, index) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

line 69why, one step back
69 t.throws(() => {

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 70why, one step back
70 execaSync('empty.js', getStdio(index, stdioOption));

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 71–73why, one step back
71 }, {message: /an iterable in sync mode/});
72};73

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 74–75why, one step back
74test('stdin option cannot be an array of strings - sync', testIterableSync, [stringArray], 0);
75test('stdio[*] option cannot be an array of strings - sync', testIterableSync, [stringArray], 3);

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 76–79why, one step back
76test('stdin option cannot be a sync iterable - sync', testIterableSync, stringGenerator(), 0);
77test('stdio[*] option cannot be a sync iterable - sync', testIterableSync, stringGenerator(), 3);78test('stdin option cannot be an async iterable - sync', testIterableSync, asyncGenerator(), 0);79test('stdio[*] option cannot be an async iterable - sync', testIterableSync, asyncGenerator(), 3);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 80why, one step back
80

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 81–85why, one step back
81// eslint-disable-next-line require-yield
82const throwingGenerator = function * () {83 throw new Error('generator error');84};85

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 86–87why, one step back
86const testIterableError = async (t, index) => {
87 const {originalMessage} = await t.throwsAsync(execa('stdin-fd.js', [`${index}`], getStdio(index, throwingGenerator())));

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 88–90why, one step back
88 t.is(originalMessage, 'generator error');
89};90

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 91–92why, one step back
91test('stdin option handles errors in iterables', testIterableError, 0);
92test('stdio[*] option handles errors in iterables', testIterableError, 3);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 93why, one step back
93

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 94why, one step back
94const testNoIterableOutput = (t, stdioOption, index, execaMethod) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

line 95why, one step back
95 t.throws(() => {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 96why, one step back
96 execaMethod('empty.js', getStdio(index, stdioOption));

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 97–99why, one step back
97 }, {message: /cannot be an iterable/});
98};99

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 100–118why, one step back
100test('stdout option cannot be an array of strings', testNoIterableOutput, [stringArray], 1, execa);
101test('stderr option cannot be an array of strings', testNoIterableOutput, [stringArray], 2, execa);102test('stdout option cannot be an array of strings - sync', testNoIterableOutput, [stringArray], 1, execaSync);103test('stderr option cannot be an array of strings - sync', testNoIterableOutput, [stringArray], 2, execaSync);104test('stdout option cannot be an iterable', testNoIterableOutput, stringGenerator(), 1, execa);105test('stderr option cannot be an iterable', testNoIterableOutput, stringGenerator(), 2, execa);106test('stdout option cannot be an iterable - sync', testNoIterableOutput, stringGenerator(), 1, execaSync);107test('stderr option cannot be an iterable - sync', testNoIterableOutput, stringGenerator(), 2, execaSync);108109const infiniteGenerator = () => {110 const controller = new AbortController();111112 const generator = async function * () {113 yield 'foo';114 await setTimeout(1e7, undefined, {signal: controller.signal});115 };116117 return {iterable: generator(), abort: controller.abort.bind(controller)};118};

Improve error handling (#647)ehmickybefore adoption5b6d859

lines 119–122why, one step back
119
120test('stdin option can be an infinite iterable', async t => {121 const {iterable, abort} = infiniteGenerator();122 try {

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 123why, one step back
123 const childProcess = execa('stdin.js', getStdio(0, iterable));

Improve error handling (#647)ehmickybefore adoption5b6d859

lines 124–131why, one step back
124 const stdout = await once(childProcess.stdout, 'data');
125 t.is(stdout.toString(), 'foo');126 childProcess.kill('SIGKILL');127 await t.throwsAsync(childProcess, {message: /SIGKILL/});128 } finally {129 abort();130 }131});

Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca

line 132why, one step back
132

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 133–134why, one step back
133const testMultipleIterable = async (t, index) => {
134 const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, [stringGenerator(), asyncGenerator()]));

Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca

lines 135–137why, one step back
135 t.is(stdout, 'foobarfoobar');
136};137

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 138–139why, one step back
138test('stdin option can be multiple iterables', testMultipleIterable, 0);
139test('stdio[*] option can be multiple iterables', testMultipleIterable, 3);