From 80db18269dd9f7153a02f3efa6ff92c69948a623 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 8 Nov 2026 23:20:07 -0300 Subject: tests: the tty's own cases get their runner in the process suite, test/tty.zig (a pardes in a pty of its own, driven by a terminal's bytes, its captures compared with test/snapshots/.golden: the snapshot scripts' commands as methods, their capture and settle code moved from snapshot.zig) and process_test.zig's pool (a child a case, a core's worth at once, a failure run again alone with the wider settle probe), with six empty files for the script families the next changes fill, so each family can be reverted alone Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- test/process_test.zig | 93 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 7 deletions(-) (limited to 'test/process_test.zig') diff --git a/test/process_test.zig b/test/process_test.zig index b1f64a45..bab80183 100644 --- a/test/process_test.zig +++ b/test/process_test.zig @@ -1,10 +1,14 @@ //! The process tests (T2 of docs/next-tests.md): each case starts a real //! pardes through test/process.zig and drives it over its socket. A file -//! in test/process/ a section of the book, as test/behaviour/'s are. +//! in test/process/ a section of the book, as test/behaviour/'s are. The +//! tty frontend's own (test/process/tty_*.zig) drive it through a pty of +//! its own instead (test/tty.zig), on the tty alone. //! -//! pardes-process-test [filter] +//! pardes-process-test [--backend=tui|gui] [--addon=]... +//! [--lspmock=] [--update] [filter] const std = @import("std"); const process = @import("process.zig"); +const tty = @import("tty.zig"); const files = .{ @import("process/builtins.zig"), @@ -20,9 +24,22 @@ const files = .{ @import("process/window.zig"), }; +/// Each case in a child of its own (Tty.init sets the child's environment +/// and directory), a core's worth at once, as each has a directory of its +/// own. One that fails runs again alone with the settle probe's wider bet +/// before it counts: a loaded machine loses the narrow one (tty.beCareful). +const tty_files = .{ + @import("process/tty_boot.zig"), + @import("process/tty_editing.zig"), + @import("process/tty_layout.zig"), + @import("process/tty_themes.zig"), + @import("process/tty_images.zig"), + @import("process/tty_terminals.zig"), +}; + pub fn main(init: std.process.Init) !void { - const all = try init.minimal.args.toSlice(init.arena.allocator()); - // [--backend=tui|gui] [--addon=]... [filter] + const arena = init.arena.allocator(); + const all = try init.minimal.args.toSlice(arena); var args: [3][:0]const u8 = undefined; var n: usize = 0; for (all[1..]) |a| { @@ -30,22 +47,28 @@ pub fn main(init: std.process.Init) !void { process.backend = std.meta.stringToEnum(process.Backend, a["--backend=".len..]) orelse return error.Usage; } else if (std.mem.startsWith(u8, a, "--addon=")) { if (process.naddons == process.addons.len) return error.Usage; - process.addons[process.naddons] = try std.Io.Dir.cwd().realPathFileAlloc(init.io, a["--addon=".len..], init.arena.allocator()); + process.addons[process.naddons] = try std.Io.Dir.cwd().realPathFileAlloc(init.io, a["--addon=".len..], arena); process.naddons += 1; + } else if (std.mem.startsWith(u8, a, "--lspmock=")) { + tty.lspmock = try std.Io.Dir.cwd().realPathFileAlloc(init.io, a["--lspmock=".len..], arena); + } else if (std.mem.eql(u8, a, "--update")) { + tty.update = true; } else if (n < args.len) { args[n] = a; n += 1; } } if (n < 1) return error.Usage; - const exe = args[0]; + // Absolute: a tty case runs in a directory of its own. + const exe = try std.Io.Dir.cwd().realPathFileAlloc(init.io, args[0], arena); const filter: []const u8 = if (n > 1) args[1] else ""; + tty.goldens = try std.Io.Dir.cwd().realPathFileAlloc(init.io, "test/snapshots", arena); const gpa = std.heap.page_allocator; const on = @tagName(process.backend); var failed: usize = 0; var skipped: usize = 0; var ran: usize = 0; - inline for (files) |file| inline for (file.cases) |case| { + if (!tty.update) inline for (files) |file| inline for (file.cases) |case| { if (std.mem.indexOf(u8, case[0], filter) != null) { ran += 1; const t0 = process.nowMs(); @@ -61,6 +84,62 @@ pub fn main(init: std.process.Init) !void { } } }; + if (process.backend == .tui) { + // pid and case of each child running, and the cases that failed. + const jobs: usize = if (tty.update) 1 else @max(1, (std.Thread.getCpuCount() catch 4) * 5 / 4); + var running: std.ArrayList(struct { pid: std.c.pid_t, name: []const u8 }) = .empty; + var again: std.ArrayList([]const u8) = .empty; + inline for (tty_files) |file| inline for (file.cases) |case| { + if (std.mem.indexOf(u8, case[0], filter) != null) { + ran += 1; + while (running.items.len >= jobs) try reap(arena, &running, &again, &skipped); + const pid = std.c.fork(); + if (pid == 0) std.process.exit(runTty(case, gpa, exe)); + if (pid < 0) return error.ForkFailed; + try running.append(arena, .{ .pid = pid, .name = case[0] }); + } + }; + while (running.items.len > 0) try reap(arena, &running, &again, &skipped); + tty.beCareful(); + for (again.items) |name| { + std.debug.print("again, alone: {s}\n", .{name}); + inline for (tty_files) |file| inline for (file.cases) |case| { + if (std.mem.eql(u8, case[0], name)) { + const pid = std.c.fork(); + if (pid == 0) std.process.exit(runTty(case, gpa, exe)); + var status: c_int = 0; + _ = std.c.waitpid(pid, &status, 0); + if (status != 0) failed += 1; + } + }; + } + } std.debug.print("{d} of {d} process tests failed on {s} ({d} skipped)\n", .{ failed, ran, on, skipped }); if (failed > 0 or ran == 0) std.process.exit(1); } + +/// One tty case, in the child: 0 passed, 1 failed, 2 skipped. +fn runTty(case: anytype, gpa: std.mem.Allocator, exe: [:0]const u8) u8 { + const t0 = process.nowMs(); + if (case[1](gpa, exe)) { + std.debug.print("{s} [tui] {d} ms {s}\n", .{ if (tty.update) "UPDATED" else "PASS", process.nowMs() - t0, case[0] }); + return 0; + } else |err| { + if (err == error.SkipZigTest) return 2; + if (@errorReturnTrace()) |t| std.debug.dumpErrorReturnTrace(t); + std.debug.print("FAIL [tui] {d} ms {s}: {t}\n", .{ process.nowMs() - t0, case[0], err }); + return 1; + } +} + +fn reap(arena: std.mem.Allocator, running: anytype, again: *std.ArrayList([]const u8), skipped: *usize) !void { + var status: c_int = 0; + const pid = std.c.waitpid(-1, &status, 0); + for (running.items, 0..) |job, i| { + if (job.pid != pid) continue; + // A child killed (a crash in the runner itself) failed too. + if (status == 2 << 8) skipped.* += 1 else if (status != 0) try again.append(arena, job.name); + _ = running.swapRemove(i); + return; + } +} -- cgit v1.3