summaryrefslogtreecommitdiff
path: root/test/process.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 16:33:08 -0300
committerGabriel Schneider <[email protected]>2026-11-04 16:48:55 -0300
commitef0d017705146a74bf7cc2706b9796f227ea6977 (patch)
treeb4f14a8724055be7918416b8893c32760d0ee7f8 /test/process.zig
parentbe3985092df1c3388cfa09ca5e989d2fa66f55ec (diff)
downloadpardes-ef0d017705146a74bf7cc2706b9796f227ea6977.tar.gz
pardes-ef0d017705146a74bf7cc2706b9796f227ea6977.zip
test: test/fs.py's discovery, run_file, exit_answers, unattended_messages_expire, directory_panes, putall_answers_after_its_saves, new_terminals_named_once, recent_survives_restart, store_shared_between_windows, odd_paths, delcol_last_keeps_window, cut_writes and edit_commands in Zig, the Python deleted (and its now unused newest, walk_tree, del_twice and the --discovery switch): at T2 in new test/process/{tree,terminals,root_ctl,edit}.zig and other_files.zig (the tree's listing, stats, log follow, ctl lock, focus, commands, sel, Save making directories, the 16 slots; a write cut at an 8 KiB msize; pty/run's answers, busy and shell gone; terminals named once; Exit; MessageLinger unattended; Putall; odd paths and ThemeFile; Edit's < | > off the loop, a held write answered after a later read and flushed, X and w; /recent across a restart and a store two sessions share), at T1 a directory pane in test/behaviour/look_and_exec.zig and 9P's new after Delcol of the last column in columns_and_tags.zig; discovery brought up to the tree as documented (sessions, kbdin, mousein at the root, /index stats 0, a sel write stays selected, the unknown-message words, a look's line in dot, a Save under / refused as permission denied), as the Python had gone stale; process.Conn gains replace, writeCut, create, newPane, newest, look, dialRefusal, send and answer, and process.sleep; a failing process test prints its error return trace; each checked to fail against a break (a detached session 161 wide, 17 connection slots, a pty taking one row, Edit's failure said as exit code, ThemeFile's .zon words changed, Exit not quitting, unattended messages never expiring, a directory pane's sub/ without its slash, Putall's modified-on-disk words changed, /recent saying shut for closed, a missing interpreter said as missing)
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'test/process.zig')
-rw-r--r--test/process.zig121
1 files changed, 121 insertions, 0 deletions
diff --git a/test/process.zig b/test/process.zig
index f797c063..56fe12ba 100644
--- a/test/process.zig
+++ b/test/process.zig
@@ -182,6 +182,119 @@ pub const Conn = struct {
}
return names.toOwnedSlice(gpa);
}
+
+ // fsa:
+
+ /// `data` written to `path` opened OTRUNC (a shell's `>`), the open
+ /// clunked after; a refusal by a write or the clunk is `error.Refused`.
+ pub fn replace(c: *Conn, path: []const u8, data: []const u8) !void {
+ try c.writeCut(path, 1 | 16, data, msize - 24);
+ }
+
+ /// `data` written to `path` (opened `mode`) in writes of at most `cut`
+ /// bytes, as a client with a smaller msize cuts them.
+ pub fn writeCut(c: *Conn, path: []const u8, mode: u8, data: []const u8, cut: usize) !void {
+ const fid = try c.hold(path, mode);
+ var at: usize = 0;
+ while (at < data.len) {
+ const n = c.writeFid(fid, at, data[at..@min(data.len, at + cut)]) catch |err| {
+ c.clunk(fid);
+ return err;
+ };
+ if (n == 0) {
+ c.clunk(fid);
+ return error.ShortWrite;
+ }
+ at += n;
+ }
+ _ = try c.call(.{ .clunk = .{ .fid = fid } });
+ }
+
+ /// A file or directory (`perm` with c9.dmdir) made in `dir`.
+ pub fn create(c: *Conn, dir: []const u8, name: []const u8, perm: u32) !void {
+ const fid = try c.walk(dir);
+ defer c.clunk(fid);
+ _ = try c.call(.{ .create = .{ .fid = fid, .name = name, .perm = perm, .mode = 0 } });
+ }
+
+ /// A new pane, made by an open of /pane/new; its serial.
+ pub fn newPane(c: *Conn) !u32 {
+ const fid = try c.hold("/pane/new", 0);
+ defer c.clunk(fid);
+ return std.fmt.parseInt(u32, std.mem.trim(u8, try c.readFid(fid, 0, 64), "\n"), 10);
+ }
+
+ /// The newest pane's serial: /index is ordered by serial.
+ pub fn newest(c: *Conn, gpa: std.mem.Allocator) !u32 {
+ const index = try c.read(gpa, "/index");
+ defer gpa.free(index);
+ var rows = std.mem.splitBackwardsScalar(u8, std.mem.trimEnd(u8, index, "\n"), '\n');
+ const last = rows.first();
+ return std.fmt.parseInt(u32, last[0 .. std.mem.indexOfScalar(u8, last, ' ') orelse last.len], 10);
+ }
+
+ /// A right click on `path` from a fresh scratch, which goes after: the
+ /// one pane /look names, never the scratch.
+ pub fn look(c: *Conn, gpa: std.mem.Allocator, path: []const u8) !u32 {
+ const scratch = try c.newPane();
+ var buf: [4200]u8 = undefined;
+ try c.write(try std.fmt.bufPrint(&buf, "/pane/{d}/look", .{scratch}), try std.fmt.bufPrint(buf[64..], "{s}\n", .{path}));
+ const got = try c.read(gpa, "/look");
+ defer gpa.free(got);
+ var rows = std.mem.tokenizeAny(u8, got, " \n");
+ const opened = try std.fmt.parseInt(u32, rows.next() orelse return error.NothingOpened, 10);
+ if (rows.next() != null or opened == scratch) return error.NotOnePane;
+ try c.remove(try std.fmt.bufPrint(&buf, "/pane/{d}", .{scratch}));
+ return opened;
+ }
+
+ /// The words a server refuses a new connection's Tversion with (its
+ /// slots all taken), copied into `buf`; error.NotRefused when it took it.
+ pub fn dialRefusal(gpa: std.mem.Allocator, path: []const u8, buf: []u8) ![]const u8 {
+ const c = try gpa.create(Conn);
+ defer gpa.destroy(c);
+ c.* = .{ .fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0), .cl = undefined };
+ c.cl = c9.Client.init(.{ .in = &c.in, .out = &c.out });
+ if (c.fd < 0) return error.NoSocket;
+ defer _ = libc.close(c.fd);
+ var addr: libc.sockaddr.un = .{ .path = @splat(0) };
+ if (path.len >= addr.path.len) return error.NameTooLong;
+ @memcpy(addr.path[0..path.len], path);
+ if (libc.connect(c.fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) != 0) return error.ConnectFailed;
+ _ = c.call(.{ .version = .{ .msize = msize } }) catch |err| {
+ if (err != error.Refused) return err;
+ const n = @min(buf.len, c.said.len);
+ @memcpy(buf[0..n], c.said[0..n]);
+ return buf[0..n];
+ };
+ return error.NotRefused;
+ }
+
+ /// A request sent without waiting for its answer (several in flight on
+ /// one connection); its tag.
+ pub fn send(c: *Conn, req: c9.Client.Request) !u16 {
+ const tag = c.cl.submit(req) catch return error.Protocol;
+ while (c.cl.output().len > 0) {
+ const out = c.cl.output();
+ const n = libc.write(c.fd, out.ptr, out.len);
+ if (n <= 0) return error.Hangup;
+ c.cl.wrote(@intCast(n));
+ }
+ return tag;
+ }
+
+ /// The next answer to come, to whichever request `send` sent; a read's
+ /// bytes valid until the next call.
+ pub fn answer(c: *Conn) !c9.Client.Done {
+ while (true) {
+ if (c.cl.take()) |done| return done;
+ var buf: [msize]u8 = undefined;
+ const n = libc.read(c.fd, &buf, buf.len);
+ if (n <= 0) return error.Hangup;
+ var at: usize = 0;
+ while (at < n) at += c.cl.push(buf[at..@intCast(n)]);
+ }
+ }
};
/// A running pardes's file tree, dialled by its pid or socket path.
@@ -482,3 +595,11 @@ fn sleepMs(ms: u32) void {
var ts: libc.timespec = .{ .sec = 0, .nsec = @as(isize, ms) * std.time.ns_per_ms };
_ = libc.nanosleep(&ts, null);
}
+
+// fsa:
+
+/// A wait of `ms` milliseconds, for what a test gives a session time to do.
+pub fn sleep(ms: u32) void {
+ var ts: libc.timespec = .{ .sec = @intCast(ms / 1000), .nsec = @as(isize, ms % 1000) * std.time.ns_per_ms };
+ _ = libc.nanosleep(&ts, null);
+}