@@ -23,6 +23,8 @@ pub fn build(b: *std.Build) void {
2323 "Emit documents to <zig-out>/docs (default: false)" ,
2424 ) orelse false ;
2525
26+ const kcov = b .option ([]const []const u8 , "kcov" , "Arguments for kcov in testing (default: null = disabled)" );
27+
2628 const stepCheck = b .step ("check" , "Build but don't install" );
2729 const stepTest = b .step ("test" , "Run library tests" );
2830 const stepCompatTest = b .step ("test-compat" , "Run compatibility tests" );
@@ -95,11 +97,26 @@ pub fn build(b: *std.Build) void {
9597
9698 stepCheck .dependOn (& rewriter .step );
9799
98- const COMPAT_RUN_CMD = "ZIGPAK_REWRITER=$1 bun test" ;
99-
100- const runCompatTest = b .addSystemCommand (&.{ "bun" , "exec" , COMPAT_RUN_CMD });
101- runCompatTest .addArtifactArg (rewriter );
102- stepCompatTest .dependOn (& runCompatTest .step );
100+ if (kcov ) | args | {
101+ const COMPAT_RUN_CMD = "ZIGPAK_REWRITER=$1 KCOV=kcov KCOV_ARGS=\" --include-path=$2 $3\" KCOV_REPORT=$4 bun test" ;
102+
103+ const kcovArgs = std .mem .join (
104+ b .allocator ,
105+ " " ,
106+ args [0.. @max (args .len - 1 , 0 )],
107+ ) catch @panic ("OOM" );
108+
109+ const runCompatTest = b .addSystemCommand (&.{ "bun" , "exec" , COMPAT_RUN_CMD });
110+ runCompatTest .addArtifactArg (rewriter );
111+ runCompatTest .addArgs (&.{ b .build_root .path orelse "." , kcovArgs , args [args .len - 1 ] });
112+ stepCompatTest .dependOn (& runCompatTest .step );
113+ } else {
114+ const COMPAT_RUN_CMD = "ZIGPAK_REWRITER=$1 bun test" ;
115+
116+ const runCompatTest = b .addSystemCommand (&.{ "bun" , "exec" , COMPAT_RUN_CMD });
117+ runCompatTest .addArtifactArg (rewriter );
118+ stepCompatTest .dependOn (& runCompatTest .step );
119+ }
103120 }
104121
105122 {
@@ -109,8 +126,21 @@ pub fn build(b: *std.Build) void {
109126 .optimize = optimize ,
110127 });
111128
112- const run_main_tests = b .addRunArtifact (tests );
113-
114- stepTest .dependOn (& run_main_tests .step );
129+ if (kcov ) | args | {
130+ const run = b .addSystemCommand (&.{"kcov" });
131+ const includeArg = std .fmt .allocPrint (
132+ b .allocator ,
133+ "--include-path={s}" ,
134+ .{b .build_root .path orelse "." },
135+ ) catch @panic ("OOM" );
136+ run .addArg (includeArg );
137+ run .addArgs (args );
138+ run .addArtifactArg (tests );
139+ run .enableTestRunnerMode ();
140+ stepTest .dependOn (& run .step );
141+ } else {
142+ const run = b .addRunArtifact (tests );
143+ stepTest .dependOn (& run .step );
144+ }
115145 }
116146}
0 commit comments