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 fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
lines 1–3why, one step back1import 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 back4import {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};27TweaksSindre Sorhusbefore adoption698a1b8
line 28why, one step back28const errorArrayFromAsync = async (cause, asyncIterable, lines = []) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 29–33why, one step back29 const {value} = await asyncIterable.next();30 lines.push(value);31 await asyncIterable.throw(cause);32};33TweaksSindre Sorhusbefore adoption698a1b8
line 34why, one step back34const throwsAsync = async (asyncIterable, arrayFromAsyncMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 35why, one step back35 const lines = [];TweaksSindre Sorhusbefore adoption698a1b8
line 36why, one step back36 const error = await assertRejects(arrayFromAsyncMethod(asyncIterable, lines));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 37–39why, one step back37 return {error, lines};38};39TweaksSindre Sorhusbefore adoption698a1b8
lines 40–44why, one step back40const 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 back4546 if (cause === undefined) {TweaksSindre Sorhusbefore adoption698a1b8
line 47why, one step back47 assertReadableAborted(subprocess.stdout);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 48why, one step back48 } else {TweaksSindre Sorhusbefore adoption698a1b8
line 49why, one step back49 assert.equal(subprocess.stdout.errored, cause);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 50–52why, one step back50 }51};52TweaksSindre Sorhusbefore adoption698a1b8
line 53why, one step back53const testSuccess = async (fdNumber, from, options = {}) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 54why, one step back54 const lines = await arrayFromAsync(execa('noop-fd.js', [`${fdNumber}`, simpleFull], options).iterable({from}));TweaksSindre Sorhusbefore adoption698a1b8
line 55why, one step back55 assert.deepEqual(lines, noNewlinesChunks);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 56–57why, one step back56};57TweaksSindre Sorhusbefore adoption698a1b8
lines 58–61why, one step back58test('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 back62TweaksSindre Sorhusbefore adoption698a1b8
line 63why, one step back63test('Can iterate successfully on all', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 64why, one step back64 const lines = await arrayFromAsync(execa('noop-both.js', [simpleFull], {all: true}).iterable({from: 'all'}));TweaksSindre Sorhusbefore adoption698a1b8
line 65why, one step back65 assert.deepEqual(lines, [...noNewlinesChunks, ...noNewlinesChunks]);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 66–67why, one step back66});67TweaksSindre Sorhusbefore adoption698a1b8
line 68why, one step back68test('Can iterate using Symbol.asyncIterator', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 69why, one step back69 const lines = await arrayFromAsync(execa('noop-fd.js', ['1', simpleFull]));TweaksSindre Sorhusbefore adoption698a1b8
line 70why, one step back70 assert.deepEqual(lines, noNewlinesChunks);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 71–72why, one step back71});72TweaksSindre Sorhusbefore adoption698a1b8
lines 73–74why, one step back73const assertMultipleCalls = async (iterable, iterableTwo) => {74 assert.notEqual(iterable, iterableTwo);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 75–76why, one step back75 const lines = await arrayFromAsync(iterable);76 const linesTwo = await arrayFromAsync(iterableTwo);TweaksSindre Sorhusbefore adoption698a1b8
lines 77–78why, one step back77 assert.deepEqual(lines, linesTwo);78 assert.deepEqual(lines, noNewlinesChunks);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 79–80why, one step back79};80TweaksSindre Sorhusbefore adoption698a1b8
line 81why, one step back81test('Can be called multiple times', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 82–84why, one step back82 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 back85 await assertMultipleCalls(iterable, iterableTwo);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 86–87why, one step back86});87TweaksSindre Sorhusbefore adoption698a1b8
line 88why, one step back88test('Can be called on different file descriptors', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 89–91why, one step back89 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 back92 await assertMultipleCalls(iterable, iterableTwo);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 93–94why, one step back93});94TweaksSindre Sorhusbefore adoption698a1b8
line 95why, one step back95test('Wait for the subprocess exit', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 96–97why, one step back96 const subprocess = execa('noop-delay.js', ['1', simpleFull]);97 const linesPromise = arrayFromAsync(subprocess);TweaksSindre Sorhusbefore adoption698a1b8
lines 98–99why, one step back98 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 back100});101TweaksSindre Sorhusbefore adoption698a1b8
line 102why, one step back102test('Wait for the subprocess exit on iterator.return()', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 103–104why, one step back103 const subprocess = execa('noop-delay.js', ['1', simpleFull]);104 const linesPromise = partialArrayFromAsync(subprocess);TweaksSindre Sorhusbefore adoption698a1b8
lines 105–106why, one step back105 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 back107});108TweaksSindre Sorhusbefore adoption698a1b8
line 109why, one step back109test('Wait for the subprocess exit on iterator.throw()', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 110–112why, one step back110 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 back113 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 back116});117TweaksSindre Sorhusbefore adoption698a1b8
line 118why, one step back118test('Abort stdout on iterator.return()', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 119why, one step back119 const subprocess = execa('noop-repeat.js', ['1', simpleFull]);TweaksSindre Sorhusbefore adoption698a1b8
lines 120–123why, one step back120 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 back124});125TweaksSindre Sorhusbefore adoption698a1b8
line 126why, one step back126test('Abort stdout on iterator.throw()', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 127–128why, one step back127 const subprocess = execa('noop-repeat.js', ['1', simpleFull]);128 const cause = new Error(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
lines 129–132why, one step back129 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 back133});134TweaksSindre Sorhusbefore adoption698a1b8
line 135why, one step back135test('Propagate subprocess failure', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 136why, one step back136 const subprocess = execa('noop-fail.js', ['1', simpleFull]);TweaksSindre Sorhusbefore adoption698a1b8
lines 137–139why, one step back137 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 back140});141TweaksSindre Sorhusbefore adoption698a1b8
line 142why, one step back142const testStdoutError = async (destroyStdout, isAbort, cause) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 143–147why, one step back143 const subprocess = execa('noop-repeat.js', ['1', simpleFull]);144 subprocess.stdout.once('data', () => {145 destroyStdout(subprocess.stdout, cause);146 });147TweaksSindre Sorhusbefore adoption698a1b8
lines 148–151why, one step back148 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 back152};153TweaksSindre Sorhusbefore adoption698a1b8
lines 154–156why, one step back154test('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 back157158test('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});