diff options
Diffstat (limited to 'test/process.zig')
| -rw-r--r-- | test/process.zig | 136 |
1 files changed, 134 insertions, 2 deletions
diff --git a/test/process.zig b/test/process.zig index fd423985..f272fda5 100644 --- a/test/process.zig +++ b/test/process.zig @@ -500,8 +500,28 @@ pub fn of(gpa: std.mem.Allocator, h: *const eh.Harness) Files { /// and config directory, as test/fs.py's session started one: no session /// of the user's is reached. No daemon (PARDES_DAEMON=off, as the pty /// harness starts one): the process is the session, and nothing outlives it. +/// Which build the process tests drive (`zig build process-test +/// -Dplatform=tty|gui`, the runner's --backend): a session's effects +/// (saves, shells, watches, dumps) are its host's, so each case runs on the +/// host it was built with. +pub const Backend = enum { tty, gui }; +pub var backend: Backend = .tty; + +/// A case about one frontend's own behaviour (`--tty`, a killed terminal +/// editor) skips on the other. +pub fn only(b: Backend) !void { + if (backend != b) return error.SkipZigTest; +} + pub const Detached = struct { pid: libc.pid_t, + /// The window's (gui): the pty it runs on, a thread draining it, and + /// the directory its frames and status go to (PARDES_TEST_CAPTURE_DIR, + /// outside the root, whose panes would see a file changing every frame). + master: c_int = -1, + drain: ?std.Thread = null, + cap_buf: [64]u8 = undefined, + cap_len: usize = 0, root_buf: [4096]u8 = undefined, root_len: usize = 0, socket_buf: [4096]u8 = undefined, @@ -551,7 +571,26 @@ pub const Detached = struct { const sock_path = try std.fmt.bufPrint(&d.socket_buf, "{s}/9p/pardes/{s}", .{ dir, opts.socket orelse name }); d.socket_len = sock_path.len; var arg_buf: [256]u8 = undefined; - const detach = try std.fmt.bufPrintSentinel(&arg_buf, "--detach={s}", .{name}, 0); + // A window takes no --detach: it is its own session, named by --9p. + const detach = if (backend == .gui) + try std.fmt.bufPrintSentinel(&arg_buf, "--9p={s}", .{opts.socket orelse name}, 0) + else + try std.fmt.bufPrintSentinel(&arg_buf, "--detach={s}", .{name}, 0); + var cap_z: [64]u8 = undefined; + if (backend == .gui) { + const template = "/tmp/pardes-process-cap-XXXXXX"; + @memcpy(cap_z[0..template.len], template); + cap_z[template.len] = 0; + _ = mkdtemp(cap_z[0..template.len :0]) orelse return error.NoTempDir; + @memcpy(d.cap_buf[0..template.len], cap_z[0..template.len]); + d.cap_len = template.len; + } + errdefer if (d.cap_len > 0) removeDir(d.captures()); + var slave: c_int = -1; + if (backend == .gui) { + const ws: std.posix.winsize = .{ .row = 24, .col = 80, .xpixel = 0, .ypixel = 0 }; + if (openpty(&d.master, &slave, null, null, &ws) != 0) return error.NoPty; + } var env_buf: [3][4096]u8 = undefined; const root_z = try std.fmt.bufPrintSentinel(&env_buf[0], "{s}", .{dir}, 0); const config = try std.fmt.bufPrintSentinel(&env_buf[1], "{s}/config", .{dir}, 0); @@ -580,6 +619,23 @@ pub const Detached = struct { .{ "PARDES_LSP_C", "" }, .{ "PARDES_LSP_GO", "" }, .{ "PARDES_LSP_TS", "" }, .{ "PARDES_LSP_PY", "" }, .{ "PARDES_DAEMON", "off" }, }) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126); + if (backend == .gui) { + // Hidden and offscreen, its frames and status in its own + // directory: no display of the user's is ever touched. + for ([_][*:0]const u8{ "WAYLAND_DISPLAY", "DISPLAY" }) |v| _ = unsetenv(v); + for ([_][2][*:0]const u8{ + .{ "PARDES_TEST", "1" }, .{ "PARDES_TEST_CAPTURE_DIR", cap_z[0..d.cap_len :0] }, + // A detached session's grid, so a case means the same on both. + .{ "PARDES_TEST_COLS", "160" }, .{ "PARDES_TEST_ROWS", "50" }, + .{ "SDL_VIDEO_DRIVER", "offscreen" }, .{ "TERM", "xterm-256color" }, + }) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126); + // On a terminal of its own, as one started from a shell is. + _ = libc.setsid(); + _ = ioctl(slave, 0x540E, @as(c_int, 0)); // shortcut: Linux's TIOCSCTTY + _ = libc.dup2(slave, 0); + _ = libc.dup2(slave, 1); + _ = libc.close(d.master); + } for (opts.env) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126); if (libc.chdir(root_z) != 0) _exit(126); const fd = libc.open(err_log, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644)); @@ -589,7 +645,13 @@ pub const Detached = struct { } d.pid = pid; errdefer d.stop(); - const deadline = eh.nowMs() + 10_000; + if (backend == .gui) { + _ = libc.close(slave); + // What it writes to its terminal is read and dropped, or a full + // pty would stop it. + d.drain = try std.Thread.spawn(.{}, drainPty, .{d.master}); + } + const deadline = eh.nowMs() + 20_000; var sock_z: [4096]u8 = undefined; const socket_z = try std.fmt.bufPrintSentinel(&sock_z, "{s}", .{sock_path}, 0); while (libc.access(socket_z, 0) != 0) { @@ -669,9 +731,49 @@ pub const Detached = struct { pub fn deinit(d: *Detached) void { d.files.deinit(); d.stop(); + if (d.master >= 0) _ = libc.close(d.master); + if (d.drain) |t| t.join(); + if (d.cap_len > 0) removeDir(d.captures()); if (d.owns_root) d.removeRoot(); } + /// Where the window writes its frames and status (gui alone). + pub fn captures(d: *const Detached) []const u8 { + return d.cap_buf[0..d.cap_len]; + } + + /// The window's frame once it is idle: a loop step two past the one + /// current when this was asked, nothing owing a frame (gui.zig's + /// writeTestStatus). Every request before this one has been served (a + /// 9P write returns once the core has it), so the frame is the scene's + /// however loaded the machine is. A binary PPM (P6), kept by the caller. + pub fn frame(d: *Detached, gpa: std.mem.Allocator) ![]u8 { + if (d.cap_len == 0) return error.NotAWindow; + var path: [128]u8 = undefined; + const status_path = try std.fmt.bufPrint(&path, "{s}/status", .{d.captures()}); + const deadline = eh.nowMs() + 60_000; + var first_step: ?u64 = null; + while (eh.nowMs() < deadline) : (sleepMs(20)) { + const text = readWhole(gpa, status_path) catch continue; + defer gpa.free(text); + var words = std.mem.tokenizeAny(u8, text, " \n"); + const step = std.fmt.parseInt(u64, words.next() orelse continue, 10) catch continue; + const frames = std.fmt.parseInt(u64, words.next() orelse continue, 10) catch continue; + const idle = std.mem.eql(u8, words.next() orelse continue, "1"); + const first = first_step orelse { + first_step = step; + continue; + }; + if (step >= first + 2 and idle and frames > 0) { + var ppm: [128]u8 = undefined; + const got = try readWhole(gpa, try std.fmt.bufPrint(&ppm, "{s}/latest.ppm", .{d.captures()})); + if (std.mem.startsWith(u8, got, "P6")) return got; + gpa.free(got); + } + } + return error.NeverIdle; + } + fn removeRoot(d: *Detached) void { var cmd: [4200]u8 = undefined; if (std.fmt.bufPrintSentinel(&cmd, "rm -rf -- '{s}'", .{d.root()}, 0)) |c| { @@ -1012,3 +1114,33 @@ pub fn quicIncluded(gpa: std.mem.Allocator, exe: [:0]const u8) !bool { defer gpa.free(r.said); return !has(r.said, "QUIC is not included"); } + +fn drainPty(fd: c_int) void { + var buf: [4096]u8 = undefined; + while (libc.read(fd, &buf, buf.len) > 0) {} +} + +fn removeDir(path: []const u8) void { + var cmd: [4200]u8 = undefined; + if (std.fmt.bufPrintSentinel(&cmd, "rm -rf -- '{s}'", .{path}, 0)) |c| { + _ = system(c); + } else |_| {} +} + +fn readWhole(gpa: std.mem.Allocator, path: []const u8) ![]u8 { + var buf: [4096]u8 = undefined; + const z = try std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0); + const fd = libc.open(z, .{}, @as(libc.mode_t, 0)); + if (fd < 0) return error.FileNotFound; + defer _ = libc.close(fd); + var out: std.ArrayList(u8) = .empty; + errdefer out.deinit(gpa); + var chunk: [64 * 1024]u8 = undefined; + while (true) { + const n = libc.read(fd, &chunk, chunk.len); + if (n < 0) return error.ReadFailed; + if (n == 0) break; + try out.appendSlice(gpa, chunk[0..@intCast(n)]); + } + return out.toOwnedSlice(gpa); +} |
