diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/behaviour/panes.zig | 258 | ||||
| -rw-r--r-- | test/behaviour/root_ctl.zig | 31 |
2 files changed, 289 insertions, 0 deletions
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]; +} |
