const std = @import("std");
const repeat = @import("zit").repeat;
const support = @import("support.zig");
const addManaged = support.addManaged;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const postXferCards = support.postXferCards;
const readFile = support.readFile;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const spawnServe = support.spawnServe;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
test "cli config -R: import applies to a bare store's user area, the file still read from the cwd" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var store = std.testing.tmpDir(.{});
defer store.cleanup();
var away = std.testing.tmpDir(.{});
defer away.cleanup();
try initRepo(gpa, io, store.dir);
const cwd = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd);
const store_abs = try std.fs.path.join(gpa, &.{ cwd, ".zig-cache", "tmp", &store.sub_path });
defer gpa.free(store_abs);
// The import FILE is written into `away` (the cwd), while -R names the
// store: only the store is redirected, the path is not.
try writeFile(io, away.dir, "users.txt",
\\# The "user" configuration exported from
\\# repository "tk.fossil"
\\config /user 61
\\1700000000 'alice' pw 'deadbeef' cap 's' info 'Admin' jx '{}'
\\config /user 45
\\1700000000 'nobody' pw '' cap 'gjorz' info ''
\\
);
{
var r = try runZit(gpa, io, away.dir, &.{ "config", "import", "users.txt", "-R", store_abs });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, "2 rows imported") != null);
}
// The rows are readable back through -R -- the same store table `users`
// already reached on a bare store.
{
var u = try runZit(gpa, io, away.dir, &.{ "users", "-R", store_abs });
defer freeResult(gpa, &u);
try expectExitDiag(u.term, 0, u);
try std.testing.expectEqualStrings(
"alice\ts\t1700000000\tAdmin\nnobody\tgjorz\t1700000000\t\n",
u.stdout,
);
}
}
test "cli config export: a table backed up, replaced, and restored comes back as it was, in fossil's own card format" {
// import replaces a configuration area; without its counterpart there was
// no way to keep what it replaced. The round trip is the whole contract:
// export, import something else, import the export, and the table is back.
const gpa = std.testing.allocator;
const io = std.testing.io;
const t_ = std.testing;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "users.txt",
\\config /user 53
\\1700000000 'alice' pw 'deadbeef' cap 's' info 'Admin'
\\config /user 42
\\1700000000 'bob' pw '' cap 'ei' info 'Dev'
\\config /user 45
\\1700000000 'nobody' pw '' cap 'gjorz' info ''
\\
);
{
var r = try runZit(gpa, io, tmp.dir, &.{ "config", "import", "users.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
var before = try runZit(gpa, io, tmp.dir, &.{"users"});
defer freeResult(gpa, &before);
try expectExitDiag(before.term, 0, before);
// Byte for byte what fossil writes for the same rows: one card per row,
// `config /user SIZE`, the payload, a newline. A banner line leads, as
// fossil's does, and every reader skips it.
var exported = try runZit(gpa, io, tmp.dir, &.{ "config", "export", "user" });
defer freeResult(gpa, &exported);
try expectExitDiag(exported.term, 0, exported);
try t_.expect(std.mem.startsWith(u8, exported.stdout, "# The \"user\" configuration exported from\n"));
try t_.expect(std.mem.indexOf(u8, exported.stdout, "config /user 45\n1700000000 'nobody' pw '' cap 'gjorz' info ''\n") != null);
try t_.expect(std.mem.indexOf(u8, exported.stdout, "config /user 53\n1700000000 'alice' pw 'deadbeef' cap 's' info 'Admin'\n") != null);
try t_.expectEqual(@as(usize, 3), std.mem.count(u8, exported.stdout, "config /user "));
// To a file: the same bytes, and a receipt that names where they went.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "config", "export", "user", "backup.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try t_.expectEqualStrings("user: 3 rows exported to backup.txt\n", r.stdout);
const on_disk = try readFile(gpa, io, tmp.dir, "backup.txt");
defer gpa.free(on_disk);
try t_.expectEqualStrings(exported.stdout, on_disk);
}
// Replace the table with something else entirely, then restore.
try writeFile(io, tmp.dir, "other.txt",
\\config /user 43
\\1900000000 'carol' pw '' cap 'v' info 'New'
\\
);
{
var r = try runZit(gpa, io, tmp.dir, &.{ "config", "import", "other.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
var u = try runZit(gpa, io, tmp.dir, &.{"users"});
defer freeResult(gpa, &u);
try t_.expectEqualStrings("carol\tv\t1900000000\tNew\n", u.stdout);
}
{
var r = try runZit(gpa, io, tmp.dir, &.{ "config", "import", "backup.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
var u = try runZit(gpa, io, tmp.dir, &.{"users"});
defer freeResult(gpa, &u);
try t_.expectEqualStrings(before.stdout, u.stdout);
}
}
test "cli config export: shun rows travel by file too, and a file never un-shuns anything" {
// The shun table is a takedown record. Export carries it; import and merge
// both ADD its rows. Replacing it would un-shun whatever a file did not
// name, which zit never does, so an import of a file without the row
// leaves the row where it is.
const gpa = std.testing.allocator;
const io = std.testing.io;
const t_ = std.testing;
const hash = repeat("a", 64);
const reason = "court order";
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "shun.txt", "config /shun 96\n1700000000 '" ++ hash ++ "' scom '" ++ reason ++ "'\n");
{
var r = try runZit(gpa, io, tmp_a.dir, &.{ "config", "merge", "shun.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try t_.expect(std.mem.indexOf(u8, r.stdout, "shun: 1 row added") != null);
}
var exported = try runZit(gpa, io, tmp_a.dir, &.{ "config", "export", "shun" });
defer freeResult(gpa, &exported);
try expectExitDiag(exported.term, 0, exported);
try t_.expect(std.mem.indexOf(u8, exported.stdout, "config /shun 96\n1700000000 '" ++ hash ++ "' scom '" ++ reason ++ "'\n") != null);
// Into a fresh repository by file alone.
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
try initRepo(gpa, io, tmp_b.dir);
try writeFile(io, tmp_b.dir, "backup.txt", exported.stdout);
{
var r = try runZit(gpa, io, tmp_b.dir, &.{ "config", "import", "backup.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
var list = try runZit(gpa, io, tmp_b.dir, &.{ "shun", "--list" });
defer freeResult(gpa, &list);
try expectExitDiag(list.term, 0, list);
try t_.expect(std.mem.indexOf(u8, list.stdout, hash) != null);
try t_.expect(std.mem.indexOf(u8, list.stdout, reason) != null);
}
// `import` of a file that names no shun row replaces the user table and
// leaves the shun table alone.
try writeFile(io, tmp_b.dir, "users.txt", "config /user 43\n1900000000 'carol' pw '' cap 'v' info 'New'\n");
{
var r = try runZit(gpa, io, tmp_b.dir, &.{ "config", "import", "users.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
var list = try runZit(gpa, io, tmp_b.dir, &.{ "shun", "--list" });
defer freeResult(gpa, &list);
try t_.expect(std.mem.indexOf(u8, list.stdout, hash) != null);
}
}
/// `fossil configuration export interwiki` output, byte for byte, from a
/// fossil 2.28 repository holding two interwiki tags.
const fossil_interwiki_export =
\\# The "interwiki" configuration exported from
\\# repository "r.fossil"
\\# on 2026-10-30 21:03:51
\\config /config 100
\\1793394230 'interwiki:bugs' value '{"base":"https://sqlite.org/bugs","hash":"/info","wiki":"/wiki"}'
\\config /config 99
\\1793394230 'interwiki:wp' value '{"base":"https://en.wikipedia.org/wiki/","hash":null,"wiki":null}'
\\
;
const interwiki_bugs_card = "config /config 100\n1793394230 'interwiki:bugs' value '{\"base\":\"https://sqlite.org/bugs\",\"hash\":\"/info\",\"wiki\":\"/wiki\"}'\n";
const interwiki_wp_card = "config /config 99\n1793394230 'interwiki:wp' value '{\"base\":\"https://en.wikipedia.org/wiki/\",\"hash\":null,\"wiki\":null}'\n";
test "cli config interwiki: import, merge and export carry fossil's interwiki rows, and a malformed row is skipped" {
const gpa = std.testing.allocator;
const io = std.testing.io;
const t_ = std.testing;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "iw.txt", fossil_interwiki_export);
{
var r = try runZit(gpa, io, tmp.dir, &.{ "config", "import", "iw.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try t_.expectEqualStrings("interwiki: 2 rows imported; 2 in the table\n", r.stdout);
}
{
var e = try runZit(gpa, io, tmp.dir, &.{ "config", "export", "interwiki" });
defer freeResult(gpa, &e);
try expectExitDiag(e.term, 0, e);
try t_.expect(std.mem.startsWith(u8, e.stdout, "# The \"interwiki\" configuration exported from\n"));
try t_.expect(std.mem.endsWith(u8, e.stdout, interwiki_bugs_card ++ interwiki_wp_card));
}
// MERGE keeps the newer row: an older bugs row loses, a newer one wins.
try writeFile(io, tmp.dir, "older.txt", "config /config 83\n1 'interwiki:bugs' value '{\"base\":\"https://older.example\",\"hash\":null,\"wiki\":null}'\n");
{
var r = try runZit(gpa, io, tmp.dir, &.{ "config", "merge", "older.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try t_.expectEqualStrings("interwiki: 0 rows merged; 2 in the table\n", r.stdout);
var e = try runZit(gpa, io, tmp.dir, &.{ "config", "export", "interwiki" });
defer freeResult(gpa, &e);
try t_.expect(std.mem.indexOf(u8, e.stdout, interwiki_bugs_card) != null);
}
// IMPORT replaces the row whatever its mtime, as fossil's does, and leaves
// the rows the file does not name.
{
var r = try runZit(gpa, io, tmp.dir, &.{ "config", "import", "older.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try t_.expectEqualStrings("interwiki: 1 row imported; 2 in the table\n", r.stdout);
var e = try runZit(gpa, io, tmp.dir, &.{ "config", "export", "interwiki" });
defer freeResult(gpa, &e);
try t_.expect(std.mem.indexOf(u8, e.stdout, "https://older.example") != null);
try t_.expect(std.mem.indexOf(u8, e.stdout, interwiki_wp_card) != null);
try t_.expect(std.mem.indexOf(u8, e.stdout, "sqlite.org") == null);
}
// A row with no base, a tag fossil would refuse, a value that is not JSON:
// each is counted and skipped, the good row beside them lands, and a
// `config` row of another area is ignored rather than stored.
try writeFile(io, tmp.dir, "bad.txt",
\\config /config 31
\\1 'interwiki:x' value 'notjson'
\\config /config 37
\\1 'interwiki:y' value '{"hash":"/h"}'
\\config /config 50
\\1 'interwiki:bad tag' value '{"base":"https://b"}'
\\config /config 29
\\1 'project-name' value 'evil'
\\config /config 55
\\1893394230 'interwiki:ok' value '{"base":"https://ok"}'
\\
);
{
var r = try runZit(gpa, io, tmp.dir, &.{ "config", "merge", "bad.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 1, r);
try t_.expectEqualStrings("interwiki: 1 row merged; 3 in the table\n3 unreadable and skipped\n1 card in other areas ignored\n", r.stdout);
var e = try runZit(gpa, io, tmp.dir, &.{ "config", "export", "interwiki" });
defer freeResult(gpa, &e);
try t_.expectEqual(@as(usize, 3), std.mem.count(u8, e.stdout, "config /config "));
try t_.expect(std.mem.indexOf(u8, e.stdout, "project-name") == null);
}
// The rows survive a pack, the store still verifies, and an export to a
// file says where it went.
var before = try runZit(gpa, io, tmp.dir, &.{ "config", "export", "interwiki" });
defer freeResult(gpa, &before);
{
var p = try runZit(gpa, io, tmp.dir, &.{"pack"});
defer freeResult(gpa, &p);
try expectExitDiag(p.term, 0, p);
var v = try runZit(gpa, io, tmp.dir, &.{"verify"});
defer freeResult(gpa, &v);
try expectExitDiag(v.term, 0, v);
var r = try runZit(gpa, io, tmp.dir, &.{ "config", "export", "interwiki", "backup.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try t_.expectEqualStrings("interwiki: 3 rows exported to backup.txt\n", r.stdout);
const on_disk = try readFile(gpa, io, tmp.dir, "backup.txt");
defer gpa.free(on_disk);
try t_.expectEqualStrings(before.stdout, on_disk);
}
}
test "cli config pull interwiki: a served repository's rows land in the puller, merging, and a clone carries none" {
const gpa = std.testing.allocator;
const io = std.testing.io;
const t_ = std.testing;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "origin", .default_dir);
var origin = try tmp.dir.openDir(io, "origin", .{});
defer origin.close(io);
try initRepo(gpa, io, origin);
try writeFile(io, origin, "a.txt", "a\n");
try addManaged(gpa, io, origin, &.{"a.txt"});
{
var c = try runCommitEnv(gpa, io, origin, .{ .user = "alice" }, &.{ "commit", "-m", "a" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
}
try writeFile(io, origin, "iw.txt", fossil_interwiki_export);
{
var r = try runZit(gpa, io, origin, &.{ "config", "import", "iw.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, origin, port);
defer server.kill(io);
try waitForListen(io, port);
// The wire: fossil's `config /config` cards, one per row.
const cards = try postXferCards(gpa, io, port, "pragma client-version 20000\nreqconfig /interwiki\n");
defer gpa.free(cards);
try t_.expect(std.mem.indexOf(u8, cards, interwiki_bugs_card) != null);
try t_.expect(std.mem.indexOf(u8, cards, interwiki_wp_card) != null);
try t_.expect(std.mem.indexOf(u8, cards, "error") == null);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
defer gpa.free(url);
var cl = try runZit(gpa, io, tmp.dir, &.{ "clone", "--no-open", url, "clone" });
defer freeResult(gpa, &cl);
try expectExitDiag(cl.term, 0, cl);
var clone_dir = try tmp.dir.openDir(io, "clone", .{});
defer clone_dir.close(io);
{
// A pull you ask for, never part of a clone.
var e = try runZit(gpa, io, clone_dir, &.{ "config", "export", "interwiki" });
defer freeResult(gpa, &e);
try expectExitDiag(e.term, 0, e);
try t_.expect(std.mem.indexOf(u8, e.stdout, "config /config") == null);
}
{
var r = try runZit(gpa, io, clone_dir, &.{ "config", "pull", "interwiki", url });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try t_.expectEqualStrings("interwiki: 2 rows merged from the peer; 2 in the table\n", r.stdout);
var e = try runZit(gpa, io, clone_dir, &.{ "config", "export", "interwiki" });
defer freeResult(gpa, &e);
try t_.expect(std.mem.endsWith(u8, e.stdout, interwiki_bugs_card ++ interwiki_wp_card));
}
{
// Again, by prefix: nothing newer, so nothing written.
var r = try runZit(gpa, io, clone_dir, &.{ "config", "pull", "inter", url });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try t_.expectEqualStrings("interwiki: 0 rows merged from the peer; 2 in the table\n", r.stdout);
}
}
test "cli config: the usage, the help and the unknown-area refusal name the interwiki area" {
const gpa = std.testing.allocator;
const io = std.testing.io;
const t_ = std.testing;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
{
var r = try runZit(gpa, io, tmp.dir, &.{"config"});
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 2, r);
try t_.expect(std.mem.indexOf(u8, r.stderr, "zit config pull interwiki|shun|user [URL]") != null);
try t_.expect(std.mem.indexOf(u8, r.stderr, "zit config export interwiki|shun|user [FILE]") != null);
}
{
var r = try runZit(gpa, io, tmp.dir, &.{ "config", "--help" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try t_.expect(std.mem.indexOf(u8, r.stdout, "usage: zit config pull interwiki|shun|user [URL]") != null);
try t_.expect(std.mem.indexOf(u8, r.stdout, " interwiki ") != null);
}
inline for (.{ "pull", "export" }) |sub| {
var r = try runZit(gpa, io, tmp.dir, &.{ "config", sub, "skin" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 1, r);
try t_.expectEqualStrings("zit config: unknown configuration area 'skin' (known: interwiki, shun, user)\n", r.stderr);
}
}