Commit edc0e84270

dweiller <4678790+dweiller@users.noreplay.github.com>
2023-02-08 05:04:29
allow custom test runners to import modules
1 parent 1f7390f
Changed files (6)
src
test
standalone
test_runner_module_imports
module1
module2
src
test_runner
src/Compilation.zig
@@ -1624,16 +1624,21 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
                 const test_pkg = if (options.test_runner_path) |test_runner| test_pkg: {
                     const test_dir = std.fs.path.dirname(test_runner);
                     const basename = std.fs.path.basename(test_runner);
-                    break :test_pkg try Package.create(gpa, "root", test_dir, basename);
-                }
-                else
-                    try Package.createWithDir(
-                        gpa,
-                        "root",
-                        options.zig_lib_directory,
-                        null,
-                        "test_runner.zig",
-                    );
+                    const pkg = try Package.create(gpa, "root", test_dir, basename);
+
+                    // copy package table from main_pkg to root_pkg
+                    var iter = main_pkg.table.valueIterator();
+                    while (iter.next()) |v| {
+                        try pkg.add(gpa, v.*);
+                    }
+                    break :test_pkg pkg;
+                } else try Package.createWithDir(
+                    gpa,
+                    "root",
+                    options.zig_lib_directory,
+                    null,
+                    "test_runner.zig",
+                );
                 errdefer test_pkg.destroy(gpa);
 
                 break :root_pkg test_pkg;
test/standalone/test_runner_module_imports/module1/main.zig
@@ -0,0 +1,1 @@
+pub const decl: usize = 1234567890;
test/standalone/test_runner_module_imports/module2/main.zig
@@ -0,0 +1,1 @@
+pub const mod1 = @import("module1");
test/standalone/test_runner_module_imports/src/main.zig
@@ -0,0 +1,6 @@
+const mod2 = @import("module2");
+const std = @import("std");
+
+test {
+    try std.testing.expectEqual(@as(usize, 1234567890), mod2.mod1.decl);
+}
test/standalone/test_runner_module_imports/test_runner/main.zig
@@ -0,0 +1,9 @@
+const std = @import("std");
+const mod2 = @import("module2");
+
+pub fn main() !void {
+    try std.testing.expectEqual(@as(usize, 1234567890), mod2.mod1.decl);
+    for (@import("builtin").test_functions) |test_fn| {
+        try test_fn.func();
+    }
+}
test/standalone/test_runner_module_imports/build.zig
@@ -0,0 +1,19 @@
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+    const t = b.addTest(.{
+        .root_source_file = .{ .path = "src/main.zig" },
+    });
+    t.setTestRunner("test_runner/main.zig");
+
+    const module1 = b.createModule(.{ .source_file = .{ .path = "module1/main.zig" } });
+    const module2 = b.createModule(.{
+        .source_file = .{ .path = "module2/main.zig" },
+        .dependencies = &.{.{ .name = "module1", .module = module1 }},
+    });
+
+    t.addModule("module2", module2);
+
+    const test_step = b.step("test", "Run unit tests");
+    test_step.dependOn(&t.step);
+}