make sure cli/tui are handled by zig build test and augment coverage step for multiple modules

This commit is contained in:
Emil Lerch 2026-02-27 10:41:22 -08:00
parent 61008affd9
commit dee910c33a
Signed by: lobo
GPG key ID: A7B62D657EF764F8
3 changed files with 165 additions and 99 deletions

View file

@ -32,7 +32,7 @@ pub fn build(b: *std.Build) void {
}, },
}); });
// Shared TUI imports (used by the TUI module and its tests) // Shared imports for TUI and CLI modules
const tui_imports: []const std.Build.Module.Import = &.{ const tui_imports: []const std.Build.Module.Import = &.{
.{ .name = "zfin", .module = mod }, .{ .name = "zfin", .module = mod },
.{ .name = "srf", .module = srf_mod }, .{ .name = "srf", .module = srf_mod },
@ -40,13 +40,18 @@ pub fn build(b: *std.Build) void {
.{ .name = "z2d", .module = z2d_dep.module("z2d") }, .{ .name = "z2d", .module = z2d_dep.module("z2d") },
}; };
// TUI module (imported by the unified binary)
const tui_mod = b.addModule("tui", .{ const tui_mod = b.addModule("tui", .{
.root_source_file = b.path("src/tui/main.zig"), .root_source_file = b.path("src/tui/main.zig"),
.target = target, .target = target,
.imports = tui_imports, .imports = tui_imports,
}); });
const cli_imports: []const std.Build.Module.Import = &.{
.{ .name = "zfin", .module = mod },
.{ .name = "srf", .module = srf_mod },
.{ .name = "tui", .module = tui_mod },
};
// Unified executable (CLI + TUI in one binary) // Unified executable (CLI + TUI in one binary)
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "zfin", .name = "zfin",
@ -54,11 +59,7 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("src/cli/main.zig"), .root_source_file = b.path("src/cli/main.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.imports = &.{ .imports = cli_imports,
.{ .name = "zfin", .module = mod },
.{ .name = "srf", .module = srf_mod },
.{ .name = "tui", .module = tui_mod },
},
}), }),
}); });
b.installArtifact(exe); b.installArtifact(exe);
@ -72,15 +73,13 @@ pub fn build(b: *std.Build) void {
run_cmd.addArgs(args); run_cmd.addArgs(args);
} }
// Tests // Tests: Zig test discovery doesn't cross module boundaries, so each
// module (lib, TUI, CLI) needs its own test target.
const test_step = b.step("test", "Run all tests"); const test_step = b.step("test", "Run all tests");
const mod_tests = b.addTest(.{ .root_module = mod }); const mod_tests = b.addTest(.{ .root_module = mod });
test_step.dependOn(&b.addRunArtifact(mod_tests).step); test_step.dependOn(&b.addRunArtifact(mod_tests).step);
const exe_tests = b.addTest(.{ .root_module = exe.root_module });
test_step.dependOn(&b.addRunArtifact(exe_tests).step);
const tui_tests = b.addTest(.{ .root_module = b.createModule(.{ const tui_tests = b.addTest(.{ .root_module = b.createModule(.{
.root_source_file = b.path("src/tui/main.zig"), .root_source_file = b.path("src/tui/main.zig"),
.target = target, .target = target,
@ -89,6 +88,14 @@ pub fn build(b: *std.Build) void {
}) }); }) });
test_step.dependOn(&b.addRunArtifact(tui_tests).step); test_step.dependOn(&b.addRunArtifact(tui_tests).step);
const cli_tests = b.addTest(.{ .root_module = b.createModule(.{
.root_source_file = b.path("src/cli/main.zig"),
.target = target,
.optimize = optimize,
.imports = cli_imports,
}) });
test_step.dependOn(&b.addRunArtifact(cli_tests).step);
// Docs // Docs
const lib = b.addLibrary(.{ const lib = b.addLibrary(.{
.name = "zfin", .name = "zfin",
@ -108,6 +115,21 @@ pub fn build(b: *std.Build) void {
.install_subdir = "docs", .install_subdir = "docs",
}).step); }).step);
// Coverage: `zig build coverage` (Linux only, uses kcov) // Coverage: `zig build coverage` (uses kcov, Linux x86_64/aarch64 only)
_ = Coverage.addCoverageStep(b, mod, "zfin"); {
const cov = Coverage.init(b);
_ = cov.addModule(mod, "zfin-lib");
_ = cov.addModule(b.createModule(.{
.root_source_file = b.path("src/tui/main.zig"),
.target = target,
.optimize = optimize,
.imports = tui_imports,
}), "zfin-tui");
_ = cov.addModule(b.createModule(.{
.root_source_file = b.path("src/cli/main.zig"),
.target = target,
.optimize = optimize,
.imports = cli_imports,
}), "zfin-cli");
}
} }

View file

@ -4,40 +4,41 @@ const Build = std.Build;
const Coverage = @This(); const Coverage = @This();
/// Adds test coverage. This will create a new test coverage executable to the /// Whether the host platform supports kcov-based coverage.
/// build graph, generated only if coverage is a target. It will create an /// Only x86_64 and aarch64 Linux are supported (kcov binary availability).
/// option -Dcoverage-threshold that will fail the build if the threshold is /// On unsupported platforms, the coverage step will fail at runtime with
/// not met. It will also add a step that downloads a zig fork of the kcov /// a clear error from the kcov download or execution step.
/// executable into zig cache if it doesn't already exist // pub const supported = builtin.os.tag == .linux and
// (builtin.cpu.arch == .x86_64 or builtin.cpu.arch == .aarch64);
/// Initialize coverage infrastructure. Creates the "coverage" build step,
/// registers build options (-Dcoverage-threshold, -Dcoverage-dir),
/// and sets up the kcov download step. The kcov binary is downloaded into the
/// zig cache on first use and reused thereafter.
/// ///
/// Because it is creating a new test executable from the root module provided, /// Use `zig build coverage --verbose` to see per-file coverage breakdown.
///
/// Call `addModule()` on the returned value to add test modules to the coverage run.
/// Each module gets its own kcov invocation, threshold check, and output subdirectory.
///
/// Because addModule creates a new test executable from the root module provided,
/// if there are any linking steps being done to your test executable, those /// if there are any linking steps being done to your test executable, those
/// must also be done to the test_exe returned by this function /// must also be done to the test_exe returned by addModule.
pub fn addCoverageStep(b: *Build, root_module: *Build.Module, coverage_name: []const u8) *Coverage { pub fn init(b: *Build) Coverage {
//verify host requirements
{
const supported = builtin.os.tag == .linux and
(builtin.cpu.arch == .x86_64 or builtin.cpu.arch == .aarch64);
if (!supported)
@panic("Coverage only supported on x86_64-linux or aarch64-linux");
}
// Add options // Add options
const coverage_threshold = b.option(u7, "coverage-threshold", "Minimum coverage percentage required") orelse 0; const coverage_threshold = b.option(u7, "coverage-threshold", "Minimum coverage percentage required") orelse 0;
const coverage_dir = b.option([]const u8, "coverage-dir", "Coverage output directory") orelse const coverage_dir = b.option([]const u8, "coverage-dir", "Coverage output directory") orelse
b.pathJoin(&.{ b.build_root.path orelse ".", "coverage" }); b.pathJoin(&.{ b.build_root.path orelse ".", "coverage" });
const coverage_step = b.step("coverage", "Generate test coverage report"); const coverage_step = b.step("coverage", "Generate test coverage report");
// Set up kcov download // Set up kcov download.
// We can't do it directly because we are sandboxed during build, but // We can't download directly because we are sandboxed during build, but
// we can create a program and run that program. First we need the destination // we can create a helper program and run it. First we need the destination
// directory // directory, keyed by architecture.
const kcov = blk: {
const arch_name = switch (builtin.cpu.arch) { const arch_name = switch (builtin.cpu.arch) {
.x86_64 => "x86_64", .x86_64 => "x86_64",
.aarch64 => "aarch64", .aarch64 => "aarch64",
else => unreachable, else => @tagName(builtin.cpu.arch),
}; };
const Algo = std.crypto.hash.sha2.Sha256; const Algo = std.crypto.hash.sha2.Sha256;
@ -53,12 +54,9 @@ pub fn addCoverageStep(b: *Build, root_module: *Build.Module, coverage_name: []c
b.fmt("{s}", .{std.fmt.bytesToHex(cache_hash, .lower)}), b.fmt("{s}", .{std.fmt.bytesToHex(cache_hash, .lower)}),
}); });
const kcov_name = b.fmt("kcov-{s}", .{arch_name}); const kcov_path = b.pathJoin(&.{ cache_dir, b.fmt("kcov-{s}", .{arch_name}) });
break :blk .{ .path = b.pathJoin(&.{ cache_dir, kcov_name }), .arch = arch_name };
};
// Create download and coverage build steps // Create the download helper executable
return blk: {
const download_exe = b.addExecutable(.{ const download_exe = b.addExecutable(.{
.name = "download-kcov", .name = "download-kcov",
.root_module = b.createModule(.{ .root_module = b.createModule(.{
@ -68,54 +66,91 @@ pub fn addCoverageStep(b: *Build, root_module: *Build.Module, coverage_name: []c
}); });
const run_download = b.addRunArtifact(download_exe); const run_download = b.addRunArtifact(download_exe);
run_download.addArg(kcov.path); run_download.addArg(kcov_path);
run_download.addArg(kcov.arch); run_download.addArg(arch_name);
const run_coverage = b.addSystemCommand(&.{kcov.path}); return .{
.b = b,
.coverage_step = coverage_step,
.coverage_dir = coverage_dir,
.coverage_threshold = coverage_threshold,
.kcov_path = kcov_path,
.run_download = run_download,
};
}
/// Add a test module to the coverage run. Each module gets its own kcov
/// invocation and threshold check, all wired into the shared "coverage" step.
/// Returns the test executable so the caller can add any extra linking steps.
pub fn addModule(self: Coverage, root_module: *Build.Module, name: []const u8) *Build.Step.Compile {
const b = self.b;
// Set up kcov run: filter to src/ only, use custom CSS for HTML report
const run_coverage = b.addSystemCommand(&.{self.kcov_path});
const include_path = b.pathJoin(&.{ b.build_root.path.?, "src" }); const include_path = b.pathJoin(&.{ b.build_root.path.?, "src" });
run_coverage.addArgs(&.{ "--include-path", include_path }); run_coverage.addArgs(&.{ "--include-path", include_path });
const css_file = b.pathJoin(&.{ b.build_root.path.?, "build", "bcov.css" }); const css_file = b.pathJoin(&.{ b.build_root.path.?, "build", "bcov.css" });
run_coverage.addArg(b.fmt("--configure=css-file={s}", .{css_file})); run_coverage.addArg(b.fmt("--configure=css-file={s}", .{css_file}));
run_coverage.addArg(coverage_dir); run_coverage.addArg(self.coverage_dir);
// Create a test executable for this module.
// We need to set use_llvm because the self-hosted backend
// does not emit the DWARF data that kcov needs.
const test_exe = b.addTest(.{ const test_exe = b.addTest(.{
.name = coverage_name, .name = name,
.root_module = root_module, .root_module = root_module,
// we need to set the test exe to use llvm as the self hosted backend
// does not support the data kcov needs
.use_llvm = true, .use_llvm = true,
}); });
run_coverage.addArtifactArg(test_exe); run_coverage.addArtifactArg(test_exe);
run_coverage.step.dependOn(&test_exe.step); run_coverage.step.dependOn(&test_exe.step);
run_coverage.step.dependOn(&run_download.step); run_coverage.step.dependOn(&self.run_download.step);
const json_path = b.fmt("{s}/{s}/coverage.json", .{ coverage_dir, coverage_name }); // Wire up the threshold check step (reads coverage.json after kcov finishes)
const verbose = b.option(bool, "coverage-verbose", "Show test coverage for each file") orelse false; const json_path = b.fmt("{s}/{s}/coverage.json", .{ self.coverage_dir, name });
const check_step = create(b, test_exe, json_path, coverage_threshold, verbose); const check_step = create(b, test_exe, json_path, self.coverage_threshold);
check_step.step.dependOn(&run_coverage.step); check_step.step.dependOn(&run_coverage.step);
coverage_step.dependOn(&check_step.step); self.coverage_step.dependOn(&check_step.step);
break :blk check_step;
}; return test_exe;
} }
step: Build.Step, // Coverage struct fields
json_path: []const u8,
threshold: u7,
test_exe: *std.Build.Step.Compile,
verbose: bool,
pub fn create(owner: *Build, test_exe: *std.Build.Step.Compile, xml_path: []const u8, threshold: u7, verbose: bool) *Coverage { // Fields used by init() to configure the shared coverage infrastructure
b: *Build,
coverage_step: *Build.Step,
coverage_dir: []const u8,
coverage_threshold: u7,
kcov_path: []const u8,
run_download: *Build.Step.Run,
// Fields used by make() for per-module threshold checking.
// These are only meaningful on instances created by create(), not by init().
step: Build.Step = undefined,
json_path: []const u8 = "",
threshold: u7 = 0,
test_exe: *Build.Step.Compile = undefined,
/// Create a coverage check step that reads the kcov JSON output after
/// the coverage run completes and verifies the threshold is met.
fn create(owner: *Build, test_exe: *Build.Step.Compile, json_path: []const u8, threshold: u7) *Coverage {
const check = owner.allocator.create(Coverage) catch @panic("OOM"); const check = owner.allocator.create(Coverage) catch @panic("OOM");
check.* = .{ check.* = .{
.b = owner,
.coverage_step = undefined,
.coverage_dir = "",
.coverage_threshold = 0,
.kcov_path = "",
.run_download = undefined,
.step = Build.Step.init(.{ .step = Build.Step.init(.{
.id = .custom, .id = .custom,
.name = "check coverage", .name = "check coverage",
.owner = owner, .owner = owner,
.makeFn = make, .makeFn = make,
}), }),
.json_path = xml_path, .json_path = json_path,
.threshold = threshold, .threshold = threshold,
.test_exe = test_exe, .test_exe = test_exe,
.verbose = verbose,
}; };
return check; return check;
} }
@ -144,6 +179,9 @@ const File = struct {
} }
}; };
/// Build step make function: reads the kcov coverage.json output,
/// prints summary (and per-file breakdown if verbose), and fails
/// the build if coverage is below the configured threshold.
fn make(step: *Build.Step, options: Build.Step.MakeOptions) !void { fn make(step: *Build.Step, options: Build.Step.MakeOptions) !void {
_ = options; _ = options;
const check: *Coverage = @fieldParentPtr("step", step); const check: *Coverage = @fieldParentPtr("step", step);
@ -162,7 +200,7 @@ fn make(step: *Build.Step, options: Build.Step.MakeOptions) !void {
var stdout_buffer: [1024]u8 = undefined; var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface; const stdout = &stdout_writer.interface;
if (step.owner.verbose or check.verbose) { if (step.owner.verbose) {
const files = coverage.files; const files = coverage.files;
std.mem.sort(File, files, files, File.coverageLessThanDesc); std.mem.sort(File, files, files, File.coverageLessThanDesc);
for (files) |f| for (files) |f|

View file

@ -3866,6 +3866,12 @@ fn freeWatchlist(allocator: std.mem.Allocator, watchlist: ?[][]const u8) void {
} }
} }
// Force test discovery for imported TUI sub-modules
comptime {
_ = keybinds;
_ = theme_mod;
}
/// Entry point for the interactive TUI. /// Entry point for the interactive TUI.
pub fn run(allocator: std.mem.Allocator, config: zfin.Config, args: []const []const u8) !void { pub fn run(allocator: std.mem.Allocator, config: zfin.Config, args: []const []const u8) !void {
var portfolio_path: ?[]const u8 = null; var portfolio_path: ?[]const u8 = null;