@@ -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