const std = @import("std"); const Platform = @import("../build.zig").Platform; /// unit-test and the test roots it runs, each with its own step where it /// has one: core-test, shell-test, term-test, pane-test, behaviour-test, /// addon-test (and the addons it loads), kitty-test, the 9P roots. pub fn add(b: *std.Build, c: struct { addon_mod: *std.Build.Module, atlas_test: ?*std.Build.Step.Compile, cells_test: ?*std.Build.Step.Compile, cloud9: *std.Build.Module, core_lean: *std.Build.Module, core_module: *std.Build.Module, exe: *std.Build.Step.Compile, fs_bench_test: *std.Build.Step.Compile, ghostty_vt_for_tests: ?*std.Build.Module, image_addon: *std.Build.Step.Compile, image_mod: *std.Build.Module, lib_test_step: *std.Build.Step, mupdf_check: *std.Build.Step, optimize: std.lang.Optimize, pdf_addon: ?*std.Build.Step.Compile, perf_gate_step: *std.Build.Step, perf_tests: *std.Build.Step.Compile, plain_mod: *std.Build.Module, platform: Platform, root_mod: *std.Build.Module, snake_shader_lib: ?*std.Build.Step.Compile, target: std.Build.ResolvedTarget, term_mod: *std.Build.Module, test_build: *std.Build.Step, test_filters: []const []const u8, test_nonce: ?*std.Build.Module, unit_profile: *std.Build.Step.Compile, }) void { const unit_step = b.step("unit-test", "run native shell and module unit tests"); // One copy of each codec in the binary (docs/shared-libs.md): the // linked symbol table, read for the families the build chose against. // ELF only; it runs with every unit-test, like the perf gate. if (c.target.result.ofmt == .elf) { const libs_check = b.addExecutable(.{ .name = "pardes-shared-libs-check", .root_module = b.createModule(.{ .target = b.graph.host, .optimize = .debug, .root_source_file = b.path("tools/shared_libs_check.zig"), .link_libc = true, }), }); const run_libs_check = b.addRunArtifact(libs_check); run_libs_check.addArtifactArg(c.exe); const libs_check_step = b.step("shared-libs-check", "fail when the binary links a second copy of a codec the build chose one of (docs/shared-libs.md)"); libs_check_step.dependOn(&run_libs_check.step); unit_step.dependOn(&run_libs_check.step); } // The perf gate runs with every unit-test (a regression like a // contrast search per grapheme costs 100x, far past the gate). unit_step.dependOn(c.perf_gate_step); unit_step.dependOn(c.lib_test_step); const units: Units = .{ .nonce = c.test_nonce, .build = c.test_build, .unit = unit_step, .match = testMatch(b, c.unit_profile, c.test_filters, unit_step), .profiler = c.unit_profile, .filters = c.test_filters }; // The perf and fs bench programs' own tests: they compile no editor. _ = units.add(b, c.perf_tests, null); _ = units.add(b, c.fs_bench_test, null); if (c.cells_test) |t| _ = units.add(b, t, null); if (c.atlas_test) |t| _ = units.add(b, t, null); const core_test = b.addTest(.{ .root_module = c.core_lean, .filters = c.test_filters }); const run_core_test = b.addRunArtifact(core_test); run_core_test.has_side_effects = true; const core_test_step = b.step("core-test", "run core tests without native shell tests, on the core without a terminal (a test that needs one skips)"); core_test_step.dependOn(&run_core_test.step); if (testMatch(b, c.unit_profile, c.test_filters, core_test_step)) |check| check.addArtifactArg(core_test); // The terminal module's own tests (src/term/): the emulator without a core. const run_term_test = units.add(b, b.addTest(.{ .root_module = c.term_mod, .filters = c.test_filters }), null); b.step("term-test", "run the terminal module's own tests (src/term/)").dependOn(&run_term_test.step); const pane_module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("test/panes.zig"), .link_libc = true, }); pane_module.addImport("pardes", c.core_module); // test/terminal.zig reads the emulator behind a core's terminal. pane_module.addImport("term", c.term_mod); pane_module.addImport("plain", c.plain_mod); if (c.ghostty_vt_for_tests) |x| pane_module.addImport("ghostty-vt", x); pane_module.addImport("pardes_config", c.core_module.import_table.get("pardes_config").?); const test_addons = b.addOptions(); test_addons.addOptionPath("image", c.image_addon.getEmittedBin()); test_addons.addOption(bool, "has_pdf", c.pdf_addon != null); if (c.pdf_addon) |l| test_addons.addOptionPath("pdf", l.getEmittedBin()); pane_module.addOptions("test_addons", test_addons); const pane_tests = b.addTest(.{ .root_module = pane_module, .filters = c.test_filters }); _ = units.add(b, pane_tests, b.step("pane-test", "run pane, output and namespace integration tests")); // The behavioural tests (docs/next-tests.md): their module has the // session alone, so none can reach past what a 9P client sees. const session_module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("test/session.zig"), .link_libc = true, }); session_module.addImport("pardes", c.core_module); // The scripted language server its language words ask // (Session.initLanguage), as the tty cases do. const session_options = b.addOptions(); session_options.addOptionPath("lspmock", b.addExecutable(.{ .name = "pardes-lspmock", .root_module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("test/lspmock.zig"), .link_libc = true, }), }).getEmittedBin()); var fake_addon_lib: ?*std.Build.Step.Compile = null; // The fake addon (addons/fake/fake.c) and three copies of it built // wrong, which the loader must refuse (test/behaviour/addons.zig). for ([_]struct { []const u8, []const u8 }{ .{ "fake_addon", "-DFAKE_PLAIN" }, .{ "fake_addon_abi", "-DFAKE_ABI=0x20002" }, .{ "fake_addon_no_export", "-DFAKE_NO_EXPORT" }, .{ "fake_addon_core_word", "-DFAKE_WORD=\"Save\"" }, .{ "fake_addon_no_dump", "-DFAKE_NO_DUMP" }, }) |variant| { const lib = b.addLibrary(.{ .name = variant[0], .linkage = .dynamic, .root_module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .link_libc = true }) }); lib.root_module.addIncludePath(b.path("src/kinds")); const name_flag = if (std.mem.eql(u8, variant[0], "fake_addon_abi")) "-DFAKE_NAME=\"fakeabi\"" else if (std.mem.eql(u8, variant[0], "fake_addon_core_word")) "-DFAKE_NAME=\"fakeword\"" else "-DFAKE_PLAIN"; // The header is the whole contract: it compiles clean under every warning. lib.root_module.addCSourceFile(.{ .file = b.path("addons/fake/fake.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror", "-Wno-override-init", variant[1], name_flag } }); session_options.addOptionPath(variant[0], lib.getEmittedBin()); if (std.mem.eql(u8, variant[0], "fake_addon")) fake_addon_lib = lib; } session_options.addOptionPath("image_addon", c.image_addon.getEmittedBin()); session_options.addOption(bool, "has_pdf_addon", c.pdf_addon != null); if (c.pdf_addon) |l| session_options.addOptionPath("pdf_addon", l.getEmittedBin()); session_module.addOptions("session_options", session_options); const behaviour_module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("test/behaviour.zig"), .link_libc = true, }); behaviour_module.addImport("session", session_module); // The build's options alone (platform), never the core: a test of a // window's word knows which build it runs in. behaviour_module.addImport("pardes_config", c.core_module.import_table.get("pardes_config").?); const behaviour_tests = b.addTest(.{ .root_module = behaviour_module, .filters = c.test_filters }); const behaviour_test_step = b.step("behaviour-test", "run the behavioural tests, through the 9P tree alone (test/behaviour/), and the T1 runner (-- [--seed N]... | --monkey | --replay)"); _ = units.add(b, behaviour_tests, behaviour_test_step); // An addon's own test: `--addon-test` over the fake (the template) // and the snake (a shipped example, built but not installed), each // with its script, compared with what it printed when written. const addon_test_step = b.step("addon-test", "pardes --addon-test over the fake, snake and pdf addons, against addons/*/*.out, and the Zig addons' own tests"); const snake_mod = b.createModule(.{ .root_source_file = b.path("addons/snake/snake.zig"), .target = c.target, .optimize = c.optimize, .imports = &.{.{ .name = "addon", .module = c.addon_mod }} }); const snake_lib = b.addLibrary(.{ .name = "snake", .linkage = .dynamic, .root_module = snake_mod }); const life_mod = b.createModule(.{ .root_source_file = b.path("addons/life/life.zig"), .target = c.target, .optimize = c.optimize, .imports = &.{.{ .name = "addon", .module = c.addon_mod }} }); const life_lib = b.addLibrary(.{ .name = "life", .linkage = .dynamic, .root_module = life_mod }); for ([_]*std.Build.Module{ snake_mod, life_mod, c.image_mod }) |m| addon_test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m })).step); // ...and the pdf addon's, which mupdf-check runs too (its script // opens docs/design.pdf, from the build root). const libs = [_]struct { ?*std.Build.Step.Compile, []const u8 }{ .{ fake_addon_lib.?, "addons/fake/fake" }, .{ snake_lib, "addons/snake/snake" }, .{ c.snake_shader_lib, "addons/snake_shader/snake_shader" }, .{ life_lib, "addons/life/life" }, .{ c.pdf_addon, "addons/pdf/pdf" } }; for (libs) |t| if (t[0]) |lib| { const run_addon_test = b.addRunArtifact(c.exe); run_addon_test.setCwd(b.path(".")); run_addon_test.addArg("--addon-test"); run_addon_test.addArtifactArg(lib); run_addon_test.addFileArg(b.path(b.fmt("{s}.script", .{t[1]}))); const compare = b.addSystemCommand(&.{ "diff", "-u" }); compare.addFileArg(b.path(b.fmt("{s}.out", .{t[1]}))); compare.addFileArg(run_addon_test.captureStdOut(.{})); behaviour_test_step.dependOn(&compare.step); addon_test_step.dependOn(&compare.step); if (lib == c.pdf_addon) c.mupdf_check.dependOn(&compare.step); }; // The T1 runner (test/monkey9p.zig): its modes over an in-process // session, random 9P requests with the documented rules checked // after each, random input hunting panics (`-- --monkey `), // and its seeds' tapes replayed (`-- --replay`). Bare, a short // fixed-seed run of the first, part of behaviour-test. const t1_runner = b.addExecutable(.{ .name = "pardes-monkey-session", .root_module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("test/monkey9p.zig"), .link_libc = true, }), }); t1_runner.root_module.addImport("session", session_module); const run_t1_runner = b.addRunArtifact(t1_runner); run_t1_runner.addPassthruArgs(); run_t1_runner.has_side_effects = true; if (c.test_filters.len == 0) behaviour_test_step.dependOn(&run_t1_runner.step); // Kitty graphics conformance at the core: what a terminal pane's // program sends, what it reads back, what ghostty stores and what a // frame draws (test/kitty.zig). The end-to-end arm, through a real // pty and the shell's output, is the kitty-harness step. const kitty_module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("test/kitty.zig"), .link_libc = true, }); kitty_module.addImport("pardes", c.core_module); const kitty_tests = b.addTest(.{ .root_module = kitty_module, .filters = c.test_filters }); _ = units.add(b, kitty_tests, b.step("kitty-test", "run the kitty graphics conformance cases for terminal panes")); // 9p_io.zig's and fs.zig's tests as roots of their own, on the // core's imports and C libraries: native transport integration // stays reachable even when Zig's filter leaves the core's // anonymous test-import block out. for ([_]struct { path: []const u8, step: []const u8, about: []const u8, filters: []const []const u8 }{ .{ .path = "src/9p_io.zig", .step = "9p-io-test", .about = "run native 9P transport and client integration tests", .filters = &.{ "Unix and TCP", "one fetch", "expired sessions", "posts itself" } }, .{ .path = "src/fs.zig", .step = "fs-unit-test", .about = "run filesystem unit tests", .filters = c.test_filters }, }) |t| { const m = b.createModule(.{ .root_source_file = b.path(t.path), .target = c.target, .optimize = c.optimize, .link_libc = true }); m.link_objects.appendSlice(b.allocator, c.core_module.link_objects.items) catch @panic("OOM"); var imports = c.core_module.import_table.iterator(); while (imports.next()) |entry| m.addImport(entry.key_ptr.*, entry.value_ptr.*); b.step(t.step, t.about).dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m, .filters = t.filters })).step); } const protocol_step = b.step("9p-test", "run freestanding 9P protocol tests"); const protocol_match = testMatch(b, c.unit_profile, c.test_filters, protocol_step); for ([_]struct { path: []const u8, libc: bool, optimize: ?std.builtin.OptimizeMode = null }{ .{ .path = "test/history.zig", .libc = false }, .{ .path = "src/9p.zig", .libc = false }, .{ .path = "src/fonts.zig", .libc = true }, .{ .path = "src/c_heap.zig", .libc = false }, // Its lookup goes wrong only where the optimizer folds it. .{ .path = "src/c_heap.zig", .libc = false, .optimize = .ReleaseSafe }, }) |entry| { const tests = b.addTest(.{ .root_module = b.createModule(.{ .target = c.target, .optimize = entry.optimize orelse c.optimize, .root_source_file = b.path(entry.path), .link_libc = entry.libc, }), .filters = c.test_filters, }); tests.root_module.addImport("cloud9", c.cloud9); const run_tests = units.add(b, tests, null); if (std.mem.eql(u8, entry.path, "src/9p.zig")) { protocol_step.dependOn(&run_tests.step); if (protocol_match) |check| check.addArtifactArg(tests); } } const deck_module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("test/deck/replay.zig"), }); deck_module.addImport("deck", b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("src/gui/deck.zig"), })); _ = units.add(b, b.addTest(.{ .root_module = deck_module, .filters = c.test_filters }), null); if (c.platform == .tui or c.platform == .gui) { const run_shell_test = units.add(b, b.addTest(.{ .root_module = c.root_mod, .filters = c.test_filters }), null); // The native shell's tests alone, without the perf gate's // ReleaseFast build: what a -Dtest-filter iteration wants. b.step("shell-test", "run the native shell's unit tests (src/main.zig's)").dependOn(&run_shell_test.step); const profile_test = b.addExecutable(.{ .name = "pardes-unit-profile-test", .root_module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("test/unit_profile_test.zig"), }), }); const run_profile_test = b.addRunArtifact(profile_test); run_profile_test.addArtifactArg(c.unit_profile); run_profile_test.has_side_effects = true; b.step("unit-profile-test", "test profiler timing, failures and deadlines").dependOn(&run_profile_test.step); if (c.test_filters.len == 0) { unit_step.dependOn(&run_profile_test.step); c.test_build.dependOn(&c.unit_profile.step); c.test_build.dependOn(&profile_test.step); } } } /// What every unit-test program is wired into: test-build, unit-test, and /// unit-test's one filter check (`match`: a filter need match in one only). const Units = struct { nonce: ?*std.Build.Module, build: *std.Build.Step, unit: *std.Build.Step, match: ?*std.Build.Step.Run, profiler: *std.Build.Step.Compile, filters: []const []const u8, /// `t` wired in, and run by `own` too, with a filter check of its own, /// when it has a step of its own. fn add(u: Units, b: *std.Build, t: *std.Build.Step.Compile, own: ?*std.Build.Step) *std.Build.Step.Run { if (u.nonce) |nonce| t.root_module.addImport("test_rebuild_nonce", nonce); u.build.dependOn(&t.step); const run = b.addRunArtifact(t); run.has_side_effects = true; u.unit.dependOn(&run.step); if (u.match) |check| check.addArtifactArg(t); const step = own orelse return run; step.dependOn(&run.step); if (testMatch(b, u.profiler, u.filters, step)) |check| check.addArtifactArg(t); return run; } }; pub fn testMatch(b: *std.Build, profiler: *std.Build.Step.Compile, filters: []const []const u8, step: *std.Build.Step) ?*std.Build.Step.Run { if (filters.len == 0) return null; const check = b.addRunArtifact(profiler); check.addArgs(&.{ "--check-filter", filters[0] }); check.has_side_effects = true; step.dependOn(&check.step); return check; }