summaryrefslogtreecommitdiff
path: root/test/process/repls.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/process/repls.zig')
-rw-r--r--test/process/repls.zig79
1 files changed, 79 insertions, 0 deletions
diff --git a/test/process/repls.zig b/test/process/repls.zig
new file mode 100644
index 00000000..09e25e96
--- /dev/null
+++ b/test/process/repls.zig
@@ -0,0 +1,79 @@
+//! docs/typ/reference.typ, REPLs <repls>: an exec sent to a real REPL in
+//! a real pty.
+const std = @import("std");
+const process = @import("../process.zig");
+const Conn = process.Conn;
+const has = process.has;
+const check = process.check;
+
+pub const cases = .{
+ .{ "repls: a one-line exec sent to Python's own REPL (pyrepl, bracketed paste) runs at once, after idling too, and a block runs whole", pythonRepl },
+};
+
+/// Waits up to `ms` for the terminal's body to hold `text`.
+fn waitFor(a: std.mem.Allocator, c: *Conn, tty: u32, text: []const u8, ms: i64) !bool {
+ const deadline = process.nowMs() + ms;
+ while (process.nowMs() < deadline) {
+ if (has(try c.pane(a, tty, "body"), text)) return true;
+ process.nap(100);
+ }
+ return false;
+}
+
+fn pythonRepl(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ // 3.13's pyrepl asks for bracketed paste; an older one or none is no test.
+ const version = try process.run(a, &.{ "python3", "-c", "import sys; sys.exit(sys.version_info < (3, 13))" }, .{});
+ if (version.code != 0) {
+ std.debug.print("SKIP no python3 3.13 or later\n", .{});
+ return;
+ }
+ var d = try process.Detached.start(gpa, exe, "repl", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const before = try c.serials(a);
+ try c.write("/pane/1/ctl", "Tty\n");
+ const deadline = process.nowMs() + 10_000;
+ var tty: u32 = 0;
+ while (tty == 0 and process.nowMs() < deadline) : (process.nap(50)) {
+ for (try c.serials(a)) |n| if (std.mem.indexOfScalar(u32, before, n) == null) {
+ tty = @max(tty, n);
+ };
+ }
+ try check(tty != 0, "Tty made no pane", .{});
+ try check(try waitFor(a, c, tty, "$", 15_000) or try waitFor(a, c, tty, ">", 15_000), "no prompt: {s}", .{try c.pane(a, tty, "body")});
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/pty/data", .{tty}), "python3 -q\r");
+ try check(try waitFor(a, c, tty, ">>>", 15_000), "no >>>: {s}", .{try c.pane(a, tty, "body")});
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{tty}), "Repl python\n");
+ // An exec on the REPL's own body goes to the REPL, as its event record
+ // written back does: an empty range acts on its text.
+ const fid = try c.hold(try std.fmt.allocPrint(a, "/pane/{d}/event", .{tty}), 2);
+ defer c.clunk(fid);
+ _ = try c.writeFid(fid, 0, "MX0 0 0 10 print(6*7)\n");
+ try check(try waitFor(a, c, tty, "\n42", 15_000), "no 42: {s}", .{try c.pane(a, tty, "body")});
+ // After the session has sat idle, the Enter still waits for the paste:
+ // core time is as old as the last frame then.
+ for (0..5) |n| {
+ process.nap(2_000);
+ const line = try std.fmt.allocPrint(a, "print({d})", .{1000 + n});
+ _ = try c.writeFid(fid, 0, try std.fmt.allocPrint(a, "MX0 0 0 {d} {s}\n", .{ line.len, line }));
+ try check(try waitFor(a, c, tty, try std.fmt.allocPrint(a, "\n{d}", .{1000 + n}), 15_000), "{d}: {s}", .{ n, try c.pane(a, tty, "body") });
+ }
+ // More than one line, with and without a newline at the end, and a block
+ // with a statement after it: each runs whole, and the REPL is back at
+ // its prompt, not at `...`.
+ for ([_]struct { []const u8, []const []const u8 }{
+ .{ "print(11)\nprint(22)", &.{ "11", "22" } },
+ .{ "print(33)\nprint(44)\n", &.{ "33", "44" } },
+ .{ "for i in range(2):\n print(55 + i)\nprint(77)\n", &.{ "55", "56", "77" } },
+ }) |row| {
+ _ = try c.writeFid(fid, 0, try std.fmt.allocPrint(a, "MX0 0 0 {d} {s}\n", .{ row[0].len, row[0] }));
+ for (row[1]) |out| try check(try waitFor(a, c, tty, try std.fmt.allocPrint(a, "\n{s}\n", .{out}), 15_000), "{s}: no {s}: {s}", .{ row[0], out, try c.pane(a, tty, "body") });
+ process.nap(500);
+ const body = try c.pane(a, tty, "body");
+ try check(std.mem.endsWith(u8, std.mem.trimEnd(u8, body, " \t\r\n"), ">>>"), "{s}: not at its prompt: {s}", .{ row[0], body[body.len -| 200..] });
+ }
+}