sequence.js
This file
Path
test/pipe/sequence.js
313 lines · 11.8 KiB · JavaScript
Last changed by
Upgrade xo (#980)ehmickybefore adoptionccfa412
History of this fileTake it
Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 1–5why, one step back1import {once} from 'node:events';2import process from 'node:process';3import {PassThrough} from 'node:stream';4import test from 'ava';5import {execa} from '../../index.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
line 6why, one step back6import {setFixtureDirectory} from '../helpers/fixtures-directory.js';Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 7–10why, one step back7import {foobarString} from '../helpers/input.js';8import {noopGenerator} from '../helpers/generator.js';9import {prematureClose} from '../helpers/stdio.js';10Upgrade xo (#980)ehmickybefore adoptionccfa412
line 11why, one step back11setFixtureDirectory();Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 12–29why, one step back1213const isLinux = process.platform === 'linux';1415test('Source stream abort -> destination success', async t => {16 const source = execa('noop-repeat.js');17 const destination = execa('stdin.js');18 const pipePromise = source.pipe(destination);19 source.stdout.destroy();2021 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));22 t.like(await t.throwsAsync(source), {exitCode: 1});23 await destination;24});2526test('Source stream error -> destination success', async t => {27 const source = execa('noop-repeat.js');28 const destination = execa('stdin.js');29 const pipePromise = source.pipe(destination);Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 30–31why, one step back30 const cause = new Error('test');31 source.stdout.destroy(cause);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 32–33why, one step back3233 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 34why, one step back34 t.like(await t.throwsAsync(source), {originalMessage: cause.message, exitCode: 1});Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 35–52why, one step back35 await destination;36});3738test('Destination stream abort -> source failure', async t => {39 const source = execa('noop-repeat.js');40 const destination = execa('stdin.js');41 const pipePromise = source.pipe(destination);42 destination.stdin.destroy();4344 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));45 t.like(await t.throwsAsync(destination), prematureClose);46 t.like(await t.throwsAsync(source), {exitCode: 1});47});4849test('Destination stream error -> source failure', async t => {50 const source = execa('noop-repeat.js');51 const destination = execa('stdin.js');52 const pipePromise = source.pipe(destination);Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 53–54why, one step back53 const cause = new Error('test');54 destination.stdin.destroy(cause);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 55–56why, one step back5556 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 57why, one step back57 t.like(await t.throwsAsync(destination), {originalMessage: cause.message, exitCode: 0});Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 58–96why, one step back58 t.like(await t.throwsAsync(source), {exitCode: 1});59});6061test('Source success -> destination success', async t => {62 const source = execa('noop.js', [foobarString]);63 const destination = execa('stdin.js');64 const pipePromise = source.pipe(destination);6566 t.like(await source, {stdout: foobarString});67 t.like(await destination, {stdout: foobarString});68 t.is(await pipePromise, await destination);69});7071test('Destination stream end -> 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.end();7677 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));78 await destination;79 t.like(await t.throwsAsync(source), {exitCode: 1});80});8182test('Source normal failure -> destination success', async t => {83 const source = execa('noop-fail.js', ['1', foobarString]);84 const destination = execa('stdin.js');85 const pipePromise = source.pipe(destination);8687 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));88 t.like(await t.throwsAsync(source), {stdout: foobarString, exitCode: 2});89 await destination;90});9192test('Source normal failure -> deep destination success', async t => {93 const source = execa('noop-fail.js', ['1', foobarString]);94 const destination = execa('stdin.js');95 const secondDestination = execa('stdin.js');96 const pipePromise = source.pipe(destination);Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa
line 97why, one step back97 const secondPipePromise = pipePromise.pipe(secondDestination);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 98–99why, one step back9899 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa
line 100why, one step back100 t.is(await t.throwsAsync(secondPipePromise), await t.throwsAsync(source));Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 101why, one step back101 t.like(await t.throwsAsync(source), {stdout: foobarString, exitCode: 2});Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa
lines 102–103why, one step back102 t.like(await destination, {stdout: foobarString});103 t.like(await secondDestination, {stdout: foobarString});Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 104–146why, one step back104});105106const testSourceTerminated = async (t, signal) => {107 const source = execa('noop-repeat.js');108 const destination = execa('stdin.js');109 const pipePromise = source.pipe(destination);110 source.kill(signal);111112 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));113 t.like(await t.throwsAsync(source), {signal});114 await destination;115};116117test('Source SIGTERM -> destination success', testSourceTerminated, 'SIGTERM');118test('Source SIGKILL -> destination success', testSourceTerminated, 'SIGKILL');119120test('Destination success before source -> source success', async t => {121 const passThroughStream = new PassThrough();122 const source = execa('stdin.js', {stdin: ['pipe', passThroughStream]});123 const destination = execa('empty.js');124 const pipePromise = source.pipe(destination);125126 await destination;127 passThroughStream.end();128 await source;129 t.is(await pipePromise, await destination);130});131132test('Destination normal failure -> source failure', async t => {133 const source = execa('noop-repeat.js');134 const destination = execa('fail.js');135 const pipePromise = source.pipe(destination);136137 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));138 t.like(await t.throwsAsync(destination), {exitCode: 2});139 t.like(await t.throwsAsync(source), {exitCode: 1});140});141142test('Destination normal failure -> deep source failure', async t => {143 const source = execa('noop-repeat.js');144 const destination = execa('stdin.js');145 const secondDestination = execa('fail.js');146 const pipePromise = source.pipe(destination);Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa
line 147why, one step back147 const secondPipePromise = pipePromise.pipe(secondDestination);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 148–193why, one step back148149 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));150 t.is(await t.throwsAsync(secondPipePromise), await t.throwsAsync(secondDestination));151 t.like(await t.throwsAsync(secondDestination), {exitCode: 2});152 t.like(await t.throwsAsync(destination), {exitCode: 1});153 t.like(await t.throwsAsync(source), {exitCode: 1});154});155156const testDestinationTerminated = async (t, signal) => {157 const source = execa('noop-repeat.js');158 const destination = execa('stdin.js');159 const pipePromise = source.pipe(destination);160 destination.kill(signal);161162 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));163 t.like(await t.throwsAsync(destination), {signal});164 t.like(await t.throwsAsync(source), {exitCode: 1});165};166167test('Destination SIGTERM -> source abort', testDestinationTerminated, 'SIGTERM');168test('Destination SIGKILL -> source abort', testDestinationTerminated, 'SIGKILL');169170test('Source already ended -> ignore source', async t => {171 const source = execa('noop.js', [foobarString]);172 await source;173 const destination = execa('stdin.js');174 const pipePromise = source.pipe(destination);175176 t.is(await pipePromise, await destination);177 t.like(await source, {stdout: foobarString});178 t.like(await destination, {stdout: ''});179});180181test('Source already aborted -> ignore source', async t => {182 const source = execa('noop.js', [foobarString]);183 source.stdout.destroy();184 const destination = execa('stdin.js');185 const pipePromise = source.pipe(destination);186187 t.is(await pipePromise, await destination);188 t.like(await source, {stdout: ''});189 t.like(await destination, {stdout: ''});190});191192test('Source already errored -> failure', async t => {193 const source = execa('noop.js', [foobarString]);Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 194–195why, one step back194 const cause = new Error('test');195 source.stdout.destroy(cause);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 196–199why, one step back196 const destination = execa('stdin.js');197 const pipePromise = source.pipe(destination);198199 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 200why, one step back200 t.like(await t.throwsAsync(source), {cause});Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 201–228why, one step back201 t.like(await destination, {stdout: ''});202});203204test('Destination already ended -> ignore source', async t => {205 const destination = execa('stdin.js');206 destination.stdin.end('.');207 await destination;208 const source = execa('noop.js', [foobarString]);209 const pipePromise = source.pipe(destination);210211 t.is(await pipePromise, await destination);212 t.like(await destination, {stdout: '.'});213 t.like(await source, {stdout: ''});214});215216test('Destination already aborted -> failure', async t => {217 const destination = execa('stdin.js');218 destination.stdin.destroy();219 t.like(await t.throwsAsync(destination), prematureClose);220 const source = execa('noop.js', [foobarString]);221 const pipePromise = source.pipe(destination);222223 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));224 t.like(await source, {stdout: ''});225});226227test('Destination already errored -> failure', async t => {228 const destination = execa('stdin.js');Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 229–231why, one step back229 const cause = new Error('test');230 destination.stdin.destroy(cause);231 t.like(await t.throwsAsync(destination), {cause});Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 232–253why, one step back232 const source = execa('noop.js', [foobarString]);233 const pipePromise = source.pipe(destination);234235 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));236 t.like(await source, {stdout: ''});237});238239test('Source normal failure + destination normal failure', async t => {240 const source = execa('noop-fail.js', ['1', foobarString]);241 const destination = execa('stdin-fail.js');242 const pipePromise = source.pipe(destination);243244 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));245 t.like(await t.throwsAsync(source), {stdout: foobarString, exitCode: 2});246 t.like(await t.throwsAsync(destination), {stdout: foobarString, exitCode: 2});247});248249test('Simultaneous error on source and destination', async t => {250 const source = execa('noop.js', ['']);251 const destination = execa('stdin.js');252 const pipePromise = source.pipe(destination);253Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 254–257why, one step back254 const sourceCause = new Error(foobarString);255 source.emit('error', sourceCause);256 const destinationCause = new Error('other');257 destination.emit('error', destinationCause);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 258–259why, one step back258259 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 260–261why, one step back260 t.like(await t.throwsAsync(source), {cause: {originalMessage: sourceCause.originalMessage}});261 t.like(await t.throwsAsync(destination), {cause: {originalMessage: destinationCause.originalMessage}});Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 262–313why, one step back262});263264test('Does not need to await individual promises', async t => {265 const source = execa('fail.js');266 const destination = execa('fail.js');267 await t.throwsAsync(source.pipe(destination));268});269270test('Need to await .pipe() return value', async t => {271 const source = execa('fail.js');272 const destination = execa('fail.js');273 const pipePromise = source.pipe(destination);274 await Promise.all([275 once(process, 'unhandledRejection'),276 t.throwsAsync(source),277 t.throwsAsync(destination),278 ]);279 await t.throwsAsync(pipePromise);280});281282if (isLinux) {283 const testYesHead = async (t, useStdoutTransform, useStdinTransform, all) => {284 const source = execa('yes', {stdout: useStdoutTransform ? noopGenerator(false) : 'pipe', all});285 const destination = execa('head', ['-n', '1'], {stdin: useStdinTransform ? noopGenerator(false) : 'pipe'});286 const pipePromise = source.pipe(destination);287 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));288 t.like(await destination, {stdout: 'y'});289 t.like(await t.throwsAsync(source), {exitCode: 1, stderr: 'yes: standard output: Connection reset by peer'});290291 t.false(source.stdout.readableEnded);292 t.is(source.stdout.errored, null);293 t.true(source.stdout.destroyed);294 t.true(source.stderr.readableEnded);295 t.is(source.stderr.errored, null);296 t.true(source.stderr.destroyed);297298 if (all) {299 t.true(source.all.readableEnded);300 t.is(source.all.errored, null);301 t.true(source.all.destroyed);302 }303 };304305 test('Works with yes | head', testYesHead, false, false, false);306 test('Works with yes | head, input transform', testYesHead, false, true, false);307 test('Works with yes | head, output transform', testYesHead, true, false, false);308 test('Works with yes | head, input/output transform', testYesHead, true, true, false);309 test('Works with yes | head, "all" option', testYesHead, false, false, true);310 test('Works with yes | head, "all" option, input transform', testYesHead, false, true, true);311 test('Works with yes | head, "all" option, output transform', testYesHead, true, false, true);312 test('Works with yes | head, "all" option, input/output transform', testYesHead, true, true, true);313}