const std = @import("std"); const Platform = @import("../build.zig").Platform; const core = @import("core.zig"); const tools = @import("tools.zig"); const program = tools.program; /// 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, /// The core without a terminal and the devices and runtime beside it: core-test. core_lean: core.Modules, /// The shell's root, core, devices and runtime: shell-test. root_set: core.Modules, /// The wire (abi/wire.zig) on the core's configuration: shell-test's. core_wire: *std.Build.Module, core_module: *std.Build.Module, /// The runtime beside core_module: the client loop the soak runs. core_runtime: *std.Build.Module, /// core_module's configuration, for the test roots that run on its imports. core_cfg: core.Core, exe: *std.Build.Step.Compile, ghostty_vt_for_tests: ?*std.Build.Module, image_addon: *std.Build.Step.Compile, /// The scripted language server the sessions' language words ask. lspmock: *std.Build.Step.Compile, mupdf_check: *std.Build.Step, optimize: std.lang.Optimize, parts: core.Parts, pdf_addon: ?*std.Build.Step.Compile, platform: Platform, root_mod: *std.Build.Module, snake_shader_lib: ?*std.Build.Step.Compile, target: std.Build.ResolvedTarget, test_build: *std.Build.Step, test_filters: []const []const u8, test_nonce: ?*std.Build.Module, tools: tools.Tools, 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.tools.perf_gate_step); unit_step.dependOn(c.tools.consumer_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, .target = c.target, .optimize = c.optimize }; // The perf and fs bench programs' own tests: they compile no editor. _ = units.add(b, units.root(b, "test/perf.zig", true, &.{.{ .name = "perf_options", .module = c.tools.perf_options.createModule() }}), null); _ = units.add(b, units.root(b, "test/fs_bench.zig", null, &.{}), null); 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), and the devices' and the runtime's beside it"); const core_match = testMatch(b, c.unit_profile, c.test_filters, core_test_step); for ([_]*std.Build.Module{ c.core_lean.core, c.core_lean.dev, c.core_lean.runtime, c.core_lean.wire }) |m| { const core_test = b.addTest(.{ .root_module = m, .filters = c.test_filters }); const run_core_test = b.addRunArtifact(core_test); run_core_test.has_side_effects = true; core_test_step.dependOn(&run_core_test.step); if (core_match) |check| check.addArtifactArg(core_test); } // The theme importer's own tests (tools/gen_themes.zig): its mapping, // rule by rule, on the machine that builds. const gen_themes_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("tools/gen_themes.zig"), .target = b.graph.host, .optimize = .debug }), .filters = c.test_filters }); core_test_step.dependOn(&b.addRunArtifact(gen_themes_test).step); if (core_match) |check| check.addArtifactArg(gen_themes_test); // The terminal module's own tests (core/term/): the emulator without a core. const run_term_test = units.add(b, b.addTest(.{ .root_module = c.parts.term, .filters = c.test_filters }), null); b.step("term-test", "run the terminal module's own tests (core/term/)").dependOn(&run_term_test.step); // test/terminal.zig reads the emulator behind a core's terminal. const pane_tests = units.root(b, "test/panes.zig", true, &.{ .{ .name = "pardes", .module = c.core_module }, .{ .name = "term", .module = c.parts.term }, .{ .name = "plain", .module = c.parts.plain } }); if (c.ghostty_vt_for_tests) |x| pane_tests.root_module.addImport("ghostty-vt", x); // The core's own options module, not another over the same file: // a file is in one module of a compilation. pane_tests.root_module.addImport("pardes_options", c.core_module.import_table.get("pardes_options").?); 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_tests.root_module.addOptions("test_addons", test_addons); _ = 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", c.lspmock.getEmittedBin()); var fake_addon_lib: ?*std.Build.Step.Compile = null; // The fake addon (addons/fake/fake.c), copies of it built wrong, which // the loader must refuse, and one whose head says ABI 3.3, which it // takes (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" }, .{ "fake_addon_old", "-DFAKE_ABI=0x30003" }, }) |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("abi")); 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 if (std.mem.eql(u8, variant[0], "fake_addon_old")) "-DFAKE_NAME=\"fakeold\"" 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); // The build's options alone (platform), never the core: a test of a // window's word knows which build it runs in. const behaviour_tests = units.root(b, "test/behaviour.zig", true, &.{ .{ .name = "session", .module = session_module }, .{ .name = "pardes_options", .module = c.core_module.import_table.get("pardes_options").? } }); 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 }); // A kind that breaks PA_PLAIN (test/liar.zig), for a client to catch; // it and the two games into /lib, for test/daemon.py's games. const liar_mod = b.createModule(.{ .root_source_file = b.path("test/liar.zig"), .target = c.target, .optimize = c.optimize, .imports = &.{.{ .name = "addon", .module = c.addon_mod }} }); const liar_lib = b.addLibrary(.{ .name = "liar", .linkage = .dynamic, .root_module = liar_mod }); for ([_]*std.Build.Step.Compile{ snake_lib, life_lib, liar_lib }) |lib| addon_test_step.dependOn(&b.addInstallArtifact(lib, .{}).step); // The replicas' soak, its clients showing a picture, a game of life and // a PDF (docs/design.pdf) from these libraries, as a user's addons are // loaded. const replicas_imports: []const std.Build.Module.Import = &.{ .{ .name = "pardes", .module = c.core_module }, .{ .name = "runtime", .module = c.core_runtime }, .{ .name = "term", .module = c.parts.term } }; const replicas = program(b, "pardes-replicas", "test/replicas.zig", c.target, c.optimize, replicas_imports); const run_replicas = b.addRunArtifact(replicas); for ([_]?*std.Build.Step.Compile{ c.image_addon, life_lib, c.pdf_addon }) |l| if (l) |lib| { run_replicas.addArg("--addon"); run_replicas.addArtifactArg(lib); }; if (c.pdf_addon != null) { run_replicas.addArg("--pdf"); run_replicas.addFileArg(b.path("docs/design.pdf")); } run_replicas.addPassthruArgs(); run_replicas.has_side_effects = true; const replicas_step = b.step("replicas", "one authority and several replicas in process, each client typing over a delayed link: digests at quiescence, rollback rate and costs per RTT (-- [--seeds N] [--steps M] [--clients C] [--rtt 30,80,207])"); replicas_step.dependOn(&run_replicas.step); // Its typist's own tests (what it never types), with every soak and unit-test. _ = units.add(b, units.root(b, "test/replicas.zig", true, replicas_imports), replicas_step); for ([_]*std.Build.Module{ snake_mod, life_mod, liar_mod, c.parts.image }) |m| addon_test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m })).step); if (c.pdf_addon) |lib| addon_test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = lib.root_module })).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 = program(b, "pardes-monkey-session", "test/monkey9p.zig", c.target, c.optimize, &.{.{ .name = "session", .module = 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. _ = units.add(b, units.root(b, "test/kitty.zig", true, &.{.{ .name = "pardes", .module = c.core_module }}), b.step("kitty-test", "run the kitty graphics conformance cases for terminal panes")); // 9p_io.zig's and fs.zig's tests 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. Each // from its module's root (runtime/root.zig, core/root.zig): the // modules name each other by module, whose `core` is the core's root. for ([_]struct { path: []const u8, step: []const u8, about: []const u8, filters: []const []const u8 }{ .{ .path = "runtime/root.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 = "core/root.zig", .step = "fs-unit-test", .about = "run filesystem unit tests", .filters = c.test_filters }, }) |t| { var cfg = c.core_cfg; cfg.root = t.path; const m = core.module(b, c.parts, cfg).root; b.step(t.step, t.about).dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m, .filters = t.filters })).step); } const cloud9: []const std.Build.Module.Import = &.{.{ .name = "cloud9", .module = c.parts.cloud9 }}; _ = units.add(b, units.root(b, "test/history.zig", false, cloud9), null); const protocol_tests = units.root(b, "runtime/9p.zig", false, cloud9); const protocol_step = b.step("9p-test", "run freestanding 9P protocol tests"); protocol_step.dependOn(&units.add(b, protocol_tests, null).step); if (testMatch(b, c.unit_profile, c.test_filters, protocol_step)) |check| check.addArtifactArg(protocol_tests); _ = units.add(b, units.root(b, "dev/fonts.zig", true, cloud9), null); _ = units.add(b, units.root(b, "core/c_heap.zig", false, cloud9), null); // Its lookup goes wrong only where the optimizer folds it. _ = units.add(b, b.addTest(.{ .root_module = b.createModule(.{ .target = c.target, .optimize = .safe, .root_source_file = b.path("core/c_heap.zig"), .link_libc = false, .imports = cloud9 }), .filters = c.test_filters }), null); _ = units.add(b, units.root(b, "test/deck/replay.zig", null, &.{.{ .name = "deck", .module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("dev/gui/deck.zig") }) }}), null); if (c.platform == .tui or c.platform == .gui) { // The native shell's tests alone, without the perf gate's // ReleaseFast build: what a -Dtest-filter iteration wants. const shell_test_step = b.step("shell-test", "run the native shell's unit tests (runtime/main.zig's, and its core's, devices' and runtime's)"); // The wire's on the core's own set: a window's set links the window's C, // whose allocator is gui.zig's, which nothing in the wire reaches. for ([_]*std.Build.Module{ c.root_set.root, c.root_set.core, c.root_set.dev, c.root_set.runtime, c.core_wire }) |m| shell_test_step.dependOn(&units.add(b, b.addTest(.{ .root_module = m, .filters = c.test_filters }), null).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, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, /// A test program over `path` with `imports`, at this build's target /// and optimization, filtered by -Dtest-filter. fn root(u: Units, b: *std.Build, path: []const u8, link_libc: ?bool, imports: []const std.Build.Module.Import) *std.Build.Step.Compile { return b.addTest(.{ .root_module = b.createModule(.{ .target = u.target, .optimize = u.optimize, .root_source_file = b.path(path), .link_libc = link_libc, .imports = imports }), .filters = u.filters }); } /// `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; }