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
145
146
147
148
149
|
//! The process tests (T2 of docs/next-tests.md): each case starts a real
//! pardes through test/process.zig and drives it over its socket. A file
//! in test/process/ a section of the book, as test/behaviour/'s are. The
//! tty frontend's own (test/process/tty_*.zig) drive it through a pty of
//! its own instead (test/tty.zig), on the tty alone.
//!
//! pardes-process-test <pardes> [--backend=tui|gui] [--addon=<lib>]...
//! [--lspmock=<exe>] [--update] [--replay [--replay-with=<pardes>]] [filter]
const std = @import("std");
const process = @import("process.zig");
const tty = @import("tty.zig");
const files = .{
@import("process/builtins.zig"),
@import("process/edit.zig"),
@import("process/flags.zig"),
@import("process/look_and_exec.zig"),
@import("process/mounts.zig"),
@import("process/other_files.zig"),
@import("process/repls.zig"),
@import("process/root_ctl.zig"),
@import("process/terminals.zig"),
@import("process/tree.zig"),
@import("process/window.zig"),
};
/// Each case in a child of its own (Tty.init sets the child's environment
/// and directory), a core's worth at once, as each has a directory of its
/// own. One that fails runs again alone with the settle probe's wider bet
/// before it counts: a loaded machine loses the narrow one (tty.beCareful).
const tty_files = .{
@import("process/tty_boot.zig"),
@import("process/tty_editing.zig"),
@import("process/tty_layout.zig"),
@import("process/tty_themes.zig"),
@import("process/tty_images.zig"),
@import("process/tty_terminals.zig"),
};
pub fn main(init: std.process.Init) !void {
const arena = init.arena.allocator();
const all = try init.minimal.args.toSlice(arena);
var args: [3][:0]const u8 = undefined;
var n: usize = 0;
for (all[1..]) |a| {
if (std.mem.startsWith(u8, a, "--backend=")) {
process.backend = std.meta.stringToEnum(process.Backend, a["--backend=".len..]) orelse return error.Usage;
} else if (std.mem.startsWith(u8, a, "--addon=")) {
if (process.naddons == process.addons.len) return error.Usage;
process.addons[process.naddons] = try std.Io.Dir.cwd().realPathFileAlloc(init.io, a["--addon=".len..], arena);
process.naddons += 1;
} else if (std.mem.startsWith(u8, a, "--lspmock=")) {
tty.lspmock = try std.Io.Dir.cwd().realPathFileAlloc(init.io, a["--lspmock=".len..], arena);
} else if (std.mem.eql(u8, a, "--update")) {
tty.update = true;
} else if (std.mem.eql(u8, a, "--replay")) {
tty.replay = true;
} else if (std.mem.startsWith(u8, a, "--replay-with=")) {
tty.replay_with = try std.Io.Dir.cwd().realPathFileAlloc(init.io, a["--replay-with=".len..], arena);
} else if (n < args.len) {
args[n] = a;
n += 1;
}
}
if (n < 1) return error.Usage;
// Absolute: a tty case runs in a directory of its own.
const exe = try std.Io.Dir.cwd().realPathFileAlloc(init.io, args[0], arena);
const filter: []const u8 = if (n > 1) args[1] else "";
tty.goldens = std.Io.Dir.cwd().realPathFileAlloc(init.io, "test/snapshots", arena) catch tty.goldens;
const gpa = std.heap.page_allocator;
const on = @tagName(process.backend);
var failed: usize = 0;
var skipped: usize = 0;
var ran: usize = 0;
if (!tty.update and !tty.replay) inline for (files) |file| inline for (file.cases) |case| {
if (std.mem.indexOf(u8, case[0], filter) != null) {
ran += 1;
const t0 = process.nowMs();
if (case[1](gpa, exe)) {
std.debug.print("PASS [{s}] {d} ms {s}\n", .{ on, process.nowMs() - t0, case[0] });
} else |err| if (err == error.SkipZigTest) {
skipped += 1;
std.debug.print("SKIP [{s}] {s}\n", .{ on, case[0] });
} else {
failed += 1;
if (@errorReturnTrace()) |t| std.debug.dumpErrorReturnTrace(t);
std.debug.print("FAIL [{s}] {d} ms {s}: {t}\n", .{ on, process.nowMs() - t0, case[0], err });
}
}
};
if (process.backend == .tui) {
// pid and case of each child running, and the cases that failed.
const jobs: usize = if (tty.update) 1 else @max(1, (std.Thread.getCpuCount() catch 4) * 5 / 4);
var running: std.ArrayList(struct { pid: std.c.pid_t, name: []const u8 }) = .empty;
var again: std.ArrayList([]const u8) = .empty;
inline for (tty_files) |file| inline for (file.cases) |case| {
if (std.mem.indexOf(u8, case[0], filter) != null) {
ran += 1;
while (running.items.len >= jobs) try reap(arena, &running, &again, &skipped);
const pid = std.c.fork();
if (pid == 0) std.process.exit(runTty(case, gpa, exe));
if (pid < 0) return error.ForkFailed;
try running.append(arena, .{ .pid = pid, .name = case[0] });
}
};
while (running.items.len > 0) try reap(arena, &running, &again, &skipped);
tty.beCareful();
for (again.items) |name| {
std.debug.print("again, alone: {s}\n", .{name});
inline for (tty_files) |file| inline for (file.cases) |case| {
if (std.mem.eql(u8, case[0], name)) {
const pid = std.c.fork();
if (pid == 0) std.process.exit(runTty(case, gpa, exe));
var status: c_int = 0;
_ = std.c.waitpid(pid, &status, 0);
if (status != 0) failed += 1;
}
};
}
}
std.debug.print("{d} of {d} process tests failed on {s} ({d} skipped)\n", .{ failed, ran, on, skipped });
if (failed > 0 or ran == 0) std.process.exit(1);
}
/// One tty case, in the child: 0 passed, 1 failed, 2 skipped.
fn runTty(case: anytype, gpa: std.mem.Allocator, exe: [:0]const u8) u8 {
const t0 = process.nowMs();
if (case[1](gpa, exe)) {
std.debug.print("{s} [tui] {d} ms {s}\n", .{ if (tty.update) "UPDATED" else "PASS", process.nowMs() - t0, case[0] });
return 0;
} else |err| {
if (err == error.SkipZigTest) return 2;
if (@errorReturnTrace()) |t| std.debug.dumpErrorReturnTrace(t);
std.debug.print("FAIL [tui] {d} ms {s}: {t}\n", .{ process.nowMs() - t0, case[0], err });
return 1;
}
}
fn reap(arena: std.mem.Allocator, running: anytype, again: *std.ArrayList([]const u8), skipped: *usize) !void {
var status: c_int = 0;
const pid = std.c.waitpid(-1, &status, 0);
for (running.items, 0..) |job, i| {
if (job.pid != pid) continue;
// A child killed (a crash in the runner itself) failed too.
if (status == 2 << 8) skipped.* += 1 else if (status != 0) try again.append(arena, job.name);
_ = running.swapRemove(i);
return;
}
}
|