summaryrefslogtreecommitdiff
path: root/build/tests.zig
blob: 2d43d74355bed733dc021fc84f1d92390ff7ffaa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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 runtime beside it: core-test.
    core_lean: core.Modules,
    /// The shell's root, core and runtime: shell-test.
    root_set: core.Modules,
    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);
    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 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.runtime }) |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 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 });
    // A kind that breaks PA_PLAIN (test/liar.zig), for a client to catch;
    // it and the two games into <prefix>/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 = program(b, "pardes-replicas", "test/replicas.zig", c.target, c.optimize, &.{ .{ .name = "pardes", .module = c.core_module }, .{ .name = "term", .module = c.parts.term } });
    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;
    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])").dependOn(&run_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);
    // ...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 = "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 = "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).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, "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) {
        // 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 and runtime's)");
        for ([_]*std.Build.Module{ c.root_set.root, c.root_set.core, c.root_set.runtime }) |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;
}