From fb9b3a3304e0bdbeffc68e56dfe2ad6511a6a64e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 10:35:50 -0300 Subject: test: nineteen of src/ninep/pane.zig's tests through the 9P tree alone: in test/behaviour/flags.zig a > into an empty body one undo step (Undo empties it), a write that changes nothing no step and no new qid version (an empty : > data, a line written over itself, Edit's s/d/d/), and an open's hundred writes one edit (version +1, one Undo); in addresses.zig a 9P write moving dot as acme's textinsert (dot written, read back), an addr pair out of order or range refused in the address form's words, every address on a rune (a combining mark, a lone \r, a match inside a rune), and truncating data or xdata deleting only the addressed range; in panes.zig name refusing a directory (EISDIR), two lines on a held open or not, a component over 255 bytes by the write that holds it, a name cut across writes applied at its newline or close, a scratch promoted by name without touching its body, its file on disk or its undo, relative names from the file's directory and from a pane/new scratch's session directory and a /virtual one, errors appending to one +Errors (found in /index), and a truncated body's stat; in tags.zig a pane tag read whole and appended to up to 4096 bytes, and a truncated tag cleared, undone by u in the tag, clicked on, and a > dropping its one trailing newline; in look_and_exec.zig a pane's tagexec; each checked to fail against a break (a truncated empty body's step joined, spliceBody's no-op return gone, every held write put in at once, shiftOne growing nothing at dot's start, a pair's range unchecked, #n not snapped to a rune, data's truncation to the end, is_dir false, o.named never set, a held name's component limit 1255, a pane open's closing line dropped, a rename pushing an undo step, names resolved from /, pane/new taking the active pane's directory, a new +Errors every write, the tag's room one more, the tag's clear not remembered, the results read with a blank, an empty truncation of body); two old tests trimmed to what is an effect (a long path's failed Save; the watch a rename asks for); the line-index U test, the clock-held writes X test and the terminal and shell ones (pty bytes, output, spawns) stay Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/ninep/pane.zig | 505 +++-------------------------------------------------- 1 file changed, 21 insertions(+), 484 deletions(-) (limited to 'src') diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index bf9dc381..cfc93ee5 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -312,56 +312,6 @@ pub fn spliceBody(p: *Pardes, pane: *Pane, q0: usize, q1: usize, bytes: []const return take; } -test "a shell's > into an empty pane's body is one undo step, as into any other" { - const p = try withFile(testing.allocator, ""); - defer p.deinit(); - const pane = p.panes[0].?; - const f = &pane.file.?; - const body = Node.of(pane.serial, .body); - const steps = f.history.undo_len; - const h = call(p, .{ .tag = 1, .op = .open, .node = body, .omode = 1 }).reply.handle; - _ = call(p, .{ .tag = 2, .op = .setattr, .node = body, .truncate = true }); - try testing.expectEqual(tree.Status.ok, call(p, .{ .tag = 3, .op = .write, .node = body, .handle = h, .data = "fresh\n" }).reply.status); - _ = call(p, .{ .tag = 4, .op = .release, .node = body, .handle = h, .opened = true }); - try testing.expectEqualStrings("fresh\n", f.content); - try testing.expectEqual(steps + 1, f.history.undo_len); -} - -test "a write that changes nothing, an empty range deleted or a range replaced by itself, makes no undo step" { - const p = try withFile(testing.allocator, "abc\ndef\n"); - defer p.deinit(); - const pane = p.panes[0].?; - const serial = pane.serial; - const f = &pane.file.?; - const steps = f.history.undo_len; - const revision = f.revision; - const data = Node.of(serial, .data); - // Each as a shell does it, on an open of its own: `: > data` after - // an empty address, then `echo abc > data` over the line it holds. - _ = wr(p, Node.of(serial, .addr), "#2,#2"); - const h = call(p, .{ .tag = 1, .op = .open, .node = data, .omode = 1 }).reply.handle; - try testing.expectEqual(tree.Status.ok, call(p, .{ .tag = 2, .op = .setattr, .node = data, .truncate = true }).reply.status); - _ = call(p, .{ .tag = 3, .op = .release, .node = data, .handle = h, .opened = true }); - for ([_][]const u8{ "abc\n", "abd\n" }) |text| { - _ = wr(p, Node.of(serial, .addr), "1"); - const w = call(p, .{ .tag = 4, .op = .open, .node = data, .omode = 1 }).reply.handle; - try testing.expectEqual(tree.Status.ok, call(p, .{ .tag = 5, .op = .write, .node = data, .handle = w, .data = text }).reply.status); - _ = call(p, .{ .tag = 6, .op = .release, .node = data, .handle = w, .opened = true }); - if (text[2] == 'c') { - try testing.expectEqualStrings("abc\ndef\n", f.content); - try testing.expectEqual(steps, f.history.undo_len); - try testing.expectEqual(revision, f.revision); - } - } - // Edit's `s/d/d/` is the same nothing. - const undone = f.history.undo_len; - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .ctl), "Edit ,s/d/d/\n").reply.status); - try testing.expectEqual(undone, f.history.undo_len); - // A real change is still one. - try testing.expectEqualStrings("abd\ndef\n", f.content); - try testing.expectEqual(steps + 1, f.history.undo_len); -} - test "rows through the line index are the ones counting newlines gives, at every offset" { const p = try withFile(testing.allocator, "héllo\nwo\u{301}rld\n\n last line é\ntail"); defer p.deinit(); @@ -1114,38 +1064,6 @@ test "a body write to a terminal pane types at its shell" { try testing.expectEqual(Status.ok, r.reply.status); } -test "tag reads the whole tag and writes append to the editable tail" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const pane = p.panes[0].?; - const node = Node.of(pane.serial, .tag); - - const whole = rd(p, node, 0, 4096); - try testing.expect(std.mem.startsWith(u8, whole.bytes, "/test.txt")); - try testing.expect(std.mem.indexOf(u8, whole.bytes, "Del") != null); - try testing.expectEqual(@as(u64, whole.bytes.len), call(p, .{ .tag = 1, .op = .getattr, .node = node }).reply.attr.size); - - const before = rd(p, node, 0, 4096).bytes.len; - const w = wr(p, node, " Mine"); - try testing.expectEqual(@as(u32, 5), w.reply.written); - try testing.expect(std.mem.endsWith(u8, tagline.curTail(pane), " Mine")); - const after = rd(p, node, 0, 4096); - try testing.expectEqual(before + 5, after.bytes.len); - try testing.expect(std.mem.endsWith(u8, after.bytes, " Mine")); - - // A tag is a text like the body: a newline written to it stays one, and - // the tag reads back taller by it. - try testing.expectEqual(@as(u32, 10), wr(p, node, "\nNext Line").reply.written); - try testing.expect(std.mem.endsWith(u8, tagline.curTail(pane), " Mine\nNext Line")); - try testing.expect(std.mem.endsWith(u8, rd(p, node, 0, 4096).bytes, " Mine\nNext Line")); - - if (pane.tag.own) |own| p.gpa.free(own); - pane.tag.own = try p.gpa.alloc(u8, limits.max_tag_tail); - @memset(pane.tag.own.?, 'x'); - try testing.expectEqual(E.NOSPC, wr(p, node, "x").errno()); -} - test "data never splits a rune, in either direction" { const gpa = testing.allocator; const p = try withFile(gpa, "\u{00e9}x\n"); @@ -1179,32 +1097,6 @@ test "the writes of one open are one undo step, as bash's line-buffered printf m } } -test "an open's writes in a row are held and go in as one edit, seen by the next request" { - const p = try withFile(testing.allocator, "one\n"); - defer p.deinit(); - const serial = serialOf(p); - const body = Node.of(serial, .body); - const h = call(p, .{ .tag = 1, .op = .open, .node = body, .omode = 1 }).reply.handle; - const revision = p.panes[0].?.file.?.revision; - for (0..100) |_| _ = call(p, .{ .tag = 2, .op = .write, .node = body, .handle = h, .data = "0123456789\n" }); - // Held: nothing edited yet. - try testing.expectEqual(revision, p.panes[0].?.file.?.revision); - // Any other request puts them in, once. - try testing.expectEqual(@as(usize, 4 + 1100), rd(p, body, 0, 4096).bytes.len); - try testing.expectEqual(revision + 1, p.panes[0].?.file.?.revision); - _ = call(p, .{ .tag = 3, .op = .release, .node = body, .handle = h, .opened = true }); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Undo").reply.status); - try testing.expectEqualStrings("one\n", rd(p, body, 0, 64).bytes); - // data inserts at its address go on the same way. - const data = Node.of(serial, .data); - _ = wr(p, Node.of(serial, .addr), "#0"); - const d = call(p, .{ .tag = 4, .op = .open, .node = data, .omode = 1 }).reply.handle; - _ = call(p, .{ .tag = 5, .op = .write, .node = data, .handle = d, .data = "a" }); - _ = call(p, .{ .tag = 5, .op = .write, .node = data, .handle = d, .data = "b" }); - _ = call(p, .{ .tag = 6, .op = .release, .node = data, .handle = d, .opened = true }); - try testing.expectEqualStrings("abone\n", rd(p, body, 0, 64).bytes); -} - test "writes held before the shell's clock is first seen go in at its first step" { const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); @@ -1266,97 +1158,12 @@ test "after a failed address, one that goes from it says there is none" { try testing.expectEqual(Status.ok, wr(p, addr, ".").reply.status); } -test "a 9P write moves dot only as acme's textinsert does, and scrolls only the view" { - const Case = struct { at: u32, want: State.Range }; - // dot is `cd` of `abcdef\n` (2..4); two bytes go in at `at`. - for ([_]Case{ - .{ .at = 0, .want = .{ .q0 = 4, .q1 = 6 } }, // before: shifted - .{ .at = 2, .want = .{ .q0 = 2, .q1 = 6 } }, // at its start: grown - .{ .at = 3, .want = .{ .q0 = 2, .q1 = 6 } }, // inside: grown - .{ .at = 4, .want = .{ .q0 = 2, .q1 = 4 } }, // at its end: left - .{ .at = 6, .want = .{ .q0 = 2, .q1 = 4 } }, // after: left - }) |c| { - const p = try withFile(testing.allocator, "abcdef\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[p.paneBySerial(serial).?].?; - setDot(pane, .{ .q0 = 2, .q1 = 4 }); - var addr: [16]u8 = undefined; - _ = wr(p, Node.of(serial, .addr), try std.fmt.bufPrint(&addr, "#{d}", .{c.at})); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .data), "XX").reply.status); - try testing.expectEqual(c.want, dotOf(pane)); - } - // A body append leaves dot, and a sel write after it replaces dot, not - // the text appended. - const p = try withFile(testing.allocator, "abcdef\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[p.paneBySerial(serial).?].?; - setDot(pane, .{ .q0 = 2, .q1 = 4 }); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .body), "more\n").reply.status); - try testing.expectEqual(State.Range{ .q0 = 2, .q1 = 4 }, dotOf(pane)); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .sel), "YY").reply.status); - try testing.expectEqualStrings("abYYef\nmore\n", fileOf(pane).?.content); -} - -test "an addr pair out of order or out of range is refused as the address form is, not clamped" { - const p = try withFile(testing.allocator, "abcdef\n"); - defer p.deinit(); - const serial = serialOf(p); - const addr = Node.of(serial, .addr); - try testing.expectEqualStrings(addressing.e_order, wr(p, addr, "5 2").reply.ename); - try testing.expectEqualStrings(addressing.e_order, wr(p, addr, "#5,#2").reply.ename); - try testing.expectEqualStrings(addressing.e_range, wr(p, addr, "2 99").reply.ename); - try testing.expectEqualStrings(addressing.e_range, wr(p, addr, "#2,#99").reply.ename); - try testing.expectEqual(Status.ok, wr(p, addr, "2 4").reply.status); - try testing.expectEqual(State.Range{ .q0 = 2, .q1 = 4 }, p.panes[p.paneBySerial(serial).?].?.fs.addr); -} - -test "name refuses a directory, EISDIR, and leaves the name as it was" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const node = Node.of(serial, .name); - for ([_][]const u8{ "/\n", "/tmp/\n", "sub/\n", "/tmp\n" }) |dir| { - if (comptime !pardes.hosted) if (std.mem.eql(u8, dir, "/tmp\n")) continue; - const r = wr(p, node, dir); - try testing.expectEqual(E.ISDIR, r.errno()); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "is a directory") != null); - } - try testing.expectEqualStrings("/test.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); -} - -test "a write of two lines to name is refused EINVAL, on a held open or not" { +// The rest moved to test/behaviour/panes.zig; a Save's failure and the +// watches a rename asks for are effects a host performs. +test "a long path's failed Save keeps its reason" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); const serial = serialOf(p); - const node = Node.of(serial, .name); - try testing.expectEqual(E.INVAL, wr(p, node, "/tmp/a\n/tmp/b\n").errno()); - const h = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle; - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "/tmp/a\n/tmp/b\n" }).errno()); - _ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h, .opened = true }); - try testing.expectEqualStrings("/test.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); - // Line by line, as bash writes it: the second line is refused. - const h2 = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle; - try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h2, .data = "/tmp/pardes-a\n" }).reply.status); - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h2, .data = "/tmp/pardes-b\n" }).errno()); - _ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h2, .opened = true }); - try testing.expectEqualStrings("/tmp/pardes-a", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); -} - -test "a name with a component over 255 bytes is refused, and a long path's failed Save keeps its reason" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const refused = wr(p, Node.of(serial, .name), "/tmp/" ++ &@as([256]u8, @splat('c')) ++ "/f.txt\n"); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectEqualStrings("invalid file name: a component over 255 bytes", refused.reply.ename); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .name), "/tmp/" ++ &@as([255]u8, @splat('c')) ++ "\n").reply.status); - // Held with no newline, it is refused by that write, not at the close. - const held = call(p, .{ .tag = 1, .op = .open, .node = Node.of(serial, .name), .omode = 1 }).reply.handle; - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 2, .op = .write, .node = Node.of(serial, .name), .handle = held, .data = "/tmp/" ++ &@as([256]u8, @splat('e')) }).errno()); - _ = call(p, .{ .tag = 3, .op = .release, .node = Node.of(serial, .name), .handle = held, .opened = true }); - try testing.expectEqual(@as(usize, 0), p.fs.closed_lines.items.len); // A Save of a 3000-byte path the host refuses: the waiting write's // reason is at the end, the path giving up its middle. // (Under /tmp: under / a user's Save is refused permission, not @@ -1372,28 +1179,25 @@ test "a name with a component over 255 bytes is refused, and a long path's faile try testing.expect(std.mem.endsWith(u8, late, "/f.txt: no such directory")); } -test "a name cut across writes is one name, applied once at its newline or its close" { - const p = try withFile(testing.allocator, "x\n"); +test "a scratch renamed to a file watches it only after its Save, and a virtual name never" { + const p = try withFile(testing.allocator, "opener\n"); defer p.deinit(); - const serial = serialOf(p); - const node = Node.of(serial, .name); - const h = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle; - try testing.expect(h != 0); - // A piece that fills its Twrite goes on in the next. - p.fs.write_room = "/tmp/pardes-na".len; - _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "/tmp/pardes-na" }); - p.fs.write_room = 0; - try testing.expectEqualStrings("/test.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); - _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "me.txt\n" }); - try testing.expectEqualStrings("/tmp/pardes-name.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); - _ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h, .opened = true }); - // No newline: held, whatever the write's size, and applied at the close. - const h3 = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle; - _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h3, .data = "/tmp/pardes-closed.txt" }); - try testing.expectEqualStrings("/tmp/pardes-name.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); - _ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h3, .opened = true }); - tree.runClosedLines(p); - try testing.expectEqualStrings("/tmp/pardes-closed.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); + const serial = try th.newPane(p); + const pane = p.panes[p.paneBySerial(serial).?].?; + const name = Node.of(serial, .name); + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + var directory_buf: [4096]u8 = undefined; + const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; + var line: [4120]u8 = undefined; + const renamed = wr(p, name, try std.fmt.bufPrint(&line, "{s}/renamed.zig\n", .{directory})); + try testing.expectEqual(false, renamed.watch.?); + try testing.expect(pane.file.?.watch_after_save); + const virtual = try th.newPane(p); + _ = wr(p, Node.of(virtual, .name), try std.fmt.bufPrint(&line, "/virtual/pane/{d}/body\n", .{serial})); + const unchanged = wr(p, Node.of(virtual, .name), "./body\n"); + try testing.expect(unchanged.watch == null); + try testing.expect(!p.panes[p.paneBySerial(virtual).?].?.file.?.watch_after_save); } test "a replace through data, truncate then write, is one undo step" { @@ -1424,120 +1228,6 @@ test "writes under mark 0 are one undo step, even with no history before them" { try testing.expectEqualStrings("one\ntwo\nthree\n", rd(p, body, 0, 64).bytes); } -test "truncating the tag clears its editable tail" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const pane = p.panes[0].?; - const tag = Node.of(pane.serial, .tag); - _ = wr(p, tag, " Mine"); - try testing.expect(tagline.curTail(pane).len > 0); - const cleared = call(p, .{ .tag = 1, .op = .setattr, .node = tag, .truncate = true }); - try testing.expectEqual(Status.ok, cleared.reply.status); - // Done at the first read or write after it (or the close). - _ = rd(p, tag, 0, 4096); - try testing.expectEqualStrings("", pane.tag.own.?); - try testing.expect(std.mem.indexOf(u8, rd(p, tag, 0, 4096).bytes, " Mine") == null); - // The tag keeps its undo: undoing in it brings back what was cleared. - pardes.edit.doUndo(p, &pane.tag); - try testing.expect(std.mem.endsWith(u8, tagline.curTail(pane), " Mine")); - _ = call(p, .{ .tag = 2, .op = .setattr, .node = tag, .truncate = true }); - _ = rd(p, tag, 0, 4096); - try testing.expectEqualStrings("", pane.tag.own.?); - - // Written back without a leading space, the text still stands apart - // from the path: the computed prefix brings the separator, so Look and - // Exec find `Get`, not `test.txtGet`. - _ = wr(p, tag, "Get"); - try testing.expectEqualStrings("Get", pane.tag.own.?); - const whole = rd(p, tag, 0, 4096).bytes; - try testing.expectEqualStrings("/test.txt Get", whole); - const word = config.wordBounds(whole, whole.len - "Get".len); - try testing.expectEqualStrings("Get", whole[word.lo..word.hi]); - // and a click on the G puts the tag's cursor on it - const at = tagline.tagOffsetAt(p, pane, 0, "/test.txt ".len).?; - try testing.expectEqualStrings("Get", whole[at.col..]); - - // `echo Make > tag`: the newline ending the truncating write's text is - // dropped, one only; a newline between its writes stays. - _ = call(p, .{ .tag = 3, .op = .release, .node = tag }); - _ = call(p, .{ .tag = 4, .op = .setattr, .node = tag, .truncate = true }); - _ = wr(p, tag, "Make\n"); - try testing.expectEqualStrings("Make", pane.tag.own.?); - _ = wr(p, tag, "More\n\n"); - try testing.expectEqualStrings("Make\nMore\n", pane.tag.own.?); - _ = call(p, .{ .tag = 5, .op = .release, .node = tag }); - // `>>` keeps what it is given. - _ = wr(p, tag, " Tail\n"); - try testing.expectEqualStrings("Make\nMore\n Tail\n", pane.tag.own.?); -} - -test "name reads the file name and writing it promotes a scratch without touching its body" { - const gpa = testing.allocator; - const p = try withFile(gpa, "opener\n"); - defer p.deinit(); - const serial = try th.newPane(p); - const pane = p.panes[p.paneBySerial(serial).?].?; - const name = Node.of(serial, .name); - const body = Node.of(serial, .body); - var scratch_name: [4200]u8 = undefined; - const scratch_line = try std.fmt.bufPrint(&scratch_name, "{s}\n", .{nameOf(p, pane)}); - try testing.expectEqualStrings(scratch_line, rd(p, name, 0, 4096).bytes); - try testing.expectEqual(@as(u64, scratch_line.len), call(p, .{ .tag = 1, .op = .getattr, .node = name }).reply.attr.size); - _ = wr(p, body, "scratch "); - _ = wr(p, body, "work\n"); - const undo_len = pane.file.?.history.undo_len; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "renamed.zig", .data = "existing target\n" }); - var directory_buf: [4096]u8 = undefined; - const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; - var path_buffer: [4096]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buffer, "{s}/renamed.zig", .{directory}); - var line: [4104]u8 = undefined; - const renamed = wr(p, name, try std.fmt.bufPrint(&line, "{s}\n", .{path})); - try testing.expectEqual(Status.ok, renamed.reply.status); - try testing.expectEqual(false, renamed.watch.?); - try testing.expectEqualStrings("scratch work\n", pane.file.?.content); - try testing.expectEqual(undo_len, pane.file.?.history.undo_len); - try testing.expect(pane.file.?.output == null and dirtyOf(pane)); - try testing.expect(pane.file.?.watch_after_save); - try testing.expectEqualStrings(path, std.mem.trimEnd(u8, rd(p, name, 0, 4096).bytes, "\n")); - const target = try fs.readFile(gpa, path); - defer gpa.free(target); - try testing.expectEqualStrings("existing target\n", target); - for ([_][]const u8{ "", "\n" }) |bad| - try testing.expectEqual(E.INVAL, wr(p, name, bad).errno()); - // One name, and what no file name holds: a second line, a NUL. - for ([_][2][]const u8{ - .{ "two\nlines\n", "a newline" }, - .{ "nul\x00.zig\n", "a NUL" }, - }) |c| { - const refused = wr(p, name, c[0]); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectStringStartsWith(refused.reply.ename, "bad character in file name: "); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, c[1]) != null); - } - // Any other byte a file name holds is taken, and reads back as written: - // blanks at its ends, a tab, a control byte, DEL, C1, bytes not UTF-8. - for ([_][]const u8{ " lead.zig ", "tab\t.zig", "c\x01.zig", "del\x7f.zig", "c1\xc2\x85.zig", "bad\xff.zig" }) |odd| { - try testing.expectEqual(Status.ok, wr(p, name, try std.fmt.bufPrint(&line, "{s}\n", .{odd})).reply.status); - const read_back = std.mem.trimEnd(u8, rd(p, name, 0, 4096).bytes, "\n"); - try testing.expect(std.mem.endsWith(u8, read_back, odd)); - var again: [4200]u8 = undefined; - @memcpy(again[0..read_back.len], read_back); - try testing.expectEqual(Status.ok, wr(p, name, again[0..read_back.len]).reply.status); - try testing.expectEqualStrings(again[0..read_back.len], pane.file.?.path); - } - // A name that is one line and UTF-8 is taken, é and all. - try testing.expectEqual(Status.ok, wr(p, name, "caf\xc3\xa9.zig\n").reply.status); - try testing.expect(std.mem.endsWith(u8, pane.file.?.path, "/caf\xc3\xa9.zig")); - try testing.expectEqual(Status.ok, wr(p, name, try std.fmt.bufPrint(&line, "{s}\n", .{path})).reply.status); - try testing.expectEqualStrings(path, pane.file.?.path); - try testing.expectEqual(Status.ok, wr(p, name, "two words.zig\n").reply.status); - const spaced = try std.fmt.bufPrint(&path_buffer, "{s}/two words.zig", .{directory}); - try testing.expectEqualStrings(spaced, pane.file.?.path); -} - test "name refreshes cached syntax for unchanged contents" { if (!pardes.syntax.enabled) return error.SkipZigTest; pardes.syntax.start(testing.allocator); @@ -1555,58 +1245,6 @@ test "name refreshes cached syntax for unchanged contents" { try testing.expectEqualStrings("fn check() void {}\n", pane.file.?.content); } -test "relative names use the file directory and can name a new target" { - const gpa = testing.allocator; - const p = try withFile(gpa, "retained body\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "note.txt", .data = "from pane directory\n" }); - var directory_buf: [4096]u8 = undefined; - const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; - const pane = p.panes[0].?; - const name = Node.of(pane.serial, .name); - const ctl_node = Node.of(pane.serial, .ctl); - var line: [4140]u8 = undefined; - try testing.expectEqual(Status.ok, wr(p, name, try std.fmt.bufPrint(&line, "{s}/old.txt\n", .{directory})).reply.status); - try testing.expectEqual(Status.ok, wr(p, name, "child/../note.txt\n").reply.status); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "get\n").reply.status); - try testing.expectEqualStrings("from pane directory\n", pane.file.?.content); - try testing.expectEqualStrings(directory, Pardes.paneDir(pane)); - try testing.expectEqual(Status.ok, wr(p, name, "./created.txt\n").reply.status); - var expected_buf: [4096]u8 = undefined; - const expected = try std.fmt.bufPrint(&expected_buf, "{s}/created.txt", .{directory}); - try testing.expectEqualStrings(expected, pane.file.?.path); - try testing.expect(!dirtyOf(pane)); // a name alone is no edit -} - -test "relative names follow a pane/new scratch's session directory and virtual directories" { - const p = try withFile(testing.allocator, "source body\n"); - defer p.deinit(); - const source = p.panes[0].?; - try testing.expectEqual(Status.ok, wr(p, Node.of(source.serial, .name), "/elsewhere/source.zig\n").reply.status); - // pane/new's scratch is the session's, not the active pane's. - const session = "/project/src"; - @memcpy(p.session_dir[0..session.len], session); - p.session_dir_len = session.len; - const serial = try th.newPane(p); - const scratch = p.panes[p.paneBySerial(serial).?].?; - const name = Node.of(serial, .name); - try testing.expectEqualStrings("/project/src", Pardes.paneDir(scratch)); - try testing.expectEqual(Status.ok, wr(p, name, "../out/./notes.txt\n").reply.status); - try testing.expectEqualStrings("/project/out/notes.txt", scratch.file.?.path); - try testing.expectEqualStrings("/project/out", Pardes.paneDir(scratch)); - var line: [256]u8 = undefined; - try testing.expectEqual(Status.ok, wr(p, name, try std.fmt.bufPrint(&line, "/virtual/pane/{d}/./body\n", .{source.serial})).reply.status); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "get\n").reply.status); - try testing.expectEqualStrings(source.file.?.content, scratch.file.?.content); - const unchanged = wr(p, name, "./body\n"); - try testing.expectEqual(Status.ok, unchanged.reply.status); - try testing.expect(unchanged.watch == null); - try testing.expect(!dirtyOf(scratch)); - try testing.expect(!scratch.file.?.watch_after_save); -} - test "owned cwd name promotion releases the former directory" { const p = try th.withTerm(testing.allocator); defer p.deinit(); @@ -1622,96 +1260,6 @@ test "owned cwd name promotion releases the former directory" { try testing.expectEqualStrings("/old/directory", Pardes.paneDir(pane)); } -test "errors append to one +Errors buffer per directory" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - - const live = for (p.panes) |slot| { - if (slot) |q| if (q.file) |f| if (f.output) |o| if (std.meta.activeTag(o.from) == .errors) break q; - } else null; - try testing.expect(live == null); - - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .errors), "boom\n").reply.status); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .errors), "again\n").reply.status); - - var found: usize = 0; - for (p.panes) |slot| { - const q = slot orelse continue; - const f = q.file orelse continue; - const o = f.output orelse continue; - if (std.meta.activeTag(o.from) != .errors) continue; - found += 1; - try testing.expectEqualStrings("boom\nagain\n", f.content); - try testing.expectEqualStrings("/+Errors", f.path); - } - try testing.expectEqual(@as(usize, 1), found); -} - -test "truncating data deletes only the addressed range, so > data replaces it" { - const p = try withFile(testing.allocator, "hello world\nsecond line\n"); - defer p.deinit(); - const serial = serialOf(p); - for ([_]PaneFile{ .data, .xdata }) |file| { - _ = call(p, .{ .tag = 1, .op = .setattr, .node = Node.of(serial, .body), .truncate = true }); - _ = wr(p, Node.of(serial, .body), "hello world\nsecond line\n"); - _ = wr(p, Node.of(serial, .addr), "#0,#5"); - try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .setattr, .node = Node.of(serial, file), .truncate = true }).reply.status); - try testing.expectEqualStrings(" world\nsecond line\n", p.panes[0].?.file.?.content); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, file), "HOWDY").reply.status); - try testing.expectEqualStrings("HOWDY world\nsecond line\n", p.panes[0].?.file.?.content); - } -} - -test "setattr truncation empties the body and answers fresh attributes" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - - const a = call(p, .{ .tag = 7, .op = .setattr, .node = Node.of(serial, .body), .truncate = true }); - try testing.expectEqual(Status.ok, a.reply.status); - try testing.expectEqual(@as(u64, 0), a.reply.attr.size); - try testing.expectEqualStrings("", p.panes[0].?.file.?.content); - - _ = wr(p, Node.of(serial, .body), "new text\n"); - try testing.expectEqualStrings("new text\n", p.panes[0].?.file.?.content); - - const noop = call(p, .{ .tag = 8, .op = .setattr, .node = Node.of(serial, .body) }); - try testing.expectEqual(@as(u64, 9), noop.reply.attr.size); -} - -test "addresses are in runes: a combining mark and a lone \\r are places of their own" { - const gpa = testing.allocator; - // e, U+0301 (two bytes, 1-2), x, \r, \n - const p = try withFile(gpa, "e\u{0301}x\r\n"); - defer p.deinit(); - const serial = serialOf(p); - const addr_node = Node.of(serial, .addr); - const dot = Node.of(serial, .dot); - const Case = struct { expr: []const u8, q0: u32, q1: u32 }; - for ([_]Case{ - .{ .expr = "#1,#3", .q0 = 1, .q1 = 3 }, // the mark alone - .{ .expr = "#2", .q0 = 1, .q1 = 1 }, // inside it: back to its start - .{ .expr = "#4,#5", .q0 = 4, .q1 = 5 }, // the \r alone - .{ .expr = "1:2", .q0 = 1, .q1 = 1 }, // line:col the same - .{ .expr = "1:3", .q0 = 1, .q1 = 1 }, - .{ .expr = "0/\xCC/", .q0 = 1, .q1 = 3 }, // a match inside a rune covers it - }) |c| { - try testing.expectEqual(Status.ok, wr(p, addr_node, c.expr).reply.status); - try testing.expectEqual(c.q0, p.panes[0].?.fs.addr.q0); - try testing.expectEqual(c.q1, p.panes[0].?.fs.addr.q1); - } - // Copied to dot, it reads back the same. - _ = wr(p, addr_node, "#1,#3"); - try testing.expectEqual(Status.ok, wr(p, dot, " 1 3 ").reply.status); - p.sync(); - try testing.expectEqualStrings(" 1 3 ", rd(p, dot, 0, 100).bytes); - _ = wr(p, addr_node, "#4,#5"); - try testing.expectEqualStrings("\r", rd(p, Node.of(serial, .xdata), 0, 100).bytes); -} - test "a limit write that fails keeps the limit a truncation cleared before it" { const gpa = testing.allocator; const p = try withFile(gpa, "one two\n"); @@ -1727,17 +1275,6 @@ test "a limit write that fails keeps the limit a truncation cleared before it" { try testing.expectEqualStrings("", rd(p, limit, 0, 100).bytes); } -test "a pane's tagexec runs a word as a click in its tag, and reads back what it touched" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .tagexec), "Msg from the tag\n").reply.status); - try testing.expect(th.logHas(p, "from the tag")); - var want: [16]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "{d}\n", .{serial}), rd(p, Node.of(serial, .tagexec), 0, 64).bytes); -} - test "index names a command's pane cmd, a shell's term" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); -- cgit v1.3