diff options
Diffstat (limited to 'test/snapshot.zig')
| -rw-r--r-- | test/snapshot.zig | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/test/snapshot.zig b/test/snapshot.zig index 2d8a8664..255f55bf 100644 --- a/test/snapshot.zig +++ b/test/snapshot.zig @@ -86,6 +86,9 @@ var careful = false; /// (--lspmock=); null leaves the protocol client fully disabled for the run var lspmock_bin: ?[:0]const u8 = null; var record_dir: ?[]const u8 = null; +/// --tape=DIR: each session records its tape there (PARDES_RECORD, src/tape.zig) +/// and the script fails unless every tape replays without diverging. +var tape_dir: ?[]const u8 = null; const gpa = std.heap.page_allocator; pub fn main(init: std.process.Init) !void { @@ -123,6 +126,8 @@ pub fn main(init: std.process.Init) !void { lspmock = try absPath(arena, orig_cwd, a["--lspmock=".len..]); } else if (std.mem.startsWith(u8, a, "--record=")) { record_dir = try absPath(arena, orig_cwd, a["--record=".len..]); + } else if (std.mem.startsWith(u8, a, "--tape=")) { + tape_dir = try absPath(arena, orig_cwd, a["--tape=".len..]); } else if (std.mem.startsWith(u8, a, "--jobs=")) { jobs = @max(1, try std.fmt.parseInt(usize, a["--jobs=".len..], 10)); } else if (exe == null) { @@ -148,6 +153,7 @@ pub fn main(init: std.process.Init) !void { std.mem.eql(u8, a, "--trace-stable") or std.mem.startsWith(u8, a, "--lspmock=") or std.mem.startsWith(u8, a, "--record=") or + std.mem.startsWith(u8, a, "--tape=") or std.mem.startsWith(u8, a, "--jobs=")) continue; if (!saw_exe) { saw_exe = true; @@ -179,6 +185,7 @@ pub fn main(init: std.process.Init) !void { if (update) fatal("--record and --update are separate modes", .{}); try mkdir(arena, dir, true); } + if (tape_dir) |dir| try mkdir(arena, dir, true); try mkdir(arena, SNAP_BASE, true); // one run at a time (this is the flock that used to be typed by hand) @@ -463,9 +470,15 @@ fn runOne(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const u8 const started = eh.nowMs(); const out = runScript(arena, exe_z, script_path, stem) catch |e| { std.debug.print("FAIL {s}: script error {s} ({d}ms)\n", .{ stem, @errorName(e), eh.nowMs() - started }); + // What the session did before the script gave up replays all the same. + if (tape_dir != null) if (replayTapes(arena, exe_z, stem)) |why| std.debug.print("DIVERGED {s}: {s}\n", .{ stem, why }); return 1; }; const took = eh.nowMs() - started; + if (tape_dir != null) if (replayTapes(arena, exe_z, stem)) |why| { + std.debug.print("DIVERGED {s}: {s}\n", .{ stem, why }); + return 1; + }; if (record_dir) |dir| { const path = std.fmt.allocPrintSentinel(arena, "{s}/{s}.snapshot", .{ dir, stem }, 0) catch return 1; eh.writeFile(path, out) catch return 1; @@ -495,6 +508,30 @@ fn runOne(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const u8 return 1; } +/// Every tape the script's sessions wrote, replayed by the same build +/// (PARDES_REPLAY): why the first that diverged did, or null. +fn replayTapes(arena: std.mem.Allocator, exe_z: [:0]const u8, stem: []const u8) ?[]const u8 { + const dir = tape_dir.?; + const io = std.Io.Threaded.global_single_threaded.io(); + var d = std.Io.Dir.cwd().openDir(io, dir, .{ .iterate = true }) catch return "the tape directory cannot be read"; + defer d.close(io); + var it = d.iterate(); + var tapes: usize = 0; + while (it.next(io) catch return "the tape directory cannot be listed") |entry| { + if (!std.mem.startsWith(u8, entry.name, stem) or !std.mem.endsWith(u8, entry.name, ".tape")) continue; + const pid = entry.name[stem.len..][0 .. entry.name.len - stem.len - ".tape".len]; + if (pid.len < 2 or pid[0] != '-' or std.mem.indexOfNone(u8, pid[1..], "0123456789") != null) continue; + tapes += 1; + const cmd = std.fmt.allocPrintSentinel(arena, "PARDES_RECORD= PARDES_REPLAY='{s}/{s}' timeout 300 '{s}' > '{s}/{s}.replay' 2>&1", .{ dir, entry.name, exe_z, dir, entry.name }, 0) catch return "out of memory"; + if (system(cmd) != 0) { + const why_path = std.fmt.allocPrint(arena, "{s}/{s}.replay", .{ dir, entry.name }) catch return "out of memory"; + return readFileAlloc(arena, why_path) catch "the replay failed and said nothing"; + } + } + if (tapes == 0) return "no session wrote a tape"; + return null; +} + fn reportPath(arena: std.mem.Allocator, script_path: []const u8) ![]const u8 { return std.fmt.allocPrint(arena, "{s}/{s}.report", .{ SNAP_BASE, std.fs.path.stem(script_path) }); } @@ -603,6 +640,12 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const // together. Scripts name it `$DUMP`. const dump_path = try std.fmt.allocPrint(arena, "{s}/dump.zon", .{base}); _ = setenv("PARDES_DUMP", try arena.dupeSentinel(u8, dump_path, 0), 1); + // Each session the script starts (and each pardes run inside one) + // writes its own tape, <stem>-<pid>.tape. + if (tape_dir) |dir| { + _ = system(try std.fmt.allocPrintSentinel(arena, "rm -f '{s}/{s}-'[0-9]*.tape '{s}/{s}-'[0-9]*.tape.replay", .{ dir, stem, dir, stem }, 0)); + _ = setenv("PARDES_RECORD", try std.fmt.allocPrintSentinel(arena, "{s}/{s}-", .{ dir, stem }, 0), 1); + } if (chdirRc(try arena.dupeSentinel(u8, work, 0)) < 0) return error.ChdirFailed; const chunks = try arena.alloc([]const u8, countCaptures(src)); |
