diff options
| -rw-r--r-- | src/ninep/ctl.zig | 558 | ||||
| -rw-r--r-- | test/behaviour.zig | 1 | ||||
| -rw-r--r-- | test/behaviour/edit.zig | 12 | ||||
| -rw-r--r-- | test/behaviour/from_acme.zig | 28 | ||||
| -rw-r--r-- | test/behaviour/limits.zig | 22 | ||||
| -rw-r--r-- | test/behaviour/look_and_exec.zig | 261 | ||||
| -rw-r--r-- | test/behaviour/panes.zig | 45 | ||||
| -rw-r--r-- | test/behaviour/placement.zig | 20 | ||||
| -rw-r--r-- | test/behaviour/root_ctl.zig | 18 | ||||
| -rw-r--r-- | test/behaviour/rules.zig | 22 | ||||
| -rw-r--r-- | test/behaviour/settings.zig | 20 |
11 files changed, 458 insertions, 549 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index fc09c730..f23f2579 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -1466,118 +1466,16 @@ test "commands lists every builtin with its argument and the ctl that takes it" } -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); } @@ -1795,55 +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 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; @@ -2019,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; @@ -2081,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"); @@ -2133,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"); @@ -2560,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(); @@ -2609,23 +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 "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(); @@ -2730,61 +2443,6 @@ test "a Restore of a name in the DumpDir asks for, and logs, the whole path it r } -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 "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(); @@ -2799,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(); @@ -2841,31 +2477,6 @@ test "/index shows a name as the log does: a newline in it is \\n, a control byt try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.index), 0, 4096).bytes, "/tmp/back\\\\nslash") != null); } -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 column takes panes until their documented minimums fill it, not until none can be halved" { const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 50 }); defer p.deinit(); @@ -3335,39 +2946,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(); @@ -3478,37 +3056,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(); @@ -3522,30 +3069,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 "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(); @@ -3713,45 +3236,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(); @@ -3828,30 +3312,6 @@ test "Lspinfo's answer is the +Lsp pane it fills, as a Rename preview's is its + } -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"); diff --git a/test/behaviour.zig b/test/behaviour.zig index 72806b21..c6e1dc9d 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -8,6 +8,7 @@ test { _ = @import("behaviour/edit.zig"); _ = @import("behaviour/event.zig"); _ = @import("behaviour/flags.zig"); + _ = @import("behaviour/from_acme.zig"); _ = @import("behaviour/keys.zig"); _ = @import("behaviour/limits.zig"); _ = @import("behaviour/log.zig"); diff --git a/test/behaviour/edit.zig b/test/behaviour/edit.zig index bfb17533..83b1b464 100644 --- a/test/behaviour/edit.zig +++ b/test/behaviour/edit.zig @@ -45,3 +45,15 @@ test "edit: Edit through a pane's ctl renames every foo in one undo step, and a try s.write(ctl, "Undo"); try s.expect(body, "foo x foo y foo\n"); } + +test "edit: Edit's p and = never fail for want of room for +Errors: the text is logged instead" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + s.detach(); + try s.write("/ctl", "size 80 6\n"); + const index = try s.read("/index"); + try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Edit ,p\n"); + const log = try s.read("/log"); + try std.testing.expect(std.mem.indexOf(u8, log, " one\n") != null and std.mem.indexOf(u8, log, " two\n") != null); + try s.expect("/index", index); // the +Errors that had no room is gone +} diff --git a/test/behaviour/from_acme.zig b/test/behaviour/from_acme.zig new file mode 100644 index 00000000..62d4fc55 --- /dev/null +++ b/test/behaviour/from_acme.zig @@ -0,0 +1,28 @@ +//! docs/typ/reference.typ, Coming from acme <from-acme>. +const std = @import("std"); +const Session = @import("session").Session; + +test "from-acme: acme's words run as pardes's (Put is Save, Delete a Del that does not ask) or are refused, never run as commands" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + for ([_][]const u8{ "Snarf\n", "Indent\n", "Local\n", "Abort\n" }) |line| { + for ([_][]const u8{ s.at("/pane/{d}/ctl", .{n}), s.at("/pane/{d}/exec", .{n}) }) |path| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(path, line)); + try std.testing.expect(std.mem.indexOf(u8, s.said, "is not a pardes builtin") != null); + } + } + // Load is Restore: whatever it says, never acme's word refused. + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "Load /tmp/pardes-surely-no-dump.zon\n"); + try std.testing.expect(std.mem.indexOf(u8, s.said, "is not a pardes builtin") == null); + // Put saves, as Save does. + try s.write(s.at("/pane/{d}/name", .{n}), "/tmp/pardes-put.txt\n"); + try s.write(s.at("/pane/{d}/exec", .{n}), "Put\n"); + try std.testing.expectEqual(@as(usize, 1), s.saved); + // Delete closes an edited pane without asking. + const made = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{made}), "/tmp/pardes-delete.txt\n"); + try s.write(s.at("/pane/{d}/body", .{made}), "edited\n"); + try s.write(s.at("/pane/{d}/ctl", .{made}), "Delete\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), "/tmp/pardes-delete.txt") == null); +} diff --git a/test/behaviour/limits.zig b/test/behaviour/limits.zig index 08c864eb..d84d4490 100644 --- a/test/behaviour/limits.zig +++ b/test/behaviour/limits.zig @@ -16,3 +16,25 @@ test "limits: a builtin that needs a pane says so when every slot is taken" { try std.testing.expectEqualStrings("help: no space for a pane: 64 max", s.said); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "help: no space for a pane") != null); } + +test "limits: a ctl line over 1 MiB is refused once, EINVAL, and its tail is not run as a line of its own" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = try s.hold(s.at("/pane/{d}/ctl", .{n}), 1); + const chunk: [4096]u8 = @splat('z'); + var refused: usize = 0; + for (0..270) |_| { + const errno = try s.writeFid(ctl, &chunk); + if (errno != 0) { + refused += 1; + try std.testing.expectEqual(@as(u16, 22), errno); + } + } + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ctl, "zzz\nEdit ,d\n")); + s.clunk(ctl); + try std.testing.expectEqual(@as(usize, 1), refused); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unknown control message") == null); + // What came after the long line's newline ran. + try s.expect(s.at("/pane/{d}/body", .{n}), ""); +} diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig index 360055d2..2ce95354 100644 --- a/test/behaviour/look_and_exec.zig +++ b/test/behaviour/look_and_exec.zig @@ -433,3 +433,264 @@ test "look-and-exec: a builtin that takes no argument, written with one to an ex try std.testing.expect(std.mem.startsWith(u8, s.said, "wrong #args in control message")); }; } + +/// 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)]}); +} + +test "look-and-exec: a look opens spaced paths and locations without editing the source pane" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "two words.zig", .data = "first\nsecond\nthird\n" }); + var s = try Session.initFile(std.testing.allocator, "source stays intact\n"); + defer s.deinit(); + const path = s.at("{s}/two words.zig", .{try tmpPath(&s, &tmp)}); + const source = try s.focused(); + const ctl = try s.read(s.at("/pane/{d}/ctl", .{source})); + try s.write(s.at("/pane/{d}/dot", .{source}), "3 3"); + try s.write(s.at("/pane/{d}/look", .{source}), s.at("{s}:2:3\n", .{path})); + const opened = try s.focused(); + try std.testing.expect(opened != source); + try s.expect(s.at("/pane/{d}/name", .{opened}), s.at("{s}\n", .{path})); + try s.expect(s.at("/pane/{d}/body", .{opened}), "first\nsecond\nthird\n"); + try expectEqual(@as(u32, 8), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]); // line 2, column 3 + // The source: its text, its undo steps and its cursor as they were. + try s.expect(s.at("/pane/{d}/body", .{source}), "source stays intact\n"); + try std.testing.expectEqualStrings(ctl[0..60], (try s.read(s.at("/pane/{d}/ctl", .{source})))[0..60]); + try expectEqual(.{ @as(u32, 3), @as(u32, 3) }, try s.range(s.at("/pane/{d}/dot", .{source}))); + try s.expect("/look", s.at("{d}\n", .{opened})); + // From the root, with the source focused: the same pane, moved. + try s.write("/focus", s.at("{d}\n", .{source})); + try s.write("/look", s.at("{s}:3:2\n", .{path})); + try expectEqual(opened, try s.focused()); + try expectEqual(@as(u32, 14), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]); // line 3, column 2 + try s.expect("/look", s.at("{d}\n", .{opened})); +} + +test "look-and-exec: a look validates the whole batch before opening virtual files" { + var s = try Session.initFile(std.testing.allocator, "source\n"); + defer s.deinit(); + const source = try s.focused(); + const look = s.at("/pane/{d}/look", .{source}); + const index = try s.read("/index"); + for ([_][]const u8{ "/virtual/index\x00ignored", "/virtual/index\n/virtual/\x01index\n" }) |invalid| { + try expectEqual(@as(u16, 22), try s.refused(look, invalid)); + try s.expect("/index", index); + try expectEqual(source, try s.focused()); + try s.expect(s.at("/pane/{d}/body", .{source}), "source\n"); + } + try s.write(look, "/n/self/index\n"); + const opened = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{opened}), "/virtual/index\n"); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/body", .{opened})), "/test.txt")); + try s.expect(s.at("/pane/{d}/body", .{source}), "source\n"); +} + +test "look-and-exec: status reads the editor, and look and exec validate whole batches" { + var s = try Session.initFile(std.testing.allocator, "Msg root\n"); + defer s.deinit(); + const status = try s.read("/status"); + var rows = std.mem.tokenizeScalar(u8, status, '\n'); + try std.testing.expect(std.mem.startsWith(u8, rows.next().?, "pid ")); + try std.testing.expect(std.mem.startsWith(u8, rows.next().?, "version ")); + try std.testing.expectEqualStrings(s.at("panes {d}", .{lines(try s.read("/index"))}), rows.next().?); + try std.testing.expect(rows.next() == null); + const st = try s.stat("/status"); + try expectEqual(@as(u64, status.len), st.size); + try expectEqual(@as(u16, 0o444), st.mode & 0o777); + try expectEqual(@as(u16, 1), try s.refused("/status", "anything\n")); + try s.expect("/exec", ""); + const index = try s.read("/index"); + for ([_][]const u8{ "Msg a\x00b", "Msg x\nMsg \x01y\n" }) |invalid| { + try expectEqual(@as(u16, 22), try s.refused("/exec", invalid)); + try s.expect("/index", index); + } + // An empty write is a no-op, not a command. + try s.write("/exec", "\n \n"); + try s.expect("/exec", ""); + try s.expect("/status", status); + const n = try s.focused(); + try s.write("/exec", "Msg hello there\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} hello there\n", .{n}))); + try s.expect("/exec", s.at("{d}\n", .{n})); + try s.write("/look", "/virtual/index\n"); + const opened = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{opened}), "/virtual/index\n"); + try s.expect("/look", s.at("{d}\n", .{opened})); + try s.write("/exec", "New\n"); + const made = try s.focused(); + try std.testing.expect(made != opened); + try s.expect("/exec", s.at("{d}\n", .{made})); +} + +test "look-and-exec: a look at a file it may not read fails the write naming the file" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "locked.txt", .data = "secret\n", .flags = .{ .permissions = .fromMode(0o000) } }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const path = s.at("{s}/locked.txt", .{try tmpPath(&s, &tmp)}); + if (try s.refused("/look", s.at("{s}\n", .{path})) == 0) return error.SkipZigTest; // root reads anything + try std.testing.expectEqualStrings(s.at("look: {s}: permission denied", .{path}), s.said); +} + +test "look-and-exec: a look line's trailing colon is dropped, and a look whose address fails never leaves its file open" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const before = lines(try s.read("/index")); + // Failing addresses: nothing opened, the write still answered. + for ([_][]const u8{ s.at("{s}/a.txt:/nomatch/\n", .{dir}), s.at("{s}/a.txt:9:\n", .{dir}) }) |line| { + _ = try s.refused("/look", line); + try expectEqual(before, lines(try s.read("/index"))); + } + // `a.txt:2:3:` is `a.txt:2:3`: it opens there. + try s.write("/look", s.at("{s}/a.txt:2:3:\n", .{dir})); + try expectEqual(before + 1, lines(try s.read("/index"))); + const opened = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{opened}), s.at("{s}/a.txt\n", .{dir})); + try expectEqual(@as(u32, 6), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]); +} + +test "look-and-exec: a look that misses file:/re/ names the file, and is logged as its pane's" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "alpha\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const first = try s.focused(); + try s.write("/look", s.at("{s}/f.txt\n", .{dir})); + const f = try s.focused(); + _ = try s.refused(s.at("/pane/{d}/look", .{first}), s.at("{s}/f.txt:/zzqq/\n", .{dir})); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} look: {s}/f.txt: no match for regexp", .{ f, dir }))); +} + +test "look-and-exec: a look that goes to another pane leaves the looking pane's dot alone" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f one\nf two\n" }); + var s = try Session.initFile(std.testing.allocator, "alpha bravo\ncharlie\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const source = try s.focused(); + const other = try s.newPane(); + try s.write(s.at("/pane/{d}/body", .{other}), "one\ntwo\nthree\n"); + for ([_][]const u8{ s.at("@p{d}:2\n", .{other}), s.at("@p{d}:/three/\n", .{other}), s.at("{s}/f.txt:2\n", .{dir}) }) |line| { + try s.write(s.at("/pane/{d}/dot", .{source}), "6 11"); + try s.write(s.at("/pane/{d}/look", .{source}), line); + try std.testing.expect(try s.focused() != source); + try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(s.at("/pane/{d}/dot", .{source}))); + } +} + +test "look-and-exec: a looked word is found in its own pane, next place after the dot, wrapping, and opens no pane; LookWord list lists it in +Search" { + var s = try Session.initFile(std.testing.allocator, "foo x foo y\n"); + defer s.deinit(); + const n = try s.focused(); + const look = s.at("/pane/{d}/look", .{n}); + const index = try s.read("/index"); + for ([_]u32{ 0, 6, 0 }) |at| { + try s.write(look, "foo\n"); + try expectEqual(.{ at, at + 3 }, try s.range(s.at("/pane/{d}/dot", .{n}))); + } + try s.expect("/index", index); + try s.write("/ctl", "LookWord list\n"); + try s.write(look, "foo\n"); + try std.testing.expect(lines(try s.read("/index")) > lines(index)); + // ...and look reads back that +Search pane, where the places are. + const search = try paneNamed(&s, "/+Search"); + try std.testing.expect(has(try s.read(look), s.at("{d}\n", .{search}))); +} + +test "look-and-exec: a second LookWord list look in a directory refills its +Search rather than stacking another" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "words.txt", .data = "alpha beta\nalpha beta\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/words.txt\n", .{try tmpPath(&s, &tmp)})); + try s.write(s.at("/pane/{d}/exec", .{n}), "Get\n"); + try s.write("/ctl", "LookWord list\n"); + try s.write(s.at("/pane/{d}/look", .{n}), "alpha\n"); + const first = try paneNamed(&s, "/+Search"); + try s.write(s.at("/pane/{d}/look", .{n}), "beta\n"); + try expectEqual(@as(usize, 1), std.mem.count(u8, try s.read("/index"), "/+Search ")); + try expectEqual(first, try paneNamed(&s, "/+Search")); +} + +test "look-and-exec: `@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`" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const look = s.at("/pane/{d}/look", .{n}); + const before = try errs(&s); + try s.write(look, s.at("@p{d}:\n", .{n})); + try expectEqual(before, try errs(&s)); + try s.write(look, ":99999\n"); + try s.write(look, "/test.txt:99999\n"); + try std.testing.expect(has(try s.read("/log"), "/test.txt:99999 has no line 99999 (x2)\n")); +} + +test "look-and-exec: an exec line over the command limit fails the write and says the limit; a builtin's may be longer" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const long = "echo " ++ &@as([1024]u8, @splat('y')) ++ "\n"; + try expectEqual(@as(u16, 22), try s.refused("/exec", long)); + try std.testing.expectEqualStrings("invalid command line: a command line is at most 1024 bytes", s.said); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} exec: invalid command line: a command line is at most 1024 bytes", .{n}))); + _ = try s.refused("/exec", "echo a\x01b\n"); + try std.testing.expectEqualStrings("invalid command line: it holds a control character (or DEL) other than a tab", s.said); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} exec: invalid command line: it holds", .{n}))); + try s.write("/exec", "Msg " ++ &@as([1100]u8, @splat('z')) ++ "\n"); +} + +test "look-and-exec: a builtin that fails through exec, tagexec or a column's exec fails the write with one err and no msg" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + // Another failure first: the pane's `new` is logged after it. + _ = try s.refused("/ctl", "Unmount yyy\n"); + for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{n}), "/tagexec", s.at("/col/{d}/exec", .{try s.column(0)}) }) |path| { + try std.testing.expect(try s.refused(path, "Unmount zzz\n") != 0); + try std.testing.expect(has(s.said, "Unmount: no such mount zzz")); + } + const log = try s.read("/log"); + try std.testing.expect(!has(log, "msg ")); + // The root's exec ran at the pane, so its err and the pane's exec's are + // the same record, counted. + try expectEqual(@as(usize, 3), std.mem.count(u8, log, "Unmount: no such mount zzz")); + try std.testing.expect(has(log, "mount zzz (x2)")); + // A look miss: one err, no msg; the same miss again is that record counted. + try s.write("/look", "zzqqnotthere\n"); + try std.testing.expect(has(try s.read("/log"), "look: no match for \"zzqqnotthere\"")); + try std.testing.expect(!has(try s.read("/log"), "msg ")); + try s.write("/look", "zzqqnotthere\n"); + try std.testing.expect(has(try s.read("/log"), "look: no match for \"zzqqnotthere\" (x2)\n")); +} + +test "look-and-exec: a served-tree pane spoils no Grep, and Look . from a gone directory is ENOENT, not /virtual/" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "hit.txt", .data = "needle here\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{first}), s.at("{s}/other.txt\n", .{try tmpPath(&s, &tmp)})); + // A pane of the served tree, and one named its root, whose directory is `/`. + try s.write(s.at("/pane/{d}/name", .{try s.newPane()}), "/virtual/index\n"); + try s.write(s.at("/pane/{d}/name", .{try s.newPane()}), "/virtual\n"); + try s.write(s.at("/pane/{d}/exec", .{first}), "Grep needle\n"); + // `.` from a pane whose directory is gone. + const gone = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{gone}), "/tmp/pardes-no-such-dir-zz/f.txt\n"); + try expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/look", .{gone}), ".\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "look: /tmp/pardes-no-such-dir-zz: no such directory")); +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index 9103508c..7bcf13d0 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -657,3 +657,48 @@ test "panes: Get of a file not there says so without asking about unsaved edits" 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"); } + +test "panes: a pane's ctl takes acme's words: done by the files that replace them, or refused with why" { + var s = try Session.initFile(std.testing.allocator, "one two\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const dirty = s.at("/pane/{d}/dirty", .{n}); + try s.write(ctl, "name /tmp/pardes-acme-ctl.txt\n"); + try s.expect(s.at("/pane/{d}/name", .{n}), "/tmp/pardes-acme-ctl.txt\n"); + try s.write(ctl, "dirty\n"); + try s.expect(dirty, "1\n"); + try s.write(ctl, "clean\n"); + try s.expect(dirty, "0\n"); + try s.write(s.at("/pane/{d}/addr", .{n}), "/two/"); + try s.write(ctl, "dot=addr\nshow\nnomark\nmark\nlimit=addr\n"); + try std.testing.expectEqual(@as(u32, 4), (try s.range(s.at("/pane/{d}/dot", .{n})))[0]); + try std.testing.expectEqual(.{ @as(u32, 4), @as(u32, 7) }, try s.range(s.at("/pane/{d}/limit", .{n}))); + try s.write(ctl, "cleartag\n"); + for ([_][]const u8{ "menu\n", "nomenu\n", "dump echo hi\n", "dumpdir /tmp\n", "font Go Mono\n" }) |line| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, line)); + try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid: acme's ")); + } + // Checked whole: a refused word anywhere and nothing ran. + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "dirty\nmenu\n")); + try s.expect(dirty, "0\n"); +} + +test "panes: +Unsaved drops a pane once it has closed" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const a = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{a}), "/tmp/pardes-unsaved-a.txt\n"); + try s.write(s.at("/pane/{d}/body", .{a}), "edited\n"); + const b = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{b}), "/tmp/pardes-unsaved-b.txt\n"); + try s.write(s.at("/pane/{d}/body", .{b}), "edited\n"); + try std.testing.expect(try s.refused("/ctl", "Exit\n") != 0); + const list = s.at("/pane/{d}/body", .{(try panesNamed(&s, "/+Unsaved"))[0]}); + try std.testing.expect(has(try s.read(list), "/tmp/pardes-unsaved-a.txt: Modified")); + // a closes (discarded): its row goes. + _ = try s.refused(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try std.testing.expectEqual(@as(usize, 0), (try panesNamed(&s, "/pardes-unsaved-a.txt")).len); + try std.testing.expect(!has(try s.read(list), "/tmp/pardes-unsaved-a.txt: Modified")); +} diff --git a/test/behaviour/placement.zig b/test/behaviour/placement.zig index 079b5a5e..893cd9a4 100644 --- a/test/behaviour/placement.zig +++ b/test/behaviour/placement.zig @@ -59,3 +59,23 @@ test "placement: Placement pardes puts a new pane right under the asking one, an try s.write("/ctl", "Placement\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPlacement acme\n") != null); } + +test "placement: a look under pardes's placement, from a pane pane/new made after Delcol, opens its file in a column" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "file\n" }); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{first}), "Placement\n"); + try s.write(s.at("/pane/{d}/exec", .{first}), "Delcol\n"); + const made = try s.newPane(); + try s.write(s.at("/pane/{d}/look", .{made}), s.at("{s}/f.txt:#4\n", .{dir})); + const opened = try s.focused(); + try std.testing.expect(opened != made); + // The pane pane/new made is placed, and so is the one its look opened. + const cols = try s.columns(); + for ([_]u32{ made, opened }) |n| try std.testing.expect(std.mem.indexOf(u8, cols, s.at(":{d}", .{n})) != null or std.mem.indexOf(u8, cols, s.at(",{d}", .{n})) != null); +} diff --git a/test/behaviour/root_ctl.zig b/test/behaviour/root_ctl.zig index 3e546237..12f2b56a 100644 --- a/test/behaviour/root_ctl.zig +++ b/test/behaviour/root_ctl.zig @@ -123,3 +123,21 @@ fn lastIncl(s: *Session) ![]const u8 { 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]; } + +test "root-ctl: Joincol with no column to the right and Theme with no such theme say so; a theme's name is any case" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try std.testing.expect(try s.refused("/ctl", "Joincol\n") != 0); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Joincol: no column to the right")); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Theme drak\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "bad value in control message; like it: ")); + try std.testing.expect(has(s.said, ": dr")); + try std.testing.expect(std.mem.endsWith(u8, s.said, "\"drak\"")); + // Within the 128 bytes an Rerror carries, the quoted line kept. + try std.testing.expect(s.said.len <= 128); + // A click on the word says it in the log. + _ = try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Theme drak\n"); + try std.testing.expect(has(try s.read("/log"), "no theme \"drak\"")); + try s.write("/ctl", "Theme DRACULA\n"); + try std.testing.expect(has(try s.read("/ctl"), "\nTheme dracula\n")); +} diff --git a/test/behaviour/rules.zig b/test/behaviour/rules.zig index 4b853a40..8f5597ae 100644 --- a/test/behaviour/rules.zig +++ b/test/behaviour/rules.zig @@ -37,3 +37,25 @@ test "rules: body reads at any offset and writes append; a stat says its length, try std.testing.expect((try s.stat(body)).version != st.version); try std.testing.expectEqual(@as(u32, 0), (try s.stat(s.at("/pane/{d}/tag", .{try s.focused()}))).version); } + +test "rules: every EINVAL a write gets says why, in its err record too; DEL is a control character in a line" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + for ([_][2][]const u8{ + .{ "/exec", "echo a\x7fb\n" }, + .{ "/look", "a\x7fb\n" }, + }) |c| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(c[0], c[1])); + try std.testing.expect(std.mem.indexOf(u8, s.said, "control character") != null); + } + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/name", .{n}), "\n")); + try std.testing.expect(std.mem.indexOf(u8, s.said, "an empty name") != null); + const log = try s.hold("/log", 2); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(log, "bogus\n")); + try std.testing.expect(std.mem.indexOf(u8, s.said, "takes `follow`") != null); + s.clunk(log); + const text = try s.read("/log"); + try std.testing.expect(std.mem.indexOf(u8, text, "Invalid argument") == null); + try std.testing.expect(std.mem.indexOf(u8, text, "an empty name") != null); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig index b850a532..f3ed57de 100644 --- a/test/behaviour/settings.zig +++ b/test/behaviour/settings.zig @@ -112,3 +112,23 @@ test "settings: a bad setting value is quoted, so two different bad values log a try std.testing.expect(has(log, "takes on, off \"nope\"\n")); try std.testing.expect(!has(log, "(x2)")); } + +test "settings: a setting this frontend cannot show says GUI-only, never run as a command, and DumpDir reads back" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + if (comptime @import("pardes_config").platform != .gui) { + _ = try s.refused("/ctl", "Lift on\n"); + try std.testing.expectEqualStrings("Lift is GUI-only, invalid here \"Lift\"", s.said); + _ = try s.refused("/ctl", "GripWidth 120\n"); + try std.testing.expectEqualStrings("GripWidth is GUI-only, invalid here \"GripWidth\"", s.said); + // A builtin the GUI alone has, written to a ctl or an exec, never + // runs as a shell command. + _ = try s.refused("/ctl", "Fonts\n"); + try std.testing.expectEqualStrings("Fonts is GUI-only, invalid here \"Fonts\"", s.said); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/exec", "Fonts\n")); + try std.testing.expectEqualStrings("Fonts is GUI-only, invalid here", s.said); + } + _ = try s.refused("/ctl", "Liftt on\n"); + try std.testing.expectEqualStrings("unknown control message \"Liftt\"", s.said); + try std.testing.expect(has(try s.read("/ctl"), "\nDumpDir /")); +} |
