summaryrefslogtreecommitdiff
path: root/test/process/terminals.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/process/terminals.zig')
-rw-r--r--test/process/terminals.zig93
1 files changed, 93 insertions, 0 deletions
diff --git a/test/process/terminals.zig b/test/process/terminals.zig
index d9c3bcca..151b221b 100644
--- a/test/process/terminals.zig
+++ b/test/process/terminals.zig
@@ -16,6 +16,7 @@ pub const cases = .{
.{ "terminals: a program's prompt shows and holds still, input sent unsubmitted shows, and ^U takes the screen back to what it was", prompt },
.{ "terminals: what a program prints past a screenful stays in the terminal's body, and its prompt after it shows", history },
.{ "terminals: a control byte through pty/data reaches a program in raw mode, which then carries on", rawKey },
+ .{ "terminals: a shell started into a pane runs at the pane's grid, not the screen's: a new terminal, its shell started again by pty/ctl's exec, a finished command pane run again", sizedFork },
};
fn has(hay: []const u8, needle: []const u8) bool {
@@ -78,6 +79,98 @@ const Other = struct {
}
};
+/// What `stty size` in terminal `term` says, beside the grid its pty/ctl
+/// reads back, each as "rows cols".
+fn sttySize(a: std.mem.Allocator, c: *process.Conn, term: u32) ![2][]const u8 {
+ var lines = std.mem.splitScalar(u8, try run(a, c, term, "stty size\n"), '\n');
+ _ = lines.next();
+ return .{ lines.next() orelse "", try grid(a, c, term) };
+}
+
+/// Pane `n`'s grid, as pty/ctl reads it back ("winsize C R"): "rows cols".
+fn grid(a: std.mem.Allocator, c: *process.Conn, n: u32) ![]const u8 {
+ var w = std.mem.tokenizeAny(u8, try c.read(a, try std.fmt.allocPrint(a, "/pane/{d}/pty/ctl", .{n})), " \n");
+ _ = w.next();
+ const cols = w.next() orelse "";
+ return std.fmt.allocPrint(a, "{s} {s}", .{ w.next() orelse "", cols });
+}
+
+/// The lines of pane `n`'s body that are two numbers (`stty size`'s).
+fn sizeLines(a: std.mem.Allocator, c: *process.Conn, n: u32) ![]const []const u8 {
+ var out: std.ArrayList([]const u8) = .empty;
+ var rows = std.mem.tokenizeScalar(u8, try c.read(a, try std.fmt.allocPrint(a, "/pane/{d}/body", .{n})), '\n');
+ while (rows.next()) |row| {
+ var w = std.mem.tokenizeScalar(u8, std.mem.trim(u8, row, " \r"), ' ');
+ const r = w.next() orelse continue;
+ const k = w.next() orelse continue;
+ if (w.next() != null) continue;
+ _ = std.fmt.parseInt(u16, r, 10) catch continue;
+ _ = std.fmt.parseInt(u16, k, 10) catch continue;
+ try out.append(a, try std.fmt.allocPrint(a, "{s} {s}", .{ r, k }));
+ }
+ return out.items;
+}
+
+/// A shell takes runs: its first prompt, or the one started again.
+fn prompted(a: std.mem.Allocator, c: *process.Conn, term: u32) !void {
+ const deadline = eh.nowMs() + 10_000;
+ while (!std.mem.eql(u8, try run(a, c, term, "true\n"), "exit 0\n")) {
+ if (eh.nowMs() > deadline) return error.NoPrompt;
+ process.sleep(50);
+ }
+}
+
+fn sizedFork(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "sized", .{});
+ defer d.deinit();
+ const f = &d.files;
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const c = try process.Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const text = (try serials(a, c))[0];
+ try c.write("/exec", "Shell bash\n");
+ // A terminal beside the text: its grid is its rect less the tag, never
+ // the screen's 160x50.
+ try c.write("/exec", "Term\n");
+ const term = try c.newest(gpa);
+ try prompted(a, c, term);
+ var got = try sttySize(a, c, term);
+ try expect(!std.mem.eql(u8, got[1], "50 160"));
+ try expectEqualStrings(got[1], got[0]);
+ // Its shell started again: the pane is sized already, and nothing
+ // resizes it after.
+ const pid = try run(a, c, term, "echo $$\n");
+ try c.write(f.at("/pane/{d}/pty/ctl", .{term}), "exec\n");
+ const again_by = eh.nowMs() + 10_000;
+ while (true) {
+ const now = run(a, c, term, "echo $$\n") catch "";
+ if (std.mem.startsWith(u8, now, "exit 0\n") and !std.mem.eql(u8, now, pid)) break;
+ if (eh.nowMs() > again_by) return error.NeverStartedAgain;
+ process.sleep(50);
+ }
+ got = try sttySize(a, c, term);
+ try expectEqualStrings(got[1], got[0]);
+ // A command pane, then the same line again: the finished pane is run
+ // again (exec.zig `runCommand`), a pane sized already. (The new one is
+ // placed a step after its fork: its first size can be the screen's.)
+ try c.exec(text, "stty size");
+ var words = std.mem.tokenizeAny(u8, try c.pane(a, text, "exec"), " \n");
+ var last: []const u8 = "";
+ while (words.next()) |w| last = w;
+ const cmd = try std.fmt.parseInt(u32, last, 10);
+ for (1..3) |k| {
+ if (k == 2) try c.exec(text, "stty size");
+ const by = eh.nowMs() + 10_000;
+ while ((try sizeLines(a, c, cmd)).len < k) {
+ if (eh.nowMs() > by) return error.NoSize;
+ process.sleep(50);
+ }
+ if (k == 2) try expectEqualStrings(try grid(a, c, cmd), (try sizeLines(a, c, cmd))[1]);
+ }
+}
+
fn runFile(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
var d = try process.Detached.start(gpa, exe, "run", .{});
defer d.deinit();