summaryrefslogtreecommitdiff
path: root/test/behaviour/settings.zig
blob: 5970e6b58800075eda2245baef7920f1ddaf28d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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"));
}