Commit 3b51b43dc8

Alex Rønne Petersen <alex@alexrp.com>
2024-08-13 23:59:50
build/test: Add -Dtest-target-filter for filtering module tests by target triple.
This is useful during porting work where you're not interested in running all targets all the time while iterating on changes.
1 parent 4a2b23c
Changed files (2)
test/tests.zig
@@ -965,6 +965,7 @@ pub fn addRunTranslatedCTests(
 
 const ModuleTestOptions = struct {
     test_filters: []const []const u8,
+    test_target_filters: []const []const u8,
     root_src: []const u8,
     name: []const u8,
     desc: []const u8,
@@ -986,6 +987,13 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
 
         const resolved_target = b.resolveTargetQuery(test_target.target);
         const target = resolved_target.result;
+        const triple_txt = target.zigTriple(b.allocator) catch @panic("OOM");
+
+        if (options.test_target_filters.len > 0) {
+            for (options.test_target_filters) |filter| {
+                if (std.mem.indexOf(u8, triple_txt, filter) != null) break;
+            } else continue;
+        }
 
         if (options.skip_libc and test_target.link_libc == true)
             continue;
@@ -1034,7 +1042,6 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
         if (!want_this_mode) continue;
 
         const libc_suffix = if (test_target.link_libc == true) "-libc" else "";
-        const triple_txt = target.zigTriple(b.allocator) catch @panic("OOM");
         const model_txt = target.cpu.model.name;
 
         // wasm32-wasi builds need more RAM, idk why
build.zig
@@ -379,6 +379,7 @@ pub fn build(b: *std.Build) !void {
     }
 
     const test_filters = b.option([]const []const u8, "test-filter", "Skip tests that do not match any filter") orelse &[0][]const u8{};
+    const test_target_filters = b.option([]const []const u8, "test-target-filter", "Skip tests whose target triple do not match any filter") orelse &[0][]const u8{};
 
     const test_cases_options = b.addOptions();
 
@@ -457,6 +458,7 @@ pub fn build(b: *std.Build) !void {
 
     test_step.dependOn(tests.addModuleTests(b, .{
         .test_filters = test_filters,
+        .test_target_filters = test_target_filters,
         .root_src = "test/behavior.zig",
         .name = "behavior",
         .desc = "Run the behavior tests",
@@ -470,6 +472,7 @@ pub fn build(b: *std.Build) !void {
 
     test_step.dependOn(tests.addModuleTests(b, .{
         .test_filters = test_filters,
+        .test_target_filters = test_target_filters,
         .root_src = "test/c_import.zig",
         .name = "c-import",
         .desc = "Run the @cImport tests",
@@ -482,6 +485,7 @@ pub fn build(b: *std.Build) !void {
 
     test_step.dependOn(tests.addModuleTests(b, .{
         .test_filters = test_filters,
+        .test_target_filters = test_target_filters,
         .root_src = "lib/compiler_rt.zig",
         .name = "compiler-rt",
         .desc = "Run the compiler_rt tests",
@@ -495,6 +499,7 @@ pub fn build(b: *std.Build) !void {
 
     test_step.dependOn(tests.addModuleTests(b, .{
         .test_filters = test_filters,
+        .test_target_filters = test_target_filters,
         .root_src = "lib/c.zig",
         .name = "universal-libc",
         .desc = "Run the universal libc tests",
@@ -521,6 +526,7 @@ pub fn build(b: *std.Build) !void {
     test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filters, optimization_modes));
     test_step.dependOn(tests.addModuleTests(b, .{
         .test_filters = test_filters,
+        .test_target_filters = test_target_filters,
         .root_src = "lib/std/std.zig",
         .name = "std",
         .desc = "Run the standard library tests",