Skip to content

Lightpanda: the headless browser designed for AI and automation

build: add a kcov coverage step

Adrià Arrufatb553b961df4a
diff --git a/Makefile b/Makefile
index 1894cb0cd..49c77b837 100644
--- a/Makefile
+++ b/Makefile
@@ -77,7 +77,7 @@ help:
# $(ZIG) commands
# ------------
-.PHONY: build build-v8-snapshot build-dev download-v8 run run-release test bench data end2end clean
+.PHONY: build build-v8-snapshot build-dev download-v8 run run-release test coverage bench data end2end clean
## Download the prebuilt V8 libraries (skips the 10+ min source build)
download-v8:
@@ -128,6 +128,10 @@ run-debug: build-dev
test:
TEST_FILTER="$(or $(F),$(TEST_FILTER))" $(ZIG) build $(ZIGFLAGS) test -freference-trace
+## Run unit tests under kcov, report in zig-out/coverage/index.html (F= filters)
+coverage:
+ TEST_FILTER="$(or $(F),$(TEST_FILTER))" $(ZIG) build $(ZIGFLAGS) coverage
+
## Run demo/runner end to end tests
end2end:
@test -d ../demo
diff --git a/build.zig b/build.zig
index cce7e0c3d..bb41b48a6 100644
--- a/build.zig
+++ b/build.zig
@@ -203,6 +203,27 @@ pub fn build(b: *Build) !void {
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_tests.step);
}
+
+ {
+ // kcov can't read line info from the self-hosted backend's DWARF.
+ const cov_tests = b.addTest(.{
+ .root_module = lightpanda_module,
+ .use_llvm = true,
+ .test_runner = .{ .path = b.path("src/test_runner.zig"), .mode = .simple },
+ });
+ const kcov = b.addSystemCommand(&.{ "kcov", "--clean" });
+ kcov.addPrefixedDirectoryArg("--include-path=", b.path("src"));
+ const report = kcov.addOutputDirectoryArg("coverage");
+ kcov.addArtifactArg(cov_tests);
+ // Shards are separate processes, each with its own report.
+ kcov.setEnvironmentVariable("TEST_JOBS", "1");
+ // Part of the cache key, so each filter gets a fresh report directory.
+ kcov.setEnvironmentVariable("TEST_FILTER", b.graph.environ_map.get("TEST_FILTER") orelse "");
+
+ const install = b.addInstallDirectory(.{ .source_dir = report, .install_dir = .prefix, .install_subdir = "coverage" });
+ const coverage_step = b.step("coverage", "Run unit tests under kcov (zig-out/coverage/index.html)");
+ coverage_step.dependOn(&install.step);
+ }
}
const Deps = struct {
diff --git a/src/crash_handler.zig b/src/crash_handler.zig
index 9d5a3ed1d..6e268ada4 100644
--- a/src/crash_handler.zig
+++ b/src/crash_handler.zig
@@ -457,7 +457,23 @@ fn openPty(fds: *[2]std.c.fd_t) c_int {
return 0;
}
+/// A tracer that follows forks (kcov) intercepts the child's fatal signal,
+/// so a hardware fault re-executes forever instead of terminating it.
+fn isTraced() bool {
+ const fd = std.c.open("/proc/self/status", .{ .ACCMODE = .RDONLY });
+ if (fd < 0) return false;
+ defer _ = std.c.close(fd);
+ var buf: [4096]u8 = undefined;
+ const n = std.c.read(fd, &buf, buf.len);
+ if (n <= 0) return false;
+ const status = buf[0..@intCast(n)];
+ const start = (std.mem.find(u8, status, "TracerPid:") orelse return false) + "TracerPid:".len;
+ const end = std.mem.findScalarPos(u8, status, start, '\n') orelse status.len;
+ return !std.mem.eql(u8, std.mem.trim(u8, status[start..end], " \t"), "0");
+}
+
fn testSignal(sig: std.posix.SIG, mode: SignalTestMode) !void {
+ if (isTraced()) return error.SkipZigTest;
const guard = if (mode == .hardware) try std.posix.mmap(null, std.heap.pageSize(), .{}, .{ .TYPE = .PRIVATE, .ANONYMOUS = true }, -1, 0) else null;
defer if (guard) |memory| std.posix.munmap(memory);
const file = if (mode == .regular_file) std.c.memfd_create("fatal-signal-test", std.c.MFD.CLOEXEC) else -1;