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
+3 -50
View File
@@ -1,57 +1,10 @@
local t = require("test")
local helpers = require("test.git.helpers")
require("git").init()
---@param dir string
---@param ... string
local function git(dir, ...)
local r = vim.system({ "git", ... }, { cwd = dir, text = true }):wait()
if r.code ~= 0 then
error(
string.format(
"git %s failed: %s",
table.concat({ ... }, " "),
vim.trim(r.stderr or "")
),
2
)
end
return r
end
---Build a temporary git repo with the given committed contents and
---queue cleanup (stop fs watchers, drop test buffers, delete the dir).
---@param files table<string, string>?
---@return string dir
local function make_repo(files)
local dir = vim.fn.tempname()
vim.fn.mkdir(dir, "p")
git(dir, "init", "-q", "-b", "main")
git(dir, "config", "user.email", "t@t.com")
git(dir, "config", "user.name", "t")
if files and next(files) then
for path, content in pairs(files) do
t.write(dir, path, content)
end
git(dir, "add", ".")
git(dir, "commit", "-q", "-m", "init")
end
t.defer(function()
pcall(vim.cmd.cd, "/tmp")
pcall(function()
require("git.repo").stop_all()
end)
vim.wait(60)
for _, b in ipairs(vim.api.nvim_list_bufs()) do
local name = vim.api.nvim_buf_get_name(b)
if name:find(dir, 1, true) or name:match("^git[a-z]*://") then
pcall(vim.api.nvim_buf_delete, b, { force = true })
end
end
vim.fn.delete(dir, "rf")
end)
return dir
end
local git = helpers.git
local make_repo = helpers.make_repo
---Replicate the user's global cursor-restore autocmd. Scoped to a
---named augroup + cleanup so it doesn't leak between tests.