const std = @import("std");
const support = @import("support.zig");
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runZit = support.runZit;
test "cli uv: misuse is refused before any connection" {
// Nothing listens on port 1: a usage error that reached the network
// would fail differently, and slowly.
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 url = "http://127.0.0.1:1/";
const cases = [_]struct { args: []const []const u8, says: []const u8 }{
.{ .args = &.{ "uv", "bogus", url }, .says = "bogus" },
.{ .args = &.{ "uv", "list" }, .says = "" },
.{ .args = &.{ "uv", "list", url, "extra" }, .says = "takes only the URL" },
.{ .args = &.{ "uv", "pull", url }, .says = "needs a NAME or --all" },
.{ .args = &.{ "uv", "pull", url, "a", "--all" }, .says = "not both" },
.{ .args = &.{ "uv", "push", url }, .says = "needs a FILE" },
.{ .args = &.{ "uv", "push", url, "a", "b", "--as", "c" }, .says = "--as names one FILE" },
.{ .args = &.{ "uv", "rm", url, "../escape" }, .says = "not an unversioned file name" },
.{ .args = &.{ "uv", "push", url, "a", "--mtime", "yesterday" }, .says = "--mtime takes seconds" },
.{ .args = &.{ "uv", "list", url, "-o", "d" }, .says = "belong to 'uv pull'" },
};
for (cases) |c| {
var res = try runZit(gpa, io, tmp.dir, c.args);
defer freeResult(gpa, &res);
expectExit(res.term, 2) catch |e| {
std.debug.print("\nargs {any}\nstderr: {s}\n", .{ c.args, res.stderr });
return e;
};
if (std.mem.indexOf(u8, res.stderr, c.says) == null) {
std.debug.print("\nargs {any}\nwanted '{s}' in: {s}\n", .{ c.args, c.says, res.stderr });
return error.TestUnexpectedResult;
}
}
var help = try runZit(gpa, io, tmp.dir, &.{ "uv", "--help" });
defer freeResult(gpa, &help);
try expectExitDiag(help.term, 0, help);
try std.testing.expect(std.mem.startsWith(u8, help.stdout, "usage: zit uv list URL"));
}