From b8a6b0589ddbe65a4c80272ff44ceb77d2f0d02f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 19 Nov 2026 21:38:07 -0300 Subject: core, docs: the control tree's test harness is core/ninep/harness.zig, no longer core/ninep/testing.zig (P18, the user's ruling on the tree step's name collisions: the file's name collided with the future `core.testing`): the file moved whole in this change, so its history follows it (no line of it changed), and its 27 imports follow, nine `@import("testing.zig")` in core/ninep (addr, cols, ctl, events, pane, pty, screen, sources, tree) and 18 `@import("ninep/testing.zig")` in core (Addon, Core three, Dir, Effect, Query, dump three, edit_cmd, fs, keys, look two, mouse, sam_edit, tape), with the two design notes naming it (docs/next-tests.md, docs/next-terminal.md); the build and the root's test lists never named it; a rename only, nothing else on any line; tui core-test 848/991 (143 skipped), shell-test 1027/1030, the window builds (`install -Dplatform=gui`), the macOS library for Linux and aarch64-macos and the FreeBSD cross-build rc 0, under a load of 22 to 60 Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Addon.zig | 2 +- core/Core.zig | 6 +- core/Dir.zig | 2 +- core/Effect.zig | 2 +- core/Query.zig | 2 +- core/dump.zig | 6 +- core/edit_cmd.zig | 2 +- core/fs.zig | 2 +- core/keys.zig | 2 +- core/look.zig | 4 +- core/mouse.zig | 2 +- core/ninep/addr.zig | 2 +- core/ninep/cols.zig | 2 +- core/ninep/ctl.zig | 2 +- core/ninep/events.zig | 2 +- core/ninep/harness.zig | 168 +++++++++++++++++++++++++++++++++++++++++++++++++ core/ninep/pane.zig | 2 +- core/ninep/pty.zig | 2 +- core/ninep/screen.zig | 2 +- core/ninep/sources.zig | 2 +- core/ninep/testing.zig | 168 ------------------------------------------------- core/ninep/tree.zig | 2 +- core/sam_edit.zig | 2 +- core/tape.zig | 2 +- 24 files changed, 195 insertions(+), 195 deletions(-) create mode 100644 core/ninep/harness.zig delete mode 100644 core/ninep/testing.zig (limited to 'core') diff --git a/core/Addon.zig b/core/Addon.zig index ec2f796a..0b60ca9c 100644 --- a/core/Addon.zig +++ b/core/Addon.zig @@ -911,7 +911,7 @@ test "addon reload gives migration the old record version and saves the new vers return if (version == 1 and n == 1 and bytes[0] == 7) Kind.c.PA_OK else 1; } }; - const p = try @import("ninep/testing.zig").withFile(std.testing.allocator, "x\n"); + const p = try @import("ninep/harness.zig").withFile(std.testing.allocator, "x\n"); defer p.deinit(); var old = std.mem.zeroInit(Kind.Ops, .{ .head = std.mem.zeroInit(Kind.c.pa_head, .{ .size = @sizeOf(Kind.c.pa_head), .abi = Kind.abi, .version = 1 }), .open = Fake.open, .paint = Fake.paint, .dump = Fake.save, .load = Fake.load }); @memcpy(old.head.name[0..7], "migrate"); diff --git a/core/Core.zig b/core/Core.zig index e9f4f0e4..b109b073 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -2945,7 +2945,7 @@ test "a REPL bound for a language takes the exec made on a body in it; tags, Exe // A body's click written back to event is done as the click: sent. A // line written to exec came back as F and runs as the command it was. { - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); const event = ctlfs.Node.of(file.serial, .event); const h = th.call(p, .{ .tag = 1, .op = .open, .node = event }).reply.handle; defer _ = th.call(p, .{ .tag = 2, .op = .release, .node = event, .handle = h }); @@ -3039,7 +3039,7 @@ test "a REPL bound for a language takes the exec made on a body in it; tags, Exe try std.testing.expect(Sent.len[1] == 0 and file.prompt == .none); // The question is in the log, and a script answers it on the ctl. { - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); _ = exec.executeFrom(p, 0, "print(5)", true); var want: [32]u8 = undefined; try std.testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "ask {d} repl a b\n", .{file.serial}))); @@ -3541,7 +3541,7 @@ test "Tab N draws every pane's tab N cells wide and clicks count past it; bare T try std.testing.expect(p.executeBuiltinLine(0, "Tab 0")); try std.testing.expectEqual(@as(u16, 8), p.settings.tab); // The ctl read's tab column, and a Dump's settings. - const ctl_text = @import("ninep/testing.zig").rd(p, ctlfs.Node.of(pane.serial, .ctl), 0, 4096).bytes; + const ctl_text = @import("ninep/harness.zig").rd(p, ctlfs.Node.of(pane.serial, .ctl), 0, 4096).bytes; try std.testing.expect(std.mem.indexOf(u8, ctl_text, " 8 ") != null); try dump.dumpState(p); const restored = try dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); diff --git a/core/Dir.zig b/core/Dir.zig index a82887fb..29c5cdc8 100644 --- a/core/Dir.zig +++ b/core/Dir.zig @@ -430,7 +430,7 @@ test "an exec in a directory pane runs in its directory, and its +Errors goes th } test "over 9P a directory pane is a text pane whose name ends in /, its body the listing, ctl's isdir 1, and get reads it again" { - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); const tree = @import("ninep/tree.zig"); var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); diff --git a/core/Effect.zig b/core/Effect.zig index 7166294f..7475cc84 100644 --- a/core/Effect.zig +++ b/core/Effect.zig @@ -681,7 +681,7 @@ test "a save path that names no file is refused before anything is rewritten" { } test "a Save the host could not do logs no save record" { - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); const p = try th.withFile(std.testing.allocator, "x\n"); defer p.deinit(); const Failing = struct { diff --git a/core/Query.zig b/core/Query.zig index dacc11a1..b76a5aa4 100644 --- a/core/Query.zig +++ b/core/Query.zig @@ -2754,6 +2754,6 @@ test "a query row's address reaches every way a look arrives: an empty look writ // the whole row written to the pane's look: the place a.body.cur_row = 0; const tree = @import("ninep/tree.zig"); - try std.testing.expectEqual(tree.Status.ok, @import("ninep/testing.zig").wr(p, tree.Node.of(qp.serial, .look), line).reply.status); + try std.testing.expectEqual(tree.Status.ok, @import("ninep/harness.zig").wr(p, tree.Node.of(qp.serial, .look), line).reply.status); try std.testing.expectEqual(@as(i32, 3), a.body.cur_row); } diff --git a/core/dump.zig b/core/dump.zig index 1616f557..35975a9b 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -985,7 +985,7 @@ test "a session with every column it may hold dumps and restores them all" { } test "a restored session's column and workspace tags take more than one selection, and its 9P files have a time" { - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true }); defer p.deinit(); @@ -1037,7 +1037,7 @@ test "a pane restored with unsaved text is asked about by Exit, Restore, Del and const id = for (restored.panes, 0..) |slot, i| { if (slot) |candidate| if (candidate.file != null and candidate.file.?.output == null) break i; } else return error.FileLost; - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); const node = if (word == .Del or word == .get) ctlfs.Node.of(restored.panes[id].?.serial, .ctl) else @@ -1171,7 +1171,7 @@ test "a session's byte dump read by name, field by field as another build reads test "an old dump's file, a byte dump, is read by -l and by a Restore of its path, for one release" { const gpa = std.testing.allocator; - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); const p = try Pardes.init(gpa, .{ .tty_only = true }); defer p.deinit(); _ = try p.setTestFile("kept in a .dump\n"); diff --git a/core/edit_cmd.zig b/core/edit_cmd.zig index 43006503..9f1f7862 100644 --- a/core/edit_cmd.zig +++ b/core/edit_cmd.zig @@ -452,7 +452,7 @@ fn commit(p: *Pardes, pd: *Pending, outputs: []const []const u8) void { } const testing = std.testing; -const th = @import("ninep/testing.zig"); +const th = @import("ninep/harness.zig"); const tree = @import("ninep/tree.zig"); /// The answer of the Edit waiting on its commands: as a host's worker diff --git a/core/fs.zig b/core/fs.zig index 0a10825d..0797beb0 100644 --- a/core/fs.zig +++ b/core/fs.zig @@ -11,7 +11,7 @@ const modal = @import("modal.zig"); const look = @import("look.zig"); pub const tree = @import("ninep/tree.zig"); const sources_fs = @import("ninep/sources.zig"); -const th = @import("ninep/testing.zig"); +const th = @import("ninep/harness.zig"); const Pardes = pardes.Pardes; const MAX_PANES = pardes.MAX_PANES; diff --git a/core/keys.zig b/core/keys.zig index 92e78b0e..72e3bb3b 100644 --- a/core/keys.zig +++ b/core/keys.zig @@ -632,7 +632,7 @@ test "Del from a key asks which neighbour takes the rows; nothing else asks" { p.newScratchBelow(foot); const tail = p.view().active; { - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); const asked = foot; p.view().active = asked; p.sync(); diff --git a/core/look.zig b/core/look.zig index f7084079..1023a532 100644 --- a/core/look.zig +++ b/core/look.zig @@ -2302,7 +2302,7 @@ test "every entry point to Look takes one expansion: a diff line and a file:12:5 var fx: DiffFixture = undefined; try fx.init(true, &.{.{ "src/x.zig", numbered }}, diff_text); defer fx.tmp.cleanup(); - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); // `column` -1: wherever a click puts it, every other way the same. const Case = struct { text: []const u8, name: []const u8, row: u16, col: u16, sweep: [2]u16, line: i32, column: i32 }; const cases = [_]Case{ @@ -2391,7 +2391,7 @@ test "a tag word looked at by a click and by Enter is one record to an event rea var fx: DiffFixture = undefined; try fx.init(true, &.{.{ "src/x.zig", "line one\n" }}, diff_text); defer fx.tmp.cleanup(); - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 30 }); defer p.deinit(); const pane = try fx.open(p, diff_text); diff --git a/core/mouse.zig b/core/mouse.zig index 3d7c5af9..cefc0335 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -1497,7 +1497,7 @@ test "mouse thumb buttons navigate once and clear selection gestures" { } test "a chorded exec an event reader holds is told with its argument, and written back runs with it" { - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); const p = try th.withFile(std.testing.allocator, "Msg said\narg-text\n"); defer p.deinit(); const serial = p.panes[0].?.serial; diff --git a/core/ninep/addr.zig b/core/ninep/addr.zig index 72a2d815..fd343363 100644 --- a/core/ninep/addr.zig +++ b/core/ninep/addr.zig @@ -319,7 +319,7 @@ pub const Addr = struct { }; const testing = std.testing; -const th = @import("testing.zig"); +const th = @import("harness.zig"); const tree = @import("tree.zig"); const Node = tree.Node; const E = tree.E; diff --git a/core/ninep/cols.zig b/core/ninep/cols.zig index 561a5ffa..88d8ef88 100644 --- a/core/ninep/cols.zig +++ b/core/ninep/cols.zig @@ -241,7 +241,7 @@ pub fn remove(p: *Pardes, req: Req, serial: u32, file: tree.ColFile) Reply { // ---- tests ---- const testing = std.testing; -const th = @import("testing.zig"); +const th = @import("harness.zig"); const Node = tree.Node; test "New written to a column's exec makes its scratch in the session's directory, as tagexec's does" { diff --git a/core/ninep/ctl.zig b/core/ninep/ctl.zig index 72b18c88..7b4e2754 100644 --- a/core/ninep/ctl.zig +++ b/core/ninep/ctl.zig @@ -1323,7 +1323,7 @@ pub fn get(p: *Pardes, pane: *Pane) GetError!void { // ---- tests ---- const testing = std.testing; -const th = @import("testing.zig"); +const th = @import("harness.zig"); const call = th.call; const rd = th.rd; const wr = th.wr; diff --git a/core/ninep/events.zig b/core/ninep/events.zig index 93a1187a..1951a434 100644 --- a/core/ninep/events.zig +++ b/core/ninep/events.zig @@ -828,7 +828,7 @@ pub fn writeEvent(p: *Pardes, asked: Req, id: usize) Reply { // ---- tests ---- const testing = std.testing; -const th = @import("testing.zig"); +const th = @import("harness.zig"); const call = th.call; const rd = th.rd; const wr = th.wr; diff --git a/core/ninep/harness.zig b/core/ninep/harness.zig new file mode 100644 index 00000000..cf75d80d --- /dev/null +++ b/core/ninep/harness.zig @@ -0,0 +1,168 @@ +//! Test helpers shared by the control-tree modules: drive one request +//! through the core and collect the reply and the effects it raised. +const std = @import("std"); +const pardes = @import("../root.zig"); +const tree = @import("tree.zig"); +const cloud9 = @import("cloud9"); + +const Pardes = pardes.Pardes; +pub const Req = tree.Req; +pub const Reply = tree.Reply; +pub const E = tree.E; + +pub const Answer = struct { + reply: Reply = .{ .tag = 0, .status = .err, .errno = E.IO }, + bytes: []const u8 = "", + saved: bool = false, + watch: ?bool = null, + pty_buf: [256]u8 = undefined, + pty_len: usize = 0, + winsize: ?struct { cols: u16, rows: u16 } = null, + signal: ?pardes.PtySignal = null, + spawned: bool = false, + closed: bool = false, + + pub fn pty(a: *const Answer) []const u8 { + return a.pty_buf[0..a.pty_len]; + } + + pub fn errno(a: Answer) u16 { + return if (a.reply.status == .err) a.reply.errno else 0; + } +}; + +pub fn call(p: *Pardes, req: Req) Answer { + var ans: Answer = .{}; + ans.reply = p.serveFs(req); + // An EINVAL or ENOENT refusal says so in words a 9ns mount maps back + // to it (cloud9's enameErrno), or a shell through the mount hears EIO. + const said = ans.reply; + if (said.status == .err and said.ename.len > 0 and (said.errno == E.INVAL or said.errno == E.NOENT) and cloud9.fs.enameErrno(said.ename) != said.errno) { + std.debug.print("a mount reads this refusal as errno {d}, not {d}: \"{s}\"\n", .{ cloud9.fs.enameErrno(said.ename), said.errno, said.ename }); + @panic("a refusal's words do not say its errno"); + } + ans.bytes = p.fsPayload(ans.reply); + while (p.nextEffect()) |e| switch (e) { + .save_file, .save_text => ans.saved = true, + .watch => |w| ans.watch = w.on, + .write => |w| { + const b = w.bytes.slice(); + const n = @min(b.len, ans.pty_buf.len - ans.pty_len); + @memcpy(ans.pty_buf[ans.pty_len..][0..n], b[0..n]); + ans.pty_len += n; + }, + .resize_pty => |r| ans.winsize = .{ .cols = r.cols, .rows = r.rows }, + .signal_pty => |s| ans.signal = s.sig, + .close_pty => ans.closed = true, + .spawn => ans.spawned = true, + else => {}, + }; + return ans; +} + +pub fn rd(p: *Pardes, node: u64, off: u64, size: u32) Answer { + return call(p, .{ .tag = 1, .op = .read, .node = node, .off = off, .size = size }); +} + +/// Whether the log, read whole from a fresh open, holds `needle`. +pub fn logHas(p: *Pardes, needle: []const u8) bool { + return logCount(p, needle) > 0; +} + +/// How many times the log, read whole from a fresh open, holds `needle`. +pub fn logCount(p: *Pardes, needle: []const u8) usize { + const log = @backingInt(tree.TopFile.log); + const h = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle; + defer _ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h }); + const text = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = h, .size = 1 << 16 }).bytes; + return std.mem.count(u8, text, needle); +} + +pub fn wr(p: *Pardes, node: u64, data: []const u8) Answer { + return call(p, .{ .tag = 2, .op = .write, .node = node, .data = data }); +} + +pub fn rdir(p: *Pardes, node: u64, skip: u64) Answer { + return call(p, .{ .tag = 4, .op = .readdir, .node = node, .off = skip, .size = 4096 }); +} + +pub fn look_up(p: *Pardes, dir: u64, name: []const u8) Answer { + return call(p, .{ .tag = 3, .op = .lookup, .node = dir, .data = name }); +} + +/// Makes a pane the way a client does, by walking to /pane/new, and answers +/// the serial of the pane the walk landed on. +/// The open is what makes the pane; the handle it answers is the serial, which +/// is also what a read of that fid reports. +pub fn newPane(p: *Pardes) !u32 { + const made = call(p, .{ .tag = 8, .op = .open, .node = @backingInt(tree.TopFile.new) }); + if (made.reply.status != .ok or made.reply.handle == 0) return error.NoPane; + return made.reply.handle; +} + +pub fn rmdir(p: *Pardes, node: u64) Answer { + return call(p, .{ .tag = 9, .op = .release, .node = node, .remove = true }); +} + +pub fn withFile(gpa: std.mem.Allocator, text: []const u8) !*Pardes { + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + errdefer p.deinit(); + while (p.nextEffect()) |_| {} + _ = try p.setTestFile(text); + while (p.nextEffect()) |_| {} + return p; +} + +pub fn withTerm(gpa: std.mem.Allocator) !*Pardes { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + errdefer p.deinit(); + while (p.nextEffect()) |_| {} + std.debug.assert(p.panes[0].?.isTerminal()); + return p; +} + +pub fn serialOf(p: *Pardes) u32 { + return p.panes[0].?.serial; +} + +pub const Dirent = struct { node: u64, dir: bool, name: []const u8 }; + +pub fn dirents(bytes: []const u8, out: []Dirent) []Dirent { + var n: usize = 0; + var i: usize = 0; + while (i + 10 <= bytes.len and n < out.len) { + const node = std.mem.readInt(u64, bytes[i..][0..8], .little); + const kind = bytes[i + 8]; + const len = bytes[i + 9]; + i += 10; + if (i + len > bytes.len) break; + out[n] = .{ .node = node, .dir = kind == 1, .name = bytes[i .. i + len] }; + i += len; + n += 1; + } + return out[0..n]; +} + +pub fn nameAt(list: []const Dirent, want: []const u8) ?Dirent { + for (list) |d| if (std.mem.eql(u8, d.name, want)) return d; + return null; +} + +/// A host that starts shells and does nothing else: the core alone starts +/// none. +pub const starting: pardes.Host.VTable = .{ .spawn = startNothing }; + +fn startNothing(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} + +pub const FakeTty = struct { + taken: bool, + + pub const vtable: pardes.Host.VTable = .{ .tty_taken = answer }; + + fn answer(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { + _ = id; + const f: *FakeTty = @ptrCast(@alignCast(ctx.?)); + return f.taken; + } +}; diff --git a/core/ninep/pane.zig b/core/ninep/pane.zig index 8be26602..1d514a3c 100644 --- a/core/ninep/pane.zig +++ b/core/ninep/pane.zig @@ -1031,7 +1031,7 @@ pub fn truncate(p: *Pardes, pane: *Pane, file: PaneFile) tree.Status { // ---- tests ---- const testing = std.testing; -const th = @import("testing.zig"); +const th = @import("harness.zig"); const call = th.call; const rd = th.rd; const wr = th.wr; diff --git a/core/ninep/pty.zig b/core/ninep/pty.zig index 6f3a8af0..26bb7661 100644 --- a/core/ninep/pty.zig +++ b/core/ninep/pty.zig @@ -469,7 +469,7 @@ pub fn writeData(p: *Pardes, req: Req, id: usize) Reply { // ---- tests ---- const testing = std.testing; -const th = @import("testing.zig"); +const th = @import("harness.zig"); const events = @import("events.zig"); const call = th.call; const rd = th.rd; diff --git a/core/ninep/screen.zig b/core/ninep/screen.zig index 1e2791b1..1e31bac0 100644 --- a/core/ninep/screen.zig +++ b/core/ninep/screen.zig @@ -83,7 +83,7 @@ pub fn listenersText(p: *Pardes) std.Io.Writer.Error![]const u8 { // ---- tests ---- const testing = std.testing; -const th = @import("testing.zig"); +const th = @import("harness.zig"); const fs = @import("../fs.zig"); const call = th.call; const rd = th.rd; diff --git a/core/ninep/sources.zig b/core/ninep/sources.zig index 1d804e31..540506d4 100644 --- a/core/ninep/sources.zig +++ b/core/ninep/sources.zig @@ -109,7 +109,7 @@ pub fn handle(p: *Pardes, req: Req) Reply { } const testing = std.testing; -const th = @import("testing.zig"); +const th = @import("harness.zig"); const limits = @import("../memory.zig").limits; test "embedded sources appear under the root only when the build carries them" { diff --git a/core/ninep/testing.zig b/core/ninep/testing.zig deleted file mode 100644 index cf75d80d..00000000 --- a/core/ninep/testing.zig +++ /dev/null @@ -1,168 +0,0 @@ -//! Test helpers shared by the control-tree modules: drive one request -//! through the core and collect the reply and the effects it raised. -const std = @import("std"); -const pardes = @import("../root.zig"); -const tree = @import("tree.zig"); -const cloud9 = @import("cloud9"); - -const Pardes = pardes.Pardes; -pub const Req = tree.Req; -pub const Reply = tree.Reply; -pub const E = tree.E; - -pub const Answer = struct { - reply: Reply = .{ .tag = 0, .status = .err, .errno = E.IO }, - bytes: []const u8 = "", - saved: bool = false, - watch: ?bool = null, - pty_buf: [256]u8 = undefined, - pty_len: usize = 0, - winsize: ?struct { cols: u16, rows: u16 } = null, - signal: ?pardes.PtySignal = null, - spawned: bool = false, - closed: bool = false, - - pub fn pty(a: *const Answer) []const u8 { - return a.pty_buf[0..a.pty_len]; - } - - pub fn errno(a: Answer) u16 { - return if (a.reply.status == .err) a.reply.errno else 0; - } -}; - -pub fn call(p: *Pardes, req: Req) Answer { - var ans: Answer = .{}; - ans.reply = p.serveFs(req); - // An EINVAL or ENOENT refusal says so in words a 9ns mount maps back - // to it (cloud9's enameErrno), or a shell through the mount hears EIO. - const said = ans.reply; - if (said.status == .err and said.ename.len > 0 and (said.errno == E.INVAL or said.errno == E.NOENT) and cloud9.fs.enameErrno(said.ename) != said.errno) { - std.debug.print("a mount reads this refusal as errno {d}, not {d}: \"{s}\"\n", .{ cloud9.fs.enameErrno(said.ename), said.errno, said.ename }); - @panic("a refusal's words do not say its errno"); - } - ans.bytes = p.fsPayload(ans.reply); - while (p.nextEffect()) |e| switch (e) { - .save_file, .save_text => ans.saved = true, - .watch => |w| ans.watch = w.on, - .write => |w| { - const b = w.bytes.slice(); - const n = @min(b.len, ans.pty_buf.len - ans.pty_len); - @memcpy(ans.pty_buf[ans.pty_len..][0..n], b[0..n]); - ans.pty_len += n; - }, - .resize_pty => |r| ans.winsize = .{ .cols = r.cols, .rows = r.rows }, - .signal_pty => |s| ans.signal = s.sig, - .close_pty => ans.closed = true, - .spawn => ans.spawned = true, - else => {}, - }; - return ans; -} - -pub fn rd(p: *Pardes, node: u64, off: u64, size: u32) Answer { - return call(p, .{ .tag = 1, .op = .read, .node = node, .off = off, .size = size }); -} - -/// Whether the log, read whole from a fresh open, holds `needle`. -pub fn logHas(p: *Pardes, needle: []const u8) bool { - return logCount(p, needle) > 0; -} - -/// How many times the log, read whole from a fresh open, holds `needle`. -pub fn logCount(p: *Pardes, needle: []const u8) usize { - const log = @backingInt(tree.TopFile.log); - const h = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle; - defer _ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h }); - const text = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = h, .size = 1 << 16 }).bytes; - return std.mem.count(u8, text, needle); -} - -pub fn wr(p: *Pardes, node: u64, data: []const u8) Answer { - return call(p, .{ .tag = 2, .op = .write, .node = node, .data = data }); -} - -pub fn rdir(p: *Pardes, node: u64, skip: u64) Answer { - return call(p, .{ .tag = 4, .op = .readdir, .node = node, .off = skip, .size = 4096 }); -} - -pub fn look_up(p: *Pardes, dir: u64, name: []const u8) Answer { - return call(p, .{ .tag = 3, .op = .lookup, .node = dir, .data = name }); -} - -/// Makes a pane the way a client does, by walking to /pane/new, and answers -/// the serial of the pane the walk landed on. -/// The open is what makes the pane; the handle it answers is the serial, which -/// is also what a read of that fid reports. -pub fn newPane(p: *Pardes) !u32 { - const made = call(p, .{ .tag = 8, .op = .open, .node = @backingInt(tree.TopFile.new) }); - if (made.reply.status != .ok or made.reply.handle == 0) return error.NoPane; - return made.reply.handle; -} - -pub fn rmdir(p: *Pardes, node: u64) Answer { - return call(p, .{ .tag = 9, .op = .release, .node = node, .remove = true }); -} - -pub fn withFile(gpa: std.mem.Allocator, text: []const u8) !*Pardes { - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); - errdefer p.deinit(); - while (p.nextEffect()) |_| {} - _ = try p.setTestFile(text); - while (p.nextEffect()) |_| {} - return p; -} - -pub fn withTerm(gpa: std.mem.Allocator) !*Pardes { - if (pardes.Term.ops == null) return error.SkipZigTest; // a shell - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); - errdefer p.deinit(); - while (p.nextEffect()) |_| {} - std.debug.assert(p.panes[0].?.isTerminal()); - return p; -} - -pub fn serialOf(p: *Pardes) u32 { - return p.panes[0].?.serial; -} - -pub const Dirent = struct { node: u64, dir: bool, name: []const u8 }; - -pub fn dirents(bytes: []const u8, out: []Dirent) []Dirent { - var n: usize = 0; - var i: usize = 0; - while (i + 10 <= bytes.len and n < out.len) { - const node = std.mem.readInt(u64, bytes[i..][0..8], .little); - const kind = bytes[i + 8]; - const len = bytes[i + 9]; - i += 10; - if (i + len > bytes.len) break; - out[n] = .{ .node = node, .dir = kind == 1, .name = bytes[i .. i + len] }; - i += len; - n += 1; - } - return out[0..n]; -} - -pub fn nameAt(list: []const Dirent, want: []const u8) ?Dirent { - for (list) |d| if (std.mem.eql(u8, d.name, want)) return d; - return null; -} - -/// A host that starts shells and does nothing else: the core alone starts -/// none. -pub const starting: pardes.Host.VTable = .{ .spawn = startNothing }; - -fn startNothing(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} - -pub const FakeTty = struct { - taken: bool, - - pub const vtable: pardes.Host.VTable = .{ .tty_taken = answer }; - - fn answer(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { - _ = id; - const f: *FakeTty = @ptrCast(@alignCast(ctx.?)); - return f.taken; - } -}; diff --git a/core/ninep/tree.zig b/core/ninep/tree.zig index d5a6b2fe..46a4e3d2 100644 --- a/core/ninep/tree.zig +++ b/core/ninep/tree.zig @@ -1504,7 +1504,7 @@ fn writeSend(p: *Pardes, req: Req) Reply { // ---- tests ---- const testing = std.testing; -const th = @import("testing.zig"); +const th = @import("harness.zig"); const config = @import("../config.zig"); const call = th.call; const rd = th.rd; diff --git a/core/sam_edit.zig b/core/sam_edit.zig index 0f4a70ba..700c2c01 100644 --- a/core/sam_edit.zig +++ b/core/sam_edit.zig @@ -1158,7 +1158,7 @@ fn expectEdit(text: []const u8, command: []const u8, want: []const u8) !void { } test "an x over 100k lines is 100k searches, each with its own step budget" { - const th = @import("ninep/testing.zig"); + const th = @import("ninep/harness.zig"); const line = &@as([80]u8, @splat('x')) ++ "\n"; const text = try std.testing.allocator.alloc(u8, line.len * 100_000); defer std.testing.allocator.free(text); diff --git a/core/tape.zig b/core/tape.zig index 00486666..5ba999bf 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -1777,7 +1777,7 @@ test "every place that sets the global tape aside sets the core's field aside: a test "a client's frame drawn between its steps records nothing into its idle tape" { const gpa = std.testing.allocator; - const p = try @import("ninep/testing.zig").withFile(gpa, "x\n"); + const p = try @import("ninep/harness.zig").withFile(gpa, "x\n"); defer p.deinit(); const bytes = try pardes.dump.dumpBytes(p); defer p.gpa.free(bytes); -- cgit v1.3