diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-03 16:59:08 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-03 17:13:25 -0300 |
| commit | 023793be1112bc35b50551580d273f4cf611b3da (patch) | |
| tree | 4167a4e0435d545dbdb8e09e4f6578491b1dd1e2 | |
| parent | d1273bbd99daf9b1855d82bf315db8ede20abde5 (diff) | |
| download | pardes-023793be1112bc35b50551580d273f4cf611b3da.tar.gz pardes-023793be1112bc35b50551580d273f4cf611b3da.zip | |
test: sixteen of src/ninep/ctl.zig's tests through the 9P tree alone, in new test/behaviour/{log,mounts,rules,settings}.zig and appended to edit, columns_and_tags, look_and_exec, panes and root_ctl: the log's msg and err records (a root ctl's message the session's, the same failure counted, a root exec's err its pane's), Mount's refusals (and nothing mounted, read from Unmount), Pager and Shell read back from the root ctl, a control character in any ctl and in any tag, a bad command inside an Edit block, a look's :addr and file:addr, the root look's file:/re/ from the dot, exec's readback, focus (a column tag's keyboard taken by Newcol), the pane ctl's and the root ctl's words; each checked to fail against a break: the (xN) count one short, msg - written as msg 0, a root exec's err left as -, a missing socket's words changed, Pager defaulting off, a bare Shell not clearing, tree.zig's ctl check skipped, DEL taken in a tag, an Edit block's bad command naming }, a look's address evaluated from 0 not the dot, no results recorded, focus leaving the header the keyboard, "not a pane control message", every setting value taken
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | src/ninep/ctl.zig | 352 | ||||
| -rw-r--r-- | test/behaviour.zig | 4 | ||||
| -rw-r--r-- | test/behaviour/columns_and_tags.zig | 11 | ||||
| -rw-r--r-- | test/behaviour/edit.zig | 10 | ||||
| -rw-r--r-- | test/behaviour/log.zig | 36 | ||||
| -rw-r--r-- | test/behaviour/look_and_exec.zig | 89 | ||||
| -rw-r--r-- | test/behaviour/mounts.zig | 25 | ||||
| -rw-r--r-- | test/behaviour/panes.zig | 66 | ||||
| -rw-r--r-- | test/behaviour/root_ctl.zig | 55 | ||||
| -rw-r--r-- | test/behaviour/rules.zig | 19 | ||||
| -rw-r--r-- | test/behaviour/settings.zig | 43 |
11 files changed, 358 insertions, 352 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index cc300476..66f30ff4 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -1415,22 +1415,6 @@ const root_status = @backingInt(tree.TopFile.status); const root_look = @backingInt(tree.TopFile.look); const root_exec = @backingInt(tree.TopFile.exec); -test "a control character in a write to any ctl refuses the whole write, and a quoted line shows a tab as a blank" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const col = layout.columnSerial(p, 0); - for ([_]u64{ @backingInt(tree.TopFile.ctl), Node.of(serialOf(p), .ctl), Node.ofCol(col, .ctl) }) |node| { - const r = wr(p, node, "Wrap off\nbo\x01gus\n"); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expectEqualStrings(e_control, r.reply.ename); - } - const quoted = wr(p, Node.of(serialOf(p), .ctl), "bogus\tword\n"); - try testing.expectEqual(E.INVAL, quoted.errno()); - // The offending word alone is quoted: a tab ends it. - try testing.expect(std.mem.indexOf(u8, quoted.reply.ename, "\"bogus\"") != null); - try testing.expect(std.mem.indexOfScalar(u8, quoted.reply.ename, '\t') == null); -} - test "pane ctl read is acme's fields -- index's five, width in cells, font, tab width, undo, redo -- then whether it is current" { const gpa = testing.allocator; const p = try withFile(gpa, "x\n"); @@ -1503,138 +1487,6 @@ test "Undo and Redo on a pane's ctl step its body through its edits" { try testing.expect(th.logHas(p, "Undo: nothing to undo (x4)\n")); } -test "focus reads the pane with the keyboard, and a serial written to it moves the keyboard there" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const first = serialOf(p); - const second = try th.newPane(p); - const focus = @backingInt(tree.TopFile.focus); - var line: [16]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&line, "{d}\n", .{second}), rd(p, focus, 0, 64).bytes); - try testing.expectEqual(@as(u64, rd(p, focus, 0, 64).bytes.len), call(p, .{ .tag = 1, .op = .getattr, .node = focus }).reply.attr.size); - try testing.expect(std.mem.indexOf(u8, rd(p, Node.of(first, .ctl), 0, 4096).bytes, " notcurrent ") != null); - - // A header has the keyboard: no pane does, and a write takes it back. - tagline.enterHeader(p, null); - try testing.expectEqualStrings("", rd(p, focus, 0, 64).bytes); - try testing.expectEqual(@as(u64, 0), call(p, .{ .tag = 1, .op = .getattr, .node = focus }).reply.attr.size); - try testing.expect(std.mem.indexOf(u8, rd(p, Node.of(second, .ctl), 0, 4096).bytes, " notcurrent ") != null); - try testing.expectEqual(Status.ok, wr(p, focus, try std.fmt.bufPrint(&line, "{d}\n", .{first})).reply.status); - try testing.expect(!p.header_focus); - try testing.expectEqual(p.paneBySerial(first).?, p.active); - try testing.expect(std.mem.indexOf(u8, rd(p, Node.of(first, .ctl), 0, 4096).bytes, " current ") != null); - try testing.expect(std.mem.indexOf(u8, rd(p, Node.of(second, .ctl), 0, 4096).bytes, " notcurrent ") != null); - - try testing.expectEqualStrings(e_no_pane, wr(p, focus, "99999\n").reply.ename); - try testing.expectEqual(E.NOENT, wr(p, focus, "99999").errno()); - for ([_][]const u8{ "", "two", "-1", "1 2" }) |bad| try testing.expectEqual(E.INVAL, wr(p, focus, bad).errno()); - try testing.expectEqual(p.paneBySerial(first).?, p.active); - - // Focus gives the keyboard only: a folded pane stays folded. - const folded = p.paneBySerial(second).?; - layout.toggleCollapse(p, folded); - try testing.expect(p.panes[folded].?.collapsed); - try testing.expectEqual(Status.ok, wr(p, focus, try std.fmt.bufPrint(&line, "{d}", .{second})).reply.status); - try testing.expectEqual(folded, p.active); - try testing.expect(p.panes[folded].?.collapsed); -} - -test "the pane ctl takes acme's verbs and the pane's builtins, and refuses the rest in Plan 9's words" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - // A builtin is its own capitalised word: `GET` and `DEL` are none; - // acme's own words are done or refused as acme's (the test below). - for ([_][]const u8{ "lock x", "bogus", "DEL", "GET", "get x", "look /tmp", "exec Del" }) |bad| { - const refused = wr(p, ctl_node, bad); - try testing.expectEqual(E.INVAL, refused.errno()); - var want: [64]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "unknown control message \"{s}\"", .{firstWord(bad)}), refused.reply.ename); - } - try testing.expectEqualStrings("not a window control message \"Newcol\": write it to /ctl", wr(p, ctl_node, "Newcol").reply.ename); - try testing.expectEqualStrings("not a window control message \"Verbose\": write it to /ctl", wr(p, ctl_node, "Verbose off").reply.ename); - try testing.expectEqualStrings("wrong #args in control message \"Collapse\"", wr(p, ctl_node, "Collapse now").reply.ename); - // Checked whole first: a bad line anywhere and nothing ran. - try testing.expectEqual(E.INVAL, wr(p, ctl_node, "Msg first\nbogus").errno()); - try testing.expect(p.panes[0].?.msg_len == 0 or !std.mem.eql(u8, p.panes[0].?.msg[0..p.panes[0].?.msg_len], "first")); - - // A pane's builtin runs at that pane. - try testing.expectEqual(Status.ok, wr(p, ctl_node, "Msg from ctl\n").reply.status); - const pane = p.panes[p.paneBySerial(serial).?].?; - try testing.expectEqualStrings("from ctl", pane.msg[0..pane.msg_len]); - // One that means nothing bare is refused before anything runs. - try testing.expectEqualStrings("wrong #args in control message \"Find\"", wr(p, ctl_node, "Find").reply.ename); - try testing.expectEqual(E.INVAL, wr(p, ctl_node, "Msg first\nMsg").errno()); - try testing.expect(!std.mem.eql(u8, pane.msg[0..pane.msg_len], "first")); - try testing.expect(pane.prompt == .none); - try testing.expect(!p.fs.no_prompt); - // Save with no name on a scratch would ask for one: refused, not asked. - const other = try th.newPane(p); - try testing.expectEqualStrings(e_prompt ++ " \"Save\"", wr(p, Node.of(other, .ctl), "Save").reply.ename); - try testing.expect(p.panes[p.paneBySerial(other).?].?.prompt == .none); - // A line after the one that closed the pane has nowhere to run. - try testing.expectEqual(E.NOENT, wr(p, Node.of(other, .ctl), "Del\nMsg after").errno()); - try testing.expect(p.paneBySerial(other) == null); -} - -test "the root ctl reads the settings as a write takes them, and takes the session's builtins" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - const first = rd(p, root_ctl, 0, 1 << 16); - try testing.expectEqual(Status.ok, first.reply.status); - const settings = try gpa.dupe(u8, first.bytes); - defer gpa.free(settings); - try testing.expectEqual(@as(u64, settings.len), call(p, .{ .tag = 1, .op = .getattr, .node = root_ctl }).reply.attr.size); - try testing.expect(std.mem.indexOf(u8, settings, "Verbose on\n") != null); - try testing.expect(std.mem.indexOf(u8, settings, "LocationsConfig ") != null); - try testing.expect(std.mem.indexOf(u8, settings, "CallstackDepth 8\n") != null); - try testing.expectEqual(Status.ok, wr(p, root_ctl, "CallstackDepth").reply.status); - // Settings only: nothing /status or the DumpConfig window says. - try testing.expect(std.mem.indexOf(u8, settings, "Platform") == null); - try testing.expect(std.mem.indexOf(u8, settings, "requested") == null); - - // Every line written back is taken, and changes nothing. - try testing.expectEqual(Status.ok, wr(p, root_ctl, settings).reply.status); - try testing.expectEqualStrings(settings, rd(p, root_ctl, 0, 1 << 16).bytes); - - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Verbose off\n").reply.status); - try testing.expect(!p.settings.verbose); - try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 1 << 16).bytes, "Verbose off\n") != null); - const columns = p.ncol; - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Newcol\n").reply.status); - try testing.expectEqual(columns + 1, p.ncol); - - try testing.expectEqualStrings("not a session control message \"Del\": write it to pane/<n>/ctl", wr(p, root_ctl, "Del").reply.ename); - try testing.expectEqualStrings("unknown control message \"Nonsense\"", wr(p, root_ctl, "Nonsense 1").reply.ename); - try testing.expectEqualStrings("bad value in control message; takes on, off \"maybe\"", wr(p, root_ctl, "Verbose maybe").reply.ename); - try testing.expectEqualStrings("bad value in control message; takes acme, pardes \"east\"", wr(p, root_ctl, "Placement east").reply.ename); - // A number's refusal names its range, a path's what it takes. - try testing.expectEqualStrings("bad value in control message; takes 0-100 (a percentage) \"200\"", wr(p, root_ctl, "InactiveDim 200").reply.ename); - try testing.expectEqualStrings("bad value in control message; takes 0-60000 (milliseconds) \"x\"", wr(p, root_ctl, "MessageLinger x").reply.ename); - for (config.Runtime.settings) |setting| if (setting.action != .theme) - try testing.expect(config.Runtime.takes(setting.action) != null); - const no_theme = wr(p, root_ctl, "Theme no-such-theme").reply.ename; - try testing.expectStringStartsWith(no_theme, "bad value in control message; like it: n"); - try testing.expect(std.mem.endsWith(u8, no_theme, "; Themes lists all \"no-such-theme\"")); - try testing.expectEqualStrings("wrong #args in control message \"Newcol\"", wr(p, root_ctl, "Newcol 2").reply.ename); - try testing.expectEqualStrings("wrong #args in control message \"Theme\"", wr(p, root_ctl, "Theme").reply.ename); - // A bare required word fails the check, so the line before never runs. - try testing.expectEqual(E.INVAL, wr(p, root_ctl, "Verbose on\nMount").errno()); - try testing.expect(!p.settings.verbose); - // A builtin that fails as it runs fails the write, quoting its error and - // its line; the lines before it have taken effect, as in acme. - const failed = wr(p, root_ctl, "Verbose on\nMount x /tmp/pardes-no-such-sock-zz\nVerbose off"); - try testing.expectEqual(E.NOENT, failed.errno()); - try testing.expectStringStartsWith(failed.reply.ename, "Mount x: /tmp/pardes-no-such-sock-zz: no such socket"); - try testing.expect(p.settings.verbose); - try testing.expect(!p.fs.no_prompt); - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Verbose off").reply.status); -} - test "a second lock is refused until the holder unlocks or closes, and binds nobody else" { const p = try withFile(testing.allocator, "one\ntwo\n"); defer p.deinit(); @@ -1718,81 +1570,6 @@ test "dirty goes back to 0 when the text is the saved text again" { try testing.expectEqualStrings("1\n", rd(p, dirty, 0, 8).bytes); } -test "a message from a root ctl write is the session's in the log" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - p.update(.tick); // the pane's `new` is logged before the log is opened - while (p.nextEffect()) |_| {} - const log = @backingInt(tree.TopFile.log); - const f = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle; - const frozen = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = f, .size = 8192 }).bytes.len; - _ = call(p, .{ .tag = 3, .op = .write, .node = log, .handle = f, .data = "follow" }); - try testing.expectEqual(Status.ok, wr(p, @backingInt(tree.TopFile.ctl), "Kill\n").reply.status); - // What it said, the session's; its own words name it, so its Verbose - // announcement gives way to them. - try testing.expectEqualStrings("msg - Kill: nothing running\n", call(p, .{ .tag = 5, .op = .read, .node = log, .handle = f, .off = frozen, .size = 4096 }).bytes); -} - -test "a look takes acme's :addr and file:addr, and a miss changes nothing" { - const gpa = testing.allocator; - const p = try withFile(gpa, "foo x foo y\nbar\n"); - defer p.deinit(); - const serial = serialOf(p); - const look_node = Node.of(serial, .look); - const dot = Node.of(serial, .dot); - // :addr is an address in this pane, evaluated from its dot. - try testing.expectEqual(Status.ok, wr(p, look_node, ":/y/\n").reply.status); - try testing.expectEqualStrings(" 10 11 ", rd(p, dot, 0, 64).bytes); - try testing.expectEqual(Status.ok, wr(p, look_node, ":#4\n").reply.status); - try testing.expectEqualStrings(" 4 4 ", rd(p, dot, 0, 64).bytes); - try testing.expectEqual(Status.ok, wr(p, look_node, ":/foo/\n").reply.status); // from #4: the second foo - try testing.expectEqualStrings(" 6 9 ", rd(p, dot, 0, 64).bytes); - // file:addr names the pane's own file here. - var line: [256]u8 = undefined; - const path = p.panes[0].?.file.?.path; - try testing.expectEqual(Status.ok, wr(p, look_node, try std.fmt.bufPrint(&line, "{s}:/bar/\n", .{path})).reply.status); - try testing.expectEqualStrings(" 12 15 ", rd(p, dot, 0, 64).bytes); - // A miss: no buffer, the selection kept, and it says so. - const panes_before = p.panes.len - std.mem.count(?*pardes.Pane, &p.panes, &.{null}); - try testing.expectEqual(Status.ok, wr(p, look_node, "zzqq\n").reply.status); - try testing.expectEqual(panes_before, p.panes.len - std.mem.count(?*pardes.Pane, &p.panes, &.{null})); - try testing.expectEqualStrings(" 12 15 ", rd(p, dot, 0, 64).bytes); - try testing.expectStringStartsWith(p.panes[0].?.msg[0..p.panes[0].?.msg_len], "Look: no match"); - // A hit answers the pane the text is selected in, not its hits buffer. - try testing.expectEqual(Status.ok, wr(p, look_node, "foo\n").reply.status); - var own: [16]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&own, "{d}\n", .{serial}), rd(p, look_node, 0, 64).bytes); - // A pattern with a blank in it; 0/re/ searches from the top, whatever dot is. - try testing.expectEqual(Status.ok, wr(p, look_node, ":/foo y/\n").reply.status); - try testing.expectEqualStrings(" 6 11 ", rd(p, dot, 0, 64).bytes); - try testing.expectEqual(Status.ok, wr(p, look_node, try std.fmt.bufPrint(&line, "{s}:0/foo/\n", .{path})).reply.status); - try testing.expectEqualStrings(" 0 3 ", rd(p, dot, 0, 64).bytes); - // file:N selects the line; a line past the end moves nothing and reads back empty. - try testing.expectEqual(Status.ok, wr(p, look_node, try std.fmt.bufPrint(&line, "{s}:2\n", .{path})).reply.status); - try testing.expectEqualStrings(" 12 16 ", rd(p, dot, 0, 64).bytes); - try testing.expectEqual(Status.ok, wr(p, look_node, try std.fmt.bufPrint(&line, "{s}:99\n", .{path})).reply.status); - try testing.expectEqualStrings(" 12 16 ", rd(p, dot, 0, 64).bytes); - try testing.expectEqualStrings("", rd(p, look_node, 0, 64).bytes); - try testing.expectStringStartsWith(p.panes[0].?.msg[0..p.panes[0].?.msg_len], "Look: "); -} - -test "the root look's file:/re/ goes on from the dot, and a miss leaves the dot as it was" { - const gpa = testing.allocator; - const p = try withFile(gpa, "foo x foo y\nfoo\n"); - defer p.deinit(); - const dot = Node.of(serialOf(p), .dot); - const root = @backingInt(tree.TopFile.look); - var line: [256]u8 = undefined; - const path = p.panes[0].?.file.?.path; - const again = try std.fmt.bufPrint(&line, "{s}:/foo/\n", .{path}); - for ([_][]const u8{ " 0 3 ", " 6 9 ", " 12 15 ", " 0 3 " }) |want| { - try testing.expectEqual(Status.ok, wr(p, root, again).reply.status); - try testing.expectEqualStrings(want, rd(p, dot, 0, 64).bytes); - } - try testing.expectEqual(Status.ok, wr(p, root, try std.fmt.bufPrint(&line, "{s}:/zzqq/\n", .{path})).reply.status); - try testing.expectEqualStrings(" 0 3 ", rd(p, dot, 0, 64).bytes); -} - test "a look's miss repeats the path whole up to 256 bytes, then gives up its middle to an ellipsis" { const gpa = testing.allocator; const p = try withFile(gpa, "x\n"); @@ -1807,31 +1584,6 @@ test "a look's miss repeats the path whole up to 256 bytes, then gives up its mi try testing.expect(th.logHas(p, "look: no match for \"" ++ std.mem.asBytes(&@as([63][2]u8, @splat("é".*))) ++ "…" ++ std.mem.asBytes(&@as([63][2]u8, @splat("é".*))) ++ "\"\n")); } -test "exec runs a builtin at the pane and records the pane it acted on" { - const gpa = testing.allocator; - const p = try withFile(gpa, "Msg fs-ran\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .exec), "Msg from exec\n").reply.status); - try testing.expectEqualStrings("from exec", pane.msg[0..pane.msg_len]); - var expected: [16]u8 = undefined; - const own = try std.fmt.bufPrint(&expected, "{d}\n", .{serial}); - try testing.expectEqualStrings(own, rd(p, root_exec, 0, 4096).bytes); - // Either command file reads back what the last one did. - try testing.expectEqualStrings(own, rd(p, Node.of(serial, .look), 0, 4096).bytes); - try testing.expectEqual(@as(u64, own.len), call(p, .{ .tag = 1, .op = .getattr, .node = root_exec }).reply.attr.size); - - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .exec), "New\n").reply.status); - const made = p.panes[p.active].?; - try testing.expect(made.serial != serial); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{made.serial}), rd(p, root_exec, 0, 4096).bytes); - - try testing.expectEqual(Status.ok, wr(p, Node.of(made.serial, .exec), "Del\n").reply.status); - try testing.expect(p.paneBySerial(made.serial) == null); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{made.serial}), rd(p, root_exec, 0, 4096).bytes); -} - test "status reads the editor, and look and exec validate whole batches" { const gpa = testing.allocator; const p = try withFile(gpa, "Msg root\n"); @@ -2335,20 +2087,6 @@ test "Save path of a file pane logs a save record naming that path" { try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "save {d} /tmp/pardes-save-copy.txt\n", .{serial}))); } -test "Mount with no dial is wrong #args, and a dial that is no address says bad dial address" { - if (comptime !pardes.hosted) return; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - const bare = wr(p, root_ctl, "Mount work\n"); - try testing.expectEqual(E.INVAL, bare.errno()); - try testing.expectStringStartsWith(bare.reply.ename, "wrong #args"); - const bad = wr(p, root_ctl, "Mount work tcp!nohost\n"); - try testing.expectEqual(E.INVAL, bad.errno()); - try testing.expectStringStartsWith(bad.reply.ename, "Mount: bad dial address: tcp!nohost"); - try testing.expectEqual(@as(usize, 0), p.fs.mounts.items.len); -} - test "an address looked at in an image pane names the image, never an empty subject" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2463,17 +2201,6 @@ test "a look at a file it may not read fails the write naming the file" { try testing.expectEqualStrings(expected, refused.reply.ename); } -test "Pager is a setting of the root ctl: pardes by default, off taken, anything else refused with its choices" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 1 << 16).bytes, "Pager pardes\n") != null); - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Pager off\n").reply.status); - try testing.expectEqual(config.Runtime.Pager.off, p.settings.pager); - try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 1 << 16).bytes, "Pager off\n") != null); - try testing.expectEqualStrings("bad value in control message; takes pardes, off \"less\"", wr(p, root_ctl, "Pager less\n").reply.ename); -} - test "a bad setting value is quoted, so two different bad values log as two records, never one counted twice" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2921,42 +2648,6 @@ test "a setting this frontend cannot show says GUI-only, and DumpDir reads back } } -test "Shell refuses a path that is no executable, and bare it goes back to the default" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - const refused = wr(p, root_ctl, "Shell /nonexistent/zzsh\n"); - try testing.expectEqual(Status.err, refused.reply.status); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, "Shell: shell \"/nonexistent/zzsh\" not found") != null); - try testing.expect(th.logHas(p, "shell \"/nonexistent/zzsh\" not found")); - // Its words say a directory, which a mount reads as EISDIR too. - try testing.expectEqual(E.ISDIR, wr(p, root_ctl, "Shell /etc\n").errno()); - try testing.expect(th.logHas(p, "Shell: not a shell: /etc is a directory")); - try testing.expectEqualStrings("", p.settings.shell.requested.get()); - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Shell /bin/sh\n").reply.status); - try testing.expectEqualStrings("/bin/sh", p.settings.shell.requested.get()); - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Shell\n").reply.status); - try testing.expectEqualStrings("", p.settings.shell.requested.get()); - try testing.expect(th.logHas(p, "Shell: the default again, ")); - var want: [300]u8 = undefined; - try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 8192).bytes, try std.fmt.bufPrint(&want, "Shell {s}\n", .{config.defaultShell()})) != null); -} - -test "a builtin that fails a ctl write logs only its err, so the same failure again is counted" { - 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 ctl_node = @backingInt(tree.TopFile.ctl); - for (0..3) |_| try testing.expectEqual(Status.err, wr(p, ctl_node, "Unmount zzz\n").reply.status); - 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.endsWith(u8, text, "err - ctl: Unmount: no such mount zzz (x3)\n")); - try testing.expect(std.mem.indexOf(u8, text, "msg - Unmount") == null); - _ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h }); -} - 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(); @@ -3011,17 +2702,6 @@ test "Mount dials its peer and fails, mounting nothing, when none answers; a loo try testing.expect(th.logHas(p, "look: /n/gone/f.txt: dial failed: no answer")); } -test "a root exec that fails logs its err against the pane it ran at" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const f = &p.panes[0].?.file.?; - f.saved_revision = f.revision -% 1; - f.saved_hash = 0; - try testing.expect(wr(p, root_exec, "Exit\n").errno() != 0); - var want: [32]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "err {d} exec: ", .{serialOf(p)}))); -} - test "Tty in a directory that is not there fails and makes no pane; a shell that never started answers a run" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -3797,17 +3477,6 @@ test "a served-tree pane spoils no Grep, and Look . from a gone directory is ENO try testing.expectStringStartsWith(looked.reply.ename, "look: /tmp/pardes-no-such-dir-zz: no such directory"); } -test "a tag write refuses NUL and the other control characters" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - for ([_]u64{ @backingInt(tree.TopFile.tag), tree.Node.ofCol(pardes.layout.columnSerial(p, 0), .tag), Node.of(serialOf(p), .tag) }) |node| { - const refused = wr(p, node, " a\x00b"); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectStringStartsWith(refused.reply.ename, "invalid tag text"); - try testing.expectEqual(E.INVAL, wr(p, node, " \x7f").errno()); - } -} - test "a malformed address is bad address syntax; on a pane with no text any address is refused as nothing to address" { const p = try th.withTerm(testing.allocator); defer p.deinit(); @@ -4603,16 +4272,6 @@ test "@p<terminal>:<address> takes any address over the terminal's lines, select try testing.expectEqualStrings("", rd(p, root, 0, 64).bytes); } -test "a bad command inside an Edit block written whole is named, not the } after it" { - const p = try withFile(testing.allocator, "abc\n"); - defer p.deinit(); - const r = wr(p, Node.of(serialOf(p), .ctl), "Edit {\n,s/a/b/\nzq\n}\n"); - try testing.expectEqual(Status.err, r.reply.status); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "\"}\"") == null); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "z") != null); - try testing.expectEqualStrings("abc\n", p.panes[0].?.file.?.content); -} - test "a refused Delcol changes nothing: no pane, no focus" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -5033,17 +4692,6 @@ test "a command or a terminal refused for room is refused before anything is log } } -test "a Mount of a socket path that is not there says no such socket, ENOENT" { - if (comptime !pardes.hosted) return; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const r = wr(p, @backingInt(tree.TopFile.ctl), "Mount peer unix!/tmp/pardes-surely-no-socket\n"); - try testing.expectEqual(E.NOENT, r.errno()); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "no such socket") != null); - try testing.expectEqual(@as(usize, 0), p.fs.mounts.items.len); - try testing.expectEqual(E.INVAL, wr(p, @backingInt(tree.TopFile.ctl), "Mount peer rel/x.sock\n").errno()); -} - test "the init file reads past a BOM, and every line it fails is a notice and an err, never silent" { const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .startup_config = "\xEF\xBB\xBFTheme dusk\nnosuchword 1\nTheme nosuchtheme\n# a comment\nInactiveDim 30 # not a comment, a bad value\n" }); defer p.deinit(); diff --git a/test/behaviour.zig b/test/behaviour.zig index 32138f94..67a22668 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -5,8 +5,12 @@ test { _ = @import("behaviour/columns_and_tags.zig"); _ = @import("behaviour/edit.zig"); + _ = @import("behaviour/log.zig"); _ = @import("behaviour/look_and_exec.zig"); + _ = @import("behaviour/mounts.zig"); _ = @import("behaviour/other_files.zig"); _ = @import("behaviour/panes.zig"); _ = @import("behaviour/root_ctl.zig"); + _ = @import("behaviour/rules.zig"); + _ = @import("behaviour/settings.zig"); } diff --git a/test/behaviour/columns_and_tags.zig b/test/behaviour/columns_and_tags.zig index 25ba4c64..71cff4b2 100644 --- a/test/behaviour/columns_and_tags.zig +++ b/test/behaviour/columns_and_tags.zig @@ -31,3 +31,14 @@ test "columns-and-tags: a look after Newcol fills the empty column the keyboard try std.testing.expect(std.mem.indexOf(u8, col, " full ") != null); try std.testing.expect(std.mem.endsWith(u8, col, s.at(" {d}", .{try s.focused()}))); } + +test "columns-and-tags: a tag write refuses NUL and the other control characters, the workspace's, a column's and a pane's" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const col = std.mem.sliceTo(try s.read("/layout"), ' '); + for ([_][]const u8{ "/tag", s.at("/col/{s}/tag", .{col}), s.at("/pane/{d}/tag", .{try s.focused()}) }) |tag| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(tag, " a\x00b")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid tag text")); + try std.testing.expectEqual(@as(u16, 22), try s.refused(tag, " \x7f")); + } +} diff --git a/test/behaviour/edit.zig b/test/behaviour/edit.zig index e95471c6..3d1a731e 100644 --- a/test/behaviour/edit.zig +++ b/test/behaviour/edit.zig @@ -19,3 +19,13 @@ test "edit: a write of an Edit block carries it whole, to a pane's ctl, the root try s.expect(body, "<foo] x\n<foo] y\nend\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unmatched") != null); } + +test "edit: a bad command inside an Edit block written whole is named, not the } after it" { + var s = try Session.initFile(std.testing.allocator, "abc\n"); + defer s.deinit(); + const n = try s.focused(); + try std.testing.expect(try s.refused(s.at("/pane/{d}/ctl", .{n}), "Edit {\n,s/a/b/\nzq\n}\n") != 0); + try std.testing.expect(std.mem.indexOf(u8, s.said, "\"}\"") == null); + try std.testing.expect(std.mem.indexOf(u8, s.said, "z") != null); + try s.expect(s.at("/pane/{d}/body", .{n}), "abc\n"); +} diff --git a/test/behaviour/log.zig b/test/behaviour/log.zig new file mode 100644 index 00000000..21bccdf0 --- /dev/null +++ b/test/behaviour/log.zig @@ -0,0 +1,36 @@ +//! docs/typ/reference.typ, The log <log>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "log: a message from a root ctl write is the session's, and its own words stand for its Verbose announcement" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Kill\n"); + const log = try s.read("/log"); + try std.testing.expect(has(log, "\nmsg - Kill: nothing running\n")); + try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, log, "Kill")); +} + +test "log: a builtin that fails a ctl write logs only its err, so the same failure again is counted" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + // Another failure first: the pane's `new` is logged after it. + _ = try s.refused("/ctl", "Unmount yyy\n"); + for (0..3) |_| try std.testing.expect(try s.refused("/ctl", "Unmount zzz\n") != 0); + const log = try s.read("/log"); + try std.testing.expect(std.mem.endsWith(u8, log, "err - ctl: Unmount: no such mount zzz (x3)\n")); + try std.testing.expect(!has(log, "msg - Unmount")); +} + +test "log: a root exec that fails logs its err against the pane it ran at" { + 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}/body", .{n}), "unsaved\n"); + try std.testing.expect(try s.refused("/exec", "Exit\n") != 0); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} exec: ", .{n}))); +} diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig index 2ff4345b..f9b49ae0 100644 --- a/test/behaviour/look_and_exec.zig +++ b/test/behaviour/look_and_exec.zig @@ -36,3 +36,92 @@ test "look-and-exec: a bare :N or :N:M look addresses the pane itself, from its try s.write(s.at("/pane/{d}/event", .{n}), "ML0 0 0 2 :1\n"); try expectEqual(.{ @as(u32, 0), @as(u32, 6) }, try s.range(dot)); } + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +fn lines(text: []const u8) usize { + return std.mem.count(u8, text, "\n"); +} + +/// Whether /index lists the pane `serial`. +fn listed(s: *Session, serial: u32) !bool { + var it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (it.next()) |line| { + var words = std.mem.tokenizeScalar(u8, line, ' '); + if (std.mem.eql(u8, words.next() orelse "", s.at("{d}", .{serial}))) return true; + } + return false; +} + +test "look-and-exec: a look takes acme's :addr and file:addr, and a miss changes nothing" { + var s = try Session.initFile(std.testing.allocator, "foo x foo y\nbar\n"); + defer s.deinit(); + const n = try s.focused(); + const look = s.at("/pane/{d}/look", .{n}); + const dot = s.at("/pane/{d}/dot", .{n}); + const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + // :addr is an address in this pane, evaluated from its dot. + try s.write(look, ":/y/\n"); + try expectEqual(.{ @as(u32, 10), @as(u32, 11) }, try s.range(dot)); + try s.write(look, ":#4\n"); + try expectEqual(.{ @as(u32, 4), @as(u32, 4) }, try s.range(dot)); + try s.write(look, ":/foo/\n"); // from #4: the second foo + try expectEqual(.{ @as(u32, 6), @as(u32, 9) }, try s.range(dot)); + // file:addr names the pane's own file here. + try s.write(look, s.at("{s}:/bar/\n", .{path})); + try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot)); + // A miss: no pane made, the selection kept, and it says so. + const panes = lines(try s.read("/index")); + try s.write(look, "zzqq\n"); + try expectEqual(panes, lines(try s.read("/index"))); + try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot)); + try std.testing.expect(has(try s.read("/log"), "no match for \"zzqq\"")); + // A hit answers the pane the text is selected in, not a hits pane. + try s.write(look, "foo\n"); + try s.expect(look, s.at("{d}\n", .{n})); + // A pattern with a blank in it; 0/re/ searches from the top, whatever dot is. + try s.write(look, ":/foo y/\n"); + try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(dot)); + try s.write(look, s.at("{s}:0/foo/\n", .{path})); + try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot)); + // file:N selects the line; a line past the end moves nothing and reads back empty. + try s.write(look, s.at("{s}:2\n", .{path})); + try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot)); + try s.write(look, s.at("{s}:99\n", .{path})); + try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot)); + try s.expect(look, ""); +} + +test "look-and-exec: the root look's file:/re/ goes on from the dot, and a miss leaves the dot as it was" { + var s = try Session.initFile(std.testing.allocator, "foo x foo y\nfoo\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + for ([_]u32{ 0, 6, 12, 0 }) |at| { + try s.write("/look", s.at("{s}:/foo/\n", .{path})); + try expectEqual(.{ at, at + 3 }, try s.range(dot)); + } + try s.write("/look", s.at("{s}:/zzqq/\n", .{path})); + try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot)); +} + +test "look-and-exec: exec runs a builtin at the pane, and look and exec read back the pane it acted on" { + var s = try Session.initFile(std.testing.allocator, "Msg fs-ran\n"); + defer s.deinit(); + const n = try s.focused(); + const own = s.at("{d}\n", .{n}); + try s.write(s.at("/pane/{d}/exec", .{n}), "Msg from exec\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from exec\n", .{n}))); + try s.expect("/exec", own); + try s.expect(s.at("/pane/{d}/look", .{n}), own); + try s.write(s.at("/pane/{d}/exec", .{n}), "New\n"); + const made = try s.focused(); + try std.testing.expect(made != n); + try s.expect("/exec", s.at("{d}\n", .{made})); + try s.write(s.at("/pane/{d}/exec", .{made}), "Del\n"); + try std.testing.expect(try listed(&s, n) and !try listed(&s, made)); + try s.expect("/exec", s.at("{d}\n", .{made})); +} diff --git a/test/behaviour/mounts.zig b/test/behaviour/mounts.zig new file mode 100644 index 00000000..57685a38 --- /dev/null +++ b/test/behaviour/mounts.zig @@ -0,0 +1,25 @@ +//! docs/typ/reference.typ, Paths and mounts <mounts>. +const std = @import("std"); +const Session = @import("session").Session; +const expectEqual = std.testing.expectEqual; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "mounts: Mount with no dial is wrong #args, any other x!y a bad dial address, a socket not there no such socket, and none mounts anything" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try expectEqual(@as(u16, 22), try s.refused("/ctl", "Mount work\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "wrong #args")); + try expectEqual(@as(u16, 22), try s.refused("/ctl", "Mount work tcp!nohost\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Mount: bad dial address: tcp!nohost")); + try expectEqual(@as(u16, 2), try s.refused("/ctl", "Mount peer unix!/tmp/pardes-surely-no-socket\n")); + try std.testing.expect(has(s.said, "no such socket")); + try expectEqual(@as(u16, 22), try s.refused("/ctl", "Mount peer rel/x.sock\n")); + // Nothing was mounted under either name. + for ([_][]const u8{ "Unmount work\n", "Unmount peer\n" }) |line| { + try expectEqual(@as(u16, 2), try s.refused("/ctl", line)); + try std.testing.expect(has(s.said, "no such mount")); + } +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index 01820a07..d48ff812 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -49,3 +49,69 @@ test "panes: Putall saves every pane with unsaved edits to a file, one each, and try s.write("/ctl", "Putall\n"); try std.testing.expectEqual(@as(usize, 1), s.saved); } + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "panes: focus reads the pane with the keyboard, and a serial written to it moves the keyboard there, and only that" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + const second = try s.newPane(); + try s.expect("/focus", s.at("{d}\n", .{second})); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " notcurrent ")); + // A column's tag has the keyboard: no pane does, and a write takes it back. + try s.write("/ctl", "Newcol\n"); + try s.expect("/focus", ""); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent ")); + try s.write("/focus", s.at("{d}\n", .{first})); + try s.expect("/focus", s.at("{d}\n", .{first})); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " current ")); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent ")); + try std.testing.expectEqual(@as(u16, 2), try s.refused("/focus", "99999\n")); + try std.testing.expectEqualStrings("no such pane", s.said); + for ([_][]const u8{ "", "two", "-1", "1 2" }) |bad| try std.testing.expectEqual(@as(u16, 22), try s.refused("/focus", bad)); + try s.expect("/focus", s.at("{d}\n", .{first})); + // Focus gives the keyboard only: a folded pane stays folded. + try s.write(s.at("/pane/{d}/ctl", .{second}), "Collapse\n"); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed ")); + try s.write("/focus", s.at("{d}", .{second})); + try s.expect("/focus", s.at("{d}\n", .{second})); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed ")); +} + +test "panes: the pane ctl takes acme's verbs and the pane's builtins, and refuses the rest in Plan 9's words" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + // A builtin is its own capitalised word: `GET` and `DEL` are none. + for ([_][]const u8{ "lock x", "bogus", "DEL", "GET", "get x", "look /tmp", "exec Del" }) |bad| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, s.at("{s}\n", .{bad}))); + try std.testing.expectEqualStrings(s.at("unknown control message \"{s}\"", .{std.mem.sliceTo(bad, ' ')}), s.said); + } + _ = try s.refused(ctl, "Newcol\n"); + try std.testing.expectEqualStrings("not a window control message \"Newcol\": write it to /ctl", s.said); + _ = try s.refused(ctl, "Verbose off\n"); + try std.testing.expectEqualStrings("not a window control message \"Verbose\": write it to /ctl", s.said); + _ = try s.refused(ctl, "Collapse now\n"); + try std.testing.expectEqualStrings("wrong #args in control message \"Collapse\"", s.said); + // Checked whole first: a bad line anywhere and nothing ran. + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nbogus\n")); + // A pane's builtin runs at that pane. + try s.write(ctl, "Msg from ctl\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from ctl\n", .{n}))); + // One that means nothing bare is refused before anything runs. + _ = try s.refused(ctl, "Find\n"); + try std.testing.expectEqualStrings("wrong #args in control message \"Find\"", s.said); + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nMsg\n")); + try std.testing.expect(!has(try s.read("/log"), "first")); + // Save with no name on a scratch would ask for one: refused, not asked. + const other = try s.newPane(); + _ = try s.refused(s.at("/pane/{d}/ctl", .{other}), "Save\n"); + try std.testing.expectEqualStrings("control message needs its argument \"Save\"", s.said); + // A line after the one that closed the pane has nowhere to run. + try std.testing.expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/ctl", .{other}), "Del\nMsg after\n")); + try std.testing.expectError(error.Refused, s.read(s.at("/pane/{d}/ctl", .{other}))); +} diff --git a/test/behaviour/root_ctl.zig b/test/behaviour/root_ctl.zig index bdf912da..f7ebbbb9 100644 --- a/test/behaviour/root_ctl.zig +++ b/test/behaviour/root_ctl.zig @@ -29,3 +29,58 @@ test "root-ctl: size sets the screen of a session no frontend is attached to, an try std.testing.expect(std.mem.indexOf(u8, s.said, "too small for the panes") != null); try std.testing.expectEqual(.{ @as(usize, 120), @as(usize, 40) }, try size(&s)); } + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +fn columns(s: *Session) !usize { + return std.mem.count(u8, try s.read("/layout"), "\n") - 1; // and `active` +} + +test "root-ctl: the root ctl reads the settings as a write takes them, and takes the session's builtins" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const settings = try s.read("/ctl"); + try std.testing.expect(has(settings, "Verbose on\n")); + try std.testing.expect(has(settings, "LocationsConfig ")); + try std.testing.expect(has(settings, "CallstackDepth 8\n")); + try s.write("/ctl", "CallstackDepth\n"); + // Settings only: nothing /status or the DumpConfig window says. + try std.testing.expect(!has(settings, "Platform")); + try std.testing.expect(!has(settings, "requested")); + // Every line written back is taken, and changes nothing. + try s.write("/ctl", settings); + try s.expect("/ctl", settings); + try s.write("/ctl", "Verbose off\n"); + try std.testing.expect(has(try s.read("/ctl"), "Verbose off\n")); + const before = try columns(&s); + try s.write("/ctl", "Newcol\n"); + try std.testing.expectEqual(before + 1, try columns(&s)); + const refusals = [_]struct { []const u8, []const u8 }{ + .{ "Del", "not a session control message \"Del\": write it to pane/<n>/ctl" }, + .{ "Nonsense 1", "unknown control message \"Nonsense\"" }, + .{ "Verbose maybe", "bad value in control message; takes on, off \"maybe\"" }, + .{ "Placement east", "bad value in control message; takes acme, pardes \"east\"" }, + // A number's refusal names its range. + .{ "InactiveDim 200", "bad value in control message; takes 0-100 (a percentage) \"200\"" }, + .{ "MessageLinger x", "bad value in control message; takes 0-60000 (milliseconds) \"x\"" }, + .{ "Newcol 2", "wrong #args in control message \"Newcol\"" }, + .{ "Theme", "wrong #args in control message \"Theme\"" }, + }; + for (refusals) |r| { + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", s.at("{s}\n", .{r[0]}))); + try std.testing.expectEqualStrings(r[1], s.said); + } + _ = try s.refused("/ctl", "Theme no-such-theme\n"); + try std.testing.expect(std.mem.startsWith(u8, s.said, "bad value in control message; like it: n")); + try std.testing.expect(std.mem.endsWith(u8, s.said, "; Themes lists all \"no-such-theme\"")); + // A bare required word fails the check, so the line before never runs. + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Verbose on\nMount\n")); + try std.testing.expect(has(try s.read("/ctl"), "Verbose off\n")); + // A builtin that fails as it runs fails the write, quoting its error and + // its line; the lines before it have taken effect, as in acme. + try std.testing.expectEqual(@as(u16, 2), try s.refused("/ctl", "Verbose on\nMount x /tmp/pardes-no-such-sock-zz\nVerbose off\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Mount x: /tmp/pardes-no-such-sock-zz: no such socket")); + try std.testing.expect(has(try s.read("/ctl"), "Verbose on\n")); +} diff --git a/test/behaviour/rules.zig b/test/behaviour/rules.zig new file mode 100644 index 00000000..a29db6f0 --- /dev/null +++ b/test/behaviour/rules.zig @@ -0,0 +1,19 @@ +//! docs/typ/reference.typ, Rules for every file <rules>. +const std = @import("std"); +const Session = @import("session").Session; + +test "rules: a control character in a write to any ctl refuses the whole write, and a quoted line shows a tab as a blank" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + // The first column's serial, /layout's first word. + const col = std.mem.sliceTo(try s.read("/layout"), ' '); + for ([_][]const u8{ "/ctl", s.at("/pane/{d}/ctl", .{n}), s.at("/col/{s}/ctl", .{col}) }) |ctl| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Wrap off\nbo\x01gus\n")); + try std.testing.expectEqualStrings("invalid command line: it holds a control character (or DEL) other than a tab", s.said); + } + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/ctl", .{n}), "bogus\tword\n")); + // The offending word alone is quoted: a tab ends it. + try std.testing.expect(std.mem.indexOf(u8, s.said, "\"bogus\"") != null); + try std.testing.expect(std.mem.indexOfScalar(u8, s.said, '\t') == null); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig new file mode 100644 index 00000000..5970e6b5 --- /dev/null +++ b/test/behaviour/settings.zig @@ -0,0 +1,43 @@ +//! docs/typ/reference.typ, Settings <settings>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +/// The root ctl's line for `word`. +fn setting(s: *Session, word: []const u8) ![]const u8 { + var lines = std.mem.splitScalar(u8, try s.read("/ctl"), '\n'); + while (lines.next()) |line| if (std.mem.startsWith(u8, line, word) and line.len > word.len and line[word.len] == ' ') return line; + return error.NoSuchSetting; +} + +test "settings: Pager is pardes by default, off taken, anything else refused with its choices" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try std.testing.expectEqualStrings("Pager pardes", try setting(&s, "Pager")); + try s.write("/ctl", "Pager off\n"); + try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager")); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Pager less\n")); + try std.testing.expectEqualStrings("bad value in control message; takes pardes, off \"less\"", s.said); + try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager")); +} + +test "settings: Shell refuses a path that is no executable, and bare it goes back to the default" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const default = try setting(&s, "Shell"); + try std.testing.expect(try s.refused("/ctl", "Shell /nonexistent/zzsh\n") != 0); + try std.testing.expect(has(s.said, "Shell: shell \"/nonexistent/zzsh\" not found")); + try std.testing.expect(has(try s.read("/log"), "shell \"/nonexistent/zzsh\" not found")); + // Its words say a directory, which a mount reads as EISDIR too. + try std.testing.expectEqual(@as(u16, 21), try s.refused("/ctl", "Shell /etc\n")); + try std.testing.expect(has(try s.read("/log"), "Shell: not a shell: /etc is a directory")); + try std.testing.expectEqualStrings(default, try setting(&s, "Shell")); + try s.write("/ctl", "Shell /bin/sh\n"); + try std.testing.expectEqualStrings("Shell /bin/sh", try setting(&s, "Shell")); + try s.write("/ctl", "Shell\n"); + try std.testing.expect(has(try s.read("/log"), "Shell: the default again, ")); + try std.testing.expectEqualStrings(default, try setting(&s, "Shell")); +} |
