summaryrefslogtreecommitdiff
path: root/test/behaviour
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 00:41:40 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit65612a1cc13dd2a7e866f88176dbf6db5dd83467 (patch)
treed2b6173b9650532ff8fa8b489ca7506b75d93cbd /test/behaviour
parent797e1b05324a8de7217b7e483496f44c1e7bcdbd (diff)
downloadpardes-65612a1cc13dd2a7e866f88176dbf6db5dd83467.tar.gz
pardes-65612a1cc13dd2a7e866f88176dbf6db5dd83467.zip
plumber: Plan 9's plumber in the core, its language and its files (docs/next-plumber.md, phase 1): src/Plumb.zig owns the rules, kept as their text (Core.plumber, digested and dumped; `include basic`'s, the embedded src/plumbing, until any are written, so a session and a dump from before it start with the defaults) and parsed again for each message, and a matcher over them read from rules.c and match.c: the message's pack and unpack (plumbunpackpartial's whole, short of its data, malformed), packed attributes read and quoted rc's way, rule sets of `object verb arg` lines, variables, include (basic, the configuration directory's, an absolute path, through the taped read site), rc's quoting and `$` expansion at parse and at match, every parse refusal in rules.c's words with its file and line, `$1`-`$9` refused in words (the regular expressions have no capture groups), `matches` over regexp.zig whole (`^(...)$`) or around a click counted in runes with Pike's two patterns, `is`, `set` rewriting for good, `add`, `delete` failing on an attribute not there as match.c's code does, `isfile` and `isdir` finding a name as Look does (past its place, the message's directory, a look's jump list's, Incl's) at the taped resolve sites, once a send; dispose to a port pardes reads (edit: opened as a look opens it, at addr=; web: the host's link opener; a loaded kind by its name), to a rule set's start or client run as exec runs a line (a builtin in place, else a command pane in the message's wdir: Core.exec_dir, exec.runCommand's reuse skipping the sender), or Plan 9's words for what went nowhere; rules print back in the order they were written, a variable where it was set, so a printed text reads back the same; plumb/rules (a write adds rule sets as each is whole, the rest at the close, `>` replaces them, one writer at a time) and plumb/send (a message put together across writes) in the tree, with fs-help's line, the reference's section and tree, and the fstree test's paths; nothing calls the plumber from Look yet; test: Plumb.zig's eleven (every parse error, print and read back, plan9port's basic less its submatch rule sets, its ¡-￿ classes UTF-8's bytes, whole and click matches with horse.gift, quoting, a set's rewrite kept, delete of an absent attribute, dst filtering, isfile and isdir, dispose, a dump keeping the rules), test/behaviour/plumber.zig's two through the tree (Session records the link a host would open); the root's listings in tree.zig's and the process tests gain plumb, and the README folds a blank line to stay 45 lines; the changelog says so
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'test/behaviour')
-rw-r--r--test/behaviour/plumber.zig65
1 files changed, 65 insertions, 0 deletions
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"));
+}