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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
//! docs/typ/reference.typ, The pardes command <command-line>: the commands
//! of a real pardes, against a session started for the case and against
//! none (a core the command makes in its own process).
const std = @import("std");
const libc = std.c;
const process = @import("../process.zig");
const Conn = process.Conn;
const has = process.has;
const check = process.check;
pub const cases = .{
.{ "cli: help prints the map, a command, a builtin and a file of the tree, with no session", helpOffline },
.{ "cli: fs with no session reads README, commands and ctl from a core made here, and says the rest is a session's", fsOffline },
.{ "cli: do, words and fs act on the session -s names, or the only one", againstSession },
.{ "cli: completion prints a script per shell and answers each word back", completion },
.{ "cli: version, skill, and the old spellings", versionAndOld },
};
extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8;
/// A root of no session: HOME, runtime and config directories that hold none.
fn emptyRoot(buf: *[64]u8) ![]const u8 {
const template = "/tmp/pardes-cli-XXXXXX";
@memcpy(buf[0..template.len], template);
buf[template.len] = 0;
_ = mkdtemp(buf[0..template.len :0]) orelse return error.NoTempDir;
return buf[0..template.len];
}
/// `pardes args...` in `root`: its exit, and stdout and stderr together.
fn pardes(a: std.mem.Allocator, exe: [:0]const u8, root: []const u8, args: []const [:0]const u8) !process.Ran {
var argv: std.ArrayList([:0]const u8) = .empty;
try argv.appendSlice(a, &.{ "/bin/sh", "-c", "exec \"$0\" \"$@\" 1>&2", exe });
try argv.appendSlice(a, args);
return process.run(a, argv.items, .{ .root = root });
}
fn expectRan(r: process.Ran, code: c_int, said: []const u8, what: []const u8) !void {
try check(r.code == code and has(r.said, said), "{s}: exit {d}, not {d}; wanted '{s}' in: {s}", .{ what, r.code, code, said, r.said });
}
fn helpOffline(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const a = arena.allocator();
var buf: [64]u8 = undefined;
const root = try emptyRoot(&buf);
defer process.removeDir(root);
const first = try pardes(a, exe, root, &.{"help"});
try expectRan(first, 0, "\nSessions:\n ps ", "help");
try expectRan(first, 0, "\nFiles:\n fs VERB [PATH]", "help");
try expectRan(try pardes(a, exe, root, &.{ "help", "attach" }), 0, "\n pardes attach box:notes ", "help attach");
try expectRan(try pardes(a, exe, root, &.{ "help", "new" }), 0, " --detach[=NAME]", "help new");
try expectRan(try pardes(a, exe, root, &.{ "help", "fs" }), 0, "usage: fs ls [PATH]", "help fs");
try expectRan(try pardes(a, exe, root, &.{ "help", "Newcol" }), 0, "Newcol (the root ctl; SPC c n; column)", "help Newcol");
try expectRan(try pardes(a, exe, root, &.{ "help", "Save" }), 0, "pardes do Save", "help Save");
try expectRan(try pardes(a, exe, root, &.{ "help", "pane/3/addr" }), 0, "pane/<n>/ name body tag", "help pane/3/addr");
try expectRan(try pardes(a, exe, root, &.{ "help", "nowhere" }), 1, "is not a command, a builtin", "help nowhere");
const all = try pardes(a, exe, root, &.{ "help", "--all" });
try expectRan(all, 0, "== pardes help version", "help --all");
try expectRan(all, 0, "Pardes virtual filesystem", "help --all");
// The same map, by its old spelling.
try check(std.mem.eql(u8, first.said, (try pardes(a, exe, root, &.{"--help"})).said), "--help is not help", .{});
}
fn fsOffline(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const a = arena.allocator();
var buf: [64]u8 = undefined;
const root = try emptyRoot(&buf);
defer process.removeDir(root);
try expectRan(try pardes(a, exe, root, &.{ "fs", "ls" }), 0, "README\n", "fs ls");
try expectRan(try pardes(a, exe, root, &.{ "fs", "ls" }), 0, "pane/\n", "fs ls");
try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "README" }), 0, "Pardes virtual filesystem", "fs read README");
try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "commands" }), 0, "Newcol root -- ", "fs read commands");
try expectRan(try pardes(a, exe, root, &.{ "fs", "complete", "pa" }), 0, "pager\npane/\n", "fs complete pa");
try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "index" }), 1, "that is a running session's, and none runs", "fs read index");
try expectRan(try pardes(a, exe, root, &.{ "fs", "write", "ctl", "Newcol" }), 1, "no session runs to write", "fs write");
try expectRan(try pardes(a, exe, root, &.{ "do", "Newcol" }), 1, "no session runs: start one", "do");
const w = try pardes(a, exe, root, &.{"words"});
try expectRan(w, 0, "column:\n Newcol ", "words");
try expectRan(try pardes(a, exe, root, &.{ "words", "nosuch" }), 1, "no group nosuch", "words nosuch");
}
fn againstSession(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const a = arena.allocator();
var d = try process.Detached.start(gpa, exe, "cli", .{});
defer d.deinit();
const c = try Conn.dial(gpa, d.socket());
defer c.deinit(gpa);
const root = d.root();
const cols = std.mem.count(u8, try c.read(a, "/layout"), "\n");
try expectRan(try pardes(a, exe, root, &.{ "do", "Newcol", "-s", "cli" }), 0, "", "do Newcol -s cli");
try check(std.mem.count(u8, try c.read(a, "/layout"), "\n") == cols + 1, "do Newcol made no column: {s}", .{try c.read(a, "/layout")});
// The only session, unnamed.
try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "index" }), 0, "1 text 0 ", "fs read index");
try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "pane/1/body", "-s", "cli" }), 0, "initial\n", "fs read body");
try expectRan(try pardes(a, exe, root, &.{ "fs", "write", "pane/1/ctl", "bogus" }), 1, "fs: pane/1/ctl: ", "fs write refused");
try expectRan(try pardes(a, exe, root, &.{ "do", "Save" }), 1, "is a pane's word: name the pane with -p N", "do Save");
try expectRan(try pardes(a, exe, root, &.{ "do", "Nosuch" }), 1, "no word Nosuch in this session", "do Nosuch");
try expectRan(try pardes(a, exe, root, &.{ "words", "column" }), 0, "Newcol", "words column");
try expectRan(try pardes(a, exe, root, &.{ "help", "index" }), 0, "index a line per pane", "help index");
try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "index", "-s", "nosuch" }), 1, "no session answers at ", "-s nosuch");
}
fn completion(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const a = arena.allocator();
var buf: [64]u8 = undefined;
const root = try emptyRoot(&buf);
defer process.removeDir(root);
try expectRan(try pardes(a, exe, root, &.{ "completion", "fish" }), 0, "pardes completion -- ", "completion fish");
try expectRan(try pardes(a, exe, root, &.{ "completion", "bash" }), 0, "complete -o filenames -F _pardes pardes", "completion bash");
try expectRan(try pardes(a, exe, root, &.{ "completion", "zsh" }), 0, "compdef _pardes pardes", "completion zsh");
try expectRan(try pardes(a, exe, root, &.{ "completion", "csh" }), 1, "no shell csh", "completion csh");
try expectRan(try pardes(a, exe, root, &.{ "completion", "--", "he" }), 0, "help\tthis, a command", "complete he");
try expectRan(try pardes(a, exe, root, &.{ "completion", "--", "do", "Newc" }), 0, "Newcol\t", "complete do Newc");
try expectRan(try pardes(a, exe, root, &.{ "completion", "--", "fs", "read", "pane/" }), 0, "pane/new\n", "complete fs read pane/");
try expectRan(try pardes(a, exe, root, &.{ "completion", "--", "new", "--det" }), 0, "--detach\t", "complete new --det");
}
fn versionAndOld(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const a = arena.allocator();
var buf: [64]u8 = undefined;
const root = try emptyRoot(&buf);
defer process.removeDir(root);
const v = try pardes(a, exe, root, &.{"version"});
try expectRan(v, 0, "\nsemantics ", "version");
try expectRan(v, 0, "9p 9P2000 msize ", "version");
try check(std.mem.eql(u8, v.said, (try pardes(a, exe, root, &.{"--version"})).said), "--version is not version", .{});
try expectRan(try pardes(a, exe, root, &.{"skill"}), 0, "pardes help PATH", "skill");
// The daemon is off in a case's environment: both spellings say so.
try expectRan(try pardes(a, exe, root, &.{"ps"}), 1, "no pardes daemon is running", "ps");
try expectRan(try pardes(a, exe, root, &.{"--ps"}), 1, "no pardes daemon is running", "--ps");
try expectRan(try pardes(a, exe, root, &.{"kill"}), 1, "kill needs a session's name", "kill");
try expectRan(try pardes(a, exe, root, &.{"--kill"}), 1, "--kill needs a session's name", "--kill");
try expectRan(try pardes(a, exe, root, &.{"--tty=x"}), 1, "--tty takes no value", "--tty=x");
}
|