summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/behaviour.zig1
-rw-r--r--test/behaviour/plumber.zig65
-rw-r--r--test/process/tree.zig2
-rw-r--r--test/session.zig3
4 files changed, 70 insertions, 1 deletions
diff --git a/test/behaviour.zig b/test/behaviour.zig
index 5b31b6d9..9fe1f50b 100644
--- a/test/behaviour.zig
+++ b/test/behaviour.zig
@@ -25,6 +25,7 @@ test {
_ = @import("behaviour/other_files.zig");
_ = @import("behaviour/panes.zig");
_ = @import("behaviour/placement.zig");
+ _ = @import("behaviour/plumber.zig");
_ = @import("behaviour/repls.zig");
_ = @import("behaviour/root_ctl.zig");
_ = @import("behaviour/rules.zig");
diff --git a/test/behaviour/plumber.zig b/test/behaviour/plumber.zig
new file mode 100644
index 00000000..f87c0449
--- /dev/null
+++ b/test/behaviour/plumber.zig
@@ -0,0 +1,65 @@
+//! docs/typ/reference.typ, The plumber <plumber>: plumb/rules and
+//! plumb/send, Plan 9's plumber(4) files.
+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 "plumber: plumb/rules reads the rules in force, a write adds rule sets, > replaces them, a bad one is refused in plumb(6)'s words, one writer at a time" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ // The defaults, `include basic`'s rules in their place.
+ try std.testing.expect(has(try s.read("/plumb/rules"), "type\tis\ttext\ndata\tmatches\t'https?://.*'\nplumb\tto\tweb\n\n"));
+ // Added, the last rule set at the close.
+ try s.write("/plumb/rules", "type is text\ndata matches 'gopher://.*'\nplumb to web\n");
+ try std.testing.expect(std.mem.endsWith(u8, try s.read("/plumb/rules"), "plumb\tto\tedit\n\ntype\tis\ttext\ndata\tmatches\t'gopher://.*'\nplumb\tto\tweb\n\n"));
+ // A rule set refused at the write it is whole in, said with its line;
+ // the rules stay as they were.
+ const before = try s.read("/plumb/rules");
+ try std.testing.expectEqual(@as(u16, 22), try s.refused("/plumb/rules", "type is text\nplumb to web\n\ndata isnt x\nplumb to web\n\n"));
+ try std.testing.expectEqualStrings("plumb/rules:4: unknown verb isnt", s.said);
+ try std.testing.expectEqualStrings(before, try s.read("/plumb/rules"));
+ try std.testing.expectEqual(@as(u16, 22), try s.refused("/plumb/rules", "data matches '([a-z]+)\\.c'\narg isfile $1\nplumb to edit\n\n"));
+ try std.testing.expect(has(s.said, "$1: no submatches"));
+ // `>`: replaced.
+ const f = try s.hold("/plumb/rules", 1);
+ try s.truncate(f);
+ try std.testing.expectEqual(@as(u16, 0), try s.writeFid(f, "type is text\nplumb to edit\n\n"));
+ // One writer at a time, as plumber(4)'s.
+ try std.testing.expectError(error.Refused, s.hold("/plumb/rules", 1));
+ s.clunk(f);
+ try std.testing.expectEqualStrings("type\tis\ttext\nplumb\tto\tedit\n\n", try s.read("/plumb/rules"));
+}
+
+test "plumber: plumb/send takes a message in its six lines and data, written in pieces or whole, and sends it where the rules say, or says why not in Plan 9's words" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ // A URL: the web port, the host's link opener.
+ try s.write("/plumb/send", "test\n\n/tmp\ntext\n\n19\nhttps://ziglang.org");
+ try std.testing.expectEqualStrings("https://ziglang.org", s.link);
+ // In pieces: its data's count says when it is whole.
+ const f = try s.hold("/plumb/send", 1);
+ try std.testing.expectEqual(@as(u16, 0), try s.writeFid(f, "test\n\n/tmp\ntext\n\n19\nhttps://"));
+ try std.testing.expectEqualStrings("https://ziglang.org", s.link);
+ try std.testing.expectEqual(@as(u16, 0), try s.writeFid(f, "example.org"));
+ try std.testing.expectEqualStrings("https://example.org", s.link);
+ s.clunk(f);
+ // No rule takes it, and it names no port.
+ try std.testing.expect(try s.refused("/plumb/send", "test\n\n/\ntext\n\n9\nno-such-x") != 0);
+ try std.testing.expectEqualStrings("couldn't find destination for message", s.said);
+ // Not a message at all.
+ try std.testing.expect(try s.refused("/plumb/send", "test\ntext\n") != 0);
+ try std.testing.expectEqualStrings("bad plumb message format", s.said);
+ try std.testing.expect(try s.refused("/plumb/send", "a\nb\nc\nd\ne\n1\nxy") != 0);
+ try std.testing.expectEqualStrings("bad plumb message format", s.said);
+ // A file to the edit port: opened as Look opens one, at its place.
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "plumbed.txt", .data = "one\ntwo\nthree\n" });
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ try s.write("/plumb/send", s.at("test\n\n{s}\ntext\n\n{d}\nplumbed.txt:2", .{ dir, "plumbed.txt:2".len }));
+ try std.testing.expect(has(try s.read("/index"), "/plumbed.txt"));
+}
diff --git a/test/process/tree.zig b/test/process/tree.zig
index d28ac1ed..8fe3d881 100644
--- a/test/process/tree.zig
+++ b/test/process/tree.zig
@@ -105,7 +105,7 @@ fn discovery(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
const top = try c.list(a, "/");
// A set: which files the root holds, not the order a listing gives.
- const want = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "addons", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "pane", "col", "os" };
+ const want = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "addons", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "plumb", "pane", "col", "os" };
var others: usize = 0;
for (top) |n| {
if (contains(&.{ "src", "shaders" }, n)) continue;
diff --git a/test/session.zig b/test/session.zig
index fd956aa9..056966b9 100644
--- a/test/session.zig
+++ b/test/session.zig
@@ -30,6 +30,8 @@ pub const Session = struct {
arena: std.heap.ArenaAllocator,
/// Saves the core asked its host for (a host would perform them).
saved: usize = 0,
+ /// The last link the core asked its host to open (a browser would).
+ link: []const u8 = "",
/// The words of the last refusal `refused` answered.
said: []const u8 = "",
tag: u64 = 0,
@@ -277,6 +279,7 @@ pub const Session = struct {
fn drain(s: *Session) void {
while (pardesOf(s).nextEffect()) |e| switch (e) {
.save_file, .save_text => s.saved += 1,
+ .open_link => |u| s.link = s.arena.allocator().dupe(u8, u.slice()) catch "",
// Into the session's state (a database in memory, or its file).
.write_dump => pardes.host_io.writeDump(pardesOf(s), pardesOf(s).dump_out.?),
.addon => pardesOf(s).perform(e),