Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–2why, one step back
1import assert from 'node:assert/strict';
2import test from 'node:test';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 3–6why, one step back
3import {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 back
7import {assertLike} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 8–10why, one step back
8
9setFixtureDirectory();10

TweaksSindre Sorhusbefore adoption698a1b8

line 11why, one step back
11test('Destination stream is ended when first argument is invalid', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 12–15why, one step back
12 const source = execa('empty.js', {stdout: 'ignore'});
13 const destination = execa('stdin.js');14 const pipePromise = source.pipe(destination);15

TweaksSindre Sorhusbefore adoption698a1b8

line 16why, one step back
16 await assertPipeError(pipePromise, 'option is incompatible');

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 17why, one step back
17 await source;

TweaksSindre Sorhusbefore adoption698a1b8

line 18why, one step back
18 assertLike(await destination, {stdout: ''});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 19–20why, one step back
19});
20

TweaksSindre Sorhusbefore adoption698a1b8

line 21why, one step back
21test('Destination stream is ended when first argument is invalid - $', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 22why, one step back
22 const pipePromise = execa('empty.js', {stdout: 'ignore'}).pipe`stdin.js`;

TweaksSindre Sorhusbefore adoption698a1b8

line 23why, one step back
23 await assertPipeError(pipePromise, 'option is incompatible');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 24–25why, one step back
24});
25

TweaksSindre Sorhusbefore adoption698a1b8

line 26why, one step back
26test('Source stream is aborted when second argument is invalid', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 27–29why, one step back
27 const source = execa('noop.js', [foobarString]);
28 const pipePromise = source.pipe(false);29

TweaksSindre Sorhusbefore adoption698a1b8

lines 30–31why, one step back
30 await assertPipeError(pipePromise, 'an Execa subprocess');
31 assertLike(await source, {stdout: ''});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 32–33why, one step back
32});
33

TweaksSindre Sorhusbefore adoption698a1b8

line 34why, one step back
34test('Both arguments might be invalid', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 35–37why, one step back
35 const source = execa('empty.js', {stdout: 'ignore'});
36 const pipePromise = source.pipe(false);37

TweaksSindre Sorhusbefore adoption698a1b8

lines 38–40why, one step back
38 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 back
41});

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

line 42why, one step back
42

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

lines 43–65why, one step back
43// 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'));65

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

lines 66–67why, one step back
66// 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 back
68test('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 back
69 const {stdout} = await execa('pipe-destination-error.js', ['fail.js']);

TweaksSindre Sorhusbefore adoption698a1b8

line 70why, one step back
70 assert.equal(stdout, 'REACHED THE END');

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

lines 71–72why, one step back
71});
72

TweaksSindre Sorhusbefore adoption698a1b8

line 73why, one step back
73test('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 back
74 const {stdout} = await execa('pipe-destination-error.js', ['stdin.js'], {timeout: 10_000});

TweaksSindre Sorhusbefore adoption698a1b8

line 75why, one step back
75 assert.equal(stdout, 'REACHED THE END');

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

lines 76–77why, one step back
76});
77

TweaksSindre Sorhusbefore adoption698a1b8

line 78why, one step back
78test('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 back
79 const source = execa('empty.js', {stdout: 'ignore'});
80 const destination = execa('stdin.js');81 const pipePromise = source.pipe(destination, {to: 'fd9'});82

TweaksSindre Sorhusbefore adoption698a1b8

line 83why, one step back
83 await assertPipeError(pipePromise, 'fd9');

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

lines 84–85why, one step back
84 destination.stdin.end(foobarString);
85 const {stdout} = await destination;

TweaksSindre Sorhusbefore adoption698a1b8

line 86why, one step back
86 assert.equal(stdout, foobarString);

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

line 87why, one step back
87});

Report `null` pipe options as a rejectionSindre Sorhusbefore adoptionecda56b

lines 88–97why, one step back
88
89test('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});