ReviewOS

also looking at this

zig-utils/zig-test-framework

fix: honor discovery-mode CLI options

#17
Merged glennmichael123 wants to merge codex/issue-2-discovery-cli into main
10 files +192 -24
src/test_loader.zigmodified+45-3
Changes to src/test_loader.zig
@@ -12,6 +12,8 @@ pub const LoaderOptions = struct {
1212 filter: ?[]const u8 = null,
1313 /// Whether to show verbose output
1414 verbose: bool = false,
15 /// Whether child Zig test processes may emit colors
16 use_colors: bool = true,
1517 /// Coverage options
1618 coverage_options: ?coverage.CoverageOptions = null,
1719 /// UI server for real-time updates
@@ -133,27 +135,48 @@ fn runTestFile(
133135 file_path: []const u8,
134136 options: LoaderOptions,
135137) !bool {
138 var test_args: std.ArrayList([]const u8) = .empty;
139 defer test_args.deinit(allocator);
140
141 try appendTestArgs(allocator, &test_args, options);
142
136143 // If coverage is enabled, use coverage.runTestWithCoverage
137144 if (options.coverage_options) |cov_opts| {
138145 if (cov_opts.enabled) {
139146 // Disable clean for individual test runs since we cleaned once at the start
140147 var modified_opts = cov_opts;
141148 modified_opts.clean = false;
142 return coverage.runTestWithCoverage(allocator, file_path, modified_opts) catch |err| {
149 return coverage.runTestWithCoverageArgs(allocator, file_path, test_args.items, modified_opts) catch |err| {
143150 std.debug.print("Warning: Coverage failed for {s}: {any}\n", .{ file_path, err });
144151 // Fall back to running without coverage
145 return runTestFileWithoutCoverage(allocator, file_path);
152 return runTestFileWithoutCoverage(allocator, file_path, test_args.items);
146153 };
147154 }
148155 }
149156
150 return runTestFileWithoutCoverage(allocator, file_path);
157 return runTestFileWithoutCoverage(allocator, file_path, test_args.items);
158}
159
160fn appendTestArgs(
161 allocator: std.mem.Allocator,
162 args: *std.ArrayList([]const u8),
163 options: LoaderOptions,
164) !void {
165 if (options.filter) |filter| {
166 try args.append(allocator, "--test-filter");
167 try args.append(allocator, filter);
168 }
169 if (!options.use_colors) {
170 try args.append(allocator, "--color");
171 try args.append(allocator, "off");
172 }
151173}
152174
153175/// Run a single test file without coverage
154176fn runTestFileWithoutCoverage(
155177 allocator: std.mem.Allocator,
156178 file_path: []const u8,
179 test_args: []const []const u8,
157180) !bool {
158181 // Build zig test command
159182 var argv: std.ArrayList([]const u8) = .empty;
@@ -162,6 +185,7 @@ fn runTestFileWithoutCoverage(
162185 try argv.append(allocator, "zig");
163186 try argv.append(allocator, "test");
164187 try argv.append(allocator, file_path);
188 try argv.appendSlice(allocator, test_args);
165189
166190 // Run the test
167191 const term = try compat.spawnAndWait(allocator, argv.items, .Inherit, .Inherit);
@@ -182,9 +206,27 @@ test "LoaderOptions default values" {
182206 try std.testing.expectEqual(false, options.bail);
183207 try std.testing.expectEqual(@as(?[]const u8, null), options.filter);
184208 try std.testing.expectEqual(false, options.verbose);
209 try std.testing.expectEqual(true, options.use_colors);
185210 try std.testing.expectEqual(@as(?coverage.CoverageOptions, null), options.coverage_options);
186211}
187212
213test "test command options forward filter and color" {
214 const allocator = std.testing.allocator;
215 var args: std.ArrayList([]const u8) = .empty;
216 defer args.deinit(allocator);
217
218 try appendTestArgs(allocator, &args, .{
219 .filter = "selected test",
220 .use_colors = false,
221 });
222
223 try std.testing.expectEqual(@as(usize, 4), args.items.len);
224 try std.testing.expectEqualStrings("--test-filter", args.items[0]);
225 try std.testing.expectEqualStrings("selected test", args.items[1]);
226 try std.testing.expectEqualStrings("--color", args.items[2]);
227 try std.testing.expectEqualStrings("off", args.items[3]);
228}
229
188230test "LoaderOptions with custom values" {
189231 const cov_opts = coverage.CoverageOptions{
190232 .enabled = true,