Skip to content
Myr

abort.js

This file

Path

test/pipe/abort.js

199 lines · 7.2 KiB · JavaScript

Last changed by

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

History of this file

Take it

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

TweaksSindre Sorhusbefore adoption698a1b8

line 1why, one step back
1import assert from 'node:assert/strict';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 2why, one step back
2import {once} from 'node:events';

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 3why, one step back
3import {setTimeout} from 'node:timers/promises';

TweaksSindre Sorhusbefore adoption698a1b8

lines 4–5why, one step back
4import test from 'node:test';
5import {assertRejects, assertLike} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 6–11why, one step back
6import {execa} from '../../index.js';
7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8import {foobarString} from '../helpers/input.js';910setFixtureDirectory();11

TweaksSindre Sorhusbefore adoption698a1b8

lines 12–13why, one step back
12const assertUnPipeError = async pipePromise => {
13 const error = await assertRejects(pipePromise);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 14why, one step back
14

TweaksSindre Sorhusbefore adoption698a1b8

lines 15–16why, one step back
15 assert.equal(error.command, 'source.pipe(destination)');
16 assert.equal(error.escapedCommand, error.command);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 17why, one step back
17

TweaksSindre Sorhusbefore adoption698a1b8

lines 18–30why, one step back
18 assert.equal(typeof error.cwd, 'string');
19 assert.equal(error.failed, true);20 assert.equal(error.timedOut, false);21 assert.equal(error.isCanceled, false);22 assert.equal(error.isTerminated, false);23 assert.equal(error.exitCode, undefined);24 assert.equal(error.signal, undefined);25 assert.equal(error.signalDescription, undefined);26 assert.equal(error.stdout, undefined);27 assert.equal(error.stderr, undefined);28 assert.equal(error.all, undefined);29 assert.deepEqual(error.stdio, Array.from({length: error.stdio.length}));30 assert.deepEqual(error.pipedFrom, []);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 31why, one step back
31

TweaksSindre Sorhusbefore adoption698a1b8

lines 32–35why, one step back
32 assert.ok(error.originalMessage.includes('Pipe canceled'));
33 assert.ok(error.shortMessage.includes(`Command failed: ${error.command}`));34 assert.ok(error.shortMessage.includes(error.originalMessage));35 assert.ok(error.message.includes(error.shortMessage));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 36–37why, one step back
36};
37

TweaksSindre Sorhusbefore adoption698a1b8

line 38why, one step back
38test('Can unpipe a single subprocess', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 39–44why, one step back
39 const abortController = new AbortController();
40 const source = execa('stdin.js');41 const destination = execa('stdin.js');42 const pipePromise = source.pipe(destination, {unpipeSignal: abortController.signal});4344 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

line 45why, one step back
45 await assertUnPipeError(pipePromise);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 46–49why, one step back
46
47 source.stdin.end(foobarString);48 destination.stdin.end('.');49

TweaksSindre Sorhusbefore adoption698a1b8

lines 50–51why, one step back
50 assertLike(await destination, {stdout: '.'});
51 assertLike(await source, {stdout: foobarString});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 52–53why, one step back
52});
53

TweaksSindre Sorhusbefore adoption698a1b8

line 54why, one step back
54test('Can use an already aborted signal', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 55–60why, one step back
55 const abortController = new AbortController();
56 abortController.abort();57 const source = execa('empty.js');58 const destination = execa('empty.js');59 const pipePromise = source.pipe(destination, {unpipeSignal: abortController.signal});60

TweaksSindre Sorhusbefore adoption698a1b8

line 61why, one step back
61 await assertUnPipeError(pipePromise);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 62–63why, one step back
62});
63

TweaksSindre Sorhusbefore adoption698a1b8

line 64why, one step back
64test('Can unpipe a subprocess among other sources', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 65–72why, one step back
65 const abortController = new AbortController();
66 const source = execa('stdin.js');67 const secondSource = execa('noop.js', [foobarString]);68 const destination = execa('stdin.js');69 const pipePromise = source.pipe(destination, {unpipeSignal: abortController.signal});70 const secondPipePromise = secondSource.pipe(destination);7172 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

line 73why, one step back
73 await assertUnPipeError(pipePromise);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 74–76why, one step back
74
75 source.stdin.end('.');76

TweaksSindre Sorhusbefore adoption698a1b8

lines 77–80why, one step back
77 assert.equal(await secondPipePromise, await destination);
78 assertLike(await destination, {stdout: foobarString});79 assertLike(await source, {stdout: '.'});80 assertLike(await secondSource, {stdout: foobarString});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 81–82why, one step back
81});
82

TweaksSindre Sorhusbefore adoption698a1b8

line 83why, one step back
83test('Can unpipe a subprocess among other sources on the same subprocess', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 84–90why, one step back
84 const abortController = new AbortController();
85 const source = execa('stdin-both.js');86 const destination = execa('stdin.js');87 const pipePromise = source.pipe(destination, {unpipeSignal: abortController.signal});88 const secondPipePromise = source.pipe(destination, {from: 'stderr'});8990 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

line 91why, one step back
91 await assertUnPipeError(pipePromise);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 92–94why, one step back
92
93 source.stdin.end(foobarString);94

TweaksSindre Sorhusbefore adoption698a1b8

lines 95–97why, one step back
95 assert.equal(await secondPipePromise, await destination);
96 assertLike(await destination, {stdout: foobarString});97 assertLike(await source, {stdout: foobarString, stderr: foobarString});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 98–99why, one step back
98});
99

TweaksSindre Sorhusbefore adoption698a1b8

line 100why, one step back
100test('Can unpipe a subprocess among other destinations', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 101–108why, one step back
101 const abortController = new AbortController();
102 const source = execa('stdin.js');103 const destination = execa('stdin.js');104 const secondDestination = execa('stdin.js');105 const pipePromise = source.pipe(destination, {unpipeSignal: abortController.signal});106 const secondPipePromise = source.pipe(secondDestination);107108 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

line 109why, one step back
109 await assertUnPipeError(pipePromise);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 110–113why, one step back
110
111 source.stdin.end(foobarString);112 destination.stdin.end('.');113

TweaksSindre Sorhusbefore adoption698a1b8

lines 114–117why, one step back
114 assert.equal(await secondPipePromise, await secondDestination);
115 assertLike(await destination, {stdout: '.'});116 assertLike(await source, {stdout: foobarString});117 assertLike(await secondDestination, {stdout: foobarString});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 118–119why, one step back
118});
119

TweaksSindre Sorhusbefore adoption698a1b8

line 120why, one step back
120test('Can unpipe then re-pipe a subprocess', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 121–127why, one step back
121 const abortController = new AbortController();
122 const source = execa('stdin.js');123 const destination = execa('stdin.js');124 const pipePromise = source.pipe(destination, {unpipeSignal: abortController.signal});125126 source.stdin.write('.');127 const [firstWrite] = await once(source.stdout, 'data');

TweaksSindre Sorhusbefore adoption698a1b8

line 128why, one step back
128 assert.equal(firstWrite.toString(), '.');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 129–130why, one step back
129
130 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

line 131why, one step back
131 await assertUnPipeError(pipePromise);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 132–135why, one step back
132
133 source.pipe(destination);134 source.stdin.end('.');135

TweaksSindre Sorhusbefore adoption698a1b8

lines 136–137why, one step back
136 assertLike(await destination, {stdout: '..'});
137 assertLike(await source, {stdout: '..'});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 138–139why, one step back
138});
139

TweaksSindre Sorhusbefore adoption698a1b8

line 140why, one step back
140test('Can unpipe to prevent termination to propagate to source', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 141–146why, one step back
141 const abortController = new AbortController();
142 const source = execa('stdin.js');143 const destination = execa('stdin.js');144 const pipePromise = source.pipe(destination, {unpipeSignal: abortController.signal});145146 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

line 147why, one step back
147 await assertUnPipeError(pipePromise);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 148–149why, one step back
148
149 destination.kill();

TweaksSindre Sorhusbefore adoption698a1b8

line 150why, one step back
150 assertLike(await assertRejects(destination), {signal: 'SIGTERM'});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 151–152why, one step back
151
152 source.stdin.end(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

line 153why, one step back
153 assertLike(await source, {stdout: foobarString});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 154–155why, one step back
154});
155

TweaksSindre Sorhusbefore adoption698a1b8

line 156why, one step back
156test('Can unpipe to prevent termination to propagate to destination', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 157–162why, one step back
157 const abortController = new AbortController();
158 const source = execa('noop-forever.js', [foobarString]);159 const destination = execa('stdin.js');160 const pipePromise = source.pipe(destination, {unpipeSignal: abortController.signal});161162 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

line 163why, one step back
163 await assertUnPipeError(pipePromise);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 164–165why, one step back
164
165 source.kill();

TweaksSindre Sorhusbefore adoption698a1b8

line 166why, one step back
166 assertLike(await assertRejects(source), {signal: 'SIGTERM'});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 167–168why, one step back
167
168 destination.stdin.end(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

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

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 170–198why, one step back
170});
171172test('The "unpipeSignal" option must be an AbortSignal', async () => {173 const source = execa('noop.js', [foobarString]);174 const {cause} = await assertRejects(source.pipe('stdin.js', {unpipeSignal: foobarString}));175 assert.equal(cause.message, `The \`unpipeSignal\` option must be an AbortSignal: ${foobarString}.`);176 await source;177});178179// The unpiped destination ending later must not abort the source, which is still piped to the other destination180test('Unpiping one destination does not affect the other destinations of the same source', async () => {181 const source = execa('noop-repeat.js', ['1', `${foobarString}\n`]);182 const unpipedDestination = execa('stdin.js');183 const receivingDestination = execa('stdin.js');184 const controller = new AbortController();185 const pipePromise = source.pipe(unpipedDestination, {unpipeSignal: controller.signal});186 source.pipe(receivingDestination).catch(() => {});187188 controller.abort();189 await assertRejects(pipePromise);190 unpipedDestination.stdin.end();191 await unpipedDestination;192 await setTimeout(100);193 source.kill();194195 const {isTerminated} = await assertRejects(source);196 assert.ok(isTerminated);197 const {stdout} = await receivingDestination;198 assert.ok(stdout.split('\n').length > 2);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 199why, one step back
199});