Skip to content
Myr

iterable.js

This file

Path

test/convert/iterable.js

164 lines · 6.5 KiB · JavaScript

Last changed by

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–3why, one step back
1import assert from 'node:assert/strict';
2import test from 'node:test';3import {assertRejects} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 4–27why, one step back
4import {execa} from '../../index.js';
5import {setFixtureDirectory} from '../helpers/fixtures-directory.js';6import {foobarString} from '../helpers/input.js';7import {fullStdio, assertEpipe} from '../helpers/stdio.js';8import {9 arrayFromAsync,10 assertWritableAborted,11 assertReadableAborted,12 assertProcessNormalExit,13} from '../helpers/convert.js';14import {simpleFull, noNewlinesChunks} from '../helpers/lines.js';1516setFixtureDirectory();1718const partialArrayFromAsync = async (asyncIterable, lines = []) => {19 // eslint-disable-next-line no-unreachable-loop20 for await (const line of asyncIterable) {21 lines.push(line);22 break;23 }2425 return lines;26};27

TweaksSindre Sorhusbefore adoption698a1b8

line 28why, one step back
28const errorArrayFromAsync = async (cause, asyncIterable, lines = []) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 29–33why, one step back
29 const {value} = await asyncIterable.next();
30 lines.push(value);31 await asyncIterable.throw(cause);32};33

TweaksSindre Sorhusbefore adoption698a1b8

line 34why, one step back
34const throwsAsync = async (asyncIterable, arrayFromAsyncMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 35why, one step back
35 const lines = [];

TweaksSindre Sorhusbefore adoption698a1b8

line 36why, one step back
36 const error = await assertRejects(arrayFromAsyncMethod(asyncIterable, lines));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 37–39why, one step back
37 return {error, lines};
38};39

TweaksSindre Sorhusbefore adoption698a1b8

lines 40–44why, one step back
40const assertStdoutAbort = (subprocess, error, cause) => {
41 assertProcessNormalExit(error, 1);42 assertEpipe(error.stderr);43 assertWritableAborted(subprocess.stdin);44 assert.equal(subprocess.stderr.readableEnded, true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 45–46why, one step back
45
46 if (cause === undefined) {

TweaksSindre Sorhusbefore adoption698a1b8

line 47why, one step back
47 assertReadableAborted(subprocess.stdout);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 48why, one step back
48 } else {

TweaksSindre Sorhusbefore adoption698a1b8

line 49why, one step back
49 assert.equal(subprocess.stdout.errored, cause);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 50–52why, one step back
50 }
51};52

TweaksSindre Sorhusbefore adoption698a1b8

line 53why, one step back
53const testSuccess = async (fdNumber, from, options = {}) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 54why, one step back
54 const lines = await arrayFromAsync(execa('noop-fd.js', [`${fdNumber}`, simpleFull], options).iterable({from}));

TweaksSindre Sorhusbefore adoption698a1b8

line 55why, one step back
55 assert.deepEqual(lines, noNewlinesChunks);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 56–57why, one step back
56};
57

TweaksSindre Sorhusbefore adoption698a1b8

lines 58–61why, one step back
58test('Uses stdout by default', () => testSuccess(1, undefined));
59test('Can iterate successfully on stdout', () => testSuccess(1, 'stdout'));60test('Can iterate successfully on stderr', () => testSuccess(2, 'stderr'));61test('Can iterate successfully on stdio[*]', () => testSuccess(3, 'fd3', fullStdio));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 62why, one step back
62

TweaksSindre Sorhusbefore adoption698a1b8

line 63why, one step back
63test('Can iterate successfully on all', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 64why, one step back
64 const lines = await arrayFromAsync(execa('noop-both.js', [simpleFull], {all: true}).iterable({from: 'all'}));

TweaksSindre Sorhusbefore adoption698a1b8

line 65why, one step back
65 assert.deepEqual(lines, [...noNewlinesChunks, ...noNewlinesChunks]);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 66–67why, one step back
66});
67

TweaksSindre Sorhusbefore adoption698a1b8

line 68why, one step back
68test('Can iterate using Symbol.asyncIterator', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 69why, one step back
69 const lines = await arrayFromAsync(execa('noop-fd.js', ['1', simpleFull]));

TweaksSindre Sorhusbefore adoption698a1b8

line 70why, one step back
70 assert.deepEqual(lines, noNewlinesChunks);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 71–72why, one step back
71});
72

TweaksSindre Sorhusbefore adoption698a1b8

lines 73–74why, one step back
73const assertMultipleCalls = async (iterable, iterableTwo) => {
74 assert.notEqual(iterable, iterableTwo);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 75–76why, one step back
75 const lines = await arrayFromAsync(iterable);
76 const linesTwo = await arrayFromAsync(iterableTwo);

TweaksSindre Sorhusbefore adoption698a1b8

lines 77–78why, one step back
77 assert.deepEqual(lines, linesTwo);
78 assert.deepEqual(lines, noNewlinesChunks);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 79–80why, one step back
79};
80

TweaksSindre Sorhusbefore adoption698a1b8

line 81why, one step back
81test('Can be called multiple times', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 82–84why, one step back
82 const subprocess = execa('noop-fd.js', ['1', simpleFull]);
83 const iterable = subprocess.iterable();84 const iterableTwo = subprocess.iterable();

TweaksSindre Sorhusbefore adoption698a1b8

line 85why, one step back
85 await assertMultipleCalls(iterable, iterableTwo);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 86–87why, one step back
86});
87

TweaksSindre Sorhusbefore adoption698a1b8

line 88why, one step back
88test('Can be called on different file descriptors', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 89–91why, one step back
89 const subprocess = execa('noop-both.js', [simpleFull]);
90 const iterable = subprocess.iterable();91 const iterableTwo = subprocess.iterable({from: 'stderr'});

TweaksSindre Sorhusbefore adoption698a1b8

line 92why, one step back
92 await assertMultipleCalls(iterable, iterableTwo);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 93–94why, one step back
93});
94

TweaksSindre Sorhusbefore adoption698a1b8

line 95why, one step back
95test('Wait for the subprocess exit', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 96–97why, one step back
96 const subprocess = execa('noop-delay.js', ['1', simpleFull]);
97 const linesPromise = arrayFromAsync(subprocess);

TweaksSindre Sorhusbefore adoption698a1b8

lines 98–99why, one step back
98 assert.equal(await Promise.race([linesPromise, subprocess]), await subprocess);
99 assert.deepEqual(await linesPromise, noNewlinesChunks);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 100–101why, one step back
100});
101

TweaksSindre Sorhusbefore adoption698a1b8

line 102why, one step back
102test('Wait for the subprocess exit on iterator.return()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 103–104why, one step back
103 const subprocess = execa('noop-delay.js', ['1', simpleFull]);
104 const linesPromise = partialArrayFromAsync(subprocess);

TweaksSindre Sorhusbefore adoption698a1b8

lines 105–106why, one step back
105 assert.equal(await Promise.race([linesPromise, subprocess]), await subprocess);
106 assert.deepEqual(await linesPromise, [noNewlinesChunks[0]]);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 107–108why, one step back
107});
108

TweaksSindre Sorhusbefore adoption698a1b8

line 109why, one step back
109test('Wait for the subprocess exit on iterator.throw()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 110–112why, one step back
110 const subprocess = execa('noop-delay.js', ['1', simpleFull]);
111 const cause = new Error(foobarString);112 const lines = [];

TweaksSindre Sorhusbefore adoption698a1b8

lines 113–115why, one step back
113 const linesPromise = assertRejects(errorArrayFromAsync(cause, subprocess.iterable(), lines));
114 assert.equal(await Promise.race([linesPromise, subprocess]), await subprocess);115 assert.deepEqual(lines, [noNewlinesChunks[0]]);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 116–117why, one step back
116});
117

TweaksSindre Sorhusbefore adoption698a1b8

line 118why, one step back
118test('Abort stdout on iterator.return()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 119why, one step back
119 const subprocess = execa('noop-repeat.js', ['1', simpleFull]);

TweaksSindre Sorhusbefore adoption698a1b8

lines 120–123why, one step back
120 const {error, lines} = await throwsAsync(subprocess, partialArrayFromAsync);
121 assert.deepEqual(lines, [noNewlinesChunks[0]]);122 assertStdoutAbort(subprocess, error);123 assert.equal(error, await assertRejects(subprocess));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 124–125why, one step back
124});
125

TweaksSindre Sorhusbefore adoption698a1b8

line 126why, one step back
126test('Abort stdout on iterator.throw()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 127–128why, one step back
127 const subprocess = execa('noop-repeat.js', ['1', simpleFull]);
128 const cause = new Error(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

lines 129–132why, one step back
129 const {error, lines} = await throwsAsync(subprocess.iterable(), errorArrayFromAsync.bind(undefined, cause));
130 assert.deepEqual(lines, [noNewlinesChunks[0]]);131 assertStdoutAbort(subprocess, error);132 assert.equal(error, await assertRejects(subprocess));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 133–134why, one step back
133});
134

TweaksSindre Sorhusbefore adoption698a1b8

line 135why, one step back
135test('Propagate subprocess failure', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 136why, one step back
136 const subprocess = execa('noop-fail.js', ['1', simpleFull]);

TweaksSindre Sorhusbefore adoption698a1b8

lines 137–139why, one step back
137 const {error, lines} = await throwsAsync(subprocess, arrayFromAsync);
138 assert.equal(error, await assertRejects(subprocess));139 assert.deepEqual(lines, noNewlinesChunks);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 140–141why, one step back
140});
141

TweaksSindre Sorhusbefore adoption698a1b8

line 142why, one step back
142const testStdoutError = async (destroyStdout, isAbort, cause) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 143–147why, one step back
143 const subprocess = execa('noop-repeat.js', ['1', simpleFull]);
144 subprocess.stdout.once('data', () => {145 destroyStdout(subprocess.stdout, cause);146 });147

TweaksSindre Sorhusbefore adoption698a1b8

lines 148–151why, one step back
148 const {error} = await throwsAsync(subprocess, arrayFromAsync);
149 assert.equal(error.cause, cause);150 assertStdoutAbort(subprocess, error, isAbort ? undefined : cause);151 assert.equal(error, await assertRejects(subprocess));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 152–153why, one step back
152};
153

TweaksSindre Sorhusbefore adoption698a1b8

lines 154–156why, one step back
154test('Propagate stdout abort', () => testStdoutError(subprocessStdout => subprocessStdout.destroy(), true));
155test('Propagate stdout error', () => testStdoutError((subprocessStdout, cause) => subprocessStdout.destroy(cause), false, new Error(foobarString)));156test('Propagate stdout "error" event', () => testStdoutError((subprocessStdout, cause) => subprocessStdout.emit('error', cause), true, new Error(foobarString)));

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 157–164why, one step back
157
158test('The "binary" iterable option must use a boolean', () => {159 assert.throws(() => execa('noop.js').iterable({binary: 'true'}), {message: /`binary` option must use a boolean/});160});161162test('The "preserveNewlines" iterable option must use a boolean', () => {163 assert.throws(() => execa('noop.js').iterable({preserveNewlines: 'true'}), {message: /`preserveNewlines` option must use a boolean/});164});