summaryrefslogtreecommitdiff
path: root/test/process/edit.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/edit.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/edit.zig')
-rw-r--r--test/process/edit.zig139
1 files changed, 139 insertions, 0 deletions
diff --git a/test/process/edit.zig b/test/process/edit.zig
new file mode 100644
index 00000000..8cccfdd1
--- /dev/null
+++ b/test/process/edit.zig
@@ -0,0 +1,139 @@
+//! docs/typ/reference.typ, Edit <edit>, through a real session's socket:
+//! its commands run in the session's shell, and its w writes.
+const std = @import("std");
+const process = @import("../process.zig");
+const eh = @import("../e2e_harness.zig");
+const expect = std.testing.expect;
+const expectEqual = std.testing.expectEqual;
+const expectEqualStrings = std.testing.expectEqualStrings;
+
+pub const cases = .{
+ .{ "edit: < | > run off the loop and answer their write once applied, a failing one changing nothing; a held Edit write is flushed; X edits every open file it matches, w writes", editCommands },
+};
+
+fn has(hay: []const u8, needle: []const u8) bool {
+ return std.mem.indexOf(u8, hay, needle) != null;
+}
+
+fn refusedSaying(c: *process.Conn, path: []const u8, data: []const u8, why: []const u8) !void {
+ try std.testing.expectError(error.Refused, c.write(path, data));
+ if (!has(c.said, why)) {
+ std.debug.print("{s} refused saying {s}, not {s}\n", .{ path, c.said, why });
+ return error.WrongWords;
+ }
+}
+
+/// An Edit written on another connection, in a thread.
+const Slow = struct {
+ conn: *process.Conn,
+ path: []const u8,
+ done: std.atomic.Value(bool) = .init(false),
+
+ fn go(s: *Slow) void {
+ s.conn.write(s.path, "Edit , | sleep 1; cat\n") catch {};
+ s.done.store(true, .release);
+ }
+};
+
+fn editCommands(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "edit", .{});
+ defer d.deinit();
+ const f = &d.files;
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const ar = arena.allocator();
+ const c = try process.Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const root = d.root();
+ try d.put("a.txt", "pear\napple\nfig\n");
+ try d.put("b.txt", "apple pie\n");
+ const a = try c.look(ar, f.at("{s}/a.txt", .{root}));
+ const b = try c.look(ar, f.at("{s}/b.txt", .{root}));
+ const actl = f.at("/pane/{d}/ctl", .{a});
+ const abody = f.at("/pane/{d}/body", .{a});
+ // The write returns with the text sorted, through the session's shell
+ // in the file's directory.
+ try c.write(actl, "Edit , | sort\n");
+ try expectEqualStrings("apple\nfig\npear\n", try c.read(ar, abody));
+ try c.write(actl, "Edit $ < pwd\n");
+ try expectEqualStrings(f.at("apple\nfig\npear\n{s}\n", .{root}), try c.read(ar, abody));
+ try c.write(actl, "Undo\n");
+ try expectEqualStrings("apple\nfig\npear\n", try c.read(ar, abody));
+ // A per-match command, one undo step.
+ try c.write(actl, "Edit ,x/[a-z]+/ | tr a-z A-Z\n");
+ try expectEqualStrings("APPLE\nFIG\nPEAR\n", try c.read(ar, abody));
+ // A failing command: nothing changed, the write fails in words, its
+ // stderr in +Errors.
+ try refusedSaying(c, actl, "Edit , | echo nope >&2; exit 4\n", "exit status 4");
+ try expectEqualStrings("APPLE\nFIG\nPEAR\n", try c.read(ar, abody));
+ var said = false;
+ var rows = std.mem.tokenizeScalar(u8, try c.read(ar, "/index"), '\n');
+ while (rows.next()) |row| {
+ if (!has(row, "+Errors")) continue;
+ said = said or has(try c.read(ar, f.at("/pane/{s}/body", .{row[0..std.mem.indexOfScalar(u8, row, ' ').?]})), "nope");
+ }
+ try expect(said);
+ // While a slow command runs, the editor answers.
+ {
+ const other = try process.Conn.dial(gpa, d.socket());
+ defer other.deinit(gpa);
+ var slow: Slow = .{ .conn = other, .path = f.at("/pane/{d}/ctl", .{b}) };
+ const worker = try std.Thread.spawn(.{}, Slow.go, .{&slow});
+ process.sleep(200);
+ const began = eh.nowMs();
+ _ = try c.read(ar, "/index");
+ const quick = eh.nowMs() - began < 500 and !slow.done.load(.acquire);
+ worker.join();
+ try expect(quick); // the editor did not wait on the command
+ try expect(slow.done.load(.acquire));
+ }
+ // On the same connection: the Edit's write is held, and a read sent
+ // after it is answered first; a Tflush of it stops it, changes nothing,
+ // and frees the way for the next Edit.
+ const ctl = try c.hold(actl, 1);
+ const status = try c.hold("/status", 0);
+ const began = eh.nowMs();
+ const held = try c.send(.{ .write = .{ .fid = ctl, .offset = 0, .data = "Edit , | sleep 1; cat\n" } });
+ const read = try c.send(.{ .read = .{ .fid = status, .offset = 0, .count = 4096 } });
+ const first = try c.answer();
+ try expect(first.tag == read and first.result == .read and eh.nowMs() - began < 500);
+ const second = try c.answer();
+ try expect(second.tag == held and second.result == .write);
+ const flushed = try c.send(.{ .write = .{ .fid = ctl, .offset = 0, .data = "Edit , | sleep 5; tr a-z A-Z\n" } });
+ process.sleep(300);
+ const flush = try c.send(.{ .flush = .{ .oldtag = flushed } });
+ const one = (try c.answer()).tag;
+ const two = (try c.answer()).tag;
+ try expect((one == flushed and two == flush) or (one == flush and two == flushed));
+ c.clunk(ctl);
+ c.clunk(status);
+ process.sleep(1000);
+ try c.write(actl, "Edit , | cat\n");
+ process.sleep(4500);
+ try expectEqualStrings("APPLE\nFIG\nPEAR\n", try c.read(ar, abody));
+ // X: each open file whose line matches; w writes it.
+ try c.write(actl, "Edit X/b\\.txt$/ ,s/apple/APPLE/\n");
+ try expectEqualStrings("APPLE pie\n", try c.read(ar, f.at("/pane/{d}/body", .{b})));
+ try c.write(f.at("/pane/{d}/ctl", .{b}), "Edit w\n");
+ try expectEqualStrings("APPLE pie\n", try d.get(ar, "b.txt"));
+ try c.write(actl, "Edit 1 w first.txt\n");
+ try expectEqualStrings("APPLE\n", try d.get(ar, "first.txt"));
+ // A file changed on disk under unsaved edits: Edit's w refuses once, as
+ // Save does, and the next w overwrites it.
+ try d.put("w1.txt", "one\n");
+ const w = try c.look(ar, f.at("{s}/w1.txt", .{root}));
+ const wctl = f.at("/pane/{d}/ctl", .{w});
+ try c.write(wctl, "Edit $a/two/\n");
+ const changed = f.at("\nchanged {d}", .{w});
+ const seen = std.mem.count(u8, try c.read(ar, "/log"), changed);
+ try eh.appendFile(try std.fmt.allocPrintSentinel(ar, "{s}/w1.txt", .{root}, 0), "external\n");
+ const deadline = eh.nowMs() + 10_000;
+ while (std.mem.count(u8, try c.read(ar, "/log"), changed) == seen) {
+ if (eh.nowMs() > deadline) return error.NeverHeard;
+ process.sleep(50);
+ }
+ try refusedSaying(c, wctl, "Edit w\n", "modified on disk since read (w again to overwrite)");
+ try expectEqualStrings("one\nexternal\n", try d.get(ar, "w1.txt"));
+ try c.write(wctl, "Edit w\n");
+ try expectEqualStrings("one\ntwo", try d.get(ar, "w1.txt"));
+}