diff options
Diffstat (limited to 'test/snapshot.zig')
| -rw-r--r-- | test/snapshot.zig | 85 |
1 files changed, 18 insertions, 67 deletions
diff --git a/test/snapshot.zig b/test/snapshot.zig index 02215abd..d93face2 100644 --- a/test/snapshot.zig +++ b/test/snapshot.zig @@ -29,11 +29,7 @@ // lines <name> <n> [tail] create file with n numbered lines, tail on each // dirmk <name> create a subdirectory // run <shell...> run a command in the script's cwd (NOT in a pane) -// start <rows> <cols> [views=N] [arg] fork the app in a pty (optional -// extra CLI arg); views=N gives it N-1 more -// terminals, each a view of the session -// view <k> type into and capture terminal k (1 is the -// app's own; a resize there SIGWINCHes the app) +// start <rows> <cols> [arg] fork the app in a pty (optional extra CLI arg) // wait <ms> <needle...> pump until needle appears on the grid (fails hard) // settle <ms> pump for a fixed duration // stable <quiet_ms> <timeout_ms> pump until the grid stops changing @@ -685,14 +681,6 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const var chunk_len: usize = 0; var h: ?eh.Harness = null; defer if (h) |*hp| hp.deinit(); - // `views=N`: the app's other terminals, and the one driven now. - var others: [3]eh.Harness = undefined; - var nothers: usize = 0; - var group: [4]*eh.Harness = undefined; - var cur: usize = 0; - defer for (others[0..nothers]) |*o| o.deinit(); - // A capture is a delta against the previous one of its terminal's. - var prevs: [4]Prev = @splat(.{}); // captures are deltas against the previous one of the same kind (see Prev) var prev: Prev = .{}; @@ -758,46 +746,16 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const const cols = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); var extra: [6]?[*:0]const u8 = @splat(null); var nextra: usize = 0; - var nviews: usize = 1; while (tok.next()) |a| { - if (std.mem.startsWith(u8, a, "views=")) { - nviews = try std.fmt.parseInt(usize, a["views=".len..], 10); - if (nviews < 1 or nviews > 4) return error.BadScript; - continue; - } if (nextra >= extra.len) return error.BadScript; extra[nextra] = (try arena.dupeSentinel(u8, a, 0)).ptr; nextra += 1; } if (h) |*hp| hp.deinit(); // scripts may restart (e.g. dump then -l) - for (others[0..nothers]) |*o| o.deinit(); - nothers = 0; - cur = 0; // a new process is a new screen: capture it whole, or its first // frame would be a delta against a dead instance's rows prev = .{}; - prevs = @splat(.{}); - var paths: std.ArrayList(u8) = .empty; - while (nothers + 1 < nviews) : (nothers += 1) { - var path: [64]u8 = undefined; - others[nothers] = try eh.Harness.initView(gpa, rows, cols, &path); - try paths.print(arena, "{s}{s}", .{ if (paths.items.len > 0) " " else "", std.mem.sliceTo(&path, 0) }); - } - if (nothers > 0) _ = setenv("PARDES_TEST_VIEWS", try arena.dupeSentinel(u8, paths.items, 0), 1); h = try eh.Harness.initArgv(gpa, exe_z.ptr, rows, cols, extra[0..nextra]); - _ = unsetenv("PARDES_TEST_VIEWS"); - group[0] = &h.?; - for (others[0..nothers], 1..) |*o, i| { - o.pid = h.?.pid; - group[i] = o; - } - for (group[0 .. nothers + 1]) |v| v.views = group[0 .. nothers + 1]; - } else if (std.mem.eql(u8, cmd, "view")) { - const k = try std.fmt.parseInt(usize, tok.next() orelse return error.BadScript, 10); - if (k < 1 or k > nothers + 1) return error.BadScript; - prevs[cur] = prev; - cur = k - 1; - prev = prevs[cur]; } else if (std.mem.eql(u8, cmd, "snapfile")) { const label = tok.next() orelse return error.BadScript; const contents = try readFileAlloc(arena, tok.rest()); @@ -807,52 +765,51 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const try appendChunk(chunks, &chunk_len, try snapFile(arena, label, contents)); } else if (std.mem.eql(u8, cmd, "wait")) { const ms = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); - try on(&h, &group, nothers, cur).expectWaitFor(tok.rest(), ms, "snapshot wait"); + try live(&h).expectWaitFor(tok.rest(), ms, "snapshot wait"); } else if (std.mem.eql(u8, cmd, "settle")) { - try on(&h, &group, nothers, cur).pump(try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10)); + try live(&h).pump(try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10)); } else if (std.mem.eql(u8, cmd, "stable")) { const quiet = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); const timeout = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); - try waitStable(on(&h, &group, nothers, cur), quiet, timeout); + try waitStable(live(&h), quiet, timeout); } else if (std.mem.eql(u8, cmd, "text")) { - try on(&h, &group, nothers, cur).send(tok.rest()); + try live(&h).send(tok.rest()); } else if (std.mem.eql(u8, cmd, "send")) { - try on(&h, &group, nothers, cur).send(try unescape(arena, tok.rest())); + try live(&h).send(try unescape(arena, tok.rest())); } else if (std.mem.eql(u8, cmd, "key")) { - while (tok.next()) |name| try on(&h, &group, nothers, cur).send(keyBytes(name) orelse return error.BadKey); + while (tok.next()) |name| try live(&h).send(keyBytes(name) orelse return error.BadKey); } else if (std.mem.eql(u8, cmd, "press") or std.mem.eql(u8, cmd, "release") or std.mem.eql(u8, cmd, "drag")) { const btn = buttonCode(tok.next() orelse return error.BadScript) orelse return error.BadScript; const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); + const col = try resolveCol(live(&h), col_spec, row); const b: u16 = if (std.mem.eql(u8, cmd, "drag")) btn + 32 else btn; const fin: u8 = if (std.mem.eql(u8, cmd, "release")) 'm' else 'M'; - try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}{c}", .{ b, col, row, fin })); + try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}{c}", .{ b, col, row, fin })); } else if (std.mem.eql(u8, cmd, "motion")) { const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); - try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<35;{d};{d}M", .{ col, row })); + const col = try resolveCol(live(&h), col_spec, row); + try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<35;{d};{d}M", .{ col, row })); } else if (std.mem.eql(u8, cmd, "wheel")) { const dir = tok.next() orelse return error.BadScript; const b: u16 = if (std.mem.eql(u8, dir, "up")) 64 else 65; const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); - try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}M", .{ b, col, row })); + const col = try resolveCol(live(&h), col_spec, row); + try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}M", .{ b, col, row })); } else if (std.mem.eql(u8, cmd, "resize")) { const rows = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); const cols = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - try on(&h, &group, nothers, cur).resize(rows, cols); + try live(&h).resize(rows, cols); // Another view's terminal is no one's controlling one: the app // is told it changed, as a stub tells it of its own. - if (cur > 0) std.posix.kill(live(&h).pid, std.posix.SIG.WINCH) catch {}; } else if (std.mem.eql(u8, cmd, "snap")) { - try appendChunk(chunks, &chunk_len, try snapText(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snapText(arena, live(&h), tok.rest(), &prev)); } else if (std.mem.eql(u8, cmd, "snapstyle")) { - try appendChunk(chunks, &chunk_len, try snapStyle(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snapStyle(arena, live(&h), tok.rest(), &prev)); } else if (std.mem.eql(u8, cmd, "snap9p")) { - try appendChunk(chunks, &chunk_len, try snap9p(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snap9p(arena, live(&h), tok.rest(), &prev)); } else { std.debug.print("unknown command {s}\n", .{cmd}); return error.BadScript; @@ -967,12 +924,6 @@ fn snapFile(arena: std.mem.Allocator, label: []const u8, contents: []const u8) ! } /// The terminal the script drives now (`view`). -fn on(h: *?eh.Harness, group: *[4]*eh.Harness, nothers: usize, cur: usize) *eh.Harness { - const app = live(h); - if (cur == 0 or cur > nothers) return app; - return group[cur]; -} - fn live(h: *?eh.Harness) *eh.Harness { if (h.*) |*v| return v; fatal("script used the app before `start`", .{}); @@ -1010,7 +961,7 @@ fn waitStable(h: *eh.Harness, quiet: i64, timeout: i64) !void { // and nothing in its process tree is runnable. Held across two probes, // because a pane's shell can write and sleep a scheduler tick before // the app wakes to read it. - if (since >= probe_after_ms and h.pendingAll() == 0 and treeAsleep(h.pid)) { + if (since >= probe_after_ms and h.pending() == 0 and treeAsleep(h.pid)) { if (asleep_since == 0) { asleep_since = now; } else if (now - asleep_since >= probe_hold_ms) { |
