also looking at this
fix: honor discovery-mode CLI options
#17
10 files
+192
-24
| @@ -12,6 +12,8 @@ pub const LoaderOptions = struct { | ||
| 12 | 12 | filter: ?[]const u8 = null, |
| 13 | 13 | /// Whether to show verbose output |
| 14 | 14 | verbose: bool = false, |
| 15 | /// Whether child Zig test processes may emit colors | |
| 16 | use_colors: bool = true, | |
| 15 | 17 | /// Coverage options |
| 16 | 18 | coverage_options: ?coverage.CoverageOptions = null, |
| 17 | 19 | /// UI server for real-time updates |
| @@ -133,27 +135,48 @@ fn runTestFile( | ||
| 133 | 135 | file_path: []const u8, |
| 134 | 136 | options: LoaderOptions, |
| 135 | 137 | ) !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 | ||
| 136 | 143 | // If coverage is enabled, use coverage.runTestWithCoverage |
| 137 | 144 | if (options.coverage_options) |cov_opts| { |
| 138 | 145 | if (cov_opts.enabled) { |
| 139 | 146 | // Disable clean for individual test runs since we cleaned once at the start |
| 140 | 147 | var modified_opts = cov_opts; |
| 141 | 148 | 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| { | |
| 143 | 150 | std.debug.print("Warning: Coverage failed for {s}: {any}\n", .{ file_path, err }); |
| 144 | 151 | // Fall back to running without coverage |
| 145 | return runTestFileWithoutCoverage(allocator, file_path); | |
| 152 | return runTestFileWithoutCoverage(allocator, file_path, test_args.items); | |
| 146 | 153 | }; |
| 147 | 154 | } |
| 148 | 155 | } |
| 149 | 156 | |
| 150 | return runTestFileWithoutCoverage(allocator, file_path); | |
| 157 | return runTestFileWithoutCoverage(allocator, file_path, test_args.items); | |
| 158 | } | |
| 159 | ||
| 160 | fn 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 | } | |
| 151 | 173 | } |
| 152 | 174 | |
| 153 | 175 | /// Run a single test file without coverage |
| 154 | 176 | fn runTestFileWithoutCoverage( |
| 155 | 177 | allocator: std.mem.Allocator, |
| 156 | 178 | file_path: []const u8, |
| 179 | test_args: []const []const u8, | |
| 157 | 180 | ) !bool { |
| 158 | 181 | // Build zig test command |
| 159 | 182 | var argv: std.ArrayList([]const u8) = .empty; |
| @@ -162,6 +185,7 @@ fn runTestFileWithoutCoverage( | ||
| 162 | 185 | try argv.append(allocator, "zig"); |
| 163 | 186 | try argv.append(allocator, "test"); |
| 164 | 187 | try argv.append(allocator, file_path); |
| 188 | try argv.appendSlice(allocator, test_args); | |
| 165 | 189 | |
| 166 | 190 | // Run the test |
| 167 | 191 | const term = try compat.spawnAndWait(allocator, argv.items, .Inherit, .Inherit); |
| @@ -182,9 +206,27 @@ test "LoaderOptions default values" { | ||
| 182 | 206 | try std.testing.expectEqual(false, options.bail); |
| 183 | 207 | try std.testing.expectEqual(@as(?[]const u8, null), options.filter); |
| 184 | 208 | try std.testing.expectEqual(false, options.verbose); |
| 209 | try std.testing.expectEqual(true, options.use_colors); | |
| 185 | 210 | try std.testing.expectEqual(@as(?coverage.CoverageOptions, null), options.coverage_options); |
| 186 | 211 | } |
| 187 | 212 | |
| 213 | test "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 | ||
| 188 | 230 | test "LoaderOptions with custom values" { |
| 189 | 231 | const cov_opts = coverage.CoverageOptions{ |
| 190 | 232 | .enabled = true, |