diff options
Diffstat (limited to 'build/tests.zig')
| -rw-r--r-- | build/tests.zig | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/build/tests.zig b/build/tests.zig new file mode 100644 index 00000000..d8bce18d --- /dev/null +++ b/build/tests.zig @@ -0,0 +1,264 @@ +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, + atlas_test: ?*std.Build.Step.Compile, + cells_test: ?*std.Build.Step.Compile, + core_lean: *std.Build.Module, + core_module: *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.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, .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); + 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.parts.term, .filters = c.test_filters }), null); + b.step("term-test", "run the terminal module's own tests (src/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_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_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) 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); + // 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_config", .module = c.core_module.import_table.get("pardes_config").? } }); + 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 <seeds> | --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.parts.image }) |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 <seeds>`), + // 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 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| { + var cfg = c.core_cfg; + cfg.root = t.path; + const m = core.module(b, c.parts, cfg); + 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, "src/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, "src/fonts.zig", true, cloud9), null); + _ = units.add(b, units.root(b, "src/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("src/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("src/gui/deck.zig") }) }}), 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, + 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; +} |
