Skip to content
Myr

error.js

This file

Path

test/error.js

225 lines · 7.0 KiB · JavaScript

Last changed by

Add `cwd` error property (#565)homersimpsonsbefore adoptionf57fdec

History of this file

Take it

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

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

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

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

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

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

Add `cwd` error property (#565)homersimpsonsbefore adoptionf57fdec

line 6why, one step back
6import {FIXTURES_DIR, setFixtureDir} from './helpers/fixtures-dir.js';

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

line 7why, one step back
7

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

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

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

line 9why, one step back
9

Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2

line 10why, one step back
10setFixtureDir();

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

line 11why, one step back
11

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

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

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

line 17why, one step back
17 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 18–22why, one step back
18 t.is(typeof stdout, 'string');
19 t.is(typeof stderr, 'string');20 t.is(typeof all, 'string');21});22

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

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

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

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

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

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

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

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

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

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

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

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

Fix error properties (#375)ehmickybefore adoptionc1b511b

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

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

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

Fix error properties (#375)ehmickybefore adoptionc1b511b

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

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

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

Upgrade dependenciesSindre Sorhusbefore adoption5d64878

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

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

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

Upgrade dependenciesSindre Sorhusbefore adoption5d64878

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

Fix error properties (#375)ehmickybefore adoptionc1b511b

lines 51–57why, one step back
51};
5253test('exitCode is 2', testExitCode, 2);54test('exitCode is 3', testExitCode, 3);55test('exitCode is 4', testExitCode, 4);5657test('error.message contains the command', async t => {

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

line 58why, one step back
58 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 59–61why, one step back
59});
6061test('error.message contains stdout/stderr if available', async t => {

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

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

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

lines 63–67why, one step back
63 t.true(message.includes('stderr'));
64 t.true(message.includes('stdout'));65});6667test('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 68why, one step back
68 const {message} = await t.throwsAsync(execa('echo-fail.js', {stdio: 'ignore'}));

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

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

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

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

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

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

Fix error properties (#375)ehmickybefore adoptionc1b511b

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

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

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

Drop support for Node 12 (#497)ehmickybefore adoptiona09cbc0

line 81why, one step back
81 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 82–84why, one step back
82});
8384test('failed is false on success', async t => {

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

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

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

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

Fix error properties (#375)ehmickybefore adoptionc1b511b

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

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

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

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

lines 91–94why, one step back
91 t.true(failed);
92});9394test('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 95why, one step back
95 const subprocess = execa('noop.js');

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

line 96why, one step back
96

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

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

line 98why, one step back
98

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

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

lines 100–103why, one step back
100 t.true(killed);
101});102103test('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 104why, one step back
104 const subprocess = execa('noop.js');

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

line 105why, one step back
105

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

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

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

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

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

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

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

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

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

lines 116–119why, one step back
116 t.false(killed);
117});118119test('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 120why, one step back
120 const {killed} = execaSync('noop.js');

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

lines 121–130why, one step back
121 t.false(killed);
122});123124test('result.killed is false on process error', async t => {125 const {killed} = await t.throwsAsync(execa('wrong command'));126 t.false(killed);127});128129test('result.killed is false on process error, in sync mode', t => {130 const {killed} = t.throws(() => {

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

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

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

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

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

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

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

line 139why, one step back
139

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

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

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

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

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

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

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

line 151why, one step back
151

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

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

line 153why, one step back
153

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

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

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

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

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

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

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

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

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

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

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

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

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

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

line 169why, one step back
169

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

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

line 171why, one step back
171

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

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

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

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

line 177why, one step back
177 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 178–179why, one step back
178 t.is(signal, 'SIGHUP');
179 });

Fix error properties (#375)ehmickybefore adoptionc1b511b

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

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

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

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 183why, one step back
183

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 185why, one step back
185

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

Fix error properties (#375)ehmickybefore adoptionc1b511b

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

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

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

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

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

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

lines 193–196why, one step back
193 t.is(signal, undefined);
194});195196test('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 197why, one step back
197 const {signal} = await t.throwsAsync(execa('exit.js', [2]), {message: getExitRegExp('2')});

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

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

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

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

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

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

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

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

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

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

Fix error properties (#375)ehmickybefore adoptionc1b511b

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

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

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

Fix error properties (#375)ehmickybefore adoptionc1b511b

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

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

line 210why, one step back
210

Fix error properties (#375)ehmickybefore adoptionc1b511b

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

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

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

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

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

Add `cwd` error property (#565)homersimpsonsbefore adoptionf57fdec

lines 214–224why, one step back
214});
215216test('error.cwd is defined on failure if applicable', async t => {217 const {cwd} = await t.throwsAsync(execa('noop-throw.js', [], {cwd: FIXTURES_DIR}));218 t.is(cwd, FIXTURES_DIR);219});220221test('error.cwd is undefined on failure if not passed as options', async t => {222 const expectedCwd = process.cwd();223 const {cwd} = await t.throwsAsync(execa('noop-throw.js'));224 t.is(cwd, expectedCwd);

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

line 225why, one step back
225});