summaryrefslogtreecommitdiff
path: root/test/behaviour/plumber.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/behaviour/plumber.zig')
-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"));
+}