diff options
Diffstat (limited to 'src/ninep')
| -rw-r--r-- | src/ninep/ctl.zig | 1483 | ||||
| -rw-r--r-- | src/ninep/pane.zig | 522 |
2 files changed, 43 insertions, 1962 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index bd460098..89549933 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -1465,386 +1465,41 @@ test "commands lists every builtin with its argument and the ctl that takes it" try testing.expect(std.mem.indexOf(u8, listed.bytes, row) != null); } -test "Undo and Redo on a pane's ctl step its body through its edits" { - const p = try withFile(testing.allocator, "one\n"); - defer p.deinit(); - const serial = serialOf(p); - const body = Node.of(serial, .body); - try testing.expectEqual(Status.ok, wr(p, body, "two\n").reply.status); - try testing.expectEqualStrings("one\ntwo\n", rd(p, body, 0, 64).bytes); - 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); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Redo").reply.status); - try testing.expectEqualStrings("one\ntwo\n", rd(p, body, 0, 64).bytes); - // Nothing left: said, and the write still succeeds, as acme's is silent. - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Redo").reply.status); - try testing.expect(th.logHas(p, "Redo: nothing to redo")); - _ = wr(p, Node.of(serial, .ctl), "Undo"); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Undo").reply.status); - try testing.expect(th.logHas(p, "Undo: nothing to undo")); - // Said again word for word, it is that line counted, as err's are. - for (0..3) |_| _ = wr(p, Node.of(serial, .ctl), "Undo"); - try testing.expect(th.logHas(p, "Undo: nothing to undo (x4)\n")); -} - -test "Edit through a pane's ctl renames every foo in one undo step, and a failed one changes nothing" { - const p = try withFile(testing.allocator, "foo x foo y foo\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - const body = Node.of(serial, .body); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "Edit ,x/foo/c/foobar/\n").reply.status); - try testing.expectEqualStrings("foobar x foobar y foobar\n", rd(p, body, 0, 64).bytes); - const failed = wr(p, ctl_node, "Edit ,s/nothing/x/\n"); - try testing.expectEqual(E.IO, failed.errno()); - try testing.expectStringStartsWith(failed.reply.ename, "Edit: no substitution"); - try testing.expectEqualStrings("foobar x foobar y foobar\n", rd(p, body, 0, 64).bytes); - // The whole rename is one undo step. - try testing.expectEqual(Status.ok, wr(p, ctl_node, "Undo").reply.status); - try testing.expectEqualStrings("foo x foo y foo\n", rd(p, body, 0, 64).bytes); -} - -test "dirty goes back to 0 when the text is the saved text again" { - const p = try withFile(testing.allocator, "one\n"); - defer p.deinit(); - const serial = serialOf(p); - const dirty = Node.of(serial, .dirty); - try testing.expectEqualStrings("0\n", rd(p, dirty, 0, 8).bytes); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .body), "two\n").reply.status); - try testing.expectEqualStrings("1\n", rd(p, dirty, 0, 8).bytes); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Undo").reply.status); - try testing.expectEqualStrings("0\n", rd(p, dirty, 0, 8).bytes); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Redo").reply.status); - try testing.expectEqualStrings("1\n", rd(p, dirty, 0, 8).bytes); -} - -test "a look's miss repeats the path whole up to 256 bytes, then gives up its middle to an ellipsis" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const root = @backingInt(tree.TopFile.look); - const long = "some/deeply/nested/directory/that/goes/on/and/on/with/a_rather_long_file_name.txt"; - var line: [512]u8 = undefined; - _ = wr(p, root, try std.fmt.bufPrint(&line, "{s}\n", .{long})); - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&line, "look: no match for \"{s}\"\n", .{long}))); - const huge = std.mem.asBytes(&@as([150][2]u8, @splat("é".*))); // 300 bytes: its middle cut to fit 256, between characters - _ = wr(p, root, huge ++ "\n"); - try testing.expect(th.logHas(p, "look: no match for \"" ++ std.mem.asBytes(&@as([63][2]u8, @splat("é".*))) ++ "…" ++ std.mem.asBytes(&@as([63][2]u8, @splat("é".*))) ++ "\"\n")); -} - -test "status reads the editor, and look and exec validate whole batches" { - const gpa = testing.allocator; - const p = try withFile(gpa, "Msg root\n"); +test "status in a window says whether the pointer is over it" { + // The rest of status is test/behaviour/look_and_exec.zig's: a session + // there is a terminal's view. + const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); - // A terminal's view first (a window's build starts windowed). - p.windowed = false; - const status = rd(p, root_status, 0, 4096); - try testing.expectEqual(Status.ok, status.reply.status); - var lines = std.mem.tokenizeScalar(u8, status.bytes, '\n'); - try testing.expect(std.mem.startsWith(u8, lines.next().?, "pid ")); - var expected: [64]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "version {s}", .{pardes.version}), lines.next().?); - var live: usize = 0; - for (p.panes) |slot| live += @intFromBool(slot != null); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "panes {d}", .{live}), lines.next().?); - try testing.expect(lines.next() == null); - // A window says whether the pointer is over it. p.windowed = true; - var again = std.mem.tokenizeScalar(u8, rd(p, root_status, 0, 4096).bytes, '\n'); - for (0..3) |_| _ = again.next(); - try testing.expectEqualStrings("pointer out", again.next().?); + var lines = std.mem.tokenizeScalar(u8, rd(p, root_status, 0, 4096).bytes, '\n'); + for (0..3) |_| _ = lines.next(); + try testing.expectEqualStrings("pointer out", lines.next().?); p.windowed = false; - const stat = call(p, .{ .tag = 1, .op = .getattr, .node = root_status }); - try testing.expectEqual(@as(u64, status.bytes.len), stat.reply.attr.size); - try testing.expectEqual(@as(u16, 0o444), stat.reply.attr.mode); - try testing.expectEqual(E.PERM, wr(p, root_status, "anything\n").errno()); - try testing.expectEqualStrings("", rd(p, root_exec, 0, 4096).bytes); - - const serial = p.next_serial; - for ([_][]const u8{ "Msg a\x00b", "Msg x\nMsg \x01y\n" }) |invalid| { - try testing.expectEqual(E.INVAL, wr(p, root_exec, invalid).errno()); - try testing.expectEqual(serial, p.next_serial); - } - // An empty write is a no-op, not a command. - try testing.expectEqual(Status.ok, wr(p, root_exec, "\n \n").reply.status); - try testing.expectEqualStrings("", rd(p, root_exec, 0, 4096).bytes); - try testing.expectEqualStrings(status.bytes, rd(p, root_status, 0, 4096).bytes); - - const pane = p.panes[0].?; - try testing.expectEqual(Status.ok, wr(p, root_exec, "Msg hello there\n").reply.status); - try testing.expectEqualStrings("hello there", pane.msg[0..pane.msg_len]); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{pane.serial}), rd(p, root_exec, 0, 4096).bytes); - - try testing.expectEqual(Status.ok, wr(p, root_look, "/virtual/index\n").reply.status); - const opened = p.panes[p.active].?; - try testing.expectEqualStrings("/virtual/index", opened.file.?.path); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{opened.serial}), rd(p, root_look, 0, 4096).bytes); - - try testing.expectEqual(Status.ok, wr(p, root_exec, "New\n").reply.status); - const made = p.panes[p.active].?; - try testing.expect(made.serial != opened.serial); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{made.serial}), rd(p, root_exec, 0, 4096).bytes); -} - -test "look opens spaced paths and locations without editing the source pane" { - if (!fs.platform_has_fs) return error.SkipZigTest; - const gpa = testing.allocator; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "two words.zig", .data = "first\nsecond\nthird\n" }); - var path_buf: [4096]u8 = undefined; - const path = path_buf[0..try tmp.dir.realPathFile(testing.io, "two words.zig", &path_buf)]; - const p = try withFile(gpa, "source stays intact\n"); - defer p.deinit(); - const source = p.panes[0].?; - const look_node = Node.of(source.serial, .look); - const revision = source.file.?.revision; - const undo_len = source.file.?.history.undo_len; - source.body.cur_col = 3; - var clicked: [4200]u8 = undefined; - try testing.expectEqual(Status.ok, wr(p, look_node, try std.fmt.bufPrint(&clicked, "{s}:2:3\n", .{path})).reply.status); - const opened = p.panes[p.active].?; - try testing.expect(opened != source); - try testing.expectEqualStrings(path, opened.file.?.path); - try testing.expectEqualStrings("first\nsecond\nthird\n", opened.file.?.content); - try testing.expectEqual(@as(i32, 1), opened.body.cur_row); - try testing.expectEqual(@as(i32, 2), opened.body.cur_col); - try testing.expectEqualStrings("source stays intact\n", source.file.?.content); - try testing.expectEqual(revision, source.file.?.revision); - try testing.expectEqual(undo_len, source.file.?.history.undo_len); - try testing.expectEqual(@as(i32, 3), source.body.cur_col); - var expected: [16]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{opened.serial}), rd(p, root_look, 0, 4096).bytes); - - p.active = 0; - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&clicked, "{s}:3:2\n", .{path})).reply.status); - try testing.expect(p.panes[p.active].? == opened); - try testing.expectEqual(@as(i32, 2), opened.body.cur_row); - try testing.expectEqual(@as(i32, 1), opened.body.cur_col); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{opened.serial}), rd(p, root_look, 0, 4096).bytes); -} - -test "look validates the whole batch before opening virtual files" { - const gpa = testing.allocator; - const p = try withFile(gpa, "source\n"); - defer p.deinit(); - const source = p.panes[0].?; - const look_node = Node.of(source.serial, .look); - const serial = p.next_serial; - for ([_][]const u8{ - "/virtual/index\x00ignored", "/virtual/index\n/virtual/\x01index\n", - }) |invalid| { - try testing.expectEqual(E.INVAL, wr(p, look_node, invalid).errno()); - try testing.expectEqual(serial, p.next_serial); - try testing.expectEqual(@as(usize, 0), p.active); - try testing.expectEqualStrings("source\n", source.file.?.content); - } - try testing.expectEqual(Status.ok, wr(p, look_node, "/n/self/index\n").reply.status); - const opened = p.panes[p.active].?; - try testing.expectEqualStrings("/virtual/index", opened.file.?.path); - try testing.expect(std.mem.indexOf(u8, opened.file.?.content, "/test.txt") != null); - try testing.expectEqualStrings("source\n", source.file.?.content); -} - -test "get reloads the pane from disk and reports a missing file without losing dirty contents" { - const gpa = testing.allocator; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); - var directory_buf: [4096]u8 = undefined; - const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; - var path_buf: [4096]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/note.txt", .{directory}); - - const p = try withFile(gpa, "in memory\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - const pane = p.panes[0].?; - - var name: [std.fs.max_path_bytes + 8]u8 = undefined; - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}\n", .{path})); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "get\n").reply.status); - try testing.expectEqualStrings("from disk\n", pane.file.?.content); - try testing.expect(!pane_files.dirtyOf(pane)); - try testing.expect(pane.file.?.history.undo_len > 0); - // Renamed but never edited, it was clean: nothing thrown away, nothing - // logged as reloaded over edits. - var reloaded: [32]u8 = undefined; - try testing.expect(!th.logHas(p, try std.fmt.bufPrint(&reloaded, "changed {d} reloaded\n", .{serial}))); - - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}/missing.txt\n", .{directory})); - _ = wr(p, Node.of(serial, .body), "unsaved\n"); - // Unsaved text is asked about once, as acme's get asks winclean. - const asked = wr(p, ctl_node, "get\n"); - try testing.expectEqual(E.IO, asked.errno()); - try testing.expect(std.mem.endsWith(u8, asked.reply.ename, ": Modified (get again to discard)")); - var unsaved: [4200]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&unsaved, "unsaved {d} {s}/missing.txt\n", .{ serial, directory }))); - try testing.expectEqual(E.NOENT, wr(p, ctl_node, "get\n").errno()); - try testing.expect(p.paneBySerial(serial) != null); - try testing.expectEqualStrings("from disk\nunsaved\n", pane.file.?.content); - try testing.expect(pane_files.dirtyOf(pane)); -} - -test "Get reads the pane's file again, refusing unsaved edits once, and Get file loads that file into the pane" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "other.txt", .data = "the other\n" }); - var directory_buf: [4096]u8 = undefined; - const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; - const p = try withFile(testing.allocator, "in memory\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var name: [std.fs.max_path_bytes + 8]u8 = undefined; - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}/note.txt\n", .{directory})); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqualStrings("from disk\n", pane.file.?.content); - try testing.expect(!pane_files.dirtyOf(pane)); - // Edited: asked once, then read again. - _ = wr(p, Node.of(serial, .body), "unsaved\n"); - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expect(p.fs.failures != failures); - try testing.expectEqualStrings("from disk\nunsaved\n", pane.file.?.content); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqualStrings("from disk\n", pane.file.?.content); - // Get file: that file, by a name from the pane's directory. - try testing.expect(p.executeBuiltinLine(0, "Get other.txt")); - try testing.expectEqualStrings("the other\n", pane.file.?.content); - try testing.expect(std.mem.endsWith(u8, pane.file.?.path, "/other.txt")); - try testing.expect(!pane_files.dirtyOf(pane)); } -test "Zerox makes a second pane on one buffer: one text, undo and dirty state, its own cursor; Save cleans both, Del of one asks nothing" { +test "Zerox twins: a Save from one, performed, cleans both" { + // The rest of Zerox is test/behaviour/panes.zig's; this needs the save done. var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); var dir_buf: [4096]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - const p = try withFile(testing.allocator, "one\ntwo\nthree\n"); + const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); - const first = serialOf(p); var name: [std.fs.max_path_bytes + 8]u8 = undefined; - _ = wr(p, Node.of(first, .name), try std.fmt.bufPrint(&name, "{s}/z.txt\n", .{dir})); + _ = wr(p, Node.of(serialOf(p), .name), try std.fmt.bufPrint(&name, "{s}/z.txt\n", .{dir})); try testing.expect(p.executeBuiltinLine(0, "Zerox")); const twin_id = for (p.panes, 0..) |slot, id| { if (id != 0) if (slot) |pane| if (pane.file != null) break id; } else return error.NoTwin; const a = p.panes[0].?; const b = p.panes[twin_id].?; - // Both in the index, each its serial, one name. - const index = rd(p, @backingInt(tree.TopFile.index), 0, 4096).bytes; - try testing.expectEqual(@as(usize, 2), std.mem.count(u8, index, "/z.txt")); - // b's cursor on `three`; a line written above it in a moves it down. - b.body.cur_row = 2; - b.body.cur_col = 1; - _ = wr(p, Node.of(first, .addr), "0"); - try testing.expectEqual(Status.ok, wr(p, Node.of(first, .data), "zero\n").reply.status); - try testing.expectEqualStrings("zero\none\ntwo\nthree\n", b.file.?.content); - try testing.expectEqual(a.file.?.content.ptr, b.file.?.content.ptr); - try testing.expectEqual(@as(i32, 3), b.body.cur_row); - try testing.expectEqual(@as(i32, 1), b.body.cur_col); - try testing.expect(pane_files.dirtyOf(a) and pane_files.dirtyOf(b)); - // A write through b is a's too; Undo in a takes it back from both. - try testing.expectEqual(Status.ok, wr(p, Node.of(b.serial, .body), "four\n").reply.status); - try testing.expectEqualStrings("zero\none\ntwo\nthree\nfour\n", a.file.?.content); - try testing.expect(p.executeBuiltinLine(0, "Undo")); - try testing.expectEqualStrings("zero\none\ntwo\nthree\n", b.file.?.content); - // Saved from b: both come clean. + try testing.expectEqual(Status.ok, wr(p, Node.of(b.serial, .body), "two\n").reply.status); try testing.expect(p.executeBuiltinLine(twin_id, "Save")); while (p.nextEffect()) |e| p.perform(e); try testing.expect(!pane_files.dirtyOf(a) and !pane_files.dirtyOf(b)); - try testing.expectEqualStrings("zero\none\ntwo\nthree\n", p.fallback.get(b.file.?.path).?); - // Edited again, Del of a asks nothing: b holds the text. - try testing.expectEqual(Status.ok, wr(p, Node.of(b.serial, .body), "five\n").reply.status); - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(0, "Del")); - p.sync(); - try testing.expectEqual(failures, p.fs.failures); - try testing.expect(p.paneBySerial(first) == null); - try testing.expect(pane_files.dirtyOf(b)); - try testing.expectEqualStrings("zero\none\ntwo\nthree\nfive\n", b.file.?.content); - // Alone now, it is no twin; Del asks about it as about any pane. - try testing.expectEqual(@as(u32, 0), b.file.?.twin); - try testing.expect(p.executeBuiltinLine(p.paneBySerial(b.serial).?, "Del")); - try testing.expect(p.fs.failures != failures); -} - -test "Undo after a load under another name (Get file, Edit e) puts the old name back with the old text and its clean state; Putall then writes nothing over the loaded file" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "other.txt", .data = "other\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "keep.txt", .data = "keep\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var other_buf: [4200]u8 = undefined; - const other = try std.fmt.bufPrint(&other_buf, "{s}/other.txt", .{dir}); - var keep_buf: [4200]u8 = undefined; - const keep = try std.fmt.bufPrint(&keep_buf, "{s}/keep.txt", .{dir}); - for ([_][]const u8{ "Get keep.txt", "Edit e keep.txt" }) |load| { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var name: [4300]u8 = undefined; - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}\n", .{other})); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqualStrings("other\n", pane.file.?.content); - try testing.expect(!pane_files.dirtyOf(pane)); - // Loaded: keep.txt's text under its name. - try testing.expect(p.executeBuiltinLine(0, load)); - try testing.expectEqualStrings("keep\n", pane.file.?.content); - try testing.expectEqualStrings(keep, pane.file.?.path); - if (std.mem.startsWith(u8, load, "Get")) try testing.expect(!pane_files.dirtyOf(pane)); - // Undo: other.txt's text, other.txt's name, clean as it was. - try testing.expect(p.executeBuiltinLine(0, "Undo")); - try testing.expectEqualStrings("other\n", pane.file.?.content); - try testing.expectEqualStrings(other, pane.file.?.path); - try testing.expect(!pane_files.dirtyOf(pane)); - // Putall saves nothing, and nothing lands on keep.txt. - while (p.nextEffect()) |_| {} - try testing.expect(p.executeBuiltinLine(0, "Putall")); - while (p.nextEffect()) |e| try testing.expect(e != .save_file); - // Redo: keep.txt again. - try testing.expect(p.executeBuiltinLine(0, "Redo")); - try testing.expectEqualStrings(keep, pane.file.?.path); - try testing.expectEqualStrings("keep\n", pane.file.?.content); - } -} - -test "a Get file that fails changes nothing: no such file, a directory, or unsaved edits asked about once" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "here.txt", .data = "here\n" }); - try tmp.dir.createDirPath(testing.io, "sub"); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var name: [4300]u8 = undefined; - const here = try std.fmt.bufPrint(&name, "{s}/here.txt\n", .{dir}); - _ = wr(p, Node.of(serial, .name), here); - try testing.expect(p.executeBuiltinLine(0, "Get")); - const undo_len = pane.file.?.history.undo_len; - for ([_][]const u8{ "Get nosuch.txt", "Get sub" }) |line| { - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(0, line)); - try testing.expect(p.fs.failures != failures); - try testing.expectEqualStrings(here[0 .. here.len - 1], pane.file.?.path); - try testing.expectEqualStrings("here\n", pane.file.?.content); - try testing.expectEqual(undo_len, pane.file.?.history.undo_len); - } - // Edited, the first Get asks and renames nothing. - _ = wr(p, Node.of(serial, .body), "more\n"); - try testing.expect(p.executeBuiltinLine(0, "Get other.txt")); - try testing.expectEqualStrings(here[0 .. here.len - 1], pane.file.?.path); - try testing.expectEqualStrings("here\nmore\n", pane.file.?.content); + try testing.expectEqualStrings("one\ntwo\n", p.fallback.get(b.file.?.path).?); } test "Zerox twins dump their text once and come back on one buffer from a Restore" { @@ -1876,36 +1531,6 @@ test "Zerox twins dump their text once and come back on one buffer from a Restor try testing.expect(std.mem.startsWith(u8, files[0].content, "one\ntwo\nunsaved ")); } -test "a bad Tab width and a bad Incl directory are refused invalid, changing nothing; a twin's rename is each twin's" { - defer config.tab_now = config.tab_width; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - for ([_][]const u8{ "Tab 0\n", "Tab 17\n", "Tab x\n" }) |line| { - try testing.expectEqual(E.INVAL, wr(p, root_ctl, line).errno()); - // Run from a pane, as a tag click runs it: said invalid too. - const r = wr(p, Node.of(serialOf(p), .exec), line); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "invalid tab width") != null); - } - try testing.expectEqual(config.tab_width, config.tab_now); - const before = p.incl.items.len; - try testing.expectEqual(E.INVAL, wr(p, root_ctl, "Incl rel/dir\n").errno()); - try testing.expectEqual(E.INVAL, wr(p, root_ctl, "Incl - /usr/include\n").errno()); - if (comptime pardes.hosted) try testing.expectEqual(E.NOENT, wr(p, root_ctl, "Incl /tmp/pardes-surely-no-such-dir\n").errno()); - try testing.expectEqual(before, p.incl.items.len); - // A twin renamed: a rename record for each. - const first = serialOf(p); - try testing.expect(p.executeBuiltinLine(0, "Zerox")); - const twin = for (p.panes) |slot| { - if (slot) |pane| if (pane.serial != first and pane.file != null) break pane.serial; - } else return error.NoTwin; - _ = wr(p, Node.of(first, .name), "/tmp/pardes-twin-renamed.txt\n"); - var rec: [64]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "rename {d} /tmp/pardes-twin-renamed.txt\n", .{first}))); - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "rename {d} /tmp/pardes-twin-renamed.txt\n", .{twin}))); -} - test "Undo to unsaved text under its old name says nothing changed on disk when nothing did, and Save then writes" { var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); @@ -1939,73 +1564,10 @@ test "Undo to unsaved text under its old name says nothing changed on disk when } } -test "round 39's small refusals: one name an open, small scratches unasked, twins saved once, bare Tab on ctl, and errnos in words a mount reads" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "f\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "noread.txt", .data = "secret\n" }); - try tmp.dir.createDirPath(testing.io, "sub"); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var line: [4300]u8 = undefined; - // One name an open: a second line, in the write or after, is refused. - try testing.expectEqual(E.INVAL, wr(p, Node.of(serial, .name), "/tmp/pardes-a\nb").errno()); - const before = try testing.allocator.dupe(u8, pane.file.?.path); - defer testing.allocator.free(before); - const h = call(p, .{ .tag = 1, .op = .open, .node = Node.of(serial, .name), .omode = 1 }).reply.handle; - try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = Node.of(serial, .name), .handle = h, .data = "/tmp/pardes-one.txt\n" }).reply.status); - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 3, .op = .write, .node = Node.of(serial, .name), .handle = h, .data = "b" }).errno()); - _ = call(p, .{ .tag = 4, .op = .release, .node = Node.of(serial, .name), .handle = h, .opened = true }); - tree.runClosedLines(p); - try testing.expectEqualStrings("/tmp/pardes-one.txt", pane.file.?.path); - // Bare Tab on the root ctl says the width, as from a pane. - try testing.expectEqual(Status.ok, wr(p, @backingInt(tree.TopFile.ctl), "Tab\n").reply.status); - // Errnos a mount reads from the words. - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&line, "{s}/f.txt\n", .{dir})); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqual(E.ISDIR, wr(p, Node.of(serial, .exec), "Get sub\n").errno()); - if (comptime pardes.hosted) { - var noread_buf: [4300]u8 = undefined; - _ = std.c.chmod(try std.fmt.bufPrintSentinel(&noread_buf, "{s}/noread.txt", .{dir}, 0), 0); - try testing.expectEqual(@as(u16, 13), wr(p, Node.of(serial, .exec), "Get noread.txt\n").errno()); - try testing.expectEqual(E.NOTDIR, wr(p, Node.of(serial, .exec), try std.fmt.bufPrint(&line, "Incl {s}/f.txt\n", .{dir})).errno()); - const zero = wr(p, Node.of(serial, .look), "/dev/zero\n"); - _ = zero; - try testing.expect(th.logHas(p, "not a regular file")); - } - // Twins saved once by Putall. - _ = wr(p, Node.of(serial, .body), "edited\n"); - try testing.expect(p.executeBuiltinLine(0, "Zerox")); - while (p.nextEffect()) |_| {} - try testing.expect(p.executeBuiltinLine(0, "Putall")); - var saves: usize = 0; - while (p.nextEffect()) |e| if (e == .save_file) { - saves += 1; - }; - try testing.expectEqual(@as(usize, 1), saves); - // A scratch of 2 bytes is loaded over unasked. - try testing.expect(p.executeBuiltinLine(0, "New")); - const scratch = p.panes[p.active].?; - _ = pane_files.spliceBody(p, scratch, 0, 0, "hi") orelse return error.NoScratch; - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(p.active, try std.fmt.bufPrint(&line, "Get {s}/f.txt", .{dir}))); - try testing.expectEqual(failures, p.fs.failures); - try testing.expectEqualStrings("f\n", scratch.file.?.content); -} - -test "Get of a file not there says so without asking about unsaved edits; the init file saved says a bad line as startup does" { +test "the init file saved says a bad line as startup does" { + // Get of a file not there is test/behaviour/panes.zig's. const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .startup_config = "Theme dusk\n", .startup_config_path = "/tmp/pardes-init-test/init" }); defer p.deinit(); - const pane = try p.setTestFile("x\n"); - _ = wr(p, Node.of(pane.serial, .body), "unsaved\n"); - try testing.expect(p.executeBuiltinLine(0, "Get /tmp/pardes-surely-no-such-file.txt")); - try testing.expect(th.logHas(p, "no such file")); - try testing.expect(!th.logHas(p, "again to discard")); - try testing.expect(pane.discard_warned == null); pardes.exec.reapplyInitFile(p, "/tmp/pardes-init-test/init", "Theme acme\nnosuchword 1\nNewcol\n"); try testing.expect(th.logHas(p, "err - init file line 2: unknown word \"nosuchword 1\"")); try testing.expect(!th.logHas(p, "init file line 3")); @@ -2131,66 +1693,6 @@ test "a refusal's +Unsaved goes in the session's directory when the active pane' try testing.expectEqualStrings(p.sessionDir(), std.fs.path.dirname(unsaved).?); } -test "a look at a file it may not read fails the write naming the file" { - if (comptime !pardes.hosted) return; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "locked.txt", .data = "secret\n", .flags = .{ .permissions = .fromMode(0o000) } }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&line, "{s}/locked.txt", .{dir}); - var want: [4200]u8 = undefined; - const expected = try std.fmt.bufPrint(&want, "look: {s}: permission denied", .{path}); - var with_newline: [4300]u8 = undefined; - const refused = wr(p, root_look, try std.fmt.bufPrint(&with_newline, "{s}\n", .{path})); - if (refused.reply.status == .ok) return error.SkipZigTest; // root reads anything - try testing.expectEqualStrings(expected, refused.reply.ename); -} - -test "a bad setting value is quoted, so two different bad values log as two records, never one counted twice" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - _ = wr(p, root_ctl, "Verbose maybe\n"); - _ = wr(p, root_ctl, "Verbose nope\n"); - try testing.expect(th.logHas(p, "takes on, off \"maybe\"\n")); - try testing.expect(th.logHas(p, "takes on, off \"nope\"\n")); - try testing.expect(!th.logHas(p, "(x2)")); -} - -test "a look line's trailing colon is dropped, and a look whose address fails never leaves its file open" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - const count = struct { - fn of(pp: *Pardes) usize { - var n: usize = 0; - for (pp.panes) |slot| n += @intFromBool(slot != null); - return n; - } - }.of; - const before = count(p); - // Failing addresses: nothing opened, the write still answered. - inline for ([_][]const u8{ "{s}/a.txt:/nomatch/\n", "{s}/a.txt:9:\n" }) |fmt| { - _ = wr(p, root_look, try std.fmt.bufPrint(&line, fmt, .{dir})); - try testing.expectEqual(before, count(p)); - } - // `a.txt:2:3:` is `a.txt:2:3`: it opens there. - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt:2:3:\n", .{dir})).reply.status); - try testing.expectEqual(before + 1, count(p)); - const opened = p.panes[p.active].?; - try testing.expect(std.mem.endsWith(u8, opened.file.?.path, "/a.txt")); - try testing.expectEqual(@as(i32, 1), opened.body.cur_row); -} - test "a builtin's Verbose announcement is logged only when it succeeds, its failure said now or by the host later" { const gpa = testing.allocator; @@ -2366,32 +1868,10 @@ test "after a ThemeFile the root ctl and DumpConfig say ThemeFile and its path, try testing.expectEqual(Status.ok, wr(p, root_ctl, "ThemeFile " ++ path ++ "\n").reply.status); } -test "Joincol with no column to the right and Theme with no such theme say so" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - const joined = wr(p, root_ctl, "Joincol\n"); - try testing.expectEqual(tree.Status.err, joined.reply.status); - try testing.expectStringStartsWith(joined.reply.ename, "Joincol: no column to the right"); - const themed = wr(p, root_ctl, "Theme drak\n"); - try testing.expectEqual(E.INVAL, themed.errno()); - try testing.expectStringStartsWith(themed.reply.ename, "bad value in control message; like it: "); - try testing.expect(std.mem.indexOf(u8, themed.reply.ename, ": dr") != null); - try testing.expect(std.mem.endsWith(u8, themed.reply.ename, "\"drak\"")); - // A click on the word says it on the message row. - _ = wr(p, Node.of(serialOf(p), .exec), "Theme drak\n"); - const pane = p.panes[p.active].?; - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "no theme \"drak\"") != null); - // Within the 128 bytes an Rerror carries, the quoted line kept. - try testing.expect(themed.reply.ename.len <= 128); - // A name in any case is the theme. - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Theme DRACULA\n").reply.status); - try testing.expectEqualStrings("dracula", pardes.themes[p.settings.theme].name); - // A misspelling far from any first letter's list still finds its like. +test "themesNear finds a misspelling's like far from any first letter, and every theme sharing the first letter when they fit" { + // Joincol and Theme's refusals are test/behaviour/root_ctl.zig's. var near: [170]u8 = undefined; try testing.expect(std.mem.indexOf(u8, pardes.colors.themesNear(&near, "9dracula"), "dracula") != null); - // Every theme sharing the first letter, when they fit. var tested = false; for ("abcdefghijklmnopqrstuvwxyz") |letter| { var count: usize = 0; @@ -2408,19 +1888,6 @@ test "Joincol with no column to the right and Theme with no such theme say so" { } -test "a look at file:addr whose file is not there is one miss, quoting what was written, in a terminal too" { - const p = try th.withTerm(testing.allocator); - defer p.deinit(); - const looked = Node.of(serialOf(p), .look); - for ([_][]const u8{ "zzq:#3", "zzq:2", "zzq:/re/" }) |written| { - var line: [64]u8 = undefined; - _ = wr(p, looked, try std.fmt.bufPrint(&line, "{s}\n", .{written})); - var want: [64]u8 = undefined; - try testing.expectEqual(@as(usize, 1), th.logCount(p, try std.fmt.bufPrint(&want, "look: no match for \"{s}\"\n", .{written}))); - } - try testing.expectEqual(@as(usize, 3), th.logCount(p, "\nerr ")); -} - test "a look whose pane is refused a place fails with that alone, not a missing line too" { const gpa = testing.allocator; // A screen with rows for one pane only. @@ -2441,26 +1908,6 @@ test "a look whose pane is refused a place fails with that alone, not a missing try testing.expectEqual(@as(usize, 1), th.logCount(p, "\nerr ")); } -test "a look under pardes's placement, from a pane pane/new made after Delcol, opens its file" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "file\n" }); - var dir: [4096]u8 = undefined; - const at = dir[0..try tmp.dir.realPath(testing.io, &dir)]; - var line: [4200]u8 = undefined; - const first = serialOf(p); - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(first, .exec), "Placement\n").reply.status); - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(first, .exec), "Delcol\n").reply.status); - const made = try th.newPane(p); - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(made, .look), try std.fmt.bufPrint(&line, "{s}/f.txt:#4\n", .{at})).reply.status); - // The pane pane/new made is placed, and so is the one its look opened. - for (p.panes, 0..) |slot, i| if (slot != null) try testing.expect(layout.findPane(p, i) != null); - try testing.expect(p.panes[p.active].?.serial != made); -} - test "a Restore of a file that is no dump fails the write before any warning, and a failed Dump fails its write" { if (comptime !pardes.hosted) return error.SkipZigTest; const p = try withFile(testing.allocator, "x\n"); @@ -2493,58 +1940,6 @@ test "a Restore of a file that is no dump fails the write before any warning, an try testing.expectEqualStrings("Dump /dev/null/pardes.dump.zon: /dev/null is not a directory", p.fs.late_failure[0..p.fs.late_failure_len]); } -test "a setting this frontend cannot show says GUI-only, and DumpDir reads back the directory in effect" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - if (!builtins.viewCapabilities(p).lift) { - try testing.expectEqualStrings("Lift is GUI-only, invalid here \"Lift\"", wr(p, root_ctl, "Lift on\n").reply.ename); - try testing.expectEqualStrings("GripWidth is GUI-only, invalid here \"GripWidth\"", wr(p, root_ctl, "GripWidth 120\n").reply.ename); - // A builtin the GUI alone has, written to a ctl or an exec, never - // runs as a shell command. - if (comptime !pardes.font_picker) { - try testing.expectEqualStrings("Fonts is GUI-only, invalid here \"Fonts\"", wr(p, root_ctl, "Fonts\n").reply.ename); - const ran = wr(p, root_exec, "Fonts\n"); - try testing.expectEqual(E.INVAL, ran.errno()); - try testing.expectEqualStrings("Fonts is GUI-only, invalid here", ran.reply.ename); - } - } - try testing.expectEqualStrings("unknown control message \"Liftt\"", wr(p, root_ctl, "Liftt on\n").reply.ename); - var buf: [900]u8 = undefined; - if (pardes.dump.directory(&buf, "")) |dir| { - var want: [920]u8 = undefined; - const line = try std.fmt.bufPrint(&want, "\nDumpDir {s}\n", .{dir}); - try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 1 << 16).bytes, line) != null); - } -} - -test "an exec line over the command limit fails the write and says the limit; a builtin's may be longer" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const long = "echo " ++ &@as([pardes.exec.command_max]u8, @splat('y')) ++ "\n"; - const refused = wr(p, root_exec, long); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectEqualStrings(e_too_long, refused.reply.ename); - var rec: [96]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "err {d} exec: invalid command line: a command line is at most 1024 bytes", .{serialOf(p)}))); - try testing.expectEqualStrings(e_control, wr(p, root_exec, "echo a\x01b\n").reply.ename); - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "err {d} exec: invalid command line: it holds", .{serialOf(p)}))); - try testing.expectEqual(Status.ok, wr(p, root_exec, "Msg " ++ &@as([1100]u8, @splat('z')) ++ "\n").reply.status); -} - -test "Edit's p and = never fail for want of room for +Errors: the text is logged instead" { - const p = try withFile(testing.allocator, "one\ntwo\n"); - defer p.deinit(); - p.update(.{ .resize = .{ .cols = 80, .rows = 6 } }); - const ctl_node = Node.of(serialOf(p), .ctl); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "Edit ,p\n").reply.status); - try testing.expect(th.logHas(p, " one\n") and th.logHas(p, " two\n")); - p.sync(); - var count: usize = 0; - for (p.panes) |slot| count += @intFromBool(slot != null); - try testing.expectEqual(@as(usize, 1), count); // the +Errors that had no room is gone -} - test "Mount dials its peer and fails, mounting nothing, when none answers; a look through a peer gone says dial failed" { const p = try withFile(testing.allocator, "x\n"); @@ -2603,16 +1998,6 @@ test "Tty in a directory that is not there fails and makes no pane; a shell that try testing.expectEqualStrings("error shell gone\n", call(p, .{ .tag = 3, .op = .read, .node = run_node, .handle = h, .size = 64 }).bytes); } -test "a builtin that needs its argument, written bare to an exec, is EINVAL as through ctl" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - for ([_]u64{ root_exec, Node.of(serialOf(p), .exec), @backingInt(tree.TopFile.tagexec) }) |node| { - const refused = wr(p, node, "Mount\n"); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectEqualStrings("wrong #args in control message \"Mount\"", refused.reply.ename); - } -} - test "a script's Grep and Find fill the pane's one query pane, a row a line under its file; a miss says not found and opens nothing" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2677,14 +2062,6 @@ test "a script's Grep and Find fill the pane's one query pane, a row a line unde try testing.expect(panes.Query.ofOwner(p, serialOf(p)) == null); } -test "Unmount of a name not mounted says no such mount, which a mount reads as ENOENT" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const refused = wr(p, @backingInt(tree.TopFile.ctl), "Unmount nothere\n"); - try testing.expectEqual(E.NOENT, refused.errno()); - try testing.expectStringStartsWith(refused.reply.ename, "Unmount: no such mount nothere"); -} - test "a terminal told the directory it already goes by logs no rename" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2938,31 +2315,6 @@ test "Grep, Symbols and Diagnostics share their directory's one +Search" { try testing.expectEqual(@as(usize, 1), searches); } -test "+Unsaved drops a pane once it has closed" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const a = try th.newPane(p); - _ = wr(p, Node.of(a, .name), "/tmp/pardes-unsaved-a.txt\n"); - _ = wr(p, Node.of(a, .body), "edited\n"); - const b = try th.newPane(p); - _ = wr(p, Node.of(b, .name), "/tmp/pardes-unsaved-b.txt\n"); - _ = wr(p, Node.of(b, .body), "edited\n"); - const root_ctl = @backingInt(tree.TopFile.ctl); - try testing.expectEqual(Status.err, wr(p, root_ctl, "Exit\n").reply.status); - var listed: ?*pardes.Pane = null; - for (p.panes) |slot| if (slot) |q| if (q.file) |f| if (f.output) |o| if (o.from == .unsaved) { - listed = q; - }; - const list = listed.?; - try testing.expect(std.mem.indexOf(u8, list.file.?.content, "/tmp/pardes-unsaved-a.txt: Modified") != null); - // a closes (discarded): its row goes; b's stays. - _ = wr(p, Node.of(a, .ctl), "Del\n"); - _ = wr(p, Node.of(a, .ctl), "Del\n"); - p.sync(); - try testing.expect(p.paneBySerial(a) == null); - try testing.expect(std.mem.indexOf(u8, list.file.?.content, "/tmp/pardes-unsaved-a.txt: Modified") == null); -} - test "a buffer whose file was deleted on disk is dirty, and Del asks first" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2987,31 +2339,6 @@ test "a failure said while a 9P write waits on it posts no msg, only the write's try testing.expect(p.fs.late_failure_len > 0); } -test "Mini with no argument is wrong #args, EINVAL, as every word that needs one" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const r = wr(p, Node.of(serialOf(p), .ctl), "Mini\n"); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expectEqualStrings("wrong #args in control message \"Mini\"", r.reply.ename); -} - -test "a look that misses file:/re/ names the file, and is logged as its pane's" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "alpha\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/f.txt\n", .{dir})); - p.sync(); - const f_serial = p.panes[p.active].?.serial; - _ = wr(p, Node.of(serialOf(p), .look), try std.fmt.bufPrint(&line, "{s}/f.txt:/zzqq/\n", .{dir})); - var want: [4300]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "err {d} look: {s}/f.txt: no match for regexp", .{ f_serial, dir }))); -} - test "with no pane at all, the root look opens its file and the root exec runs in the session's directory" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -3049,26 +2376,6 @@ test "with no pane at all, the root look opens its file and the root exec runs i try testing.expectEqualStrings(session, made.cwdSlice()); } -test "a look at file:3:0 is refused as addr 3:0 is, a column counting from 1" { - const p = try withFile(testing.allocator, "one\ntwo\nthree\n"); - defer p.deinit(); - const serial = serialOf(p); - const by_addr = wr(p, Node.of(serial, .addr), "3:0"); - const by_look = wr(p, Node.of(serial, .look), "/test.txt:3:0\n"); - try testing.expectEqual(E.INVAL, by_addr.errno()); - try testing.expectEqual(E.INVAL, by_look.errno()); - try testing.expectEqualStrings(by_addr.reply.ename, by_look.reply.ename); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .look), "/test.txt:3:1\n").reply.status); -} - -test "Kill with a name and nothing running says so and does not fail, as bare Kill does" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const r = wr(p, @backingInt(tree.TopFile.ctl), "Kill zzz\n"); - try testing.expectEqual(Status.ok, r.reply.status); - try testing.expect(th.logHas(p, "Kill: nothing running")); -} - test "a restored pane whose file is gone from disk comes back dirty, and Del asks first" { if (comptime !pardes.hosted) return error.SkipZigTest; const p = try withFile(testing.allocator, "kept text\n"); @@ -3136,98 +2443,6 @@ test "a Restore of a name in the DumpDir asks for, and logs, the whole path it r } -test "a builtin that takes no argument, written with one to an exec, is refused wrong #args, as at a ctl" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - for ([_]u64{ @backingInt(tree.TopFile.exec), Node.of(serial, .exec) }) |node| for ([_][]const u8{ "Config extra\n", "DumpConfig extra\n" }) |line| { - const r = wr(p, node, line); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expectStringStartsWith(r.reply.ename, "wrong #args in control message"); - }; -} - -test "acme's words run as pardes's (Put is Save, Delete a Del that does not ask) or are refused, never run as commands" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - for ([_][]const u8{ "Snarf\n", "Indent\n", "Local\n", "Abort\n" }) |line| { - for ([_]u64{ Node.of(serial, .ctl), Node.of(serial, .exec) }) |node| { - const r = wr(p, node, line); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "is not a pardes builtin") != null); - } - } - // Load is Restore: whatever it says, never acme's word refused. - const load = wr(p, Node.of(serial, .exec), "Load /tmp/pardes-surely-no-dump.zon\n"); - try testing.expect(std.mem.indexOf(u8, load.reply.ename, "is not a pardes builtin") == null); - // Put saves, as Save does. - _ = wr(p, Node.of(serial, .name), "/tmp/pardes-put.txt\n"); - const put = wr(p, Node.of(serial, .exec), "Put\n"); - try testing.expectEqual(Status.ok, put.reply.status); - try testing.expect(put.saved); - // Delete closes an edited pane without asking. - const made = try th.newPane(p); - _ = wr(p, Node.of(made, .name), "/tmp/pardes-delete.txt\n"); - _ = wr(p, Node.of(made, .body), "edited\n"); - try testing.expectEqual(Status.ok, wr(p, Node.of(made, .ctl), "Delete\n").reply.status); - p.sync(); - try testing.expect(p.paneBySerial(made) == null); -} - -test "a pane's ctl takes acme's words: done by the files that replace them, or refused with why" { - const p = try withFile(testing.allocator, "one two\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - const pane = p.panes[p.paneBySerial(serial).?].?; - try testing.expectEqual(Status.ok, wr(p, ctl_node, "name /tmp/pardes-acme-ctl.txt\n").reply.status); - try testing.expectEqualStrings("/tmp/pardes-acme-ctl.txt", pane.file.?.path); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "dirty\n").reply.status); - try testing.expectEqualStrings("1", std.mem.trimEnd(u8, rd(p, Node.of(serial, .dirty), 0, 8).bytes, "\n")); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "clean\n").reply.status); - try testing.expectEqualStrings("0", std.mem.trimEnd(u8, rd(p, Node.of(serial, .dirty), 0, 8).bytes, "\n")); - _ = wr(p, Node.of(serial, .addr), "/two/"); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "dot=addr\nshow\nnomark\nmark\nlimit=addr\n").reply.status); - try testing.expectEqual(@as(u32, 4), pane_files.dotOf(pane).q0); - try testing.expect(pane.fs.limit != null); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "cleartag\n").reply.status); - for ([_][]const u8{ "menu", "nomenu", "dump echo hi", "dumpdir /tmp", "font Go Mono" }) |line| { - const r = wr(p, ctl_node, line); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expect(std.mem.startsWith(u8, r.reply.ename, "invalid: acme's ")); - } - // Checked whole: a refused word anywhere and nothing ran. - try testing.expectEqual(E.INVAL, wr(p, ctl_node, "dirty\nmenu\n").errno()); - try testing.expectEqualStrings("0", std.mem.trimEnd(u8, rd(p, Node.of(serial, .dirty), 0, 8).bytes, "\n")); -} - -test "ctl reads acme's fields in acme's order, undo and redo flags included, then pardes's own" { - const p = try withFile(testing.allocator, "one\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - const fields = struct { - fn of(text: []const u8, buf: *[16][]const u8) [][]const u8 { - var it = std.mem.tokenizeScalar(u8, text, ' '); - var n: usize = 0; - while (it.next()) |t| : (n += 1) buf[n] = t; - return buf[0..n]; - } - }; - var buf: [16][]const u8 = undefined; - var f = fields.of(rd(p, ctl_node, 0, 4096).bytes, &buf); - try testing.expectEqualStrings("0", f[8]); // nothing to undo - try testing.expectEqualStrings("0", f[9]); // nothing to redo - try testing.expect(std.mem.eql(u8, f[10], "current") or std.mem.eql(u8, f[10], "notcurrent")); - _ = wr(p, Node.of(serial, .body), "two\n"); - f = fields.of(rd(p, ctl_node, 0, 4096).bytes, &buf); - try testing.expectEqualStrings("1", f[8]); - _ = wr(p, ctl_node, "Undo\n"); - f = fields.of(rd(p, ctl_node, 0, 4096).bytes, &buf); - try testing.expectEqualStrings("1", f[9]); -} - test "a write to addr, limit or mark asks for no frame; one to body does" { const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); @@ -3242,28 +2457,6 @@ test "a write to addr, limit or mark asks for no frame; one to body does" { try testing.expect(p.needs_frame); } -test "every EINVAL a write gets says why, in its err record too; DEL is a control character in a line" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const log = @backingInt(tree.TopFile.log); - const h = call(p, .{ .tag = 1, .op = .open, .node = log, .omode = 2 }).reply.handle; - const Case = struct { node: u64, data: []const u8, words: []const u8, handle: u32 = 0 }; - for ([_]Case{ - .{ .node = root_exec, .data = "echo a\x7fb\n", .words = "control character" }, - .{ .node = root_look, .data = "a\x7fb\n", .words = "control character" }, - .{ .node = Node.of(serial, .name), .data = "\n", .words = "an empty name" }, - .{ .node = log, .data = "bogus\n", .words = "takes `follow`", .handle = h }, - }) |c| { - const refused = call(p, .{ .tag = 2, .op = .write, .node = c.node, .handle = c.handle, .data = c.data }); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, c.words) != null); - } - _ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h }); - try testing.expect(!th.logHas(p, "Invalid argument")); - try testing.expect(th.logHas(p, "an empty name")); -} - test "/index shows a name as the log does: a newline in it is \\n, a control byte, C1 or byte not UTF-8 \\xNN" { const p = try th.withTerm(testing.allocator); defer p.deinit(); @@ -3278,92 +2471,7 @@ test "/index shows a name as the log does: a newline in it is \\n, a control byt // Its tag never carries the raw newline either. const tag = rd(p, Node.of(serialOf(p), .tag), 0, 4096).bytes; try testing.expect(std.mem.indexOf(u8, tag, "/tmp/two\\nlines") != null); - // A backslash of the name's own is doubled: `\n` written in a name - // reads back unlike a newline. - p.setCwd(term, "/tmp/back\\nslash"); - try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.index), 0, 4096).bytes, "/tmp/back\\\\nslash") != null); -} - -test "a look at a ./ or ../ name that is not there is a miss naming the file; a plain one is looked for as text" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.createDirPath(testing.io, "many"); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, Node.of(serialOf(p), .name), try std.fmt.bufPrint(&line, "{s}/many/a.txt\n", .{dir})); - for ([_][]const u8{ "./zz.txt", "../zz.txt" }) |name| { - const r = wr(p, Node.of(serialOf(p), .look), try std.fmt.bufPrint(&line, "{s}\n", .{name})); - try testing.expectEqual(Status.ok, r.reply.status); - var want: [64]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "look: {s}: no such file", .{name}))); - } - try testing.expectEqual(Status.ok, wr(p, Node.of(serialOf(p), .look), "zz.txt\n").reply.status); - try testing.expect(th.logHas(p, "look: no match for \"zz.txt\"")); -} - -test "a served-tree pane spoils no Grep, and Look . from a gone directory is ENOENT, not /virtual/" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "hit.txt", .data = "needle here\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, Node.of(serialOf(p), .name), try std.fmt.bufPrint(&line, "{s}/other.txt\n", .{dir})); - // A pane of the served tree. - const virtual = try th.newPane(p); - _ = wr(p, Node.of(virtual, .name), "/virtual/index\n"); - // One named the served tree's root, whose directory is `/`. - const top = try th.newPane(p); - _ = wr(p, Node.of(top, .name), "/virtual\n"); - try testing.expectEqual(Status.ok, wr(p, Node.of(serialOf(p), .exec), "Grep needle\n").reply.status); - // `.` from a pane whose directory is gone. - const gone = try th.newPane(p); - _ = wr(p, Node.of(gone, .name), "/tmp/pardes-no-such-dir-zz/f.txt\n"); - const looked = wr(p, Node.of(gone, .look), ".\n"); - try testing.expectEqual(E.NOENT, looked.errno()); - try testing.expectStringStartsWith(looked.reply.ename, "look: /tmp/pardes-no-such-dir-zz: no such directory"); -} - -test "a malformed address is bad address syntax; on a pane with no text any address is refused as nothing to address" { - const p = try th.withTerm(testing.allocator); - defer p.deinit(); - const t = try withFile(testing.allocator, "one\n"); - defer t.deinit(); - for ([_][]const u8{ "1x", "/(/", ",,x" }) |expr| { - const refused = wr(t, Node.of(serialOf(t), .addr), expr); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expect(refused.reply.ename.len > 0); - } - try testing.expectEqualStrings("bad address syntax", wr(t, Node.of(serialOf(t), .addr), "1x").reply.ename); - // A terminal (as an image or a PDF): addr, dot and limit alike. - for ([_]tree.PaneFile{ .addr, .dot, .limit }) |file| for ([_][]const u8{ "1", "1x" }) |expr| { - const refused = wr(p, Node.of(serialOf(p), file), expr); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectEqualStrings("invalid address: this pane has no text to address", refused.reply.ename); - }; -} - -test "a bad event record says what is wrong with it" { - const p = try withFile(testing.allocator, "one\n"); - defer p.deinit(); - const event = Node.of(serialOf(p), .event); - const h = call(p, .{ .tag = 1, .op = .open, .node = event, .omode = 2 }).reply.handle; - for ([_][2][]const u8{ - .{ "MI0 1 0 1 x\n", "only a look or exec" }, - .{ "MX3 1 0 0 \n", "ends before it starts" }, - .{ "MX9 9 0 0 \n", "range past end" }, - .{ "garbage\n", "a record is" }, - }) |c| { - const refused = call(p, .{ .tag = 2, .op = .write, .node = event, .handle = h, .data = c[0] }); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, c[1]) != null); - } - _ = call(p, .{ .tag = 3, .op = .release, .node = event, .handle = h }); + // A name's own backslash doubled is test/behaviour/other_files.zig's. } test "a column takes panes until their documented minimums fill it, not until none can be halved" { @@ -3384,261 +2492,6 @@ test "a column takes panes until their documented minimums fill it, not until no } } -test "Jumplist fills its one +Jumps again, as Recent does, never a second" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - var count: [2]usize = undefined; - for (&count) |*n| { - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .exec), "Jumplist\n").reply.status); - p.sync(); - n.* = 0; - for (p.panes) |slot| if (slot) |pane| if (pane.file) |f| if (f.output) |o| { - n.* += @intFromBool(std.meta.eql(o.from, panes.Output.Origin{ .cmd = .Jumplist })); - }; - } - try testing.expectEqual(@as(usize, 1), count[0]); - try testing.expectEqual(@as(usize, 1), count[1]); -} - -test "a Jumplist entry is (closed) only while no pane has its file open" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "b.txt", .data = "two\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt\n", .{dir})); - p.sync(); - const a = p.panes[p.active].?.serial; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/b.txt\n", .{dir})); - p.sync(); - _ = wr(p, Node.of(a, .ctl), "Del\n"); - p.sync(); - var closed: usize = 0; - for (p.jumps[0..p.njumps]) |j| closed += @intFromBool(j.serial == 0); - try testing.expect(closed > 0); - // Open again, by a look: no entry is closed any more. - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt\n", .{dir})); - p.sync(); - for (p.jumps[0..p.njumps]) |j| try testing.expect(j.serial != 0); -} - -test "Recent's row for a file still open is where its dot is now, not where it last closed" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt:3\n", .{dir})).reply.status); - p.sync(); - const pane = p.panes[p.active].?; - pane.body.vsel.active = false; // the look's selection of line 3 let go - pane.body.cur_row = 1; - pane.body.cur_col = 2; - try testing.expectEqual(Status.ok, wr(p, root_exec, "Recent\n").reply.status); - const rows = p.panes[p.paneBySerial(try std.fmt.parseInt(u32, std.mem.trimEnd(u8, rd(p, root_exec, 0, 64).bytes, "\n"), 10)).?].?; - // A query pane under the file (line 1 `Recent `), its rows said from - // its directory. - try testing.expect(std.mem.startsWith(u8, rows.file.?.content, "Recent \n")); - try testing.expect(std.mem.indexOf(u8, rows.file.?.content, "\na.txt:2:3 open\n") != null); -} - -test "a closed file is found again: Recent and /recent list it closed, a look at its row reopens it at its dot, and a jump back does too" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "b.txt", .data = "bee\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - const a_path = try std.fmt.bufPrint(&line, "{s}/a.txt", .{dir}); - var a_buf: [4200]u8 = undefined; - const a = a_buf[0..a_path.len]; - @memcpy(a, a_path); - // Opened, its dot on line 3, then closed. - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&line, "{s}:3\n", .{a})).reply.status); - p.sync(); - const opened = p.active; - try testing.expectEqualStrings(a, p.panes[opened].?.file.?.path); - var want: [4300]u8 = undefined; - try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.recent), 0, 8192).bytes, try std.fmt.bufPrint(&want, "open {s}\n", .{a})) != null); - // Another file, so the jumplist has somewhere to come back from. - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/b.txt\n", .{dir})).reply.status); - p.sync(); - try testing.expectEqual(Status.ok, wr(p, Node.of(p.panes[opened].?.serial, .ctl), "Del\n").reply.status); - p.sync(); - try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.recent), 0, 8192).bytes, try std.fmt.bufPrint(&want, "closed {s}\n", .{a})) != null); - // Recent lists it, closed, at its dot; a look at the row opens it there. - try testing.expectEqual(Status.ok, wr(p, root_exec, "Recent\n").reply.status); - const rows = p.panes[p.paneBySerial(try std.fmt.parseInt(u32, std.mem.trimEnd(u8, rd(p, root_exec, 0, 64).bytes, "\n"), 10)).?].?; - // Its query pane's rows are said from its directory, a.txt's. - const at = std.mem.indexOf(u8, rows.file.?.content, "\na.txt:3:").? + 1; - const row = rows.file.?.content[at..std.mem.indexOfScalarPos(u8, rows.file.?.content, at, '\n').?]; - try testing.expect(std.mem.endsWith(u8, row, " (closed)")); - try testing.expectEqual(Status.ok, wr(p, Node.of(rows.serial, .look), try std.fmt.bufPrint(&line, "{s}\n", .{row[0 .. row.len - " (closed)".len]})).reply.status); - p.sync(); - const back = p.panes[p.active].?; - try testing.expectEqualStrings(a, back.file.?.path); - try testing.expectEqual(@as(i32, 2), back.body.cur_row); - // Closed again; the jumplist keeps its entry, closed, and a jump back - // to it opens it again. - _ = wr(p, Node.of(back.serial, .ctl), "Del\n"); - p.sync(); - var closed_entry = false; - for (p.jumps[0..p.njumps]) |j| closed_entry = closed_entry or j.serial == 0; - try testing.expect(closed_entry); - // Every entry in order (JumpScope all): which file has the focus after - // a Del is the layout's to choose, and Back keeps to it first. - p.settings.jump_scope = .all; - var tries: usize = 0; - while (tries < p.njumps) : (tries += 1) { - p.jumpBy(-1); - p.sync(); - if (p.panes[p.active].?.file) |f| if (std.mem.eql(u8, f.path, a)) break; - } - try testing.expectEqualStrings(a, p.panes[p.active].?.file.?.path); -} - -test "Back and Forward over a closed file's entries: one reopen is that file's pane in every entry, so the next step moves" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - var long: [400]u8 = undefined; - var n: usize = 0; - for (0..100) |_| { - @memcpy(long[n..][0..4], "row\n"); - n += 4; - } - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = long[0..n] }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "b.txt", .data = "bee\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.txt", .data = "sea\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/b.txt\n", .{dir})); - p.sync(); - const b_serial = p.panes[p.active].?.serial; - // Two entries for a.txt, far apart in it. - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt:1\n", .{dir})); - p.sync(); - const a = p.active; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt:90\n", .{dir})); - p.sync(); - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/c.txt\n", .{dir})); - p.sync(); - const c_serial = p.panes[p.active].?.serial; - _ = wr(p, Node.of(p.panes[a].?.serial, .ctl), "Del\n"); - p.sync(); - var closed: usize = 0; - for (p.jumps[0..p.njumps]) |j| closed += @intFromBool(j.serial == 0); - try testing.expectEqual(@as(usize, 2), closed); - // Back reopens it at line 90: both its entries are that pane now. - p.jumpBy(-1); - p.sync(); - const re = p.panes[p.active].?; - try testing.expect(std.mem.endsWith(u8, re.file.?.path, "/a.txt")); - try testing.expectEqual(@as(i32, 89), re.body.cur_row); - for (p.jumps[0..p.njumps]) |j| try testing.expect(j.serial != 0); - // Back again goes to its line 1, in the same pane, then to b. - p.jumpBy(-1); - p.sync(); - try testing.expectEqual(re.serial, p.panes[p.active].?.serial); - try testing.expectEqual(@as(i32, 0), re.body.cur_row); - p.jumpBy(-1); - p.sync(); - try testing.expectEqual(b_serial, p.panes[p.active].?.serial); - // And Forward walks it all again, to c. - p.jumpBy(1); - p.sync(); - p.jumpBy(1); - p.sync(); - try testing.expectEqual(@as(i32, 89), re.body.cur_row); - p.jumpBy(1); - p.sync(); - try testing.expectEqual(c_serial, p.panes[p.active].?.serial); -} - -test "Back and Forward written to exec keep to the focused file, and JumpScope all, written to ctl, takes every place in order" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "b.txt", .data = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - // Each a look, as a user's jumps are: b1 a1 b40 a40 b80 a80. - for ([_][]const u8{ "b.txt:1", "a.txt:1", "b.txt:40", "a.txt:40", "b.txt:80", "a.txt:80" }) |place| { - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/{s}\n", .{ dir, place })).reply.status); - p.sync(); - } - const Step = struct { word: []const u8, file: []const u8, line: i32 }; - const walk = struct { - fn run(q: *Pardes, steps: []const Step) !void { - for (steps) |s| { - var cmd: [16]u8 = undefined; - try testing.expectEqual(Status.ok, wr(q, root_exec, try std.fmt.bufPrint(&cmd, "{s}\n", .{s.word})).reply.status); - q.sync(); - const pane = q.panes[q.active].?; - try testing.expect(std.mem.endsWith(u8, pane.file.?.path, s.file)); - try testing.expectEqual(s.line, pane.body.cur_row + 1); - } - } - }.run; - // a's places first, b only once a has none older; Forward mirrors it - // from b and ends at a80, where it began. - try walk(p, &.{ - .{ .word = "Back", .file = "/a.txt", .line = 40 }, - .{ .word = "Back", .file = "/a.txt", .line = 1 }, - .{ .word = "Back", .file = "/b.txt", .line = 1 }, - .{ .word = "Forward", .file = "/b.txt", .line = 40 }, - .{ .word = "Forward", .file = "/b.txt", .line = 80 }, - .{ .word = "Forward", .file = "/a.txt", .line = 80 }, - }); - try testing.expectEqual(Status.ok, wr(p, @backingInt(tree.TopFile.ctl), "JumpScope all\n").reply.status); - try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.ctl), 0, 8192).bytes, "JumpScope all\n") != null); - try walk(p, &.{ - .{ .word = "Back", .file = "/b.txt", .line = 80 }, - .{ .word = "Back", .file = "/a.txt", .line = 40 }, - .{ .word = "Forward", .file = "/b.txt", .line = 80 }, - }); -} - -test "Recent keeps only files on disk: not a name never saved, nor the served tree's" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "real.txt", .data = "real\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - const names = [_][]const u8{ try std.fmt.allocPrint(testing.allocator, "{s}/c", .{dir}), try testing.allocator.dupe(u8, "/virtual/zz"), try std.fmt.allocPrint(testing.allocator, "{s}/real.txt", .{dir}) }; - defer for (names) |n| testing.allocator.free(n); - for (names) |name| { - const made = try th.newPane(p); - _ = wr(p, Node.of(made, .name), try std.fmt.bufPrint(&line, "{s}\n", .{name})); - p.sync(); - _ = wr(p, Node.of(made, .ctl), "Del\n"); - _ = wr(p, Node.of(made, .ctl), "Del\n"); - p.sync(); - } - try testing.expect(p.recent.get(names[0]) == null); - try testing.expect(p.recent.get(names[1]) == null); - try testing.expect(p.recent.get(names[2]) != null); -} - test "a new file saved shows in /recent, open, while it is" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -3669,82 +2522,11 @@ test "a new file saved shows in /recent, open, while it is" { test "/recent shows a name as /index does, a newline in it escaped" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); - p.recent.opened(p.recentGpa(), "/tmp/a\nb\\c"); + // A backslash doubled is test/behaviour/other_files.zig's; a 9P name + // write cannot hold a newline. + p.recent.opened(p.recentGpa(), "/tmp/a\nb"); const text = rd(p, @backingInt(tree.TopFile.recent), 0, 8192).bytes; - try testing.expect(std.mem.indexOf(u8, text, "closed /tmp/a\\nb\\\\c\n") != null); -} - -test "Back over a closed file deleted since skips it, says so, and goes on to the place before" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - for ([_][]const u8{ "a.txt", "gone.txt", "c.txt" }) |name| try tmp.dir.writeFile(testing.io, .{ .sub_path = name, .data = "text\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt\n", .{dir})); - p.sync(); - const a_serial = p.panes[p.active].?.serial; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/gone.txt\n", .{dir})); - p.sync(); - const gone = p.active; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/c.txt\n", .{dir})); - p.sync(); - const c_id = p.active; - _ = wr(p, Node.of(p.panes[gone].?.serial, .ctl), "Del\n"); - p.sync(); - try tmp.dir.deleteFile(testing.io, "gone.txt"); - p.active = c_id; - p.jumpBy(-1); - p.sync(); - try testing.expectEqual(a_serial, p.panes[p.active].?.serial); - try testing.expect(std.mem.indexOf(u8, p.panes[c_id].?.msg[0..p.panes[c_id].?.msg_len], "gone.txt: deleted, skipped") != null); -} - -test "a remembered place is clamped to the file as it is now, its selection's two ends kept where they fit" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.txt", .data = "one\ntwo\nthree\nfour\nfive\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/c.txt:2\n", .{dir})); - p.sync(); - const c = p.panes[p.active].?; - // A selection over lines 2 to 4, then closed. - @import("pane.zig").setDot(c, .{ .q0 = 4, .q1 = 19 }); - try testing.expect(c.body.vsel.active); - _ = wr(p, Node.of(c.serial, .ctl), "Del\n"); - p.sync(); - // The file shrinks to two lines while it is closed. - try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.txt", .data = "one\ntwo\n" }); - _ = wr(p, root_exec, "Recent\n"); - const rows = p.panes[p.paneBySerial(try std.fmt.parseInt(u32, std.mem.trimEnd(u8, rd(p, root_exec, 0, 64).bytes, "\n"), 10)).?].?; - const at = std.mem.indexOf(u8, rows.file.?.content, "c.txt:").?; - const row = rows.file.?.content[at..std.mem.indexOfScalarPos(u8, rows.file.?.content, at, '\n').?]; - // Both ends are in the row, a range. - try testing.expect(std.mem.indexOfScalar(u8, row, '-') != null); - var full: [4400]u8 = undefined; - const loc = try std.fmt.bufPrint(&full, "{s}/{s}\n", .{ dir, row[0..std.mem.indexOfScalar(u8, row, ' ').?] }); - // A look at it opens the file, never a miss, clamped to its last line. - try testing.expectEqual(Status.ok, wr(p, Node.of(rows.serial, .look), loc).reply.status); - p.sync(); - const back = p.panes[p.active].?; - try testing.expect(std.mem.endsWith(u8, back.file.?.path, "/c.txt")); - try testing.expect(back.body.cur_row <= 1); - try testing.expect(back.body.vsel.active); - // Its ends clamped to the last character, `two`'s `o`, not its newline. - try testing.expect(back.body.cur_col <= 2 and back.body.vsel.col <= 2); - try testing.expect(std.mem.indexOf(u8, back.msg[0..back.msg_len], "c.txt has 2 lines now: at its last") != null); - // The same row with the file open: the same place, said the same way. - _ = wr(p, Node.of(rows.serial, .look), loc); - p.sync(); - try testing.expectEqual(back.serial, p.panes[p.active].?.serial); - try testing.expect(back.body.cur_row <= 1); - try testing.expect(std.mem.indexOf(u8, back.msg[0..back.msg_len], "at its last") != null); + try testing.expect(std.mem.indexOf(u8, text, "closed /tmp/a\\nb\n") != null); } test "size is monotonic: growing is never refused, and a size once taken is taken again" { @@ -3790,24 +2572,6 @@ test "size is monotonic: growing is never refused, and a size once taken is take try testing.expectStringStartsWith(bad.reply.ename, "invalid size"); } -test "Repl with no such language is invalid, EINVAL, and names a few whole and how many more" { - const p = try th.withTerm(testing.allocator); - defer p.deinit(); - const refused = wr(p, Node.of(serialOf(p), .ctl), "Repl zzlang\n"); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, "Repl: invalid language \"zzlang\"; - or one like zig ada bash c c_sharp clojure (") != null); - try testing.expect(std.mem.endsWith(u8, refused.reply.ename, " more)")); -} - -test "answer quotes the choice it refuses, and a bare name says it names nothing" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const node = Node.of(serialOf(p), .ctl); - const empty = wr(p, node, "name \n"); - try testing.expectEqual(E.INVAL, empty.errno()); - try testing.expectEqualStrings("invalid file name: name takes the name, and there is none", empty.reply.ename); -} - test "a 10k-line Edit text block is taken in linear time and memory" { const gpa = testing.allocator; const p = try withFile(gpa, "x\n"); @@ -3853,39 +2617,6 @@ test "a file changed on disk under unsaved edits is not reloaded: it says so, ge try testing.expect(wr(p, Node.of(serial, .ctl), "Save\n").saved); } -test "a looked word is found in its own pane, next place after the dot, wrapping, and opens no pane" { - const p = try withFile(testing.allocator, "foo x foo y\n"); - defer p.deinit(); - const serial = serialOf(p); - const look_node = Node.of(serial, .look); - const dot = Node.of(serial, .dot); - var count: usize = 0; - for (p.panes) |slot| count += @intFromBool(slot != null); - for ([_][]const u8{ " 0 3 ", " 6 9 ", " 0 3 " }) |want| { - try testing.expectEqual(Status.ok, wr(p, look_node, "foo\n").reply.status); - try testing.expectEqualStrings(want, rd(p, dot, 0, 64).bytes); - } - var after: usize = 0; - for (p.panes) |slot| after += @intFromBool(slot != null); - try testing.expectEqual(count, after); - // LookWord list: the +Search listing, as before. - try testing.expect(p.executeBuiltinLine(0, "LookWord list")); - try testing.expectEqual(Status.ok, wr(p, look_node, "foo\n").reply.status); - after = 0; - for (p.panes) |slot| after += @intFromBool(slot != null); - try testing.expect(after > count); - // ...and look reads back that +Search pane, where the places are. - var listed = false; - var rows = std.mem.tokenizeScalar(u8, rd(p, look_node, 0, 256).bytes, '\n'); - while (rows.next()) |row| { - const made = p.panes[p.paneBySerial(try std.fmt.parseInt(u32, row, 10)).?].?; - if (made.file) |f| if (f.output) |o| if (o.from == .search) { - listed = true; - }; - } - try testing.expect(listed); -} - test "every word's /commands description is its own doc comment's first sentence, no citation" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -3996,37 +2727,6 @@ test "get names the path it could not read, logs the edits it threw away, and a _ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h }); } -test "a builtin that fails through exec, tagexec or a column's exec fails the write with one err and no msg" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - p.update(.tick); // the pane's `new` is logged first - while (p.nextEffect()) |_| {} - const serial = serialOf(p); - for ([_]u64{ root_exec, Node.of(serial, .exec), @backingInt(tree.TopFile.tagexec), Node.ofCol(pardes.layout.columnSerial(p, 0), .exec) }) |node| { - const failed = wr(p, node, "Unmount zzz\n"); - try testing.expectEqual(Status.err, failed.reply.status); - try testing.expect(std.mem.indexOf(u8, failed.reply.ename, "Unmount: no such mount zzz") != null); - } - const log = @backingInt(tree.TopFile.log); - const h = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle; - const text = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = h, .size = 1 << 16 }).bytes; - try testing.expect(std.mem.indexOf(u8, text, "msg ") == null); - // The root's exec ran at the pane, so its err and the pane's exec's are - // the same record, counted. - try testing.expectEqual(@as(usize, 3), std.mem.count(u8, text, "Unmount: no such mount zzz")); - try testing.expect(std.mem.indexOf(u8, text, "mount zzz (x2)") != null); - _ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h }); - // A control character is refused with its reason. - try testing.expectEqualStrings(e_control, wr(p, root_exec, "Msg a\x01b\n").reply.ename); - // A look miss: one err, no msg. - _ = wr(p, @backingInt(tree.TopFile.look), "zzqqnotthere\n"); - try testing.expect(th.logHas(p, "look: no match for \"zzqqnotthere\"")); - try testing.expect(!th.logHas(p, "msg 1 Look: no match")); - // The same miss again is that record counted, as any repeated err is. - _ = wr(p, @backingInt(tree.TopFile.look), "zzqqnotthere\n"); - try testing.expect(th.logHas(p, "look: no match for \"zzqqnotthere\" (x2)\n")); -} - test "a word written to a raw terminal's look is listed as in normal mode" { const p = try th.withTerm(testing.allocator); defer p.deinit(); @@ -4040,64 +2740,6 @@ test "a word written to a raw terminal's look is listed as in normal mode" { try testing.expect(made.file.?.output.?.from == .search); } -test "@p<serial>:<address> takes any sam address; a miss and an unknown serial are errs" { - const p = try withFile(testing.allocator, "foo x foo y\nbar\n"); - defer p.deinit(); - const serial = serialOf(p); - const root = @backingInt(tree.TopFile.look); - const dot = Node.of(serial, .dot); - const Case = struct { addr: []const u8, want: []const u8 }; - for ([_]Case{ - .{ .addr = "0/foo/", .want = " 0 3 " }, - .{ .addr = "/foo/", .want = " 6 9 " }, - .{ .addr = "#12,#15", .want = " 12 15 " }, - .{ .addr = "$", .want = " 16 16 " }, - .{ .addr = "1,2", .want = " 0 16 " }, - // Backward, as in addr: from the dot's start, wrapping to the last. - .{ .addr = "?foo?", .want = " 6 9 " }, - .{ .addr = "$-?y?", .want = " 10 11 " }, - }) |c| { - var line: [64]u8 = undefined; - try testing.expectEqual(Status.ok, wr(p, root, try std.fmt.bufPrint(&line, "@p{d}:{s}\n", .{ serial, c.addr })).reply.status); - try testing.expectEqualStrings(c.want, rd(p, dot, 0, 64).bytes); - } - var line: [64]u8 = undefined; - _ = wr(p, root, try std.fmt.bufPrint(&line, "@p{d}:/zzqq/\n", .{serial})); - try testing.expect(th.logHas(p, ": no match for regexp")); - try testing.expectEqualStrings("", rd(p, root, 0, 64).bytes); - _ = wr(p, root, "@p999:/foo/\n"); - try testing.expect(th.logHas(p, "look: no pane @p999")); - try testing.expectEqualStrings("", rd(p, root, 0, 64).bytes); - // A line address to no pane is a miss too, not the active pane. - _ = wr(p, root, "@p77:1\n"); - try testing.expect(th.logHas(p, "look: no pane @p77")); - try testing.expectEqualStrings("", rd(p, root, 0, 64).bytes); -} - -test "a look that goes to another pane leaves the looking pane's dot alone" { - const p = try withFile(testing.allocator, "alpha bravo\ncharlie\n"); - defer p.deinit(); - const source = serialOf(p); - const other = try th.newPane(p); - _ = wr(p, Node.of(other, .body), "one\ntwo\nthree\n"); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "f one\nf two\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - for ([_][]const u8{ "@p{d}:2", "@p{d}:/three/", "{s}/f.txt:2" }) |form| { - @import("pane.zig").setDot(p.panes[p.paneBySerial(source).?].?, .{ .q0 = 6, .q1 = 11 }); - const text = if (form[0] == '@') - (if (std.mem.eql(u8, form, "@p{d}:2")) try std.fmt.bufPrint(&line, "@p{d}:2\n", .{other}) else try std.fmt.bufPrint(&line, "@p{d}:/three/\n", .{other})) - else - try std.fmt.bufPrint(&line, "{s}/f.txt:2\n", .{dir}); - try testing.expectEqual(Status.ok, wr(p, Node.of(source, .look), text).reply.status); - try testing.expect(p.active != p.paneBySerial(source).?); - try testing.expectEqualStrings(" 6 11 ", rd(p, Node.of(source, .dot), 0, 64).bytes); - } -} - test "@p<terminal>:<address> takes any address over the terminal's lines, selecting the match" { const p = try th.withTerm(testing.allocator); defer p.deinit(); @@ -4265,45 +2907,6 @@ test "Edit on an image pane is refused as a data write there is, EINVAL, before try testing.expectEqual(E.INVAL, wr(p, Node.of(doc.serial, .name), "/tmp/other.ppm\n").errno()); } -test "`@pN:` looks at pane N as `a.txt:` looks at a.txt, and a line past the end is said one way, `:N` or `file:N`" { - const p = try withFile(testing.allocator, "one\ntwo\n"); - defer p.deinit(); - const s = serialOf(p); - var buf: [64]u8 = undefined; - p.look_missed = false; - pardes.look.lookAt(p, 0, try std.fmt.bufPrint(&buf, "@p{d}:", .{s})); - try testing.expect(!p.look_missed); - const f = pane_files.fileOf(p.panes[0].?).?; - for ([_][]const u8{ ":99999", try std.fmt.bufPrint(&buf, "{s}:99999", .{f.path}) }) |looked| { - pardes.look.lookAt(p, 0, looked); - const said = p.panes[0].?.msg[0..p.panes[0].?.msg_len]; - try testing.expect(std.mem.endsWith(u8, said, "/test.txt:99999 has no line 99999")); - } -} - -test "a second LookWord list look in a directory refills its +Search rather than stacking another" { - if (comptime !pardes.hosted) return; - const gpa = testing.allocator; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "words.txt", .data = "alpha beta\nalpha beta\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var path_buf: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/words.txt", .{dir}); - const p = try Pardes.init(gpa, .{ .file = path, .tty_only = true, .cols = 80, .rows = 30 }); - defer p.deinit(); - _ = wr(p, @backingInt(tree.TopFile.ctl), "LookWord list\n"); - pardes.look.lookAt(p, 0, "alpha"); - pardes.look.lookAt(p, 0, "beta"); - var searches: usize = 0; - for (p.panes) |slot| if (slot) |pn| if (pn.file) |f| if (std.mem.endsWith(u8, f.path, "+Search")) { - searches += 1; - try testing.expectEqualStrings("beta", f.output.?.arg()); - }; - try testing.expectEqual(@as(usize, 1), searches); -} - test "Petscii, Palette and Ascii take the state their tag word names, set rather than flipped" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -4380,40 +2983,6 @@ test "Lspinfo's answer is the +Lsp pane it fills, as a Rename preview's is its + } -test "a look of ./x that is not there is a miss, as x and /abs/x are: answered, the err logged" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - for ([_][]const u8{ "./zzq-nosuch.txt\n", "zzq-nosuch.txt\n", "/zzq-nosuch.txt\n" }) |line| { - const r = wr(p, Node.of(serialOf(p), .look), line); - try testing.expectEqual(Status.ok, r.reply.status); - } - try testing.expect(th.logHas(p, "look: ./zzq-nosuch.txt: no such file")); -} - -test "a ctl line over 1 MiB is refused once, EINVAL, and its tail is not run as a line of its own" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const node = Node.of(serialOf(p), .ctl); - const h = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle; - const chunk: [4096]u8 = @splat('z'); - var refused: usize = 0; - var off: u64 = 0; - for (0..270) |_| { - const r = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .off = off, .data = &chunk }); - if (r.reply.status == .err) { - refused += 1; - try testing.expectEqual(E.INVAL, r.errno()); - } - off += chunk.len; - } - try testing.expectEqual(Status.ok, call(p, .{ .tag = 3, .op = .write, .node = node, .handle = h, .off = off, .data = "zzz\nEdit ,d\n" }).reply.status); - try testing.expectEqual(@as(usize, 1), refused); - try testing.expect(!th.logHas(p, "unknown control message")); - // What came after the long line's newline ran. - try testing.expectEqualStrings("", pane_files.fileOf(p.panes[0].?).?.content); - _ = call(p, .{ .tag = 4, .op = .release, .node = node, .handle = h }); -} - test "DumpDir refuses a relative directory and one that may not be written, up front; a Dump there says permission denied" { if (comptime !pardes.hosted) return; const p = try withFile(testing.allocator, "x\n"); @@ -4429,14 +2998,6 @@ test "DumpDir refuses a relative directory and one that may not be written, up f try testing.expect(std.mem.endsWith(u8, p.fs.late_failure[0..p.fs.late_failure_len], ": permission denied")); } -test "a Mount dial with a network it does not know is a bad dial address, EINVAL" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const r = wr(p, @backingInt(tree.TopFile.ctl), "Mount peer nowhere!x\n"); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expectStringStartsWith(r.reply.ename, "Mount: bad dial address: nowhere!x"); -} - test "a command or a terminal refused for room is refused before anything is logged of it, and its serial is never handed out again" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index bf9dc381..28f1882d 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" { +// 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); - 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" { - 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,189 +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); - defer pardes.syntax.stop(); - const p = try withFile(testing.allocator, "fn check() void {}\n"); - defer p.deinit(); - const pane = p.panes[0].?; - panes.File.refreshHighlights(p); - try testing.expectEqual(@as(usize, 0), pane.file.?.highlights.len); - try testing.expectEqual(Status.ok, wr(p, Node.of(pane.serial, .name), "renamed.zig\n").reply.status); - panes.File.refreshHighlights(p); - try testing.expect(pane.file.?.highlights.len >= 2); - try testing.expectEqual(@backingInt(pardes.syntax.Syn.keyword), pane.file.?.highlights[0]); - try testing.expectEqual(@backingInt(pardes.syntax.Syn.keyword), pane.file.?.highlights[1]); - 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 +1243,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 +1258,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(); |
