-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
error.js
165 lines (129 loc) · 5.09 KB
/
error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import path from 'path';
import childProcess from 'child_process';
import test from 'ava';
import execa from '..';
process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;
const TIMEOUT_REGEXP = /timed out after/;
const getExitRegExp = exitMessage => new RegExp(`failed with exit code ${exitMessage}`);
test('stdout/stderr/all available on errors', async t => {
const {stdout, stderr, all} = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});
t.is(typeof stdout, 'string');
t.is(typeof stderr, 'string');
t.is(typeof all, 'string');
});
const WRONG_COMMAND = process.platform === 'win32' ?
'\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :
'';
test('stdout/stderr/all on process errors', async t => {
const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command'));
t.is(stdout, '');
t.is(stderr, WRONG_COMMAND);
t.is(all, WRONG_COMMAND);
});
test('stdout/stderr/all on process errors, in sync mode', t => {
const {stdout, stderr, all} = t.throws(() => {
execa.sync('wrong command');
});
t.is(stdout, '');
t.is(stderr, WRONG_COMMAND);
t.is(all, undefined);
});
test('allow unknown exit code', async t => {
const {exitCode, exitCodeName} = await t.throwsAsync(execa('exit', ['255']), {message: /exit code 255 \(Unknown system error -255\)/});
t.is(exitCode, 255);
t.is(exitCodeName, 'Unknown system error -255');
});
test('execa() does not return code and failed properties on success', async t => {
const {exitCode, exitCodeName, failed} = await execa('noop', ['foo']);
t.is(exitCode, 0);
t.is(exitCodeName, 'SUCCESS');
t.false(failed);
});
test('execa() returns code and failed properties', async t => {
const {exitCode, exitCodeName, failed} = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});
t.is(exitCode, 2);
const expectedName = process.platform === 'win32' ? 'Unknown system error -2' : 'ENOENT';
t.is(exitCodeName, expectedName);
t.true(failed);
});
test('error.killed is true if process was killed directly', async t => {
const cp = execa('forever');
cp.kill();
const {killed} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});
t.true(killed);
});
test('error.killed is false if process was killed indirectly', async t => {
const cp = execa('forever');
process.kill(cp.pid, 'SIGINT');
// `process.kill()` is emulated by Node.js on Windows
const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/;
const {killed} = await t.throwsAsync(cp, {message});
t.false(killed);
});
test('result.killed is false if not killed', async t => {
const {killed} = await execa('noop');
t.false(killed);
});
test('result.killed is false if not killed, in sync mode', t => {
const {killed} = execa.sync('noop');
t.false(killed);
});
test('result.killed is false on process error', async t => {
const {killed} = await t.throwsAsync(execa('wrong command'));
t.false(killed);
});
test('result.killed is false on process error, in sync mode', t => {
const {killed} = t.throws(() => {
execa.sync('wrong command');
});
t.false(killed);
});
if (process.platform === 'darwin') {
test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => {
const {pid} = childProcess.exec('forever', error => {
t.truthy(error);
t.false(error.killed);
t.end();
});
process.kill(pid, 'SIGINT');
});
}
if (process.platform !== 'win32') {
test('error.signal is SIGINT', async t => {
const cp = execa('forever');
process.kill(cp.pid, 'SIGINT');
const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGINT/});
t.is(signal, 'SIGINT');
});
test('error.signal is SIGTERM', async t => {
const cp = execa('forever');
process.kill(cp.pid, 'SIGTERM');
const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});
t.is(signal, 'SIGTERM');
});
test('custom error.signal', async t => {
const {signal} = await t.throwsAsync(execa('forever', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP}));
t.is(signal, 'SIGHUP');
});
}
test('result.signal is undefined for successful execution', async t => {
const {signal} = await execa('noop');
t.is(signal, undefined);
});
test('result.signal is undefined if process failed, but was not killed', async t => {
const {signal} = await t.throwsAsync(execa('exit', [2]), {message: getExitRegExp('2')});
t.is(signal, undefined);
});
const testExitCode = async (t, num) => {
const {exitCode} = await t.throwsAsync(execa('exit', [`${num}`]), {message: getExitRegExp(num)});
t.is(exitCode, num);
};
test('error.exitCode is 2', testExitCode, 2);
test('error.exitCode is 3', testExitCode, 3);
test('error.exitCode is 4', testExitCode, 4);
const errorMessage = async (t, expected, ...args) => {
await t.throwsAsync(execa('exit', args), {message: expected});
};
errorMessage.title = (message, expected) => `error.message matches: ${expected}`;
test(errorMessage, /Command failed with exit code 2.*: exit 2 foo bar/, 2, 'foo', 'bar');
test(errorMessage, /Command failed with exit code 3.*: exit 3 baz quz/, 3, 'baz', 'quz');