diff options
| -rw-r--r-- | src/ninep/ctl.zig | 285 | ||||
| -rw-r--r-- | test/behaviour/panes.zig | 258 | ||||
| -rw-r--r-- | test/behaviour/root_ctl.zig | 31 |
3 files changed, 297 insertions, 277 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index bb0fcb96..fc09c730 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -1580,204 +1580,28 @@ test "look validates the whole batch before opening virtual files" { 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" { @@ -1809,36 +1633,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(); @@ -1872,73 +1666,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")); diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index 7febd667..9103508c 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -399,3 +399,261 @@ test "panes: ctl reads acme's fields in acme's order, undo and redo flags includ try s.write(ctl, "Undo\n"); try std.testing.expectEqualStrings("1", field(try s.read(ctl), 9)); } + +/// A fresh directory's absolute path, kept until deinit. +fn tmpPath(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 { + var buf: [4096]u8 = undefined; + return s.at("{s}", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]}); +} + +/// The `i`th blank-separated field of a pane's ctl read. +fn ctlField(s: *Session, n: u32, i: usize) ![]const u8 { + var it = std.mem.tokenizeScalar(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), ' '); + for (0..i) |_| _ = it.next(); + return it.next() orelse error.NoField; +} + +test "panes: get reloads the pane from disk and reports a missing file without losing dirty contents" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); + var s = try Session.initFile(std.testing.allocator, "in memory\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/note.txt\n", .{dir})); + try s.write(ctl, "get\n"); + try s.expect(body, "from disk\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); + try std.testing.expectEqualStrings("1", try ctlField(&s, n, 8)); // an undo step + // Renamed but never edited, it was clean: nothing thrown away, nothing + // logged as reloaded over edits. + try std.testing.expect(!has(try s.read("/log"), s.at("changed {d} reloaded\n", .{n}))); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/missing.txt\n", .{dir})); + try s.write(body, "unsaved\n"); + // Unsaved text is asked about once, as acme's get asks winclean. + try std.testing.expectEqual(@as(u16, 5), try s.refused(ctl, "get\n")); + try std.testing.expect(std.mem.endsWith(u8, s.said, ": Modified (get again to discard)")); + try std.testing.expect(has(try s.read("/log"), s.at("unsaved {d} {s}/missing.txt\n", .{ n, dir }))); + try std.testing.expectEqual(@as(u16, 2), try s.refused(ctl, "get\n")); + try s.expect(body, "from disk\nunsaved\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n"); +} + +test "panes: Get reads the pane's file again, refusing unsaved edits once, and Get file loads that file into the pane" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "the other\n" }); + var s = try Session.initFile(std.testing.allocator, "in memory\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/note.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try s.expect(body, "from disk\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); + // Edited: asked once, then read again. + try s.write(body, "unsaved\n"); + try std.testing.expect(try s.refused(exec, "Get\n") != 0); + try s.expect(body, "from disk\nunsaved\n"); + try s.write(exec, "Get\n"); + try s.expect(body, "from disk\n"); + // Get file: that file, by a name from the pane's directory. + try s.write(exec, "Get other.txt\n"); + try s.expect(body, "the other\n"); + try s.expect(s.at("/pane/{d}/name", .{n}), s.at("{s}/other.txt\n", .{dir})); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); +} + +test "panes: 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 = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "other\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "keep.txt", .data = "keep\n" }); + for ([_][]const u8{ "Get keep.txt\n", "Edit e keep.txt\n" }) |load| { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + const name = s.at("/pane/{d}/name", .{n}); + const dirty = s.at("/pane/{d}/dirty", .{n}); + try s.write(name, s.at("{s}/other.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try s.expect(body, "other\n"); + try s.expect(dirty, "0\n"); + // Loaded: keep.txt's text under its name. + try s.write(exec, load); + try s.expect(body, "keep\n"); + try s.expect(name, s.at("{s}/keep.txt\n", .{dir})); + if (std.mem.startsWith(u8, load, "Get")) try s.expect(dirty, "0\n"); + // Undo: other.txt's text, other.txt's name, clean as it was. + try s.write(exec, "Undo\n"); + try s.expect(body, "other\n"); + try s.expect(name, s.at("{s}/other.txt\n", .{dir})); + try s.expect(dirty, "0\n"); + // Putall saves nothing, and nothing lands on keep.txt. + try s.write("/ctl", "Putall\n"); + try std.testing.expectEqual(@as(usize, 0), s.saved); + // Redo: keep.txt again. + try s.write(exec, "Redo\n"); + try s.expect(name, s.at("{s}/keep.txt\n", .{dir})); + try s.expect(body, "keep\n"); + } +} + +test "panes: a Get file that fails changes nothing: no such file, a directory, or unsaved edits asked about once" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "here.txt", .data = "here\n" }); + try tmp.dir.createDirPath(std.testing.io, "sub"); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + const name = s.at("/pane/{d}/name", .{n}); + const here = s.at("{s}/here.txt\n", .{dir}); + try s.write(name, here); + try s.write(exec, "Get\n"); + for ([_][]const u8{ "Get nosuch.txt\n", "Get sub\n" }) |line| { + try std.testing.expect(try s.refused(exec, line) != 0); + try s.expect(name, here); + try s.expect(body, "here\n"); + } + // No undo step left by them: one Undo takes back the first Get. + try s.write(exec, "Undo\n"); + try s.expect(body, "x\n"); + try s.write(exec, "Redo\n"); + // Edited, the first Get asks and renames nothing. + try s.write(body, "more\n"); + try std.testing.expect(try s.refused(exec, "Get other.txt\n") != 0); + try s.expect(name, here); + try s.expect(body, "here\nmore\n"); +} + +/// The serials /index lists with a name ending `suffix`, in its order. +fn panesNamed(s: *Session, suffix: []const u8) ![]u32 { + var out: std.ArrayList(u32) = .empty; + var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (rows.next()) |line| { + const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue]; + if (std.mem.endsWith(u8, name, suffix)) try out.append(s.arena.allocator(), try std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10)); + } + return out.items; +} + +test "panes: Zerox makes a second pane on one buffer: one text, undo and dirty state, its own cursor; Del of one asks nothing" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const a = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{a}), s.at("{s}/z.txt\n", .{dir})); + try s.write(s.at("/pane/{d}/exec", .{a}), "Zerox\n"); + // Both in the index, each its serial, one name. + const twins = try panesNamed(&s, "/z.txt"); + try std.testing.expectEqual(@as(usize, 2), twins.len); + const b = if (twins[0] == a) twins[1] else twins[0]; + // b's cursor on `three`; a line written above it in a moves it down. + try s.write(s.at("/pane/{d}/dot", .{b}), "9 9"); + try s.write(s.at("/pane/{d}/addr", .{a}), "0"); + try s.write(s.at("/pane/{d}/data", .{a}), "zero\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + try std.testing.expectEqual(.{ @as(u32, 14), @as(u32, 14) }, try s.range(s.at("/pane/{d}/dot", .{b}))); + try s.expect(s.at("/pane/{d}/dirty", .{a}), "1\n"); + try s.expect(s.at("/pane/{d}/dirty", .{b}), "1\n"); + // A write through b is a's too; Undo in a takes it back from both. + try s.write(s.at("/pane/{d}/body", .{b}), "four\n"); + try s.expect(s.at("/pane/{d}/body", .{a}), "zero\none\ntwo\nthree\nfour\n"); + try s.write(s.at("/pane/{d}/exec", .{a}), "Undo\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + // Del of a asks nothing: b holds the text. + try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try std.testing.expectEqualSlices(u32, &.{b}, try panesNamed(&s, "/z.txt")); + try s.expect(s.at("/pane/{d}/dirty", .{b}), "1\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + // Alone now, it is no twin; Del asks about it as about any pane. + try std.testing.expect(try s.refused(s.at("/pane/{d}/ctl", .{b}), "Del\n") != 0); + try std.testing.expectEqualSlices(u32, &.{b}, try panesNamed(&s, "/z.txt")); +} + +test "panes: a twin renamed logs a rename for each twin" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const a = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{a}), "Zerox\n"); + const twins = try panesNamed(&s, "/test.txt"); + try std.testing.expectEqual(@as(usize, 2), twins.len); + try s.write(s.at("/pane/{d}/name", .{a}), "/tmp/pardes-twin-renamed.txt\n"); + for (twins) |n| try std.testing.expect(has(try s.read("/log"), s.at("rename {d} /tmp/pardes-twin-renamed.txt\n", .{n}))); +} + +test "panes: small refusals: one name an open, small scratches loaded over unasked, twins saved once, and errnos in words a mount reads" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "noread.txt", .data = "secret\n" }); + try tmp.dir.createDirPath(std.testing.io, "sub"); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const name = s.at("/pane/{d}/name", .{n}); + const exec = s.at("/pane/{d}/exec", .{n}); + // One name an open: a second line, in the write or after, is refused. + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/pardes-a\nb")); + const f = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(f, "/tmp/pardes-one.txt\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(f, "b")); + s.clunk(f); + try s.expect(name, "/tmp/pardes-one.txt\n"); + // Errnos a mount reads from the words. + try s.write(name, s.at("{s}/f.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try std.testing.expectEqual(@as(u16, 21), try s.refused(exec, "Get sub\n")); // EISDIR + const noread = s.at("{s}/noread.txt\x00", .{dir}); + _ = std.c.chmod(noread[0 .. noread.len - 1 :0], 0); + try std.testing.expectEqual(@as(u16, 13), try s.refused(exec, "Get noread.txt\n")); // EACCES + try std.testing.expectEqual(@as(u16, 20), try s.refused(exec, s.at("Incl {s}/f.txt\n", .{dir}))); // ENOTDIR + _ = try s.refused(s.at("/pane/{d}/look", .{n}), "/dev/zero\n"); + try std.testing.expect(has(try s.read("/log"), "not a regular file")); + // Twins saved once by Putall. + try s.write(s.at("/pane/{d}/body", .{n}), "edited\n"); + try s.write(exec, "Zerox\n"); + const saved = s.saved; + try s.write("/ctl", "Putall\n"); + try std.testing.expectEqual(saved + 1, s.saved); + // A scratch of 2 bytes is loaded over unasked. + try s.write(exec, "New\n"); + const scratch = try s.focused(); + try s.write(s.at("/pane/{d}/body", .{scratch}), "hi"); + try s.write(s.at("/pane/{d}/exec", .{scratch}), s.at("Get {s}/f.txt\n", .{dir})); + try s.expect(s.at("/pane/{d}/body", .{scratch}), "f\n"); +} + +test "panes: Get of a file not there says so without asking about unsaved edits" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/body", .{n}), "unsaved\n"); + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "Get /tmp/pardes-surely-no-such-file.txt\n"); + const log = try s.read("/log"); + try std.testing.expect(has(log, "no such file")); + try std.testing.expect(!has(log, "again to discard")); + // Not asked, so the next Get of a file there is asked about, not run. + try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{n}), s.at("Get {s}/f.txt\n", .{dir})) != 0); + try s.expect(s.at("/pane/{d}/body", .{n}), "x\nunsaved\n"); +} diff --git a/test/behaviour/root_ctl.zig b/test/behaviour/root_ctl.zig index c00c3cfe..3e546237 100644 --- a/test/behaviour/root_ctl.zig +++ b/test/behaviour/root_ctl.zig @@ -92,3 +92,34 @@ test "root-ctl: Kill with a name and nothing running says so and does not fail, try s.write("/ctl", "Kill zzz\n"); try std.testing.expect(has(try s.read("/log"), "Kill: nothing running")); } + +test "root-ctl: a bad Tab width and a bad Incl directory are refused invalid, changing nothing" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const before = try s.read("/ctl"); + // Bare, Incl lists its directories. + try s.write("/ctl", "Incl\n"); + const listed = try lastIncl(&s); + for ([_][]const u8{ "Tab 0\n", "Tab 17\n", "Tab x\n" }) |line| { + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", line)); + // Run from a pane, as a tag click runs it: said invalid too. + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), line)); + try std.testing.expect(has(s.said, "invalid tab width")); + } + try s.expect("/ctl", before); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Incl rel/dir\n")); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Incl - /usr/include\n")); + try std.testing.expectEqual(@as(u16, 2), try s.refused("/ctl", "Incl /tmp/pardes-surely-no-such-dir\n")); + // The same list again: none taken. + try s.write("/ctl", "Incl\n"); + try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, try s.read("/log"), listed)); + // Bare Tab on the root ctl says the width, as from a pane. + try s.write("/ctl", "Tab\n"); +} + +/// The log's one `msg - Incl:` record, whole. +fn lastIncl(s: *Session) ![]const u8 { + const log = try s.read("/log"); + const at = std.mem.indexOf(u8, log, "\nmsg - Incl:") orelse return error.NoIncl; + return log[at .. std.mem.indexOfScalarPos(u8, log, at + 1, '\n').? + 1]; +} |
