Skip to content
zit Artifact [776c09eabf]
Login

Artifact [776c09eabf]

Artifact 776c09eabfad5aadfbf3016691b20a38e48247ec5c3af1a49ff4dd53b5d13ede:


const std = @import("std");
const support = @import("support.zig");
const addManaged = support.addManaged;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectManaged = support.expectManaged;
const fileExists = support.fileExists;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const readManagedImage = support.readManagedImage;
const runZit = support.runZit;
const touch = support.touch;
const writeFile = support.writeFile;

// ==========================================================================
// `zit addremove` (contracts/addremove.md, B-addremove-1..13). One scan, two
// verdicts: every untracked file the SAME `extras.zig` sweep reports is ADDED,
// every tracked file missing from disk is REMOVED — fossil/hg parity (git spells
// it `add -A`). Mutates `.zit-managed` only; working files are never touched.
// Each test drives the real binary; expected bytes come from the clause text and
// the settled help/report strings, never from running fossil.
// ==========================================================================

/// The verbatim `zit addremove` help — an independent transcription of
/// `help.zig`'s `addremove_help` (B-addremove-9/cli-help-verbatim-stdout-exit-0),
/// so a drift between the two is caught here.
const addremove_help_expected =
    \\usage: zit addremove [--dry-run] [--dotfiles]
    \\
    \\Reconcile the tracked set with the working tree in one pass: mark every
    \\untracked file to include in your next check-in, and stop tracking every
    \\file you deleted from disk. This is the set `zit extras` lists, added, plus
    \\the tracked files now missing, removed — fossil and hg spell it `addremove`;
    \\git spells it `add -A`.
    \\
    \\addremove changes only what zit tracks; it never edits, creates, or deletes
    \\a working file, and the next `zit commit` records the result. Each action is
    \\reported, sorted and relative to the checkout root:
    \\
    \\  ADDED <path>     a newly tracked file
    \\  REMOVED <path>   a tracked file no longer on disk
    \\
    \\A tracked file that is merely modified is left alone (content does not affect
    \\membership), and zit's control files are never tracked. Names beginning with
    \\a dot are skipped unless --dotfiles is given, except the files zit reads
    \\from the tree, which are always added: a root .zitignore, .zit-modules, and
    \\anything under .fossil-settings/ or .zit-settings/. When names were skipped,
    \\one line on stderr says how many (each skipped file or directory counts
    \\once). With nothing to do, the output is empty.
    \\
    \\options:
    \\  --dry-run     report what would change, one action per line; change nothing
    \\  --dotfiles    also track names beginning with a dot (also settable with
    \\                `zit settings dotfiles on`)
    \\  -h, --help    show this help and exit
    \\
    \\exit status:
    \\  0  the tracked set was reconciled (with or without changes), or a --dry-run preview
    \\  1  a path could not be tracked, or this is not a zit repository
    \\  2  usage error (an unknown option or an unexpected argument)
    \\
;

test "cli addremove B-addremove-1: adds two untracked, removes one missing, sorted report, exit 0" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    // Track `gone` then delete it (a missing-managed removal); leave two untracked
    // files in reverse-of-sorted order (an ADD each).
    try touch(io, tmp.dir, "gone");
    try addManaged(gpa, io, tmp.dir, &.{"gone"});
    try tmp.dir.deleteFile(io, "gone");
    try touch(io, tmp.dir, "zebra");
    try touch(io, tmp.dir, "apple");

    var res = try runZit(gpa, io, tmp.dir, &.{"addremove"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    // ADDED lines (sorted ascending) then the REMOVED line; diagnostics-free.
    try std.testing.expectEqualStrings("ADDED apple\nADDED zebra\nREMOVED gone\n", res.stdout);
    try std.testing.expectEqualStrings("", res.stderr);
    // The on-disk oracle: the set is exactly the two adds (gone forgotten).
    try expectManaged(gpa, io, tmp.dir, &.{ "apple", "zebra" });

    // `zit status` then shows the transitions and `zit extras` shows nothing.
    var st = try runZit(gpa, io, tmp.dir, &.{"status"});
    defer freeResult(gpa, &st);
    try expectExit(st.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, st.stdout, "ADDED      apple") != null);
    try std.testing.expect(std.mem.indexOf(u8, st.stdout, "ADDED      zebra") != null);
    try std.testing.expect(std.mem.indexOf(u8, st.stdout, "gone") == null);

    var ext = try runZit(gpa, io, tmp.dir, &.{"extras"});
    defer freeResult(gpa, &ext);
    try expectExit(ext.term, 0);
    try std.testing.expectEqualStrings("", ext.stdout);
}

test "cli addremove B-addremove-2: the ADDED paths are `zit extras` minus symlinks — same sweep, subdirs included, control files excluded" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    // A nested file, a symlink (listed, never followed), and two root files. The
    // control artifacts `.zit`/`.zit-checkout` are on disk but never swept.
    try tmp.dir.createDirPath(io, "sub/dir");
    try touch(io, tmp.dir, "sub/dir/name");
    try tmp.dir.symLink(io, "target", "link", .{});
    try touch(io, tmp.dir, "zebra");
    try touch(io, tmp.dir, "apple");

    // Capture what extras prints at this instant (the add-set oracle).
    var ext = try runZit(gpa, io, tmp.dir, &.{"extras"});
    defer freeResult(gpa, &ext);
    try expectExit(ext.term, 0);
    try std.testing.expectEqualStrings("apple\nlink\nsub/dir/name\nzebra\n", ext.stdout);

    var res = try runZit(gpa, io, tmp.dir, &.{"addremove"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    // The ADDED paths are the extras set MINUS symlinks (an `ADDED ` prefix per
    // line, no REMOVED lines — nothing was tracked to go missing). Still ONE
    // sweep: a second walker would diverge on the subdir or the control names.
    // `link` is absent since F-B3 — 0.1.0 does not version symlinks, and
    // tracking one here is exactly what wedged `commit` (blocker B3). The
    // withholding is silent: addremove's report is a ledger of transitions, and
    // a skipped symlink is a non-transition (`zit extras` still shows it).
    try std.testing.expectEqualStrings(
        "ADDED apple\nADDED sub/dir/name\nADDED zebra\n",
        res.stdout,
    );
    try std.testing.expectEqualStrings("", res.stderr);
    // The three regular files are tracked, the symlink is not, and no control
    // name leaked in.
    try expectManaged(gpa, io, tmp.dir, &.{ "apple", "sub/dir/name", "zebra" });
    // And it is still an extra — withheld from tracking, not hidden.
    var ext2 = try runZit(gpa, io, tmp.dir, &.{"extras"});
    defer freeResult(gpa, &ext2);
    try expectExit(ext2.term, 0);
    try std.testing.expectEqualStrings("link\n", ext2.stdout);
}

test "cli addremove B-addremove-3: the rm set is the missing-managed set; a present-but-modified file is never removed or re-added" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    // `keep` is tracked and stays on disk but is MODIFIED in place; `gone` is
    // tracked and deleted. Only `gone` is a removal; `keep` is untouched.
    try writeFile(io, tmp.dir, "keep", "original");
    try touch(io, tmp.dir, "gone");
    try addManaged(gpa, io, tmp.dir, &.{ "keep", "gone" });
    try writeFile(io, tmp.dir, "keep", "changed contents");
    try tmp.dir.deleteFile(io, "gone");

    var res = try runZit(gpa, io, tmp.dir, &.{"addremove"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    // Exactly one removal; `keep` is neither removed nor re-added (content is
    // irrelevant to membership).
    try std.testing.expectEqualStrings("REMOVED gone\n", res.stdout);
    try std.testing.expectEqualStrings("", res.stderr);
    try expectManaged(gpa, io, tmp.dir, &.{"keep"});
    // The modified working file is byte-identical after the run (never touched).
    const keep_bytes = try tmp.dir.readFileAlloc(io, "keep", gpa, .limited(1 << 16));
    defer gpa.free(keep_bytes);
    try std.testing.expectEqualStrings("changed contents", keep_bytes);
}

test "cli addremove B-addremove-4: --dry-run previews exactly and leaves .zit-managed byte-unchanged" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try touch(io, tmp.dir, "gone");
    try addManaged(gpa, io, tmp.dir, &.{"gone"});
    try tmp.dir.deleteFile(io, "gone");
    try touch(io, tmp.dir, "zebra");
    try touch(io, tmp.dir, "apple");

    // The exact `.zit-managed` bytes before the dry run.
    const before = try readManagedImage(gpa, io, tmp.dir);
    defer gpa.free(before);

    var dry = try runZit(gpa, io, tmp.dir, &.{ "addremove", "--dry-run" });
    defer freeResult(gpa, &dry);
    try expectExit(dry.term, 0);
    try std.testing.expectEqualStrings("ADDED apple\nADDED zebra\nREMOVED gone\n", dry.stdout);
    try std.testing.expectEqualStrings("", dry.stderr);

    // `.zit-managed` is byte-identical — the dry run persisted nothing.
    const after = try readManagedImage(gpa, io, tmp.dir);
    defer gpa.free(after);
    try std.testing.expectEqualStrings(before, after);

    // Dry-run output equals the real run's report on the same tree.
    var real = try runZit(gpa, io, tmp.dir, &.{"addremove"});
    defer freeResult(gpa, &real);
    try expectExit(real.term, 0);
    try std.testing.expectEqualStrings(dry.stdout, real.stdout);
}

test "cli addremove B-addremove-5: a clean tree (no extras, no missing) prints nothing and exits 0" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    // One tracked file that is present, nothing untracked: nothing to do.
    try touch(io, tmp.dir, "keep");
    try addManaged(gpa, io, tmp.dir, &.{"keep"});

    var res = try runZit(gpa, io, tmp.dir, &.{"addremove"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings("", res.stdout);
    try std.testing.expectEqualStrings("", res.stderr);
    try expectManaged(gpa, io, tmp.dir, &.{"keep"});
}

test "cli addremove B-addremove-6/-8: a control-looking file is never added and the working tree is byte-identical afterward" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    // `.zit-cache` is a never-tracked control name (B-add-7 set): present on disk
    // but the sweep excludes it, so addremove never tracks it — the sibling `real`
    // is still added, and the run succeeds.
    try touch(io, tmp.dir, ".zit-cache");
    try writeFile(io, tmp.dir, "real", "payload");

    var res = try runZit(gpa, io, tmp.dir, &.{"addremove"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings("ADDED real\n", res.stdout);
    try std.testing.expectEqualStrings("", res.stderr);
    try expectManaged(gpa, io, tmp.dir, &.{"real"});

    // B-addremove-8: working files are NEVER touched — every file still present
    // and byte-identical (no deletion, no write).
    try std.testing.expect(fileExists(io, tmp.dir, ".zit-cache"));
    const real_bytes = try tmp.dir.readFileAlloc(io, "real", gpa, .limited(1 << 16));
    defer gpa.free(real_bytes);
    try std.testing.expectEqualStrings("payload", real_bytes);
}

test "cli addremove B-addremove-8: a deleted-managed file is forgotten, never recreated on disk" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try touch(io, tmp.dir, "gone");
    try addManaged(gpa, io, tmp.dir, &.{"gone"});
    try tmp.dir.deleteFile(io, "gone");

    var res = try runZit(gpa, io, tmp.dir, &.{"addremove"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings("REMOVED gone\n", res.stdout);
    // addremove touches only `.zit-managed`: the deleted file is NOT resurrected.
    try std.testing.expect(!fileExists(io, tmp.dir, "gone"));
    try expectManaged(gpa, io, tmp.dir, &.{});
}

test "cli addremove B-addremove-9: -h and --help print verbatim help to stdout, exit 0, help wins over a trailing token" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);

    const invocations = [_][]const []const u8{
        &.{ "addremove", "--help" },
        &.{ "addremove", "-h" },
        &.{ "addremove", "--help", "foo" },
        &.{ "addremove", "-h", "--dry-run" },
    };
    for (invocations) |argv| {
        var res = try runZit(gpa, io, tmp.dir, argv);
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
        try std.testing.expectEqualStrings(addremove_help_expected, res.stdout);
        try std.testing.expectEqualStrings("", res.stderr);
    }
}

test "cli addremove B-addremove-10: an unknown option or a positional is a usage error, exit 2, stderr, empty stdout, no mutation" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    // Pre-state: a missing-managed file that a real run WOULD remove — so the
    // "no mutation" assertion has teeth.
    try touch(io, tmp.dir, "gone");
    try addManaged(gpa, io, tmp.dir, &.{"gone"});
    try tmp.dir.deleteFile(io, "gone");
    try touch(io, tmp.dir, "extra");

    const cases = [_]struct { argv: []const []const u8, tok: []const u8, kind: []const u8 }{
        .{ .argv = &.{ "addremove", "--force" }, .tok = "--force", .kind = "unknown option" },
        .{ .argv = &.{ "addremove", "-n" }, .tok = "-n", .kind = "unknown option" },
        .{ .argv = &.{ "addremove", "foo" }, .tok = "foo", .kind = "unexpected argument" },
        .{ .argv = &.{ "addremove", "--", "foo" }, .tok = "foo", .kind = "unexpected argument" },
    };
    for (cases) |c| {
        var res = try runZit(gpa, io, tmp.dir, c.argv);
        defer freeResult(gpa, &res);
        try expectExit(res.term, 2);
        try std.testing.expectEqualStrings("", res.stdout);
        const expected = try std.fmt.allocPrint(
            gpa,
            "zit addremove: {s} '{s}'\ntry 'zit addremove --help'\n",
            .{ c.kind, c.tok },
        );
        defer gpa.free(expected);
        try std.testing.expectEqualStrings(expected, res.stderr);
    }
    // No usage error mutated the set: `gone` is still tracked, `extra` still not.
    try expectManaged(gpa, io, tmp.dir, &.{"gone"});
}

test "cli addremove B-addremove-11: outside a repository, exit 1, not_a_repo on stderr, empty stdout" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try touch(io, tmp.dir, "a"); // a file, but no repo anywhere above

    const invocations = [_][]const []const u8{
        &.{"addremove"},
        &.{ "addremove", "--dry-run" },
    };
    for (invocations) |argv| {
        var res = try runZit(gpa, io, tmp.dir, argv);
        defer freeResult(gpa, &res);
        try expectExit(res.term, 1);
        try std.testing.expectEqualStrings("", res.stdout);
        try std.testing.expectEqualStrings(
            "zit: no zit checkout here (looked for .zit-checkout in this directory and its parents)\n" ++
                "if this is a bare repository, name it with -R, or run 'zit open' to create a working tree\n",
            res.stderr,
        );
    }
}

test "cli addremove B-addremove-12: action lines to stdout, diagnostics to stderr, no interleaving under redirection" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try touch(io, tmp.dir, "gone");
    try addManaged(gpa, io, tmp.dir, &.{"gone"});
    try tmp.dir.deleteFile(io, "gone");
    try touch(io, tmp.dir, "apple");

    // The child's stdout and stderr are dup2'd onto independent OS pipes (the same
    // fd split any shell redirection uses). Only the ADDED/REMOVED report reaches
    // stdout; stderr is empty on a clean run — proving no interleaving. An in-tree
    // `> file` cannot be used here: the redirect target would itself be swept as an
    // extra and pollute the report.
    var res = try runZit(gpa, io, tmp.dir, &.{"addremove"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings("ADDED apple\nREMOVED gone\n", res.stdout);
    try std.testing.expectEqualStrings("", res.stderr);
}

test "cli addremove B-addremove-13: 'addremove' is listed in the command help, routes to its own help, and is suggested for a near-miss" {
    // The verb joins the single-source Verb enum, so the command list names it,
    // `zit help addremove` routes to the verb's own verbatim help, and the
    // did-you-mean suggester finds a near-miss.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    var help = try runZit(gpa, io, tmp.dir, &.{"help"});
    defer freeResult(gpa, &help);
    try expectExit(help.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, help.stdout, "addremove") != null);

    var topic = try runZit(gpa, io, tmp.dir, &.{ "help", "addremove" });
    defer freeResult(gpa, &topic);
    try expectExit(topic.term, 0);
    try std.testing.expectEqualStrings(addremove_help_expected, topic.stdout);

    var typo = try runZit(gpa, io, tmp.dir, &.{"addremvoe"});
    defer freeResult(gpa, &typo);
    try expectExit(typo.term, 2);
    try std.testing.expect(std.mem.indexOf(u8, typo.stderr, "did you mean 'addremove'?") != null);
}

test "cli addremove: clean-glob matches are skipped by the reconcile scan" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try tmp.dir.createDir(io, ".fossil-settings", .default_dir);
    try writeFile(io, tmp.dir, ".fossil-settings/clean-glob", "*.o\n");
    try tmp.dir.createDir(io, "src", .default_dir);
    try writeFile(io, tmp.dir, "src/a.c", "int main(void){return 0;}\n");
    try writeFile(io, tmp.dir, "src/a.o", "\x00object\n");

    var ar = try runZit(gpa, io, tmp.dir, &.{"addremove"});
    defer freeResult(gpa, &ar);
    try expectExitDiag(ar.term, 0, ar);
    // The settings file is one zit reads from the tree, so it is added though
    // its name begins with a dot.
    try expectManaged(gpa, io, tmp.dir, &.{ ".fossil-settings/clean-glob", "src/a.c" });
}