authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-08 13:56:03+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-08 14:30:37+11:00
log1f7390f3999e80f775dbc0e62f1dcb071c3bed77
tree825d01e81a5f51759798ae62339c5639d1ca33ee
parent9ccd8ed0ad4cc9e68c2a2e0c9b1e32d50259357e

fix custom test runner package path resolution

Fixes #13970. This fix makes test runners resolve package paths relative to the directory the test runner is in. This means it is not possible to import a file from outside the file tree root at the directory containing the test runner.

8 files changed, 36 insertions(+), 2 deletions(-)

src/Compilation.zig+5-2
......@@ -1621,8 +1621,11 @@ 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)
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 break :test_pkg try Package.create(gpa, "root", test_dir, basename);
1628 }
16261629 else
16271630 try Package.createWithDir(
16281631 gpa,
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