test(git): cover repo caching and :G completion dispatch

This commit is contained in:
2026-05-07 16:47:22 +02:00
parent c07e9c8de3
commit 50b05b0c81
4 changed files with 308 additions and 50 deletions
+105
View File
@@ -1,6 +1,25 @@
local t = require("test")
local helpers = require("test.git.helpers")
local cmd = require("git.cmd")
require("git").init()
local git = helpers.git
---@param files table<string, string>?
---@return string dir
local function make_repo(files)
return helpers.make_repo(files, { cd = true })
end
---@param actual string[]
---@param expected string[]
local function eq_sorted(actual, expected, msg)
table.sort(actual)
table.sort(expected)
t.eq(actual, expected, msg)
end
t.test("parse_args splits on whitespace", function()
t.eq(
cmd.parse_args("config user.name value"),
@@ -129,3 +148,89 @@ t.test("positional_index ignores flags", function()
t.eq(cmd._positional_index({ "push", "--force", "origin" }), 2)
t.eq(cmd._positional_index({ "checkout", "-b", "feature" }), 2)
end)
t.test("complete returns subcommands at first position", function()
local matches = cmd.complete("ch", "G ch", 4)
t.truthy(vim.tbl_contains(matches, "checkout"))
t.truthy(vim.tbl_contains(matches, "cherry-pick"))
end)
t.test("complete returns flags when arg starts with -", function()
local matches = cmd.complete("--am", "G commit --am", 13)
t.eq(matches, { "--amend" })
end)
t.test("complete branch returns plain refs (no pseudo, no stash)", function()
local dir = make_repo({ a = "x" })
git(dir, "branch", "feature")
git(dir, "tag", "v1")
t.write(dir, "a", "modified")
git(dir, "stash")
local matches = cmd.complete("", "G branch ", 9)
eq_sorted(matches, { "feature", "main", "v1" })
end)
t.test("complete merge returns refs + pseudo + stash", function()
local dir = make_repo({ a = "x" })
git(dir, "branch", "feature")
t.write(dir, "a", "y")
git(dir, "stash")
local matches = cmd.complete("", "G merge ", 8)
eq_sorted(
matches,
{ "HEAD", "ORIG_HEAD", "feature", "main", "stash", "stash@{0}" }
)
end)
t.test("complete push first positional returns remotes", function()
local dir = make_repo({ a = "x" })
git(dir, "remote", "add", "origin", "/tmp/nope")
git(dir, "remote", "add", "upstream", "/tmp/nope")
local matches = cmd.complete("", "G push ", 7)
eq_sorted(matches, { "origin", "upstream" })
end)
t.test("complete push second positional returns refs", function()
local dir = make_repo({ a = "x" })
git(dir, "branch", "feature")
local matches = cmd.complete("", "G push origin ", 14)
eq_sorted(matches, { "HEAD", "feature", "main" })
end)
t.test("complete add returns only unstaged/untracked paths", function()
local dir = make_repo({ tracked = "x" })
t.write(dir, "tracked", "modified")
t.write(dir, "newfile", "new")
local r = assert(require("git.repo").resolve(dir))
r:refresh()
vim.wait(500, function()
return r.status and #vim.tbl_keys(r.status.entries) > 0
end)
local matches = cmd.complete("", "G add ", 6)
eq_sorted(matches, { "newfile", "tracked" })
end)
t.test("complete after `--` returns tracked paths only", function()
local dir = make_repo({ a = "x", b = "y" })
t.write(dir, "untracked", "z")
local matches = cmd.complete("", "G log -- ", 9)
eq_sorted(matches, { "a", "b" })
end)
t.test("complete stash returns subsubcommands", function()
local dir = make_repo({ a = "x" })
local matches = cmd.complete("p", "G stash p", 9)
eq_sorted(matches, { "pop", "push" })
end)
t.test("complete show with <rev>:<path> returns tree paths", function()
local dir = make_repo({ a = "x", ["sub/b"] = "y" })
local matches = cmd.complete("HEAD:", "G show HEAD:", 12)
eq_sorted(matches, { "HEAD:a", "HEAD:sub/" })
end)
t.test("complete unknown subcommand falls back to tracked paths", function()
local dir = make_repo({ a = "x", b = "y" })
local matches = cmd.complete("", "G nonexistent ", 14)
eq_sorted(matches, { "a", "b" })
end)