throw.js
This file
Path
test/pipe/throw.js
97 lines · 4.3 KiB · JavaScript
Last changed by
Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
lines 1–2why, one step back1import assert from 'node:assert/strict';2import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 3–6why, one step back3import {execa} from '../../index.js';4import {foobarString} from '../helpers/input.js';5import {setFixtureDirectory} from '../helpers/fixtures-directory.js';6import {assertPipeError} from '../helpers/pipe.js';TweaksSindre Sorhusbefore adoption698a1b8
line 7why, one step back7import {assertLike} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 8–10why, one step back89setFixtureDirectory();10TweaksSindre Sorhusbefore adoption698a1b8
line 11why, one step back11test('Destination stream is ended when first argument is invalid', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 12–15why, one step back12 const source = execa('empty.js', {stdout: 'ignore'});13 const destination = execa('stdin.js');14 const pipePromise = source.pipe(destination);15TweaksSindre Sorhusbefore adoption698a1b8
line 16why, one step back16 await assertPipeError(pipePromise, 'option is incompatible');100% test coverage (#1018)ehmickybefore adoption9f65b86
line 17why, one step back17 await source;TweaksSindre Sorhusbefore adoption698a1b8
line 18why, one step back18 assertLike(await destination, {stdout: ''});100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 19–20why, one step back19});20TweaksSindre Sorhusbefore adoption698a1b8
line 21why, one step back21test('Destination stream is ended when first argument is invalid - $', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 22why, one step back22 const pipePromise = execa('empty.js', {stdout: 'ignore'}).pipe`stdin.js`;TweaksSindre Sorhusbefore adoption698a1b8
line 23why, one step back23 await assertPipeError(pipePromise, 'option is incompatible');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 24–25why, one step back24});25TweaksSindre Sorhusbefore adoption698a1b8
line 26why, one step back26test('Source stream is aborted when second argument is invalid', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 27–29why, one step back27 const source = execa('noop.js', [foobarString]);28 const pipePromise = source.pipe(false);29TweaksSindre Sorhusbefore adoption698a1b8
lines 30–31why, one step back30 await assertPipeError(pipePromise, 'an Execa subprocess');31 assertLike(await source, {stdout: ''});100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 32–33why, one step back32});33TweaksSindre Sorhusbefore adoption698a1b8
line 34why, one step back34test('Both arguments might be invalid', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 35–37why, one step back35 const source = execa('empty.js', {stdout: 'ignore'});36 const pipePromise = source.pipe(false);37TweaksSindre Sorhusbefore adoption698a1b8
lines 38–40why, one step back38 await assertPipeError(pipePromise, 'an Execa subprocess');39 const {stdout} = await source;40 assert.equal(stdout, undefined);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 41why, one step back41});Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9
line 42why, one step back42Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
lines 43–65why, one step back43// When a previous `.pipe()` failed because of its arguments, the next `.pipe()` has no source.44// It must reject with that error, without spawning its destination, which would otherwise keep waiting for its `stdin`.45const testChainedInvalidPipe = async (getPipePromise, firstPipeArguments = [false], expectedMessage = 'an Execa subprocess') => {46 const source = execa('noop.js', [foobarString]);47 let isSpawned = false;48 const verbose = (verboseLine, {type}) => {49 if (type === 'command') {50 isSpawned = true;51 }52 };5354 const pipePromise = getPipePromise(source.pipe(...firstPipeArguments), verbose);55 await assertPipeError(pipePromise, expectedMessage);56 assert.equal(isSpawned, false);57 await source;58};5960test('.pipe("file") after invalid arguments', () => testChainedInvalidPipe((pipePromise, verbose) => pipePromise.pipe('stdin.js', {verbose})));61test('.pipe`command` after invalid arguments', () => testChainedInvalidPipe((pipePromise, verbose) => pipePromise.pipe({verbose})`stdin.js`));62test('.pipe.pipe("file") after invalid arguments', () => testChainedInvalidPipe((pipePromise, verbose) => pipePromise.pipe('stdin.js', {verbose}).pipe('stdin.js', {verbose})));63test('.pipe(false) after invalid arguments', () => testChainedInvalidPipe(pipePromise => pipePromise.pipe(false)));64test('.pipe("file") after an invalid "to" option', () => testChainedInvalidPipe((pipePromise, verbose) => pipePromise.pipe('stdin.js', {verbose}), ['empty.js', {to: 'fd9'}], 'fd9'));65Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9
lines 66–67why, one step back66// The destination subprocess is spawned before the `to` option is validated, so its error must be handled.67// This runs in another process, since that is where the unhandled rejection crashes it.TweaksSindre Sorhusbefore adoption698a1b8
line 68why, one step back68test('Destination subprocess error is not unhandled when the "to" option is invalid', async () => {Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9
line 69why, one step back69 const {stdout} = await execa('pipe-destination-error.js', ['fail.js']);TweaksSindre Sorhusbefore adoption698a1b8
line 70why, one step back70 assert.equal(stdout, 'REACHED THE END');Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9
lines 71–72why, one step back71});72TweaksSindre Sorhusbefore adoption698a1b8
line 73why, one step back73test('Destination subprocess is terminated when the "to" option is invalid', async () => {Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9
line 74why, one step back74 const {stdout} = await execa('pipe-destination-error.js', ['stdin.js'], {timeout: 10_000});TweaksSindre Sorhusbefore adoption698a1b8
line 75why, one step back75 assert.equal(stdout, 'REACHED THE END');Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9
lines 76–77why, one step back76});77TweaksSindre Sorhusbefore adoption698a1b8
line 78why, one step back78test('Destination subprocess passed by the user is not terminated when the "to" option is invalid', async () => {Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9
lines 79–82why, one step back79 const source = execa('empty.js', {stdout: 'ignore'});80 const destination = execa('stdin.js');81 const pipePromise = source.pipe(destination, {to: 'fd9'});82TweaksSindre Sorhusbefore adoption698a1b8
line 83why, one step back83 await assertPipeError(pipePromise, 'fd9');Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9
lines 84–85why, one step back84 destination.stdin.end(foobarString);85 const {stdout} = await destination;TweaksSindre Sorhusbefore adoption698a1b8
line 86why, one step back86 assert.equal(stdout, foobarString);Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9
line 87why, one step back87});Report `null` pipe options as a rejectionSindre Sorhusbefore adoptionecda56b
lines 88–97why, one step back8889test('The pipe options cannot be null', async () => {90 const source = execa('empty.js');91 const destination = execa('stdin.js');92 const pipePromise = source.pipe(destination, null);9394 await assertPipeError(pipePromise, 'not `null`');95 destination.stdin.end();96 await Promise.all([source, destination]);97});