authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-02-11 14:40:14+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-02-11 14:40:14+02:00
logba680aa98737ae1f572de3bf0b7ef7a7da27ed31
tree911c9dad4cd6b3f9e0b8484c45ec5fb96bbdf6fc
parentc63be507cf0e9ccbd45cc70e4dc0ac619930733c
parent948754c5d4b707c991c4db9cdfd968e6f4866ea4
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14588 from dweiller/test-runner-imports

fix custom test runner file import path resolution

13 files changed, 82 insertions(+), 10 deletions(-)

src/Compilation.zig+15-10
......@@ -1621,16 +1621,21 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16211621 const root_pkg = if (options.is_test) root_pkg: {
16221622 // TODO: we currently have two packages named 'root' here, which is weird. This
16231623 // should be changed as part of the resolution of #12201
1624 const test_pkg = if (options.test_runner_path) |test_runner|
1625 try Package.create(gpa, "root", null, test_runner)
1626 else
1627 try Package.createWithDir(
1628 gpa,
1629 "root",
1630 options.zig_lib_directory,
1631 null,
1632 "test_runner.zig",
1633 );
1624 const test_pkg = if (options.test_runner_path) |test_runner| test_pkg: {
1625 const test_dir = std.fs.path.dirname(test_runner);
1626 const basename = std.fs.path.basename(test_runner);
1627 const pkg = try Package.create(gpa, "root", test_dir, basename);
1628
1629 // copy package table from main_pkg to root_pkg
1630 pkg.table = try main_pkg.table.clone(gpa);
1631 break :test_pkg pkg;
1632 } else try Package.createWithDir(
1633 gpa,
1634 "root",
1635 options.zig_lib_directory,
1636 null,
1637 "test_runner.zig",
1638 );
16341639 errdefer test_pkg.destroy(gpa);
16351640
16361641 break :root_pkg test_pkg;
test/standalone.zig+1
......@@ -27,6 +27,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
2727 cases.add("test/standalone/noreturn_call/inline.zig");
2828 cases.add("test/standalone/noreturn_call/as_arg.zig");
2929 cases.addBuildFile("test/standalone/test_runner_path/build.zig", .{ .requires_stage2 = true });
30 cases.addBuildFile("test/standalone/issue_13970/build.zig", .{});
3031 cases.addBuildFile("test/standalone/main_pkg_path/build.zig", .{});
3132 cases.addBuildFile("test/standalone/shared_library/build.zig", .{});
3233 cases.addBuildFile("test/standalone/mix_o_files/build.zig", .{});
test/standalone/issue_13970/build.zig created+21
......@@ -0,0 +1,21 @@
1const std = @import("std");
2
3pub fn build(b: *std.Build) void {
4 const test1 = b.addTest(.{
5 .root_source_file = .{ .path = "test_root/empty.zig" },
6 });
7 const test2 = b.addTest(.{
8 .root_source_file = .{ .path = "src/empty.zig" },
9 });
10 const test3 = b.addTest(.{
11 .root_source_file = .{ .path = "empty.zig" },
12 });
13 test1.setTestRunner("src/main.zig");
14 test2.setTestRunner("src/main.zig");
15 test3.setTestRunner("src/main.zig");
16
17 const test_step = b.step("test", "Test package path resolution of custom test runner");
18 test_step.dependOn(&test1.step);
19 test_step.dependOn(&test2.step);
20 test_step.dependOn(&test3.step);
21}
test/standalone/issue_13970/empty.zig created
test/standalone/issue_13970/src/empty.zig created
test/standalone/issue_13970/src/main.zig created+8
......@@ -0,0 +1,8 @@
1const std = @import("std");
2const package = @import("package.zig");
3const root = @import("root");
4const builtin = @import("builtin");
5
6pub fn main() !void {
7 _ = package.decl;
8}
test/standalone/issue_13970/src/package.zig created+1
......@@ -0,0 +1 @@
1pub const decl = 0;
test/standalone/issue_13970/test_root/empty.zig created
test/standalone/test_runner_module_imports/build.zig created+19
......@@ -0,0 +1,19 @@
1const std = @import("std");
2
3pub fn build(b: *std.Build) void {
4 const t = b.addTest(.{
5 .root_source_file = .{ .path = "src/main.zig" },
6 });
7 t.setTestRunner("test_runner/main.zig");
8
9 const module1 = b.createModule(.{ .source_file = .{ .path = "module1/main.zig" } });
10 const module2 = b.createModule(.{
11 .source_file = .{ .path = "module2/main.zig" },
12 .dependencies = &.{.{ .name = "module1", .module = module1 }},
13 });
14
15 t.addModule("module2", module2);
16
17 const test_step = b.step("test", "Run unit tests");
18 test_step.dependOn(&t.step);
19}
test/standalone/test_runner_module_imports/module1/main.zig created+1
......@@ -0,0 +1 @@
1pub const decl: usize = 1234567890;
test/standalone/test_runner_module_imports/module2/main.zig created+1
......@@ -0,0 +1 @@
1pub const mod1 = @import("module1");
test/standalone/test_runner_module_imports/src/main.zig created+6
......@@ -0,0 +1,6 @@
1const mod2 = @import("module2");
2const std = @import("std");
3
4test {
5 try std.testing.expectEqual(@as(usize, 1234567890), mod2.mod1.decl);
6}
test/standalone/test_runner_module_imports/test_runner/main.zig created+9
......@@ -0,0 +1,9 @@
1const std = @import("std");
2const mod2 = @import("module2");
3
4pub fn main() !void {
5 try std.testing.expectEqual(@as(usize, 1234567890), mod2.mod1.decl);
6 for (@import("builtin").test_functions) |test_fn| {
7 try test_fn.func();
8 }
9}