sequence.js
This file
Path
test/pipe/sequence.js
381 lines · 14.3 KiB · JavaScript
Last changed by
Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
History of this fileTake it
The walk stopped after 200 commits: lines older than ed052d4 point at it.
Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
lines 1–3why, one step back1import {once} from 'node:events';2import process from 'node:process';3import {PassThrough} from 'node:stream';Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
line 4why, one step back4import {setTimeout} from 'node:timers/promises';Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
lines 5–6why, one step back5import test from 'ava';6import {execa} from '../../index.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
line 7why, one step back7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
lines 8–11why, one step back8import {foobarString} from '../helpers/input.js';9import {noopGenerator} from '../helpers/generator.js';10import {prematureClose} from '../helpers/stdio.js';11Upgrade xo (#980)ehmickybefore adoptionccfa412
line 12why, one step back12setFixtureDirectory();Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
lines 13–14why, one step back1314const isLinux = process.platform === 'linux';Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
line 15why, one step back15const timeoutSymbol = Symbol('timeout');Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
line 16why, one step back16Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
lines 17–47why, one step back17const assertUnhandledRejection = async (t, pipePromise, unhandledRejectionPromise) => {18 const result = await Promise.race([19 unhandledRejectionPromise,20 setTimeout(500, timeoutSymbol),21 ]);2223 t.not(result, timeoutSymbol);2425 if (result === timeoutSymbol) {26 return;27 }2829 const [reason, unhandledPromise] = result;30 t.is(unhandledPromise, pipePromise);31 t.regex(reason.message, /Command failed with exit code 2/);32};3334const assertUnhandledPipePromise = async (t, {destinationOptions = {}, inspectPipePromise} = {}) => {35 const source = execa('fail.js');36 const destination = execa('fail.js', destinationOptions);37 const pipePromise = source.pipe(destination);38 const unhandledRejectionPromise = once(process, 'unhandledRejection');39 await inspectPipePromise?.(pipePromise);40 await assertUnhandledRejection(t, pipePromise, unhandledRejectionPromise);41 await Promise.all([42 t.throwsAsync(source),43 t.throwsAsync(destination),44 ]);45 await t.throwsAsync(pipePromise);46};47Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
lines 48–287why, one step back48test('Source stream abort -> destination success', async t => {49 const source = execa('noop-repeat.js');50 const destination = execa('stdin.js');51 const pipePromise = source.pipe(destination);52 source.stdout.destroy();5354 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));55 t.like(await t.throwsAsync(source), {exitCode: 1});56 await destination;57});5859test('Source stream error -> destination success', async t => {60 const source = execa('noop-repeat.js');61 const destination = execa('stdin.js');62 const pipePromise = source.pipe(destination);63 const cause = new Error('test');64 source.stdout.destroy(cause);6566 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));67 t.like(await t.throwsAsync(source), {originalMessage: cause.message, exitCode: 1});68 await destination;69});7071test('Destination stream abort -> source failure', async t => {72 const source = execa('noop-repeat.js');73 const destination = execa('stdin.js');74 const pipePromise = source.pipe(destination);75 destination.stdin.destroy();7677 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));78 t.like(await t.throwsAsync(destination), prematureClose);79 t.like(await t.throwsAsync(source), {exitCode: 1});80});8182test('Destination stream error -> source failure', async t => {83 const source = execa('noop-repeat.js');84 const destination = execa('stdin.js');85 const pipePromise = source.pipe(destination);86 const cause = new Error('test');87 destination.stdin.destroy(cause);8889 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));90 t.like(await t.throwsAsync(destination), {originalMessage: cause.message, exitCode: 0});91 t.like(await t.throwsAsync(source), {exitCode: 1});92});9394test('Source success -> destination success', async t => {95 const source = execa('noop.js', [foobarString]);96 const destination = execa('stdin.js');97 const pipePromise = source.pipe(destination);9899 t.like(await source, {stdout: foobarString});100 t.like(await destination, {stdout: foobarString});101 t.is(await pipePromise, await destination);102});103104test('Destination stream end -> source failure', async t => {105 const source = execa('noop-repeat.js');106 const destination = execa('stdin.js');107 const pipePromise = source.pipe(destination);108 destination.stdin.end();109110 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));111 await destination;112 t.like(await t.throwsAsync(source), {exitCode: 1});113});114115test('Source normal failure -> destination success', async t => {116 const source = execa('noop-fail.js', ['1', foobarString]);117 const destination = execa('stdin.js');118 const pipePromise = source.pipe(destination);119120 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));121 t.like(await t.throwsAsync(source), {stdout: foobarString, exitCode: 2});122 await destination;123});124125test('Source normal failure -> deep destination success', async t => {126 const source = execa('noop-fail.js', ['1', foobarString]);127 const destination = execa('stdin.js');128 const secondDestination = execa('stdin.js');129 const pipePromise = source.pipe(destination);130 const secondPipePromise = pipePromise.pipe(secondDestination);131132 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));133 t.is(await t.throwsAsync(secondPipePromise), await t.throwsAsync(source));134 t.like(await t.throwsAsync(source), {stdout: foobarString, exitCode: 2});135 t.like(await destination, {stdout: foobarString});136 t.like(await secondDestination, {stdout: foobarString});137});138139const testSourceTerminated = async (t, signal) => {140 const source = execa('noop-repeat.js');141 const destination = execa('stdin.js');142 const pipePromise = source.pipe(destination);143 source.kill(signal);144145 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));146 t.like(await t.throwsAsync(source), {signal});147 await destination;148};149150test('Source SIGTERM -> destination success', testSourceTerminated, 'SIGTERM');151test('Source SIGKILL -> destination success', testSourceTerminated, 'SIGKILL');152153test('Destination success before source -> source success', async t => {154 const passThroughStream = new PassThrough();155 const source = execa('stdin.js', {stdin: ['pipe', passThroughStream]});156 const destination = execa('empty.js');157 const pipePromise = source.pipe(destination);158159 await destination;160 passThroughStream.end();161 await source;162 t.is(await pipePromise, await destination);163});164165test('Destination normal failure -> source failure', async t => {166 const source = execa('noop-repeat.js');167 const destination = execa('fail.js');168 const pipePromise = source.pipe(destination);169170 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));171 t.like(await t.throwsAsync(destination), {exitCode: 2});172 t.like(await t.throwsAsync(source), {exitCode: 1});173});174175test('Destination normal failure -> deep source failure', async t => {176 const source = execa('noop-repeat.js');177 const destination = execa('stdin.js');178 const secondDestination = execa('fail.js');179 const pipePromise = source.pipe(destination);180 const secondPipePromise = pipePromise.pipe(secondDestination);181182 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));183 t.is(await t.throwsAsync(secondPipePromise), await t.throwsAsync(secondDestination));184 t.like(await t.throwsAsync(secondDestination), {exitCode: 2});185 t.like(await t.throwsAsync(destination), {exitCode: 1});186 t.like(await t.throwsAsync(source), {exitCode: 1});187});188189const testDestinationTerminated = async (t, signal) => {190 const source = execa('noop-repeat.js');191 const destination = execa('stdin.js');192 const pipePromise = source.pipe(destination);193 destination.kill(signal);194195 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));196 t.like(await t.throwsAsync(destination), {signal});197 t.like(await t.throwsAsync(source), {exitCode: 1});198};199200test('Destination SIGTERM -> source abort', testDestinationTerminated, 'SIGTERM');201test('Destination SIGKILL -> source abort', testDestinationTerminated, 'SIGKILL');202203test('Source already ended -> ignore source', async t => {204 const source = execa('noop.js', [foobarString]);205 await source;206 const destination = execa('stdin.js');207 const pipePromise = source.pipe(destination);208209 t.is(await pipePromise, await destination);210 t.like(await source, {stdout: foobarString});211 t.like(await destination, {stdout: ''});212});213214test('Source already aborted -> ignore source', async t => {215 const source = execa('noop.js', [foobarString]);216 source.stdout.destroy();217 const destination = execa('stdin.js');218 const pipePromise = source.pipe(destination);219220 t.is(await pipePromise, await destination);221 t.like(await source, {stdout: ''});222 t.like(await destination, {stdout: ''});223});224225test('Source already errored -> failure', async t => {226 const source = execa('noop.js', [foobarString]);227 const cause = new Error('test');228 source.stdout.destroy(cause);229 const destination = execa('stdin.js');230 const pipePromise = source.pipe(destination);231232 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));233 t.like(await t.throwsAsync(source), {cause});234 t.like(await destination, {stdout: ''});235});236237test('Destination already ended -> ignore source', async t => {238 const destination = execa('stdin.js');239 destination.stdin.end('.');240 await destination;241 const source = execa('noop.js', [foobarString]);242 const pipePromise = source.pipe(destination);243244 t.is(await pipePromise, await destination);245 t.like(await destination, {stdout: '.'});246 t.like(await source, {stdout: ''});247});248249test('Destination already aborted -> failure', async t => {250 const destination = execa('stdin.js');251 destination.stdin.destroy();252 t.like(await t.throwsAsync(destination), prematureClose);253 const source = execa('noop.js', [foobarString]);254 const pipePromise = source.pipe(destination);255256 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));257 t.like(await source, {stdout: ''});258});259260test('Destination already errored -> failure', async t => {261 const destination = execa('stdin.js');262 const cause = new Error('test');263 destination.stdin.destroy(cause);264 t.like(await t.throwsAsync(destination), {cause});265 const source = execa('noop.js', [foobarString]);266 const pipePromise = source.pipe(destination);267268 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));269 t.like(await source, {stdout: ''});270});271272test('Source normal failure + destination normal failure', async t => {273 const source = execa('noop-fail.js', ['1', foobarString]);274 const destination = execa('stdin-fail.js');275 const pipePromise = source.pipe(destination);276277 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));278 t.like(await t.throwsAsync(source), {stdout: foobarString, exitCode: 2});279 t.like(await t.throwsAsync(destination), {stdout: foobarString, exitCode: 2});280});281282test('Simultaneous error on source and destination', async t => {283 const source = execa('noop.js', ['']);284 const destination = execa('stdin.js');285 const pipePromise = source.pipe(destination);286287 const sourceCause = new Error(foobarString);Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 288why, one step back288 source.nodeChildProcess.emit('error', sourceCause);Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
line 289why, one step back289 const destinationCause = new Error('other');Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 290why, one step back290 destination.nodeChildProcess.emit('error', destinationCause);Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
lines 291–302why, one step back291292 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));293 t.like(await t.throwsAsync(source), {cause: {originalMessage: sourceCause.originalMessage}});294 t.like(await t.throwsAsync(destination), {cause: {originalMessage: destinationCause.originalMessage}});295});296297test('Does not need to await individual promises', async t => {298 const source = execa('fail.js');299 const destination = execa('fail.js');300 await t.throwsAsync(source.pipe(destination));301});302Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
lines 303–321why, one step back303test.serial('Need to await .pipe() return value', async t => {304 await assertUnhandledPipePromise(t);305});306307test.serial('Need to await .pipe() return value, "all" option', async t => {308 await assertUnhandledPipePromise(t, {destinationOptions: {all: true}});309});310311test.serial('Need to await .pipe() return value after inspecting lazy .all', async t => {312 await assertUnhandledPipePromise(t, {313 destinationOptions: {all: true},314 inspectPipePromise(pipePromise) {315 const descriptor = Object.getOwnPropertyDescriptor(pipePromise, 'all');316 t.is(typeof descriptor.get, 'function');317 },318 });319});320321test.serial('Need to await .pipe() return value after getOneMessage()', async t => {Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
line 322why, one step back322 const source = execa('fail.js');Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
line 323why, one step back323 const destination = execa('ipc-send-twice.js', {ipc: true});Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
line 324why, one step back324 const pipePromise = source.pipe(destination);Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c
lines 325–344why, one step back325 const unhandledRejectionPromise = once(process, 'unhandledRejection');326327 t.is(await pipePromise.getOneMessage(), 'foo');328 await assertUnhandledRejection(t, pipePromise, unhandledRejectionPromise);329 await t.throwsAsync(source);330 await destination;331 await t.throwsAsync(pipePromise);332});333334test.serial('Need to await .pipe() return value after paused getEachMessage()', async t => {335 const source = execa('fail.js');336 const destination = execa('ipc-send-twice.js', {ipc: true});337 const pipePromise = source.pipe(destination);338 const unhandledRejectionPromise = once(process, 'unhandledRejection');339 const iterator = pipePromise.getEachMessage();340341 t.deepEqual(await iterator.next(), {done: false, value: 'foo'});342 await assertUnhandledRejection(t, pipePromise, unhandledRejectionPromise);343 await t.throwsAsync(source);344 await destination;Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
lines 345–354why, one step back345 await t.throwsAsync(pipePromise);346});347348if (isLinux) {349 const testYesHead = async (t, useStdoutTransform, useStdinTransform, all) => {350 const source = execa('yes', {stdout: useStdoutTransform ? noopGenerator(false) : 'pipe', all});351 const destination = execa('head', ['-n', '1'], {stdin: useStdinTransform ? noopGenerator(false) : 'pipe'});352 const pipePromise = source.pipe(destination);353 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));354 t.like(await destination, {stdout: 'y'});Fix flaky testSindre Sorhusbefore adoption522654a
lines 355–357why, one step back355 const sourceError = await t.throwsAsync(source);356 t.is(sourceError.exitCode, 1);357 t.regex(sourceError.stderr, /^yes: standard output: (?:Broken pipe|Connection reset by peer)$/);Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
lines 358–372why, one step back358359 t.false(source.stdout.readableEnded);360 t.is(source.stdout.errored, null);361 t.true(source.stdout.destroyed);362 t.true(source.stderr.readableEnded);363 t.is(source.stderr.errored, null);364 t.true(source.stderr.destroyed);365366 if (all) {367 t.true(source.all.readableEnded);368 t.is(source.all.errored, null);369 t.true(source.all.destroyed);370 }371 };372Fix some randomly failing tests (#1080)ehmickybefore adoption7bf6ac2
lines 373–380why, one step back373 test.serial('Works with yes | head', testYesHead, false, false, false);374 test.serial('Works with yes | head, input transform', testYesHead, false, true, false);375 test.serial('Works with yes | head, output transform', testYesHead, true, false, false);376 test.serial('Works with yes | head, input/output transform', testYesHead, true, true, false);377 test.serial('Works with yes | head, "all" option', testYesHead, false, false, true);378 test.serial('Works with yes | head, "all" option, input transform', testYesHead, false, true, true);379 test.serial('Works with yes | head, "all" option, output transform', testYesHead, true, false, true);380 test.serial('Works with yes | head, "all" option, input/output transform', testYesHead, true, true, true);Add support for `verbose` option to `execaSync()` (#950)ehmickybefore adoptioned052d4
line 381why, one step back381}