mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 14:25:16 +00:00
Merge pull request #14588 from dweiller/test-runner-imports
fix custom test runner file import path resolution
This commit is contained in:
commit
ba680aa987
@ -1621,16 +1621,21 @@ 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)
|
||||
else
|
||||
try Package.createWithDir(
|
||||
gpa,
|
||||
"root",
|
||||
options.zig_lib_directory,
|
||||
null,
|
||||
"test_runner.zig",
|
||||
);
|
||||
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);
|
||||
const pkg = try Package.create(gpa, "root", test_dir, basename);
|
||||
|
||||
// copy package table from main_pkg to root_pkg
|
||||
pkg.table = try main_pkg.table.clone(gpa);
|
||||
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;
|
||||
|
||||
@ -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", .{});
|
||||
|
||||
21
test/standalone/issue_13970/build.zig
Normal file
21
test/standalone/issue_13970/build.zig
Normal file
@ -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);
|
||||
}
|
||||
0
test/standalone/issue_13970/empty.zig
Normal file
0
test/standalone/issue_13970/empty.zig
Normal file
0
test/standalone/issue_13970/src/empty.zig
Normal file
0
test/standalone/issue_13970/src/empty.zig
Normal file
8
test/standalone/issue_13970/src/main.zig
Normal file
8
test/standalone/issue_13970/src/main.zig
Normal file
@ -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;
|
||||
}
|
||||
1
test/standalone/issue_13970/src/package.zig
Normal file
1
test/standalone/issue_13970/src/package.zig
Normal file
@ -0,0 +1 @@
|
||||
pub const decl = 0;
|
||||
0
test/standalone/issue_13970/test_root/empty.zig
Normal file
0
test/standalone/issue_13970/test_root/empty.zig
Normal file
19
test/standalone/test_runner_module_imports/build.zig
Normal file
19
test/standalone/test_runner_module_imports/build.zig
Normal file
@ -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);
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
pub const decl: usize = 1234567890;
|
||||
@ -0,0 +1 @@
|
||||
pub const mod1 = @import("module1");
|
||||
6
test/standalone/test_runner_module_imports/src/main.zig
Normal file
6
test/standalone/test_runner_module_imports/src/main.zig
Normal file
@ -0,0 +1,6 @@
|
||||
const mod2 = @import("module2");
|
||||
const std = @import("std");
|
||||
|
||||
test {
|
||||
try std.testing.expectEqual(@as(usize, 1234567890), mod2.mod1.decl);
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user