some tests

This commit is contained in:
2025-12-12 09:21:33 +01:00
parent b73ac766bf
commit d3271963a8
6 changed files with 183 additions and 0 deletions

View File

@@ -47,8 +47,56 @@ pub fn build(b: *std.Build) !void {
run_cmd.addArgs(args);
}
try compileTestApplications(b, target, optimize, false, false);
try compileTestApplications(b, target, optimize, false, true);
try compileTestApplications(b, target, optimize, true, true);
const exe_tests = b.addTest(.{ .root_module = mod });
const run_exe_tests = b.addRunArtifact(exe_tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(b.getInstallStep());
test_step.dependOn(&run_exe_tests.step);
}
pub fn compileTestApplications(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
comptime link_libc: bool,
comptime pie: bool,
) !void {
// Compile test applications
const test_path = "src/test/";
const test_prefix = prefix: {
const p1 = "test_" ++ if (link_libc) "libc_" else "nolibc_";
const p2 = p1 ++ if (pie) "pie_" else "nopie_";
break :prefix p2;
};
var test_dir = try std.fs.cwd().openDir(test_path, .{ .iterate = true });
defer test_dir.close();
var iterator = test_dir.iterate();
while (try iterator.next()) |entry| {
if (entry.kind != .file) continue;
if (!std.mem.endsWith(u8, entry.name, ".zig")) continue;
const name = try std.mem.concat(b.allocator, u8, &.{
test_prefix, entry.name[0 .. entry.name.len - 4], // strip .zig suffix
});
const test_executable = b.addExecutable(.{
.name = name,
.root_module = b.createModule(.{
.root_source_file = b.path(b.pathJoin(&.{ test_path, entry.name })),
.optimize = optimize,
.target = target,
.link_libc = link_libc,
.link_libcpp = false,
.pic = pie,
}),
.linkage = if (link_libc) .dynamic else .static,
.use_llvm = true,
.use_lld = true,
});
test_executable.pie = pie;
b.installArtifact(test_executable);
}
}