-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathcli-errors.test.ts
More file actions
418 lines (345 loc) · 11.8 KB
/
cli-errors.test.ts
File metadata and controls
418 lines (345 loc) · 11.8 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import test from "ava";
import * as sinon from "sinon";
import { CommandInvocationError } from "./actions-util";
import {
CliError,
CliConfigErrorCategory,
wrapCliConfigurationError,
} from "./cli-errors";
import { setupTests } from "./testing-utils";
import { ConfigurationError } from "./util";
setupTests(test);
test("CliError constructor with fatal errors", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "finalize"],
32,
"Running TRAP import for CodeQL database...\nA fatal error occurred: Evaluator heap must be at least 384.00 MiB\nA fatal error occurred: Dataset import failed with code 2",
);
const cliError = new CliError(commandError);
t.is(cliError.exitCode, 32);
t.is(
cliError.stderr,
"Running TRAP import for CodeQL database...\nA fatal error occurred: Evaluator heap must be at least 384.00 MiB\nA fatal error occurred: Dataset import failed with code 2",
);
t.true(
cliError.message.includes(
"A fatal error occurred: Dataset import failed with code 2.",
),
);
t.true(
cliError.message.includes(
"Context: A fatal error occurred: Evaluator heap must be at least 384.00 MiB.",
),
);
});
test("CliError constructor with single fatal error", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "create"],
1,
"A fatal error occurred: Out of memory",
);
const cliError = new CliError(commandError);
t.is(cliError.exitCode, 1);
t.true(cliError.message.includes("A fatal error occurred: Out of memory"));
t.false(cliError.message.includes("Context:"));
});
test("CliError constructor with autobuild errors", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "create"],
1,
"[autobuild] [ERROR] Build failed\n[autobuild] [ERROR] Compilation error",
);
const cliError = new CliError(commandError);
t.is(cliError.exitCode, 1);
t.true(
cliError.message.includes(
"We were unable to automatically build your code",
),
);
t.true(cliError.message.includes("Build failed\nCompilation error"));
});
test("CliError constructor with truncated autobuild errors", (t) => {
const stderr = Array.from(
{ length: 12 },
(_, i) => `[autobuild] [ERROR] Error ${i + 1}`,
).join("\n");
const commandError = new CommandInvocationError(
"codeql",
["database", "create"],
1,
stderr,
);
const cliError = new CliError(commandError);
t.true(cliError.message.includes("(truncated)"));
// Should only include first 10 errors plus truncation message
const errorLines = cliError.message
.split("Encountered the following error: ")[1]
.split("\n");
t.is(errorLines.length, 11); // 10 errors + "(truncated)"
});
test("CliError constructor with generic error", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["version"],
1,
"Some generic error message\nLast line of error",
);
const cliError = new CliError(commandError);
t.is(cliError.exitCode, 1);
t.true(
cliError.message.includes(
'Encountered a fatal error while running "codeql version"',
),
);
t.true(
cliError.message.includes(
"Exit code was 1 and last log line was: Last line of error.",
),
);
});
test("CliError constructor with empty stderr", (t) => {
const commandError = new CommandInvocationError("codeql", ["version"], 1, "");
const cliError = new CliError(commandError);
t.true(cliError.message.includes("last log line was: n/a"));
});
for (const [platform, arch] of [
["weird_plat", "x64"],
["linux", "arm64"],
["win32", "arm64"],
]) {
test.serial(
`wrapCliConfigurationError - ${platform}/${arch} unsupported`,
(t) => {
sinon.stub(process, "platform").value(platform);
sinon.stub(process, "arch").value(arch);
const commandError = new CommandInvocationError(
"codeql",
["version"],
1,
"Some error",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
t.true(
wrappedError.message.includes(
"CodeQL CLI does not support the platform/architecture combination",
),
);
t.true(wrappedError.message.includes(`${platform}/${arch}`));
},
);
}
test("wrapCliConfigurationError - supported platform", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["version"],
1,
"Some error",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
// Should return the original error since platform is supported
t.is(wrappedError, cliError);
});
test("wrapCliConfigurationError - autobuild error", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "create"],
1,
"We were unable to automatically build your code",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
t.true(
wrappedError.message.includes(
"We were unable to automatically build your code",
),
);
});
test("wrapCliConfigurationError - init called twice", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "create"],
1,
"Refusing to create databases /some/path but could not process any of it",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
t.true(
wrappedError.message.includes(
'Is the "init" action called twice in the same job?',
),
);
});
test("wrapCliConfigurationError - no source code seen by exit code", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "finalize"],
32,
"Some other error message",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
});
test("wrapCliConfigurationError - no source code seen by message", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "finalize"],
1,
"CodeQL detected code written in JavaScript but could not process any of it",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
});
test("wrapCliConfigurationError - out of memory error with additional message", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "analyze"],
1,
"CodeQL is out of memory.",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
t.true(
wrappedError.message.includes(
"For more information, see https://gh.io/troubleshooting-code-scanning/out-of-disk-or-memory",
),
);
});
test("wrapCliConfigurationError - gradle build failed", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "create"],
1,
"[autobuild] FAILURE: Build failed with an exception.",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
});
test("wrapCliConfigurationError - maven build failed", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "create"],
1,
"[autobuild] [ERROR] Failed to execute goal",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
});
test("wrapCliConfigurationError - swift build failed", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "create"],
1,
"[autobuilder/build] [build-command-failed] `autobuild` failed to run the build command",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
});
test("wrapCliConfigurationError - pack cannot be found", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["pack", "install"],
1,
"Query pack my-pack cannot be found. Check the spelling of the pack.",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
});
test("wrapCliConfigurationError - unknown query file", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "init"],
2,
"my-query-file is not a .ql file, .qls file, a directory, or a query pack specification. See the logs for more details.",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
});
test("wrapCliConfigurationError - pack missing auth", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["pack", "download"],
1,
"GitHub Container registry returned 403 Forbidden",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
});
test("wrapCliConfigurationError - invalid config file", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["database", "create"],
1,
"Config file .codeql/config.yml is not valid",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
});
test("wrapCliConfigurationError - incompatible CLI version", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["version"],
1,
"is not compatible with this CodeQL CLI",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
t.true(wrappedError instanceof ConfigurationError);
});
test("wrapCliConfigurationError - unknown error remains unchanged", (t) => {
const commandError = new CommandInvocationError(
"codeql",
["version"],
1,
"Some unknown error that doesn't match any patterns",
);
const cliError = new CliError(commandError);
const wrappedError = wrapCliConfigurationError(cliError);
// Should return the original CliError since it doesn't match any known patterns
t.is(wrappedError, cliError);
t.true(wrappedError instanceof CliError);
t.false(wrappedError instanceof ConfigurationError);
});
// Test all error categories to ensure they're properly configured
test("all CLI config error categories have valid configurations", (t) => {
const allCategories = Object.values(CliConfigErrorCategory);
for (const category of allCategories) {
// Each category should be a string
t.is(typeof category, "string");
// Create a test error that matches this category
let testError: CliError;
switch (category) {
case CliConfigErrorCategory.NoSourceCodeSeen:
// This category matches by exit code
testError = new CliError(
new CommandInvocationError("codeql", [], 32, "some error"),
);
break;
default:
// For other categories, we'll test with a generic message that should not match
testError = new CliError(
new CommandInvocationError("codeql", [], 1, "generic error"),
);
break;
}
// The test should not throw an error when processing
t.notThrows(() => wrapCliConfigurationError(testError));
}
});