Skip to content
Myr

error.js

This file

Path

test/error.js

215 lines · 6.7 KiB · JavaScript

Last changed by

Drop support for Node 12 (#497)ehmickybefore adoptiona09cbc0

History of this file

Take it

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 1–5why, one step back
1import path from 'node:path';
2import process from 'node:process';3import childProcess from 'node:child_process';4import {fileURLToPath} from 'node:url';5import {promisify} from 'node:util';

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 6why, one step back
6import test from 'ava';

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 7why, one step back
7import {execa, execaSync} from '../index.js';

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 8why, one step back
8

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 9why, one step back
9const pExec = promisify(childProcess.exec);

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 10why, one step back
10

Meta tweaksSindre Sorhusbefore adoption5bac052

line 11why, one step back
11process.env.PATH = fileURLToPath(new URL('fixtures', import.meta.url)) + path.delimiter + process.env.PATH;

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 12why, one step back
12

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 13–17why, one step back
13const TIMEOUT_REGEXP = /timed out after/;
1415const getExitRegExp = exitMessage => new RegExp(`failed with exit code ${exitMessage}`);1617test('stdout/stderr/all available on errors', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 18why, one step back
18 const {stdout, stderr, all} = await t.throwsAsync(execa('exit.js', ['2'], {all: true}), {message: getExitRegExp('2')});

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 19–23why, one step back
19 t.is(typeof stdout, 'string');
20 t.is(typeof stderr, 'string');21 t.is(typeof all, 'string');22});23

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 24–26why, one step back
24const WRONG_COMMAND = process.platform === 'win32'
25 ? '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.'26 : '';

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 27–28why, one step back
27
28test('stdout/stderr/all on process errors', async t => {

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 29why, one step back
29 const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command', {all: true}));

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 30–36why, one step back
30 t.is(stdout, '');
31 t.is(stderr, WRONG_COMMAND);32 t.is(all, WRONG_COMMAND);33});3435test('stdout/stderr/all on process errors, in sync mode', t => {36 const {stdout, stderr, all} = t.throws(() => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 37why, one step back
37 execaSync('wrong command');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 38–43why, one step back
38 });
39 t.is(stdout, '');40 t.is(stderr, WRONG_COMMAND);41 t.is(all, undefined);42});43

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 44why, one step back
44test('exitCode is 0 on success', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 45why, one step back
45 const {exitCode} = await execa('noop.js', ['foo']);

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 46why, one step back
46 t.is(exitCode, 0);

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

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

Upgrade dependenciesSindre Sorhusbefore adoption5d64878

line 49why, one step back
49const testExitCode = async (t, number) => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 50why, one step back
50 const {exitCode} = await t.throwsAsync(execa('exit.js', [`${number}`]), {message: getExitRegExp(number)});

Upgrade dependenciesSindre Sorhusbefore adoption5d64878

line 51why, one step back
51 t.is(exitCode, number);

Fix error properties (#375)ehmickybefore adoptionc1b511b

lines 52–58why, one step back
52};
5354test('exitCode is 2', testExitCode, 2);55test('exitCode is 3', testExitCode, 3);56test('exitCode is 4', testExitCode, 4);5758test('error.message contains the command', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 59why, one step back
59 await t.throwsAsync(execa('exit.js', ['2', 'foo', 'bar']), {message: /exit.js 2 foo bar/});

Add stdout/stderr to error messages (#397)ehmickybefore adoptionbc520f9

lines 60–62why, one step back
60});
6162test('error.message contains stdout/stderr if available', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 63why, one step back
63 const {message} = await t.throwsAsync(execa('echo-fail.js'));

Add stdout/stderr to error messages (#397)ehmickybefore adoptionbc520f9

lines 64–68why, one step back
64 t.true(message.includes('stderr'));
65 t.true(message.includes('stdout'));66});6768test('error.message does not contain stdout/stderr if not available', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 69why, one step back
69 const {message} = await t.throwsAsync(execa('echo-fail.js', {stdio: 'ignore'}));

Add stdout/stderr to error messages (#397)ehmickybefore adoptionbc520f9

lines 70–74why, one step back
70 t.false(message.includes('stderr'));
71 t.false(message.includes('stdout'));72});7374test('error.shortMessage does not contain stdout/stderr', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 75why, one step back
75 const {shortMessage} = await t.throwsAsync(execa('echo-fail.js'));

Add stdout/stderr to error messages (#397)ehmickybefore adoptionbc520f9

lines 76–77why, one step back
76 t.false(shortMessage.includes('stderr'));
77 t.false(shortMessage.includes('stdout'));

Fix error properties (#375)ehmickybefore adoptionc1b511b

lines 78–80why, one step back
78});
7980test('Original error.message is kept', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 81why, one step back
81 const {originalMessage} = await t.throwsAsync(execa('noop.js', {cwd: 1}));

Drop support for Node 12 (#497)ehmickybefore adoptiona09cbc0

line 82why, one step back
82 t.true(originalMessage.startsWith('The "options.cwd" property must be of type string or an instance of Buffer or URL. Received type number'));

Fix error properties (#375)ehmickybefore adoptionc1b511b

lines 83–85why, one step back
83});
8485test('failed is false on success', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 86why, one step back
86 const {failed} = await execa('noop.js', ['foo']);

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 87–89why, one step back
87 t.false(failed);
88});89

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 90why, one step back
90test('failed is true on failure', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 91why, one step back
91 const {failed} = await t.throwsAsync(execa('exit.js', ['2']));

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 92–95why, one step back
92 t.true(failed);
93});9495test('error.killed is true if process was killed directly', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 96why, one step back
96 const subprocess = execa('noop.js');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 97why, one step back
97

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 98why, one step back
98 subprocess.kill();

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 99why, one step back
99

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 100why, one step back
100 const {killed} = await t.throwsAsync(subprocess, {message: /was killed with SIGTERM/});

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 101–104why, one step back
101 t.true(killed);
102});103104test('error.killed is false if process was killed indirectly', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 105why, one step back
105 const subprocess = execa('noop.js');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 106why, one step back
106

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 107why, one step back
107 process.kill(subprocess.pid, 'SIGINT');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 108–110why, one step back
108
109 // `process.kill()` is emulated by Node.js on Windows110 const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/;

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 111why, one step back
111 const {killed} = await t.throwsAsync(subprocess, {message});

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 112–115why, one step back
112 t.false(killed);
113});114115test('result.killed is false if not killed', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 116why, one step back
116 const {killed} = await execa('noop.js');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 117–120why, one step back
117 t.false(killed);
118});119120test('result.killed is false if not killed, in sync mode', t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 121why, one step back
121 const {killed} = execaSync('noop.js');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 122–131why, one step back
122 t.false(killed);
123});124125test('result.killed is false on process error', async t => {126 const {killed} = await t.throwsAsync(execa('wrong command'));127 t.false(killed);128});129130test('result.killed is false on process error, in sync mode', t => {131 const {killed} = t.throws(() => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 132why, one step back
132 execaSync('wrong command');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 133–137why, one step back
133 });
134 t.false(killed);135});136137if (process.platform === 'darwin') {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 138–139why, one step back
138 test('sanity check: child_process.exec also has killed.false if killed indirectly', async t => {
139 const promise = pExec('noop.js');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 140why, one step back
140

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 141–145why, one step back
141 process.kill(promise.child.pid, 'SIGINT');
142143 const error = await t.throwsAsync(promise);144 t.truthy(error);145 t.false(error.killed);

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 146–150why, one step back
146 });
147}148149if (process.platform !== 'win32') {150 test('error.signal is SIGINT', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 151why, one step back
151 const subprocess = execa('noop.js');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 152why, one step back
152

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 153why, one step back
153 process.kill(subprocess.pid, 'SIGINT');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 154why, one step back
154

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 155why, one step back
155 const {signal} = await t.throwsAsync(subprocess, {message: /was killed with SIGINT/});

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 156–158why, one step back
156 t.is(signal, 'SIGINT');
157 });158

Add `error.signalDescription` (#378)ehmickybefore adoptiond8cb08f

line 159why, one step back
159 test('error.signalDescription is defined', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 160why, one step back
160 const subprocess = execa('noop.js');

Add `error.signalDescription` (#378)ehmickybefore adoptiond8cb08f

lines 161–167why, one step back
161
162 process.kill(subprocess.pid, 'SIGINT');163164 const {signalDescription} = await t.throwsAsync(subprocess, {message: /User interruption with CTRL-C/});165 t.is(signalDescription, 'User interruption with CTRL-C');166 });167

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 168why, one step back
168 test('error.signal is SIGTERM', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 169why, one step back
169 const subprocess = execa('noop.js');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 170why, one step back
170

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 171why, one step back
171 process.kill(subprocess.pid, 'SIGTERM');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 172why, one step back
172

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 173why, one step back
173 const {signal} = await t.throwsAsync(subprocess, {message: /was killed with SIGTERM/});

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 174–177why, one step back
174 t.is(signal, 'SIGTERM');
175 });176177 test('custom error.signal', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 178why, one step back
178 const {signal} = await t.throwsAsync(execa('noop.js', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP}));

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 179–180why, one step back
179 t.is(signal, 'SIGHUP');
180 });

Fix error properties (#375)ehmickybefore adoptionc1b511b

lines 181–182why, one step back
181
182 test('exitCode is undefined on signal termination', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 183why, one step back
183 const subprocess = execa('noop.js');

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 184why, one step back
184

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 185why, one step back
185 process.kill(subprocess.pid);

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 186why, one step back
186

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 187why, one step back
187 const {exitCode} = await t.throwsAsync(subprocess);

Fix error properties (#375)ehmickybefore adoptionc1b511b

lines 188–189why, one step back
188 t.is(exitCode, undefined);
189 });

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 190–192why, one step back
190}
191192test('result.signal is undefined for successful execution', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 193why, one step back
193 const {signal} = await execa('noop.js');

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 194–197why, one step back
194 t.is(signal, undefined);
195});196197test('result.signal is undefined if process failed, but was not killed', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 198why, one step back
198 const {signal} = await t.throwsAsync(execa('exit.js', [2]), {message: getExitRegExp('2')});

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 199why, one step back
199 t.is(signal, undefined);

Add `error.signalDescription` (#378)ehmickybefore adoptiond8cb08f

lines 200–202why, one step back
200});
201202test('result.signalDescription is undefined for successful execution', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 203why, one step back
203 const {signalDescription} = await execa('noop.js');

Add `error.signalDescription` (#378)ehmickybefore adoptiond8cb08f

line 204why, one step back
204 t.is(signalDescription, undefined);

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

lines 205–206why, one step back
205});
206

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 207why, one step back
207test('error.code is undefined on success', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 208why, one step back
208 const {code} = await execa('noop.js');

Fix error properties (#375)ehmickybefore adoptionc1b511b

lines 209–210why, one step back
209 t.is(code, undefined);
210});

Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d

line 211why, one step back
211

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 212why, one step back
212test('error.code is defined on failure if applicable', async t => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 213why, one step back
213 const {code} = await t.throwsAsync(execa('noop.js', {cwd: 1}));

Remove faulty emulated ENOENT error on Windows (#447)Mark Terrelbefore adoptionbdbd975

line 214why, one step back
214 t.is(code, 'ERR_INVALID_ARG_TYPE');

Add `error.originalMessage` property (#373)ehmickybefore adoption07d2b76

line 215why, one step back
215});