sequence.js
This file
Path
test/pipe/sequence.js
400 lines · 15.1 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
1import assert from 'node:assert/strict';2import {once} from 'node:events';3import process from 'node:process';4import {PassThrough} from 'node:stream';5import {setTimeout} from 'node:timers/promises';6import test from 'node:test';7import {assertRejects, assertLike} from '../helpers/assert.js';8import {execa} from '../../index.js';9import {setFixtureDirectory} from '../helpers/fixtures-directory.js';10import {foobarString} from '../helpers/input.js';11import {noopGenerator} from '../helpers/generator.js';12import {prematureClose} from '../helpers/stdio.js';1314setFixtureDirectory();1516const isLinux = process.platform === 'linux';17const timeoutSymbol = Symbol('timeout');1819// The `unhandledRejection` event is what these tests assert on, but `node:test` also listens to it20// and fails any test during which it fires. That listener is removed while the event is observed,21// since the event itself is what `assertUnhandledRejection()` asserts on.22const observeUnhandledRejection = async () => {23 const testRunnerListeners = process.listeners('unhandledRejection');24 for (const listener of testRunnerListeners) {25 process.removeListener('unhandledRejection', listener);26 }2728 try {29 return await once(process, 'unhandledRejection');30 } finally {31 for (const listener of testRunnerListeners) {32 process.on('unhandledRejection', listener);33 }34 }35};3637const assertUnhandledRejection = async pipePromise => {38 const result = await Promise.race([39 observeUnhandledRejection(),40 setTimeout(5000, timeoutSymbol),41 ]);4243 assert.notEqual(result, timeoutSymbol);4445 if (result === timeoutSymbol) {46 return;47 }4849 const [reason, unhandledPromise] = result;50 assert.equal(unhandledPromise, pipePromise);51 assert.match(reason.message, /Command failed with exit code 2/);52};5354const assertUnhandledPipePromise = async ({destinationOptions = {}, inspectPipePromise} = {}) => {55 const source = execa('fail.js');56 const destination = execa('fail.js', destinationOptions);57 const pipePromise = source.pipe(destination);58 await inspectPipePromise?.(pipePromise);59 await assertUnhandledRejection(pipePromise);60 await Promise.all([61 assertRejects(source),62 assertRejects(destination),63 ]);64 await assertRejects(pipePromise);65};6667test('Source stream abort -> destination success', async () => {68 const source = execa('noop-repeat.js');69 const destination = execa('stdin.js');70 const pipePromise = source.pipe(destination);71 source.stdout.destroy();7273 assert.equal(await assertRejects(pipePromise), await assertRejects(source));74 assertLike(await assertRejects(source), {exitCode: 1});75 await destination;76});7778test('Source stream error -> destination success', async () => {79 const source = execa('noop-repeat.js');80 const destination = execa('stdin.js');81 const pipePromise = source.pipe(destination);82 const cause = new Error('test');83 source.stdout.destroy(cause);8485 assert.equal(await assertRejects(pipePromise), await assertRejects(source));86 assertLike(await assertRejects(source), {originalMessage: cause.message, exitCode: 1});87 await destination;88});8990test('Destination stream abort -> source failure', async () => {91 const source = execa('noop-repeat.js');92 const destination = execa('stdin.js');93 const pipePromise = source.pipe(destination);94 destination.stdin.destroy();9596 assert.equal(await assertRejects(pipePromise), await assertRejects(destination));97 assertLike(await assertRejects(destination), prematureClose);98 assertLike(await assertRejects(source), {exitCode: 1});99});100101test('Destination stream error -> source failure', async () => {102 const source = execa('noop-repeat.js');103 const destination = execa('stdin.js');104 const pipePromise = source.pipe(destination);105 const cause = new Error('test');106 destination.stdin.destroy(cause);107108 assert.equal(await assertRejects(pipePromise), await assertRejects(destination));109 assertLike(await assertRejects(destination), {originalMessage: cause.message, exitCode: 0});110 assertLike(await assertRejects(source), {exitCode: 1});111});112113test('Source success -> destination success', async () => {114 const source = execa('noop.js', [foobarString]);115 const destination = execa('stdin.js');116 const pipePromise = source.pipe(destination);117118 assertLike(await source, {stdout: foobarString});119 assertLike(await destination, {stdout: foobarString});120 assert.equal(await pipePromise, await destination);121});122123test('Destination stream end -> source failure', async () => {124 const source = execa('noop-repeat.js');125 const destination = execa('stdin.js');126 const pipePromise = source.pipe(destination);127 destination.stdin.end();128129 assert.equal(await assertRejects(pipePromise), await assertRejects(source));130 await destination;131 assertLike(await assertRejects(source), {exitCode: 1});132});133134test('Source normal failure -> destination success', async () => {135 const source = execa('noop-fail.js', ['1', foobarString]);136 const destination = execa('stdin.js');137 const pipePromise = source.pipe(destination);138139 assert.equal(await assertRejects(pipePromise), await assertRejects(source));140 assertLike(await assertRejects(source), {stdout: foobarString, exitCode: 2});141 await destination;142});143144test('Source normal failure -> deep destination success', async () => {145 const source = execa('noop-fail.js', ['1', foobarString]);146 const destination = execa('stdin.js');147 const secondDestination = execa('stdin.js');148 const pipePromise = source.pipe(destination);149 const secondPipePromise = pipePromise.pipe(secondDestination);150151 assert.equal(await assertRejects(pipePromise), await assertRejects(source));152 assert.equal(await assertRejects(secondPipePromise), await assertRejects(source));153 assertLike(await assertRejects(source), {stdout: foobarString, exitCode: 2});154 assertLike(await destination, {stdout: foobarString});155 assertLike(await secondDestination, {stdout: foobarString});156});157158const testSourceTerminated = async signal => {159 const source = execa('noop-repeat.js');160 const destination = execa('stdin.js');161 const pipePromise = source.pipe(destination);162 source.kill(signal);163164 assert.equal(await assertRejects(pipePromise), await assertRejects(source));165 assertLike(await assertRejects(source), {signal});166 await destination;167};168169test('Source SIGTERM -> destination success', () => testSourceTerminated('SIGTERM'));170test('Source SIGKILL -> destination success', () => testSourceTerminated('SIGKILL'));171172test('Destination success before source -> source success', async () => {173 const passThroughStream = new PassThrough();174 const source = execa('stdin.js', {stdin: ['pipe', passThroughStream]});175 const destination = execa('empty.js');176 const pipePromise = source.pipe(destination);177178 await destination;179 passThroughStream.end();180 await source;181 assert.equal(await pipePromise, await destination);182});183184test('Destination normal failure -> source failure', async () => {185 const source = execa('noop-repeat.js');186 const destination = execa('fail.js');187 const pipePromise = source.pipe(destination);188189 assert.equal(await assertRejects(pipePromise), await assertRejects(destination));190 assertLike(await assertRejects(destination), {exitCode: 2});191 assertLike(await assertRejects(source), {exitCode: 1});192});193194test('Destination normal failure -> deep source failure', async () => {195 const source = execa('noop-repeat.js');196 const destination = execa('stdin.js');197 const secondDestination = execa('fail.js');198 const pipePromise = source.pipe(destination);199 const secondPipePromise = pipePromise.pipe(secondDestination);200201 assert.equal(await assertRejects(pipePromise), await assertRejects(destination));202 assert.equal(await assertRejects(secondPipePromise), await assertRejects(secondDestination));203 assertLike(await assertRejects(secondDestination), {exitCode: 2});204 assertLike(await assertRejects(destination), {exitCode: 1});205 assertLike(await assertRejects(source), {exitCode: 1});206});207208const testDestinationTerminated = async signal => {209 const source = execa('noop-repeat.js');210 const destination = execa('stdin.js');211 const pipePromise = source.pipe(destination);212 destination.kill(signal);213214 assert.equal(await assertRejects(pipePromise), await assertRejects(destination));215 assertLike(await assertRejects(destination), {signal});216 assertLike(await assertRejects(source), {exitCode: 1});217};218219test('Destination SIGTERM -> source abort', () => testDestinationTerminated('SIGTERM'));220test('Destination SIGKILL -> source abort', () => testDestinationTerminated('SIGKILL'));221222test('Source already ended -> ignore source', async () => {223 const source = execa('noop.js', [foobarString]);224 await source;225 const destination = execa('stdin.js');226 const pipePromise = source.pipe(destination);227228 assert.equal(await pipePromise, await destination);229 assertLike(await source, {stdout: foobarString});230 assertLike(await destination, {stdout: ''});231});232233test('Source already aborted -> ignore source', async () => {234 const source = execa('noop.js', [foobarString]);235 source.stdout.destroy();236 const destination = execa('stdin.js');237 const pipePromise = source.pipe(destination);238239 assert.equal(await pipePromise, await destination);240 assertLike(await source, {stdout: ''});241 assertLike(await destination, {stdout: ''});242});243244test('Source already errored -> failure', async () => {245 const source = execa('noop.js', [foobarString]);246 const cause = new Error('test');247 source.stdout.destroy(cause);248 const destination = execa('stdin.js');249 const pipePromise = source.pipe(destination);250251 assert.equal(await assertRejects(pipePromise), await assertRejects(source));252 assertLike(await assertRejects(source), {cause});253 assertLike(await destination, {stdout: ''});254});255256test('Destination already ended -> ignore source', async () => {257 const destination = execa('stdin.js');258 destination.stdin.end('.');259 await destination;260 const source = execa('noop.js', [foobarString]);261 const pipePromise = source.pipe(destination);262263 assert.equal(await pipePromise, await destination);264 assertLike(await destination, {stdout: '.'});265 assertLike(await source, {stdout: ''});266});267268test('Destination already aborted -> failure', async () => {269 const destination = execa('stdin.js');270 destination.stdin.destroy();271 assertLike(await assertRejects(destination), prematureClose);272 const source = execa('noop.js', [foobarString]);273 const pipePromise = source.pipe(destination);274275 assert.equal(await assertRejects(pipePromise), await assertRejects(destination));276 assertLike(await source, {stdout: ''});277});278279test('Destination already errored -> failure', async () => {280 const destination = execa('stdin.js');281 const cause = new Error('test');282 destination.stdin.destroy(cause);283 assertLike(await assertRejects(destination), {cause});284 const source = execa('noop.js', [foobarString]);285 const pipePromise = source.pipe(destination);286287 assert.equal(await assertRejects(pipePromise), await assertRejects(destination));288 assertLike(await source, {stdout: ''});289});290291test('Source normal failure + destination normal failure', async () => {292 const source = execa('noop-fail.js', ['1', foobarString]);293 const destination = execa('stdin-fail.js');294 const pipePromise = source.pipe(destination);295296 assert.equal(await assertRejects(pipePromise), await assertRejects(destination));297 assertLike(await assertRejects(source), {stdout: foobarString, exitCode: 2});298 assertLike(await assertRejects(destination), {stdout: foobarString, exitCode: 2});299});300301test('Simultaneous error on source and destination', async () => {302 const source = execa('noop.js', ['']);303 const destination = execa('stdin.js');304 const pipePromise = source.pipe(destination);305306 const sourceCause = new Error(foobarString);307 source.nodeChildProcess.emit('error', sourceCause);308 const destinationCause = new Error('other');309 destination.nodeChildProcess.emit('error', destinationCause);310311 assert.equal(await assertRejects(pipePromise), await assertRejects(destination));312 assertLike(await assertRejects(source), {cause: sourceCause});313 assertLike(await assertRejects(destination), {cause: destinationCause});314});315316test('Does not need to await individual promises', async () => {317 const source = execa('fail.js');318 const destination = execa('fail.js');319 await assertRejects(source.pipe(destination));320});321322test('Need to await .pipe() return value', async () => {323 await assertUnhandledPipePromise();324});325326test('Need to await .pipe() return value, "all" option', async () => {327 await assertUnhandledPipePromise({destinationOptions: {all: true}});328});329330test('Need to await .pipe() return value after inspecting lazy .all', async () => {331 await assertUnhandledPipePromise({332 destinationOptions: {all: true},333 inspectPipePromise(pipePromise) {334 const descriptor = Object.getOwnPropertyDescriptor(pipePromise, 'all');335 assert.equal(typeof descriptor.get, 'function');336 },337 });338});339340test('Need to await .pipe() return value after getOneMessage()', async () => {341 const source = execa('fail.js');342 const destination = execa('ipc-send-twice.js', {ipc: true});343 const pipePromise = source.pipe(destination);344345 assert.equal(await pipePromise.getOneMessage(), 'foo');346 await assertUnhandledRejection(pipePromise);347 await assertRejects(source);348 await destination;349 await assertRejects(pipePromise);350});351352test('Need to await .pipe() return value after paused getEachMessage()', async () => {353 const source = execa('fail.js');354 const destination = execa('ipc-send-twice.js', {ipc: true});355 const pipePromise = source.pipe(destination);356 const iterator = pipePromise.getEachMessage();357358 assert.deepEqual(await iterator.next(), {done: false, value: 'foo'});359 await assertUnhandledRejection(pipePromise);360 await assertRejects(source);361 await destination;362 await assertRejects(pipePromise);363});364365if (isLinux) {366 const testYesHead = async (useStdoutTransform, useStdinTransform, all) => {367 const source = execa('yes', {stdout: useStdoutTransform ? noopGenerator(false) : 'pipe', all});368 const destination = execa('head', ['-n', '1'], {stdin: useStdinTransform ? noopGenerator(false) : 'pipe'});369 const pipePromise = source.pipe(destination);370 assert.equal(await assertRejects(pipePromise), await assertRejects(source));371 assertLike(await destination, {stdout: 'y'});372 const sourceError = await assertRejects(source);373 assert.equal(sourceError.exitCode, 1);374 assert.match(sourceError.stderr, /^yes: standard output: (?:Broken pipe|Connection reset by peer)$/);375376 assert.equal(source.stdout.readableEnded, false);377 assert.equal(source.stdout.errored, null);378 assert.equal(source.stdout.destroyed, true);379 assert.equal(source.stderr.readableEnded, true);380 assert.equal(source.stderr.errored, null);381 assert.equal(source.stderr.destroyed, true);382383 if (!all) {384 return;385 }386387 assert.equal(source.all.readableEnded, true);388 assert.equal(source.all.errored, null);389 assert.equal(source.all.destroyed, true);390 };391392 test('Works with yes | head', () => testYesHead(false, false, false));393 test('Works with yes | head, input transform', () => testYesHead(false, true, false));394 test('Works with yes | head, output transform', () => testYesHead(true, false, false));395 test('Works with yes | head, input/output transform', () => testYesHead(true, true, false));396 test('Works with yes | head, "all" option', () => testYesHead(false, false, true));397 test('Works with yes | head, "all" option, input transform', () => testYesHead(false, true, true));398 test('Works with yes | head, "all" option, output transform', () => testYesHead(true, false, true));399 test('Works with yes | head, "all" option, input/output transform', () => testYesHead(true, true, true));400}