//! docs/typ/reference.typ, 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}), null); const b = try c.look(ar, f.at("{s}/b.txt", .{root}), null); 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}), null); 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")); }