From 1f7390f3999e80f775dbc0e62f1dcb071c3bed77 Mon Sep 17 00:00:00 2001 From: dweiller <4678790+dweiller@users.noreplay.github.com> Date: Wed, 8 Feb 2023 13:56:03 +1100 Subject: [PATCH 1/3] 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. --- src/Compilation.zig | 7 +++++-- test/standalone.zig | 1 + test/standalone/issue_13970/build.zig | 21 +++++++++++++++++++ test/standalone/issue_13970/empty.zig | 0 test/standalone/issue_13970/src/empty.zig | 0 test/standalone/issue_13970/src/main.zig | 8 +++++++ test/standalone/issue_13970/src/package.zig | 1 + .../issue_13970/test_root/empty.zig | 0 8 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/standalone/issue_13970/build.zig create mode 100644 test/standalone/issue_13970/empty.zig create mode 100644 test/standalone/issue_13970/src/empty.zig create mode 100644 test/standalone/issue_13970/src/main.zig create mode 100644 test/standalone/issue_13970/src/package.zig create mode 100644 test/standalone/issue_13970/test_root/empty.zig diff --git a/src/Compilation.zig b/src/Compilation.zig index e09b8f18ab..8f265f9eb6 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1621,8 +1621,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { const root_pkg = if (options.is_test) root_pkg: { // TODO: we currently have two packages named 'root' here, which is weird. This // should be changed as part of the resolution of #12201 - const test_pkg = if (options.test_runner_path) |test_runner| - try Package.create(gpa, "root", null, test_runner) + 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, diff --git a/test/standalone.zig b/test/standalone.zig index af972ccb86..81eb1b0042 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -27,6 +27,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void { cases.add("test/standalone/noreturn_call/inline.zig"); cases.add("test/standalone/noreturn_call/as_arg.zig"); cases.addBuildFile("test/standalone/test_runner_path/build.zig", .{ .requires_stage2 = true }); + cases.addBuildFile("test/standalone/issue_13970/build.zig", .{}); cases.addBuildFile("test/standalone/main_pkg_path/build.zig", .{}); cases.addBuildFile("test/standalone/shared_library/build.zig", .{}); cases.addBuildFile("test/standalone/mix_o_files/build.zig", .{}); diff --git a/test/standalone/issue_13970/build.zig b/test/standalone/issue_13970/build.zig new file mode 100644 index 0000000000..f5e07d8903 --- /dev/null +++ b/test/standalone/issue_13970/build.zig @@ -0,0 +1,21 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const test1 = b.addTest(.{ + .root_source_file = .{ .path = "test_root/empty.zig" }, + }); + const test2 = b.addTest(.{ + .root_source_file = .{ .path = "src/empty.zig" }, + }); + const test3 = b.addTest(.{ + .root_source_file = .{ .path = "empty.zig" }, + }); + test1.setTestRunner("src/main.zig"); + test2.setTestRunner("src/main.zig"); + test3.setTestRunner("src/main.zig"); + + const test_step = b.step("test", "Test package path resolution of custom test runner"); + test_step.dependOn(&test1.step); + test_step.dependOn(&test2.step); + test_step.dependOn(&test3.step); +} diff --git a/test/standalone/issue_13970/empty.zig b/test/standalone/issue_13970/empty.zig new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/standalone/issue_13970/src/empty.zig b/test/standalone/issue_13970/src/empty.zig new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/standalone/issue_13970/src/main.zig b/test/standalone/issue_13970/src/main.zig new file mode 100644 index 0000000000..d71320d46a --- /dev/null +++ b/test/standalone/issue_13970/src/main.zig @@ -0,0 +1,8 @@ +const std = @import("std"); +const package = @import("package.zig"); +const root = @import("root"); +const builtin = @import("builtin"); + +pub fn main() !void { + _ = package.decl; +} diff --git a/test/standalone/issue_13970/src/package.zig b/test/standalone/issue_13970/src/package.zig new file mode 100644 index 0000000000..4f6037b38d --- /dev/null +++ b/test/standalone/issue_13970/src/package.zig @@ -0,0 +1 @@ +pub const decl = 0; diff --git a/test/standalone/issue_13970/test_root/empty.zig b/test/standalone/issue_13970/test_root/empty.zig new file mode 100644 index 0000000000..e69de29bb2 From edc0e84270c04acf432096c4772bfe199afdaf15 Mon Sep 17 00:00:00 2001 From: dweiller <4678790+dweiller@users.noreplay.github.com> Date: Wed, 8 Feb 2023 15:04:29 +1100 Subject: [PATCH 2/3] allow custom test runners to import modules --- src/Compilation.zig | 25 +++++++++++-------- .../test_runner_module_imports/build.zig | 19 ++++++++++++++ .../module1/main.zig | 1 + .../module2/main.zig | 1 + .../test_runner_module_imports/src/main.zig | 6 +++++ .../test_runner/main.zig | 9 +++++++ 6 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 test/standalone/test_runner_module_imports/build.zig create mode 100644 test/standalone/test_runner_module_imports/module1/main.zig create mode 100644 test/standalone/test_runner_module_imports/module2/main.zig create mode 100644 test/standalone/test_runner_module_imports/src/main.zig create mode 100644 test/standalone/test_runner_module_imports/test_runner/main.zig diff --git a/src/Compilation.zig b/src/Compilation.zig index 8f265f9eb6..f19e83680d 100644 --- a/src/Compilation.zig +++ b/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; diff --git a/test/standalone/test_runner_module_imports/build.zig b/test/standalone/test_runner_module_imports/build.zig new file mode 100644 index 0000000000..973365e495 --- /dev/null +++ b/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); +} diff --git a/test/standalone/test_runner_module_imports/module1/main.zig b/test/standalone/test_runner_module_imports/module1/main.zig new file mode 100644 index 0000000000..883c61ac7e --- /dev/null +++ b/test/standalone/test_runner_module_imports/module1/main.zig @@ -0,0 +1 @@ +pub const decl: usize = 1234567890; diff --git a/test/standalone/test_runner_module_imports/module2/main.zig b/test/standalone/test_runner_module_imports/module2/main.zig new file mode 100644 index 0000000000..f8bb21d7df --- /dev/null +++ b/test/standalone/test_runner_module_imports/module2/main.zig @@ -0,0 +1 @@ +pub const mod1 = @import("module1"); diff --git a/test/standalone/test_runner_module_imports/src/main.zig b/test/standalone/test_runner_module_imports/src/main.zig new file mode 100644 index 0000000000..9d5d5795b6 --- /dev/null +++ b/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); +} diff --git a/test/standalone/test_runner_module_imports/test_runner/main.zig b/test/standalone/test_runner_module_imports/test_runner/main.zig new file mode 100644 index 0000000000..13ce91b88f --- /dev/null +++ b/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(); + } +} From 948754c5d4b707c991c4db9cdfd968e6f4866ea4 Mon Sep 17 00:00:00 2001 From: Dominic <4678790+dweiller@users.noreply.github.com> Date: Wed, 8 Feb 2023 22:39:38 +1100 Subject: [PATCH 3/3] clone package table into custom test runner Co-authored-by: Veikka Tuominen --- src/Compilation.zig | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index f19e83680d..18d0e46892 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1627,10 +1627,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { 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.*); - } + pkg.table = try main_pkg.table.clone(gpa); break :test_pkg pkg; } else try Package.createWithDir( gpa,