-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.zig
More file actions
366 lines (313 loc) · 11.7 KB
/
Copy pathconfig.zig
File metadata and controls
366 lines (313 loc) · 11.7 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
const std = @import("std");
const compat = @import("compat.zig");
/// Configuration file format
pub const ConfigFormat = enum {
json,
toml, // Future support
};
/// Test framework configuration
pub const TestConfig = struct {
// Test discovery options
test_options: TestOptions = .{},
/// Parallel execution options
parallel: ParallelOptions = .{},
/// Reporter options
reporter: ReporterOptions = .{},
/// Snapshot testing options
snapshot: SnapshotOptions = .{},
/// Watch mode options
watch: WatchOptions = .{},
/// Memory profiling options
memory: MemoryOptions = .{},
/// UI server options
ui: UIOptions = .{},
/// Coverage options
coverage: CoverageOptions = .{},
};
pub const TestOptions = struct {
/// Test file pattern
pattern: []const u8 = "*.test.zig",
/// Test directory
test_dir: []const u8 = "src",
/// Recursive search
recursive: bool = true,
/// Filter tests by name
filter: ?[]const u8 = null,
/// Timeout per test in milliseconds
timeout: u64 = 5000,
};
pub const ParallelOptions = struct {
/// Enable parallel execution
enabled: bool = false,
/// Number of worker threads (0 = auto-detect)
jobs: usize = 0,
};
pub const ReporterOptions = struct {
/// Reporter type: spec, dot, json, tap, junit
reporter: []const u8 = "spec",
/// JUnit XML output file
junit_output: ?[]const u8 = null,
/// Verbose output
verbose: bool = false,
};
pub const SnapshotOptions = struct {
/// Snapshot directory
snapshot_dir: []const u8 = ".snapshots",
/// Update snapshots
update: bool = false,
/// Pretty print snapshots
pretty_print: bool = true,
};
pub const WatchOptions = struct {
/// Enable watch mode
enabled: bool = false,
/// Directory to watch
watch_dir: []const u8 = ".",
/// Debounce delay in milliseconds
debounce_ms: u64 = 300,
/// Clear screen between runs
clear_screen: bool = true,
};
pub const MemoryOptions = struct {
/// Enable memory profiling
enabled: bool = false,
/// Detect memory leaks
detect_leaks: bool = true,
/// Track peak memory usage
track_peak: bool = true,
/// Report threshold in bytes
report_threshold: usize = 0,
/// Fail tests on memory leaks
fail_on_leak: bool = false,
};
pub const UIOptions = struct {
/// Enable UI server
enabled: bool = false,
/// Port for UI server
port: u16 = 8080,
/// Open browser automatically
open_browser: bool = true,
};
pub const CoverageOptions = struct {
/// Enable coverage tracking
enabled: bool = false,
/// Coverage output directory
output_dir: []const u8 = "coverage",
/// Minimum coverage threshold
threshold: f64 = 0.0,
};
/// Configuration loader
pub const ConfigLoader = struct {
allocator: std.mem.Allocator,
const Self = @This();
pub fn init(allocator: std.mem.Allocator) Self {
return .{
.allocator = allocator,
};
}
/// Load configuration from file
pub fn loadFromFile(self: *Self, path: []const u8) !TestConfig {
// Detect format from extension
const format = if (std.mem.endsWith(u8, path, ".json"))
ConfigFormat.json
else if (std.mem.endsWith(u8, path, ".toml"))
ConfigFormat.toml
else
return error.UnsupportedFormat;
switch (format) {
.json => return try self.loadFromJson(path),
.toml => return error.TomlNotImplementedYet,
}
}
/// Load configuration from JSON file
fn loadFromJson(self: *Self, path: []const u8) !TestConfig {
const content = try compat.readFileAlloc(self.allocator, path);
defer self.allocator.free(content);
var parsed = try std.json.parseFromSlice(std.json.Value, self.allocator, content, .{});
defer parsed.deinit();
return try self.parseConfig(parsed.value);
}
/// Parse configuration from JSON value
fn parseConfig(self: *Self, value: std.json.Value) !TestConfig {
_ = self;
var config = TestConfig{};
if (value != .object) return config;
const root = value.object;
// Parse test options
if (root.get("test")) |test_value| {
if (test_value == .object) {
const test_obj = test_value.object;
if (test_obj.get("pattern")) |v| {
if (v == .string) config.test_options.pattern = v.string;
}
if (test_obj.get("test_dir")) |v| {
if (v == .string) config.test_options.test_dir = v.string;
}
if (test_obj.get("recursive")) |v| {
if (v == .bool) config.test_options.recursive = v.bool;
}
if (test_obj.get("timeout")) |v| {
if (v == .integer) config.test_options.timeout = @intCast(v.integer);
}
}
}
// Parse parallel options
if (root.get("parallel")) |parallel_value| {
if (parallel_value == .object) {
const parallel_obj = parallel_value.object;
if (parallel_obj.get("enabled")) |v| {
if (v == .bool) config.parallel.enabled = v.bool;
}
if (parallel_obj.get("jobs")) |v| {
if (v == .integer) config.parallel.jobs = @intCast(v.integer);
}
}
}
// Parse reporter options
if (root.get("reporter")) |reporter_value| {
if (reporter_value == .object) {
const reporter_obj = reporter_value.object;
if (reporter_obj.get("reporter")) |v| {
if (v == .string) config.reporter.reporter = v.string;
}
if (reporter_obj.get("junit_output")) |v| {
if (v == .string) config.reporter.junit_output = v.string;
}
if (reporter_obj.get("verbose")) |v| {
if (v == .bool) config.reporter.verbose = v.bool;
}
}
}
// Parse snapshot options
if (root.get("snapshot")) |snapshot_value| {
if (snapshot_value == .object) {
const snapshot_obj = snapshot_value.object;
if (snapshot_obj.get("snapshot_dir")) |v| {
if (v == .string) config.snapshot.snapshot_dir = v.string;
}
if (snapshot_obj.get("update")) |v| {
if (v == .bool) config.snapshot.update = v.bool;
}
if (snapshot_obj.get("pretty_print")) |v| {
if (v == .bool) config.snapshot.pretty_print = v.bool;
}
}
}
// Parse watch options
if (root.get("watch")) |watch_value| {
if (watch_value == .object) {
const watch_obj = watch_value.object;
if (watch_obj.get("enabled")) |v| {
if (v == .bool) config.watch.enabled = v.bool;
}
if (watch_obj.get("watch_dir")) |v| {
if (v == .string) config.watch.watch_dir = v.string;
}
if (watch_obj.get("debounce_ms")) |v| {
if (v == .integer) config.watch.debounce_ms = @intCast(v.integer);
}
if (watch_obj.get("clear_screen")) |v| {
if (v == .bool) config.watch.clear_screen = v.bool;
}
}
}
// Parse memory options
if (root.get("memory")) |memory_value| {
if (memory_value == .object) {
const memory_obj = memory_value.object;
if (memory_obj.get("enabled")) |v| {
if (v == .bool) config.memory.enabled = v.bool;
}
if (memory_obj.get("detect_leaks")) |v| {
if (v == .bool) config.memory.detect_leaks = v.bool;
}
if (memory_obj.get("track_peak")) |v| {
if (v == .bool) config.memory.track_peak = v.bool;
}
if (memory_obj.get("report_threshold")) |v| {
if (v == .integer) config.memory.report_threshold = @intCast(v.integer);
}
if (memory_obj.get("fail_on_leak")) |v| {
if (v == .bool) config.memory.fail_on_leak = v.bool;
}
}
}
// Parse UI options
if (root.get("ui")) |ui_value| {
if (ui_value == .object) {
const ui_obj = ui_value.object;
if (ui_obj.get("enabled")) |v| {
if (v == .bool) config.ui.enabled = v.bool;
}
if (ui_obj.get("port")) |v| {
if (v == .integer) config.ui.port = @intCast(v.integer);
}
if (ui_obj.get("open_browser")) |v| {
if (v == .bool) config.ui.open_browser = v.bool;
}
}
}
// Parse coverage options
if (root.get("coverage")) |coverage_value| {
if (coverage_value == .object) {
const coverage_obj = coverage_value.object;
if (coverage_obj.get("enabled")) |v| {
if (v == .bool) config.coverage.enabled = v.bool;
}
if (coverage_obj.get("output_dir")) |v| {
if (v == .string) config.coverage.output_dir = v.string;
}
if (coverage_obj.get("threshold")) |v| {
if (v == .float) config.coverage.threshold = v.float;
}
}
}
return config;
}
/// Try to find and load configuration file
pub fn autoLoad(self: *Self, config_name: []const u8) !?TestConfig {
const search_paths = [_][]const u8{
"zig-test.json",
".zig-test.json",
"config/zig-test.json",
".config/zig-test.json",
};
for (search_paths) |path| {
const full_path = if (std.mem.eql(u8, config_name, "zig-test"))
path
else
try std.fmt.allocPrint(self.allocator, "{s}.json", .{config_name});
defer if (!std.mem.eql(u8, config_name, "zig-test")) self.allocator.free(full_path);
const config = self.loadFromFile(full_path) catch |err| {
if (err == error.FileNotFound) continue;
return err;
};
return config;
}
return null;
}
};
// Tests
test "TestConfig default values" {
const config = TestConfig{};
try std.testing.expectEqualStrings("*.test.zig", config.test_options.pattern);
try std.testing.expectEqualStrings("src", config.test_options.test_dir);
try std.testing.expectEqual(true, config.test_options.recursive);
try std.testing.expectEqual(false, config.parallel.enabled);
try std.testing.expectEqualStrings("spec", config.reporter.reporter);
}
test "ConfigLoader initialization" {
const allocator = std.testing.allocator;
const loader = ConfigLoader.init(allocator);
try std.testing.expect(loader.allocator.ptr == allocator.ptr);
}
test "ConfigLoader parse empty JSON" {
const allocator = std.testing.allocator;
var loader = ConfigLoader.init(allocator);
var parsed = try std.json.parseFromSlice(std.json.Value, allocator, "{}", .{});
defer parsed.deinit();
const config = try loader.parseConfig(parsed.value);
// Should return defaults
try std.testing.expectEqualStrings("spec", config.reporter.reporter);
try std.testing.expectEqual(false, config.parallel.enabled);
}