Skip to content
Myr

cancel.js

This file

Path

test/terminate/cancel.js

218 lines · 9.4 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

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';

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

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

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 5–6why, one step back
5import {execa, execaSync} from '../../index.js';
6import {setFixtureDirectory} from '../helpers/fixtures-directory.js';

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

line 7why, one step back
7import {foobarString} from '../helpers/input.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 11–12why, one step back
11const testValidCancelSignal = cancelSignal => {
12 assertThrows(() => {

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 13–16why, one step back
13 execa('empty.js', {cancelSignal});
14 }, {message: /must be an AbortSignal/});15};16

TweaksSindre Sorhusbefore adoption698a1b8

lines 17–20why, one step back
17test('cancelSignal option cannot be AbortController', () => testValidCancelSignal(new AbortController()));
18test('cancelSignal option cannot be {}', () => testValidCancelSignal({}));19test('cancelSignal option cannot be null', () => testValidCancelSignal(null));20test('cancelSignal option cannot be a symbol', () => testValidCancelSignal(Symbol('test')));

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

line 21why, one step back
21

TweaksSindre Sorhusbefore adoption698a1b8

line 22why, one step back
22test('result.isCanceled is false when abort isn\'t called (success)', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 23why, one step back
23 const {isCanceled, isGracefullyCanceled} = await execa('noop.js');

TweaksSindre Sorhusbefore adoption698a1b8

lines 24–25why, one step back
24 assert.equal(isCanceled, false);
25 assert.equal(isGracefullyCanceled, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 26–27why, one step back
26});
27

TweaksSindre Sorhusbefore adoption698a1b8

lines 28–31why, one step back
28test('result.isCanceled is false when abort isn\'t called (failure)', async () => {
29 const {isCanceled, isGracefullyCanceled} = await assertRejects(execa('fail.js'));30 assert.equal(isCanceled, false);31 assert.equal(isGracefullyCanceled, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

line 34why, one step back
34test('result.isCanceled is false when abort isn\'t called in sync mode (success)', () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 35why, one step back
35 const {isCanceled, isGracefullyCanceled} = execaSync('noop.js');

TweaksSindre Sorhusbefore adoption698a1b8

lines 36–37why, one step back
36 assert.equal(isCanceled, false);
37 assert.equal(isGracefullyCanceled, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 38–39why, one step back
38});
39

TweaksSindre Sorhusbefore adoption698a1b8

lines 40–41why, one step back
40test('result.isCanceled is false when abort isn\'t called in sync mode (failure)', () => {
41 const {isCanceled, isGracefullyCanceled} = assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 42–43why, one step back
42 execaSync('fail.js');
43 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 44–45why, one step back
44 assert.equal(isCanceled, false);
45 assert.equal(isGracefullyCanceled, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 46–47why, one step back
46});
47

TweaksSindre Sorhusbefore adoption698a1b8

line 48why, one step back
48const testCancelSuccess = async options => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 49why, one step back
49 const abortController = new AbortController();

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 50why, one step back
50 const subprocess = execa('noop.js', {cancelSignal: abortController.signal, ...options});

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 51why, one step back
51 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

lines 52–54why, one step back
52 const {isCanceled, isGracefullyCanceled} = await assertRejects(subprocess);
53 assert.equal(isCanceled, true);54 assert.equal(isGracefullyCanceled, false);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 55why, one step back
55};

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 56why, one step back
56

TweaksSindre Sorhusbefore adoption698a1b8

lines 57–60why, one step back
57test('error.isCanceled is true when abort is used', () => testCancelSuccess({}));
58test('gracefulCancel can be false with cancelSignal', () => testCancelSuccess({gracefulCancel: false}));59test('ipc can be false with cancelSignal', () => testCancelSuccess({ipc: false}));60test('serialization can be "json" with cancelSignal', () => testCancelSuccess({ipc: true, serialization: 'json'}));

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 61why, one step back
61

TweaksSindre Sorhusbefore adoption698a1b8

line 62why, one step back
62test('error.isCanceled is false when kill method is used', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 63–65why, one step back
63 const abortController = new AbortController();
64 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});65 subprocess.kill();

TweaksSindre Sorhusbefore adoption698a1b8

lines 66–68why, one step back
66 const {isCanceled, isGracefullyCanceled} = await assertRejects(subprocess);
67 assert.equal(isCanceled, false);68 assert.equal(isGracefullyCanceled, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 69–70why, one step back
69});
70

TweaksSindre Sorhusbefore adoption698a1b8

line 71why, one step back
71test('calling abort is considered a signal termination', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 72–73why, one step back
72 const abortController = new AbortController();
73 const subprocess = execa('forever.js', {cancelSignal: abortController.signal});

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 74why, one step back
74 await once(subprocess.nodeChildProcess, 'spawn');

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 75why, one step back
75 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

lines 76–80why, one step back
76 const {isCanceled, isGracefullyCanceled, isTerminated, signal} = await assertRejects(subprocess);
77 assert.equal(isCanceled, true);78 assert.equal(isGracefullyCanceled, false);79 assert.equal(isTerminated, true);80 assert.equal(signal, 'SIGTERM');

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

line 83why, one step back
83test('cancelSignal can already be aborted', async () => {

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

line 84why, one step back
84 const cancelSignal = AbortSignal.abort();

TweaksSindre Sorhusbefore adoption698a1b8

lines 85–90why, one step back
85 const {isCanceled, isGracefullyCanceled, isTerminated, signal} = await assertRejects(execa('forever.js', {cancelSignal}));
86 assert.equal(isCanceled, true);87 assert.equal(isGracefullyCanceled, false);88 assert.equal(isTerminated, true);89 assert.equal(signal, 'SIGTERM');90 assert.deepEqual(getEventListeners(cancelSignal, 'abort'), []);

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 91–92why, one step back
91});
92

TweaksSindre Sorhusbefore adoption698a1b8

line 93why, one step back
93test('calling abort does not emit the "error" event', async () => {

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 94–96why, one step back
94 const abortController = new AbortController();
95 const subprocess = execa('forever.js', {cancelSignal: abortController.signal});96 let error;

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 97why, one step back
97 subprocess.nodeChildProcess.once('error', errorArgument => {

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 98–100why, one step back
98 error = errorArgument;
99 });100 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

lines 101–104why, one step back
101 const {isCanceled, isGracefullyCanceled} = await assertRejects(subprocess);
102 assert.equal(isCanceled, true);103 assert.equal(isGracefullyCanceled, false);104 assert.equal(error, undefined);

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 105–106why, one step back
105});
106

TweaksSindre Sorhusbefore adoption698a1b8

line 107why, one step back
107test('calling abort cleans up listeners on cancelSignal, called', async () => {

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 108–109why, one step back
108 const abortController = new AbortController();
109 const subprocess = execa('forever.js', {cancelSignal: abortController.signal});

TweaksSindre Sorhusbefore adoption698a1b8

line 110why, one step back
110 assert.equal(getEventListeners(abortController.signal, 'abort').length, 1);

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

line 111why, one step back
111 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

lines 112–115why, one step back
112 const {isCanceled, isGracefullyCanceled} = await assertRejects(subprocess);
113 assert.equal(isCanceled, true);114 assert.equal(isGracefullyCanceled, false);115 assert.equal(getEventListeners(abortController.signal, 'abort').length, 0);

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 116–117why, one step back
116});
117

TweaksSindre Sorhusbefore adoption698a1b8

line 118why, one step back
118test('calling abort cleans up listeners on cancelSignal, not called', async () => {

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 119–120why, one step back
119 const abortController = new AbortController();
120 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});

TweaksSindre Sorhusbefore adoption698a1b8

line 121why, one step back
121 assert.equal(getEventListeners(abortController.signal, 'abort').length, 1);

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

line 122why, one step back
122 await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 123why, one step back
123 assert.equal(getEventListeners(abortController.signal, 'abort').length, 0);

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 124–125why, one step back
124});
125

TweaksSindre Sorhusbefore adoption698a1b8

line 126why, one step back
126test('calling abort cleans up listeners on cancelSignal, already aborted', async () => {

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 127–128why, one step back
127 const cancelSignal = AbortSignal.abort();
128 const subprocess = execa('noop.js', {cancelSignal});

TweaksSindre Sorhusbefore adoption698a1b8

lines 129–133why, one step back
129 assert.equal(getEventListeners(cancelSignal, 'abort').length, 0);
130 const {isCanceled, isGracefullyCanceled} = await assertRejects(subprocess);131 assert.equal(isCanceled, true);132 assert.equal(isGracefullyCanceled, false);133 assert.equal(getEventListeners(cancelSignal, 'abort').length, 0);

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 134–135why, one step back
134});
135

TweaksSindre Sorhusbefore adoption698a1b8

line 136why, one step back
136test('calling abort throws an error with message "Command was canceled"', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 137–139why, one step back
137 const abortController = new AbortController();
138 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});139 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

line 140why, one step back
140 await assertRejects(subprocess, {message: /Command was canceled/});

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 141–142why, one step back
141});
142

TweaksSindre Sorhusbefore adoption698a1b8

line 143why, one step back
143test('calling abort with no argument keeps error properties', async () => {

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

line 144why, one step back
144 const abortController = new AbortController();

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 145why, one step back
145 const subprocess = execa('empty.js', {cancelSignal: abortController.signal});

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

line 146why, one step back
146 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

lines 147–152why, one step back
147 const {cause, originalMessage, shortMessage, message} = await assertRejects(subprocess);
148 assert.equal(cause.message, 'This operation was aborted');149 assert.equal(cause.name, 'AbortError');150 assert.equal(originalMessage, 'This operation was aborted');151 assert.equal(shortMessage, 'Command was canceled: empty.js\nThis operation was aborted');152 assert.equal(message, 'Command was canceled: empty.js\nThis operation was aborted');

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

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

TweaksSindre Sorhusbefore adoption698a1b8

line 155why, one step back
155test('calling abort with an error instance keeps error properties', async () => {

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

line 156why, one step back
156 const abortController = new AbortController();

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 157why, one step back
157 const subprocess = execa('empty.js', {cancelSignal: abortController.signal});

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 158–160why, one step back
158 const error = new Error(foobarString);
159 error.code = foobarString;160 abortController.abort(error);

TweaksSindre Sorhusbefore adoption698a1b8

lines 161–166why, one step back
161 const {cause, originalMessage, shortMessage, message, code} = await assertRejects(subprocess);
162 assert.equal(cause, error);163 assert.equal(originalMessage, foobarString);164 assert.equal(shortMessage, `Command was canceled: empty.js\n${foobarString}`);165 assert.equal(message, `Command was canceled: empty.js\n${foobarString}`);166 assert.equal(code, foobarString);

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

lines 167–168why, one step back
167});
168

TweaksSindre Sorhusbefore adoption698a1b8

line 169why, one step back
169test('calling abort with null keeps error properties', async () => {

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

line 170why, one step back
170 const abortController = new AbortController();

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 171why, one step back
171 const subprocess = execa('empty.js', {cancelSignal: abortController.signal});

Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977

line 172why, one step back
172 abortController.abort(null);

TweaksSindre Sorhusbefore adoption698a1b8

lines 173–177why, one step back
173 const {cause, originalMessage, shortMessage, message} = await assertRejects(subprocess);
174 assert.equal(cause, null);175 assert.equal(originalMessage, 'null');176 assert.equal(shortMessage, 'Command was canceled: empty.js\nnull');177 assert.equal(message, 'Command was canceled: empty.js\nnull');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 178–179why, one step back
178});
179

TweaksSindre Sorhusbefore adoption698a1b8

line 180why, one step back
180test('calling abort twice should show the same behaviour as calling it once', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 181–184why, one step back
181 const abortController = new AbortController();
182 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});183 abortController.abort();184 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

lines 185–187why, one step back
185 const {isCanceled, isGracefullyCanceled} = await assertRejects(subprocess);
186 assert.equal(isCanceled, true);187 assert.equal(isGracefullyCanceled, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 188–189why, one step back
188});
189

TweaksSindre Sorhusbefore adoption698a1b8

line 190why, one step back
190test('calling abort on a successfully completed subprocess does not make result.isCanceled true', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 191–192why, one step back
191 const abortController = new AbortController();
192 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 193why, one step back
193 const {isCanceled, isGracefullyCanceled} = await subprocess;

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 194why, one step back
194 abortController.abort();

TweaksSindre Sorhusbefore adoption698a1b8

lines 195–196why, one step back
195 assert.equal(isCanceled, false);
196 assert.equal(isGracefullyCanceled, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 197–198why, one step back
197});
198

TweaksSindre Sorhusbefore adoption698a1b8

line 199why, one step back
199test('Throws when using the former "signal" option name', () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 200why, one step back
200 const abortController = new AbortController();

TweaksSindre Sorhusbefore adoption698a1b8

line 201why, one step back
201 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 202–205why, one step back
202 execa('empty.js', {signal: abortController.signal});
203 }, {message: /renamed to "cancelSignal"/});204});205

TweaksSindre Sorhusbefore adoption698a1b8

line 206why, one step back
206test('Throws when using the former "signal" option name, sync', () => {

Fix the "signal" option being silently ignored by synchronous methodsSindre Sorhusbefore adoption59de065

line 207why, one step back
207 const abortController = new AbortController();

TweaksSindre Sorhusbefore adoption698a1b8

line 208why, one step back
208 assertThrows(() => {

Fix the "signal" option being silently ignored by synchronous methodsSindre Sorhusbefore adoption59de065

lines 209–212why, one step back
209 execaSync('empty.js', {signal: abortController.signal});
210 }, {message: /renamed to "cancelSignal"/});211});212

TweaksSindre Sorhusbefore adoption698a1b8

line 213why, one step back
213test('Cannot use cancelSignal, sync', () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 214why, one step back
214 const abortController = new AbortController();

TweaksSindre Sorhusbefore adoption698a1b8

line 215why, one step back
215 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 216–218why, one step back
216 execaSync('empty.js', {cancelSignal: abortController.signal});
217 }, {message: /The "cancelSignal" option cannot be used/});218});