Skip to content

Commit e1f0c53

Browse files
committed
Add test step running the example program with static and dynamic library
1 parent 01b8687 commit e1f0c53

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.zig-cache
22
/zig-out
3+
foo.gz

build.zig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,35 @@ pub fn build(b: *std.Build) void {
7575
.dest_sub_path = output_name,
7676
});
7777
b.getInstallStep().dependOn(&install_step.step);
78+
79+
// Testing is about running the test binaries and checking they return 0.
80+
const test_step = b.step("test", "Run tests");
81+
const example = addTestExecutable(b, target, optimize, upstream, lib, "example", "test/example.c");
82+
test_step.dependOn(&example.step);
83+
const examplesh = addTestExecutable(b, target, optimize, upstream, dynamic_lib, "examplesh", "test/example.c");
84+
test_step.dependOn(&examplesh.step);
85+
}
86+
87+
fn addTestExecutable(b: *std.Build, target: std.Build.ResolvedTarget,
88+
optimize: std.builtin.OptimizeMode, zlib: *std.Build.Dependency, lib: *std.Build.Step.Compile,
89+
name: []const u8, filename: []const u8) *std.Build.Step.Run {
90+
const test_exe_mod = b.createModule(.{
91+
.target = target,
92+
.optimize = optimize,
93+
.link_libc = true,
94+
.single_threaded = true,
95+
});
96+
test_exe_mod.addCSourceFiles(.{
97+
.root = zlib.path("."),
98+
.files = &[_][]const u8{
99+
filename,
100+
},
101+
});
102+
test_exe_mod.linkLibrary(lib);
103+
const test_exe = b.addExecutable(.{
104+
.name = name,
105+
.root_module = test_exe_mod,
106+
});
107+
const run_exe_tests = b.addRunArtifact(test_exe);
108+
return run_exe_tests;
78109
}

0 commit comments

Comments
 (0)