-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.zig
More file actions
539 lines (503 loc) · 22.5 KB
/
Copy pathcli.zig
File metadata and controls
539 lines (503 loc) · 22.5 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
const std = @import("std");
const test_runner = @import("test_runner.zig");
pub const CLIOptions = struct {
help: bool = false,
version: bool = false,
bail: bool = false,
filter: ?[]const u8 = null,
reporter: test_runner.ReporterType = .spec,
verbose: bool = false,
quiet: bool = false,
no_color: bool = false,
// Test discovery options
test_dir: ?[]const u8 = null,
pattern: []const u8 = "*.test.zig",
no_recursive: bool = false,
// Coverage options
coverage: bool = false,
coverage_dir: []const u8 = "coverage",
coverage_tool: []const u8 = "kcov",
// Parallel execution options
parallel: bool = false,
jobs: ?usize = null,
// UI server options
ui: bool = false,
ui_port: u16 = 8080,
ui_host: []const u8 = "127.0.0.1",
// Snapshot testing options
update_snapshots: bool = false,
snapshot_dir: []const u8 = ".snapshots",
// Watch mode options
watch: bool = false,
watch_debounce: u64 = 300,
// Memory profiling options
profile_memory: bool = false,
memory_threshold: usize = 0,
fail_on_leak: bool = false,
// Configuration file
config: ?[]const u8 = null,
// JUnit reporter options
junit_output: ?[]const u8 = null,
// Timeout options
timeout: ?u64 = null, // Global timeout in milliseconds
};
pub const CLIError = error{
InvalidArgument,
MissingValue,
};
pub const CLI = struct {
allocator: std.mem.Allocator,
options: CLIOptions,
const Self = @This();
pub fn init(allocator: std.mem.Allocator) Self {
return Self{
.allocator = allocator,
.options = CLIOptions{},
};
}
/// Parse command-line arguments
pub fn parse(self: *Self, args: []const []const u8) !void {
var i: usize = 1; // Skip program name
while (i < args.len) : (i += 1) {
const arg = args[i];
if (std.mem.eql(u8, arg, "--help") or std.mem.eql(u8, arg, "-h")) {
self.options.help = true;
} else if (std.mem.eql(u8, arg, "--version") or std.mem.eql(u8, arg, "-v")) {
self.options.version = true;
} else if (std.mem.eql(u8, arg, "--bail") or std.mem.eql(u8, arg, "-b")) {
self.options.bail = true;
} else if (std.mem.eql(u8, arg, "--verbose")) {
self.options.verbose = true;
} else if (std.mem.eql(u8, arg, "--quiet") or std.mem.eql(u8, arg, "-q")) {
self.options.quiet = true;
} else if (std.mem.eql(u8, arg, "--no-color")) {
self.options.no_color = true;
} else if (std.mem.eql(u8, arg, "--reporter")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --reporter requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
const reporter_name = args[i];
if (std.mem.eql(u8, reporter_name, "spec")) {
self.options.reporter = .spec;
} else if (std.mem.eql(u8, reporter_name, "dot")) {
self.options.reporter = .dot;
} else if (std.mem.eql(u8, reporter_name, "json")) {
self.options.reporter = .json;
} else if (std.mem.eql(u8, reporter_name, "tap")) {
self.options.reporter = .tap;
} else if (std.mem.eql(u8, reporter_name, "junit")) {
self.options.reporter = .junit;
} else {
std.debug.print("Error: Unknown reporter '{s}'. Available: spec, dot, json, tap, junit\n", .{reporter_name});
return CLIError.InvalidArgument;
}
} else if (std.mem.eql(u8, arg, "--filter") or std.mem.eql(u8, arg, "--grep")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --filter/--grep requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
self.options.filter = args[i];
} else if (std.mem.eql(u8, arg, "--test-dir")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --test-dir requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
self.options.test_dir = args[i];
} else if (std.mem.eql(u8, arg, "--pattern")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --pattern requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
self.options.pattern = args[i];
} else if (std.mem.eql(u8, arg, "--no-recursive")) {
self.options.no_recursive = true;
} else if (std.mem.eql(u8, arg, "--coverage")) {
self.options.coverage = true;
} else if (std.mem.eql(u8, arg, "--coverage-dir")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --coverage-dir requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
self.options.coverage_dir = args[i];
} else if (std.mem.eql(u8, arg, "--coverage-tool")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --coverage-tool requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
self.options.coverage_tool = args[i];
} else if (std.mem.eql(u8, arg, "--parallel") or std.mem.eql(u8, arg, "-p")) {
self.options.parallel = true;
} else if (std.mem.eql(u8, arg, "--jobs") or std.mem.eql(u8, arg, "-j")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --jobs requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
const jobs_str = args[i];
self.options.jobs = std.fmt.parseInt(usize, jobs_str, 10) catch {
std.debug.print("Error: --jobs must be a valid number\n", .{});
return CLIError.InvalidArgument;
};
} else if (std.mem.eql(u8, arg, "--ui")) {
self.options.ui = true;
} else if (std.mem.eql(u8, arg, "--ui-port")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --ui-port requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
const port_str = args[i];
self.options.ui_port = std.fmt.parseInt(u16, port_str, 10) catch {
std.debug.print("Error: --ui-port must be a valid port number (1-65535)\n", .{});
return CLIError.InvalidArgument;
};
} else if (std.mem.eql(u8, arg, "--ui-host")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --ui-host requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
self.options.ui_host = args[i];
} else if (std.mem.eql(u8, arg, "--update-snapshots") or std.mem.eql(u8, arg, "-u")) {
self.options.update_snapshots = true;
} else if (std.mem.eql(u8, arg, "--snapshot-dir")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --snapshot-dir requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
self.options.snapshot_dir = args[i];
} else if (std.mem.eql(u8, arg, "--watch") or std.mem.eql(u8, arg, "-w")) {
self.options.watch = true;
} else if (std.mem.eql(u8, arg, "--watch-debounce")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --watch-debounce requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
const debounce_str = args[i];
self.options.watch_debounce = std.fmt.parseInt(u64, debounce_str, 10) catch {
std.debug.print("Error: --watch-debounce must be a valid number\n", .{});
return CLIError.InvalidArgument;
};
} else if (std.mem.eql(u8, arg, "--profile-memory")) {
self.options.profile_memory = true;
} else if (std.mem.eql(u8, arg, "--memory-threshold")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --memory-threshold requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
const threshold_str = args[i];
self.options.memory_threshold = std.fmt.parseInt(usize, threshold_str, 10) catch {
std.debug.print("Error: --memory-threshold must be a valid number\n", .{});
return CLIError.InvalidArgument;
};
} else if (std.mem.eql(u8, arg, "--fail-on-leak")) {
self.options.fail_on_leak = true;
} else if (std.mem.eql(u8, arg, "--config") or std.mem.eql(u8, arg, "-c")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --config requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
self.options.config = args[i];
} else if (std.mem.eql(u8, arg, "--junit-output")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --junit-output requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
self.options.junit_output = args[i];
} else if (std.mem.eql(u8, arg, "--timeout")) {
if (i + 1 >= args.len) {
std.debug.print("Error: --timeout requires a value\n", .{});
return CLIError.MissingValue;
}
i += 1;
const timeout_str = args[i];
self.options.timeout = std.fmt.parseInt(u64, timeout_str, 10) catch {
std.debug.print("Error: --timeout must be a valid number (milliseconds)\n", .{});
return CLIError.InvalidArgument;
};
} else if (std.mem.startsWith(u8, arg, "--")) {
std.debug.print("Error: Unknown option '{s}'\n", .{arg});
return CLIError.InvalidArgument;
} else {
// Treat non-flag arguments as test directory path
self.options.test_dir = arg;
}
}
}
/// Print help message
pub fn printHelp(self: Self) void {
_ = self;
const help_text =
\\Zig Test Framework - A modern testing framework for Zig
\\
\\USAGE:
\\ zig-test [OPTIONS] [TEST_DIR]
\\
\\OPTIONS:
\\ -h, --help Show this help message
\\ -v, --version Show version information
\\ -b, --bail Stop test execution on first failure
\\ --filter <pattern> Run only tests matching pattern
\\ --grep <pattern> Same as --filter (alias)
\\ --reporter <name> Set reporter type (spec, dot, json, tap, junit)
\\ --verbose Enable verbose output
\\ -q, --quiet Minimal output
\\ --no-color Disable colored output
\\ --timeout <ms> Global timeout for all tests in milliseconds
\\ -c, --config <file> Load configuration from file
\\
\\TEST DISCOVERY:
\\ --test-dir <dir> Directory to search for tests (default: .)
\\ --pattern <pattern> Test file pattern (default: *.test.zig)
\\ --no-recursive Disable recursive directory search
\\
\\COVERAGE:
\\ --coverage Enable code coverage collection
\\ --coverage-dir <dir> Coverage output directory (default: coverage)
\\ --coverage-tool <tool> Coverage tool to use (default: kcov)
\\
\\PARALLEL EXECUTION:
\\ -p, --parallel Enable parallel test execution
\\ -j, --jobs <N> Number of parallel jobs (default: CPU count)
\\
\\WEB UI:
\\ --ui Enable web-based test UI
\\ --ui-port <port> UI server port (default: 8080)
\\ --ui-host <host> UI server host (default: 127.0.0.1)
\\
\\SNAPSHOT TESTING:
\\ -u, --update-snapshots Update snapshot files instead of comparing
\\ --snapshot-dir <dir> Snapshot directory (default: .snapshots)
\\
\\WATCH MODE:
\\ -w, --watch Watch files and re-run tests on changes
\\ --watch-debounce <ms> Debounce delay in milliseconds (default: 300)
\\
\\MEMORY PROFILING:
\\ --profile-memory Enable memory profiling for tests
\\ --memory-threshold <N> Report threshold in bytes (default: 0)
\\ --fail-on-leak Fail tests if memory leaks detected
\\
\\REPORTERS:
\\ spec Default hierarchical reporter with colors
\\ dot Minimal dot-based reporter
\\ json Machine-readable JSON output
\\ tap TAP (Test Anything Protocol) format
\\ junit JUnit XML format (use with --junit-output)
\\
\\JUNIT OPTIONS:
\\ --junit-output <file> Write JUnit XML to file
\\
\\EXAMPLES:
\\ zig-test Run all tests
\\ zig-test --filter "user" Run tests matching "user"
\\ zig-test --grep "auth" Run tests matching "auth"
\\ zig-test --reporter tap Output in TAP format
\\ zig-test --reporter junit --junit-output results.xml
\\ zig-test -u Update all snapshots (short flag)
\\ zig-test --update-snapshots Update all snapshots
\\ zig-test --watch Run in watch mode
\\ zig-test --timeout 10000 Set 10s timeout for all tests
\\ zig-test --profile-memory --fail-on-leak
\\ zig-test --parallel --jobs 4 Run with 4 parallel workers
\\ zig-test --config zig-test.json Load config from file
\\ zig-test --ui --parallel --coverage All features together
\\
;
std.debug.print("{s}\n", .{help_text});
}
/// Print version information
pub fn printVersion(self: Self) void {
_ = self;
std.debug.print("Zig Test Framework v2.5.0\n", .{});
std.debug.print("Features: Parallel, Coverage, UI, Snapshots, Watch, Memory Profiling, TAP/JUnit, Progress Indicators, Timeout\n", .{});
}
/// Convert CLI options to RunnerOptions
pub fn toRunnerOptions(self: Self) test_runner.RunnerOptions {
return test_runner.RunnerOptions{
.bail = self.options.bail,
.filter = self.options.filter,
.reporter_type = self.options.reporter,
.use_colors = !self.options.no_color,
.parallel = self.options.parallel,
.n_jobs = self.options.jobs,
};
}
};
test "CLI parse help" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--help" };
try cli.parse(&args);
try std.testing.expect(cli.options.help);
}
test "CLI parse version" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "-v" };
try cli.parse(&args);
try std.testing.expect(cli.options.version);
}
test "CLI parse bail" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--bail" };
try cli.parse(&args);
try std.testing.expect(cli.options.bail);
}
test "CLI parse reporter" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--reporter", "json" };
try cli.parse(&args);
try std.testing.expectEqual(test_runner.ReporterType.json, cli.options.reporter);
}
test "CLI parse filter" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--filter", "user" };
try cli.parse(&args);
try std.testing.expect(cli.options.filter != null);
try std.testing.expectEqualStrings("user", cli.options.filter.?);
}
test "CLI parse coverage flag" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--coverage" };
try cli.parse(&args);
try std.testing.expect(cli.options.coverage);
}
test "CLI parse coverage-dir" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--coverage-dir", "my-coverage" };
try cli.parse(&args);
try std.testing.expectEqualStrings("my-coverage", cli.options.coverage_dir);
}
test "CLI parse coverage-tool" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--coverage-tool", "grindcov" };
try cli.parse(&args);
try std.testing.expectEqualStrings("grindcov", cli.options.coverage_tool);
}
test "CLI parse all coverage options" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--coverage", "--coverage-dir", "cov", "--coverage-tool", "kcov" };
try cli.parse(&args);
try std.testing.expect(cli.options.coverage);
try std.testing.expectEqualStrings("cov", cli.options.coverage_dir);
try std.testing.expectEqualStrings("kcov", cli.options.coverage_tool);
}
test "CLI parse test-dir option" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--test-dir", "tests" };
try cli.parse(&args);
try std.testing.expect(cli.options.test_dir != null);
try std.testing.expectEqualStrings("tests", cli.options.test_dir.?);
}
test "CLI parse pattern option" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--pattern", "*.spec.zig" };
try cli.parse(&args);
try std.testing.expectEqualStrings("*.spec.zig", cli.options.pattern);
}
test "CLI parse no-recursive option" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--no-recursive" };
try cli.parse(&args);
try std.testing.expect(cli.options.no_recursive);
}
test "CLI parse multiple options" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{
"zig-test",
"--test-dir",
"tests",
"--coverage",
"--bail",
"--verbose",
};
try cli.parse(&args);
try std.testing.expect(cli.options.test_dir != null);
try std.testing.expectEqualStrings("tests", cli.options.test_dir.?);
try std.testing.expect(cli.options.coverage);
try std.testing.expect(cli.options.bail);
try std.testing.expect(cli.options.verbose);
}
test "CLI default values" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{"zig-test"};
try cli.parse(&args);
try std.testing.expect(!cli.options.help);
try std.testing.expect(!cli.options.version);
try std.testing.expect(!cli.options.bail);
try std.testing.expect(!cli.options.coverage);
try std.testing.expectEqualStrings("coverage", cli.options.coverage_dir);
try std.testing.expectEqualStrings("kcov", cli.options.coverage_tool);
try std.testing.expectEqualStrings("*.test.zig", cli.options.pattern);
try std.testing.expect(!cli.options.ui);
try std.testing.expectEqual(@as(u16, 8080), cli.options.ui_port);
try std.testing.expectEqualStrings("127.0.0.1", cli.options.ui_host);
}
test "CLI parse ui flag" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--ui" };
try cli.parse(&args);
try std.testing.expect(cli.options.ui);
}
test "CLI parse ui-port" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--ui-port", "3000" };
try cli.parse(&args);
try std.testing.expectEqual(@as(u16, 3000), cli.options.ui_port);
}
test "CLI parse ui-host" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--ui-host", "0.0.0.0" };
try cli.parse(&args);
try std.testing.expectEqualStrings("0.0.0.0", cli.options.ui_host);
}
test "CLI parse all ui options" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--ui", "--ui-port", "9000", "--ui-host", "localhost" };
try cli.parse(&args);
try std.testing.expect(cli.options.ui);
try std.testing.expectEqual(@as(u16, 9000), cli.options.ui_port);
try std.testing.expectEqualStrings("localhost", cli.options.ui_host);
}
test "CLI parse parallel flag" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--parallel" };
try cli.parse(&args);
try std.testing.expect(cli.options.parallel);
}
test "CLI parse jobs" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--jobs", "4" };
try cli.parse(&args);
try std.testing.expectEqual(@as(?usize, 4), cli.options.jobs);
}
test "CLI parse grep alias" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--grep", "auth" };
try cli.parse(&args);
try std.testing.expect(cli.options.filter != null);
try std.testing.expectEqualStrings("auth", cli.options.filter.?);
}
test "CLI parse timeout" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "--timeout", "5000" };
try cli.parse(&args);
try std.testing.expectEqual(@as(?u64, 5000), cli.options.timeout);
}
test "CLI parse update-snapshots short flag" {
var cli = CLI.init(std.testing.allocator);
const args = [_][]const u8{ "zig-test", "-u" };
try cli.parse(&args);
try std.testing.expect(cli.options.update_snapshots);
}