diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ninep/ctl.zig | 285 |
1 files changed, 8 insertions, 277 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index bb0fcb96..fc09c730 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -1580,204 +1580,28 @@ test "look validates the whole batch before opening virtual files" { try testing.expectEqualStrings("source\n", source.file.?.content); } -test "get reloads the pane from disk and reports a missing file without losing dirty contents" { - const gpa = testing.allocator; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); - var directory_buf: [4096]u8 = undefined; - const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; - var path_buf: [4096]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/note.txt", .{directory}); - const p = try withFile(gpa, "in memory\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - const pane = p.panes[0].?; - - var name: [std.fs.max_path_bytes + 8]u8 = undefined; - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}\n", .{path})); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "get\n").reply.status); - try testing.expectEqualStrings("from disk\n", pane.file.?.content); - try testing.expect(!pane_files.dirtyOf(pane)); - try testing.expect(pane.file.?.history.undo_len > 0); - // Renamed but never edited, it was clean: nothing thrown away, nothing - // logged as reloaded over edits. - var reloaded: [32]u8 = undefined; - try testing.expect(!th.logHas(p, try std.fmt.bufPrint(&reloaded, "changed {d} reloaded\n", .{serial}))); - - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}/missing.txt\n", .{directory})); - _ = wr(p, Node.of(serial, .body), "unsaved\n"); - // Unsaved text is asked about once, as acme's get asks winclean. - const asked = wr(p, ctl_node, "get\n"); - try testing.expectEqual(E.IO, asked.errno()); - try testing.expect(std.mem.endsWith(u8, asked.reply.ename, ": Modified (get again to discard)")); - var unsaved: [4200]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&unsaved, "unsaved {d} {s}/missing.txt\n", .{ serial, directory }))); - try testing.expectEqual(E.NOENT, wr(p, ctl_node, "get\n").errno()); - try testing.expect(p.paneBySerial(serial) != null); - try testing.expectEqualStrings("from disk\nunsaved\n", pane.file.?.content); - try testing.expect(pane_files.dirtyOf(pane)); -} - -test "Get reads the pane's file again, refusing unsaved edits once, and Get file loads that file into the pane" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "other.txt", .data = "the other\n" }); - var directory_buf: [4096]u8 = undefined; - const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; - const p = try withFile(testing.allocator, "in memory\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var name: [std.fs.max_path_bytes + 8]u8 = undefined; - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}/note.txt\n", .{directory})); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqualStrings("from disk\n", pane.file.?.content); - try testing.expect(!pane_files.dirtyOf(pane)); - // Edited: asked once, then read again. - _ = wr(p, Node.of(serial, .body), "unsaved\n"); - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expect(p.fs.failures != failures); - try testing.expectEqualStrings("from disk\nunsaved\n", pane.file.?.content); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqualStrings("from disk\n", pane.file.?.content); - // Get file: that file, by a name from the pane's directory. - try testing.expect(p.executeBuiltinLine(0, "Get other.txt")); - try testing.expectEqualStrings("the other\n", pane.file.?.content); - try testing.expect(std.mem.endsWith(u8, pane.file.?.path, "/other.txt")); - try testing.expect(!pane_files.dirtyOf(pane)); -} - - -test "Zerox makes a second pane on one buffer: one text, undo and dirty state, its own cursor; Save cleans both, Del of one asks nothing" { +test "Zerox twins: a Save from one, performed, cleans both" { + // The rest of Zerox is test/behaviour/panes.zig's; this needs the save done. var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); var dir_buf: [4096]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - const p = try withFile(testing.allocator, "one\ntwo\nthree\n"); + const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); - const first = serialOf(p); var name: [std.fs.max_path_bytes + 8]u8 = undefined; - _ = wr(p, Node.of(first, .name), try std.fmt.bufPrint(&name, "{s}/z.txt\n", .{dir})); + _ = wr(p, Node.of(serialOf(p), .name), try std.fmt.bufPrint(&name, "{s}/z.txt\n", .{dir})); try testing.expect(p.executeBuiltinLine(0, "Zerox")); const twin_id = for (p.panes, 0..) |slot, id| { if (id != 0) if (slot) |pane| if (pane.file != null) break id; } else return error.NoTwin; const a = p.panes[0].?; const b = p.panes[twin_id].?; - // Both in the index, each its serial, one name. - const index = rd(p, @backingInt(tree.TopFile.index), 0, 4096).bytes; - try testing.expectEqual(@as(usize, 2), std.mem.count(u8, index, "/z.txt")); - // b's cursor on `three`; a line written above it in a moves it down. - b.body.cur_row = 2; - b.body.cur_col = 1; - _ = wr(p, Node.of(first, .addr), "0"); - try testing.expectEqual(Status.ok, wr(p, Node.of(first, .data), "zero\n").reply.status); - try testing.expectEqualStrings("zero\none\ntwo\nthree\n", b.file.?.content); - try testing.expectEqual(a.file.?.content.ptr, b.file.?.content.ptr); - try testing.expectEqual(@as(i32, 3), b.body.cur_row); - try testing.expectEqual(@as(i32, 1), b.body.cur_col); - try testing.expect(pane_files.dirtyOf(a) and pane_files.dirtyOf(b)); - // A write through b is a's too; Undo in a takes it back from both. - try testing.expectEqual(Status.ok, wr(p, Node.of(b.serial, .body), "four\n").reply.status); - try testing.expectEqualStrings("zero\none\ntwo\nthree\nfour\n", a.file.?.content); - try testing.expect(p.executeBuiltinLine(0, "Undo")); - try testing.expectEqualStrings("zero\none\ntwo\nthree\n", b.file.?.content); - // Saved from b: both come clean. + try testing.expectEqual(Status.ok, wr(p, Node.of(b.serial, .body), "two\n").reply.status); try testing.expect(p.executeBuiltinLine(twin_id, "Save")); while (p.nextEffect()) |e| p.perform(e); try testing.expect(!pane_files.dirtyOf(a) and !pane_files.dirtyOf(b)); - try testing.expectEqualStrings("zero\none\ntwo\nthree\n", p.fallback.get(b.file.?.path).?); - // Edited again, Del of a asks nothing: b holds the text. - try testing.expectEqual(Status.ok, wr(p, Node.of(b.serial, .body), "five\n").reply.status); - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(0, "Del")); - p.sync(); - try testing.expectEqual(failures, p.fs.failures); - try testing.expect(p.paneBySerial(first) == null); - try testing.expect(pane_files.dirtyOf(b)); - try testing.expectEqualStrings("zero\none\ntwo\nthree\nfive\n", b.file.?.content); - // Alone now, it is no twin; Del asks about it as about any pane. - try testing.expectEqual(@as(u32, 0), b.file.?.twin); - try testing.expect(p.executeBuiltinLine(p.paneBySerial(b.serial).?, "Del")); - try testing.expect(p.fs.failures != failures); -} - -test "Undo after a load under another name (Get file, Edit e) puts the old name back with the old text and its clean state; Putall then writes nothing over the loaded file" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "other.txt", .data = "other\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "keep.txt", .data = "keep\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var other_buf: [4200]u8 = undefined; - const other = try std.fmt.bufPrint(&other_buf, "{s}/other.txt", .{dir}); - var keep_buf: [4200]u8 = undefined; - const keep = try std.fmt.bufPrint(&keep_buf, "{s}/keep.txt", .{dir}); - for ([_][]const u8{ "Get keep.txt", "Edit e keep.txt" }) |load| { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var name: [4300]u8 = undefined; - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}\n", .{other})); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqualStrings("other\n", pane.file.?.content); - try testing.expect(!pane_files.dirtyOf(pane)); - // Loaded: keep.txt's text under its name. - try testing.expect(p.executeBuiltinLine(0, load)); - try testing.expectEqualStrings("keep\n", pane.file.?.content); - try testing.expectEqualStrings(keep, pane.file.?.path); - if (std.mem.startsWith(u8, load, "Get")) try testing.expect(!pane_files.dirtyOf(pane)); - // Undo: other.txt's text, other.txt's name, clean as it was. - try testing.expect(p.executeBuiltinLine(0, "Undo")); - try testing.expectEqualStrings("other\n", pane.file.?.content); - try testing.expectEqualStrings(other, pane.file.?.path); - try testing.expect(!pane_files.dirtyOf(pane)); - // Putall saves nothing, and nothing lands on keep.txt. - while (p.nextEffect()) |_| {} - try testing.expect(p.executeBuiltinLine(0, "Putall")); - while (p.nextEffect()) |e| try testing.expect(e != .save_file); - // Redo: keep.txt again. - try testing.expect(p.executeBuiltinLine(0, "Redo")); - try testing.expectEqualStrings(keep, pane.file.?.path); - try testing.expectEqualStrings("keep\n", pane.file.?.content); - } -} - -test "a Get file that fails changes nothing: no such file, a directory, or unsaved edits asked about once" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "here.txt", .data = "here\n" }); - try tmp.dir.createDirPath(testing.io, "sub"); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var name: [4300]u8 = undefined; - const here = try std.fmt.bufPrint(&name, "{s}/here.txt\n", .{dir}); - _ = wr(p, Node.of(serial, .name), here); - try testing.expect(p.executeBuiltinLine(0, "Get")); - const undo_len = pane.file.?.history.undo_len; - for ([_][]const u8{ "Get nosuch.txt", "Get sub" }) |line| { - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(0, line)); - try testing.expect(p.fs.failures != failures); - try testing.expectEqualStrings(here[0 .. here.len - 1], pane.file.?.path); - try testing.expectEqualStrings("here\n", pane.file.?.content); - try testing.expectEqual(undo_len, pane.file.?.history.undo_len); - } - // Edited, the first Get asks and renames nothing. - _ = wr(p, Node.of(serial, .body), "more\n"); - try testing.expect(p.executeBuiltinLine(0, "Get other.txt")); - try testing.expectEqualStrings(here[0 .. here.len - 1], pane.file.?.path); - try testing.expectEqualStrings("here\nmore\n", pane.file.?.content); + try testing.expectEqualStrings("one\ntwo\n", p.fallback.get(b.file.?.path).?); } test "Zerox twins dump their text once and come back on one buffer from a Restore" { @@ -1809,36 +1633,6 @@ test "Zerox twins dump their text once and come back on one buffer from a Restor try testing.expect(std.mem.startsWith(u8, files[0].content, "one\ntwo\nunsaved ")); } -test "a bad Tab width and a bad Incl directory are refused invalid, changing nothing; a twin's rename is each twin's" { - defer config.tab_now = config.tab_width; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - for ([_][]const u8{ "Tab 0\n", "Tab 17\n", "Tab x\n" }) |line| { - try testing.expectEqual(E.INVAL, wr(p, root_ctl, line).errno()); - // Run from a pane, as a tag click runs it: said invalid too. - const r = wr(p, Node.of(serialOf(p), .exec), line); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "invalid tab width") != null); - } - try testing.expectEqual(config.tab_width, config.tab_now); - const before = p.incl.items.len; - try testing.expectEqual(E.INVAL, wr(p, root_ctl, "Incl rel/dir\n").errno()); - try testing.expectEqual(E.INVAL, wr(p, root_ctl, "Incl - /usr/include\n").errno()); - if (comptime pardes.hosted) try testing.expectEqual(E.NOENT, wr(p, root_ctl, "Incl /tmp/pardes-surely-no-such-dir\n").errno()); - try testing.expectEqual(before, p.incl.items.len); - // A twin renamed: a rename record for each. - const first = serialOf(p); - try testing.expect(p.executeBuiltinLine(0, "Zerox")); - const twin = for (p.panes) |slot| { - if (slot) |pane| if (pane.serial != first and pane.file != null) break pane.serial; - } else return error.NoTwin; - _ = wr(p, Node.of(first, .name), "/tmp/pardes-twin-renamed.txt\n"); - var rec: [64]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "rename {d} /tmp/pardes-twin-renamed.txt\n", .{first}))); - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "rename {d} /tmp/pardes-twin-renamed.txt\n", .{twin}))); -} - test "Undo to unsaved text under its old name says nothing changed on disk when nothing did, and Save then writes" { var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); @@ -1872,73 +1666,10 @@ test "Undo to unsaved text under its old name says nothing changed on disk when } } -test "round 39's small refusals: one name an open, small scratches unasked, twins saved once, bare Tab on ctl, and errnos in words a mount reads" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "f\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "noread.txt", .data = "secret\n" }); - try tmp.dir.createDirPath(testing.io, "sub"); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var line: [4300]u8 = undefined; - // One name an open: a second line, in the write or after, is refused. - try testing.expectEqual(E.INVAL, wr(p, Node.of(serial, .name), "/tmp/pardes-a\nb").errno()); - const before = try testing.allocator.dupe(u8, pane.file.?.path); - defer testing.allocator.free(before); - const h = call(p, .{ .tag = 1, .op = .open, .node = Node.of(serial, .name), .omode = 1 }).reply.handle; - try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = Node.of(serial, .name), .handle = h, .data = "/tmp/pardes-one.txt\n" }).reply.status); - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 3, .op = .write, .node = Node.of(serial, .name), .handle = h, .data = "b" }).errno()); - _ = call(p, .{ .tag = 4, .op = .release, .node = Node.of(serial, .name), .handle = h, .opened = true }); - tree.runClosedLines(p); - try testing.expectEqualStrings("/tmp/pardes-one.txt", pane.file.?.path); - // Bare Tab on the root ctl says the width, as from a pane. - try testing.expectEqual(Status.ok, wr(p, @backingInt(tree.TopFile.ctl), "Tab\n").reply.status); - // Errnos a mount reads from the words. - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&line, "{s}/f.txt\n", .{dir})); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqual(E.ISDIR, wr(p, Node.of(serial, .exec), "Get sub\n").errno()); - if (comptime pardes.hosted) { - var noread_buf: [4300]u8 = undefined; - _ = std.c.chmod(try std.fmt.bufPrintSentinel(&noread_buf, "{s}/noread.txt", .{dir}, 0), 0); - try testing.expectEqual(@as(u16, 13), wr(p, Node.of(serial, .exec), "Get noread.txt\n").errno()); - try testing.expectEqual(E.NOTDIR, wr(p, Node.of(serial, .exec), try std.fmt.bufPrint(&line, "Incl {s}/f.txt\n", .{dir})).errno()); - const zero = wr(p, Node.of(serial, .look), "/dev/zero\n"); - _ = zero; - try testing.expect(th.logHas(p, "not a regular file")); - } - // Twins saved once by Putall. - _ = wr(p, Node.of(serial, .body), "edited\n"); - try testing.expect(p.executeBuiltinLine(0, "Zerox")); - while (p.nextEffect()) |_| {} - try testing.expect(p.executeBuiltinLine(0, "Putall")); - var saves: usize = 0; - while (p.nextEffect()) |e| if (e == .save_file) { - saves += 1; - }; - try testing.expectEqual(@as(usize, 1), saves); - // A scratch of 2 bytes is loaded over unasked. - try testing.expect(p.executeBuiltinLine(0, "New")); - const scratch = p.panes[p.active].?; - _ = pane_files.spliceBody(p, scratch, 0, 0, "hi") orelse return error.NoScratch; - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(p.active, try std.fmt.bufPrint(&line, "Get {s}/f.txt", .{dir}))); - try testing.expectEqual(failures, p.fs.failures); - try testing.expectEqualStrings("f\n", scratch.file.?.content); -} - -test "Get of a file not there says so without asking about unsaved edits; the init file saved says a bad line as startup does" { +test "the init file saved says a bad line as startup does" { + // Get of a file not there is test/behaviour/panes.zig's. const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .startup_config = "Theme dusk\n", .startup_config_path = "/tmp/pardes-init-test/init" }); defer p.deinit(); - const pane = try p.setTestFile("x\n"); - _ = wr(p, Node.of(pane.serial, .body), "unsaved\n"); - try testing.expect(p.executeBuiltinLine(0, "Get /tmp/pardes-surely-no-such-file.txt")); - try testing.expect(th.logHas(p, "no such file")); - try testing.expect(!th.logHas(p, "again to discard")); - try testing.expect(pane.discard_warned == null); pardes.exec.reapplyInitFile(p, "/tmp/pardes-init-test/init", "Theme acme\nnosuchword 1\nNewcol\n"); try testing.expect(th.logHas(p, "err - init file line 2: unknown word \"nosuchword 1\"")); try testing.expect(!th.logHas(p, "init file line 3")); |
