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
|
//! docs/typ/reference.typ, The root ctl <root-ctl>, through a real
//! session's socket: it exits, keeps its own clock and saves.
const std = @import("std");
const libc = std.c;
const process = @import("../process.zig");
const eh = @import("../e2e_harness.zig");
const expect = std.testing.expect;
const expectEqualStrings = std.testing.expectEqualStrings;
pub const cases = .{
.{ "root-ctl: Exit is answered before the editor quits", exitAnswers },
.{ "root-ctl: with nothing attached a message leaves after MessageLinger on its own clock", messagesExpire },
.{ "root-ctl: Putall with one pane's file changed on disk fails, answered once the other panes' saves have landed", putallAfterSaves },
.{ "root-ctl: long and odd paths where a path goes (name, look, DumpDir, Dump, Save) are refused or said, never a crash; a bad ThemeFile fails the write", oddPaths },
};
fn has(hay: []const u8, needle: []const u8) bool {
return std.mem.indexOf(u8, hay, needle) != null;
}
fn exitAnswers(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
var d = try process.Detached.start(gpa, exe, "exit", .{});
defer d.deinit();
try d.files.write("/ctl", "Exit\n");
if (d.exited(5_000) == null) return error.StillRunning;
}
/// Nothing attached, no key, no click, no other request comes to move it along.
fn messagesExpire(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
var d = try process.Detached.start(gpa, exe, "msg", .{});
defer d.deinit();
try d.files.write("/pane/1/ctl", "Msg lingering-words\n");
try expect(has(try d.files.screen(), "lingering-words"));
// Reading /screen is itself a request, so wait without asking.
process.sleep(2500);
try expect(!has(try d.files.screen(), "lingering-words"));
}
fn putallAfterSaves(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
var d = try process.Detached.start(gpa, exe, "putall", .{});
defer d.deinit();
const f = &d.files;
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const ar = arena.allocator();
const c = try process.Conn.dial(gpa, d.socket());
defer c.deinit(gpa);
try d.put("a.txt", "a\n");
try d.put("b.txt", "b\n");
const a = try c.look(ar, f.at("{s}/a.txt", .{d.root()}));
const b = try c.look(ar, f.at("{s}/b.txt", .{d.root()}));
for ([_]u32{ a, b }) |serial| try c.write(f.at("/pane/{d}/body", .{serial}), "edited\n");
try d.put("b.txt", "changed on disk\n");
const deadline = eh.nowMs() + 10_000;
while (!has(try c.read(ar, "/log"), f.at("changed {d}", .{b}))) {
if (eh.nowMs() > deadline) return error.NeverHeard;
process.sleep(50);
}
try std.testing.expectError(error.Refused, c.write("/ctl", "Putall\n"));
try expect(has(c.said, "modified on disk"));
try expectEqualStrings("a\nedited\n", try d.get(ar, "a.txt"));
try expectEqualStrings("changed on disk\n", try d.get(ar, "b.txt"));
}
/// A part over 255 bytes (ENAMETOOLONG), a loop of links (ELOOP), a file
/// taken for a directory (ENOTDIR), one not ours (EACCES).
fn oddPaths(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
var d = try process.Detached.start(gpa, exe, "odd", .{});
defer d.deinit();
const f = &d.files;
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const a = arena.allocator();
const c = try process.Conn.dial(gpa, d.socket());
defer c.deinit(gpa);
const root = d.root();
const loop = try std.fmt.allocPrintSentinel(a, "{s}/loop", .{root}, 0);
try expect(libc.symlink(loop, loop) == 0);
try d.put("plain", "x\n");
const long: [300]u8 = @splat('x');
const odd = [_][]const u8{
f.at("/tmp/{s}/f.txt", .{&long}), f.at("{s}/{s}", .{ root, &long }), f.at("{s}/loop/f", .{root}),
f.at("{s}/plain/f", .{root}), "/root/nope/f", f.at("{s}/a/../b.txt", .{root}),
};
const serial = try c.newPane();
try c.write(f.at("/pane/{d}/body", .{serial}), "text\n");
for (odd) |path| {
for ([_][2][]const u8{
.{ f.at("/pane/{d}/name", .{serial}), path }, .{ "/look", path }, .{ "/ctl", f.at("DumpDir {s}", .{path}) },
.{ "/ctl", "Dump" }, .{ f.at("/pane/{d}/ctl", .{serial}), "Save" },
}) |w| {
c.write(w[0], f.at("{s}\n", .{w[1]})) catch {};
try expect((try c.read(a, "/index")).len > 0);
}
}
try c.write("/ctl", "DumpDir\n");
// A theme file that is not there, or no theme, fails the write.
try d.put("bad.zon", ".{ .nonsense = 1 }");
for ([_][2][]const u8{
.{ f.at("ThemeFile {s}/missing.zon\n", .{root}), "theme file" },
.{ f.at("ThemeFile {s}/bad.zon\n", .{root}), "theme file" },
.{ "ThemeFile notes.txt\n", "a theme file is a .zon" },
}) |t| {
try std.testing.expectError(error.Refused, c.write("/ctl", t[0]));
try expect(has(c.said, t[1]));
}
}
|