fix(git/status_view): scope dispatch to current tabpage + test cleanup
This commit is contained in:
+32
-34
@@ -1,24 +1,22 @@
|
||||
---@diagnostic disable: access-invisible
|
||||
local h = require("test.git.helpers")
|
||||
local t = require("test")
|
||||
local helpers = require("test.git.helpers")
|
||||
|
||||
require("git").init()
|
||||
|
||||
local git = helpers.git
|
||||
local make_repo = helpers.make_repo
|
||||
|
||||
---@param r ow.Git.Repo
|
||||
---@param key string
|
||||
---@param timeout integer?
|
||||
local function wait_cleared(r, key, timeout)
|
||||
vim.wait(timeout or 2000, function()
|
||||
t.wait_for(function()
|
||||
return r._cache[key] == nil
|
||||
end)
|
||||
end, key .. " cache to clear", timeout or 2000)
|
||||
end
|
||||
|
||||
t.test("list_refs returns heads, tags, remotes (no HEAD)", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
git(dir, "branch", "feature")
|
||||
git(dir, "tag", "v1")
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
h.git(dir, "branch", "feature")
|
||||
h.git(dir, "tag", "v1")
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
local refs = r:list_refs()
|
||||
table.sort(refs)
|
||||
@@ -26,13 +24,13 @@ t.test("list_refs returns heads, tags, remotes (no HEAD)", function()
|
||||
end)
|
||||
|
||||
t.test("list_pseudo_refs always includes HEAD", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
t.eq(r:list_pseudo_refs(), { "HEAD" })
|
||||
end)
|
||||
|
||||
t.test("list_pseudo_refs picks up MERGE_HEAD when present", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
t.write(dir .. "/.git", "MERGE_HEAD", "deadbeef\n")
|
||||
-- Bypass cache (file appeared after first scan).
|
||||
@@ -43,15 +41,15 @@ t.test("list_pseudo_refs picks up MERGE_HEAD when present", function()
|
||||
end)
|
||||
|
||||
t.test("list_stash_refs is empty when no stash", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
t.eq(r:list_stash_refs(), {})
|
||||
end)
|
||||
|
||||
t.test("list_stash_refs lists stash + entries when stash exists", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
t.write(dir, "a", "modified")
|
||||
git(dir, "stash")
|
||||
h.git(dir, "stash")
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
local refs = r:list_stash_refs()
|
||||
t.eq(#refs, 2)
|
||||
@@ -60,7 +58,7 @@ t.test("list_stash_refs lists stash + entries when stash exists", function()
|
||||
end)
|
||||
|
||||
t.test("get_cached memoizes by key", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
local calls = 0
|
||||
local v1 = r:get_cached("k", function()
|
||||
@@ -76,23 +74,23 @@ t.test("get_cached memoizes by key", function()
|
||||
end)
|
||||
|
||||
t.test("cache clears after top-level .git change (commit)", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
local _ = r:list_refs()
|
||||
t.truthy(r._cache.refs)
|
||||
t.write(dir, "b", "y")
|
||||
git(dir, "add", "b")
|
||||
git(dir, "commit", "-q", "-m", "two")
|
||||
h.git(dir, "add", "b")
|
||||
h.git(dir, "commit", "-q", "-m", "two")
|
||||
wait_cleared(r, "refs")
|
||||
t.falsy(r._cache.refs, "cache should be cleared after commit")
|
||||
end)
|
||||
|
||||
t.test("cache clears after slash-branch creation (polyfill)", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
local _ = r:list_refs()
|
||||
t.truthy(r._cache.refs)
|
||||
git(dir, "branch", "feat/foo")
|
||||
h.git(dir, "branch", "feat/foo")
|
||||
wait_cleared(r, "refs")
|
||||
t.falsy(r._cache.refs, "cache should clear via polyfilled subdir watcher")
|
||||
local refs = r:list_refs()
|
||||
@@ -101,10 +99,10 @@ t.test("cache clears after slash-branch creation (polyfill)", function()
|
||||
end)
|
||||
|
||||
t.test("cache clears after deeply nested slash branch", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
local _ = r:list_refs()
|
||||
git(dir, "branch", "deep/a/b/c")
|
||||
h.git(dir, "branch", "deep/a/b/c")
|
||||
wait_cleared(r, "refs")
|
||||
local refs = r:list_refs()
|
||||
table.sort(refs)
|
||||
@@ -112,9 +110,9 @@ t.test("cache clears after deeply nested slash branch", function()
|
||||
end)
|
||||
|
||||
t.test("resolve_sha returns ok + full sha for a known blob", function()
|
||||
local dir = make_repo({ a = "hello\n" })
|
||||
local dir = h.make_repo({ a = "hello\n" })
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
local blob = vim.trim(git(dir, "rev-parse", "HEAD:a").stdout)
|
||||
local blob = h.git(dir, "rev-parse", "HEAD:a").stdout
|
||||
local short = blob:sub(1, 7)
|
||||
local full, status = r:resolve_sha(short)
|
||||
t.eq(status, "ok")
|
||||
@@ -122,7 +120,7 @@ t.test("resolve_sha returns ok + full sha for a known blob", function()
|
||||
end)
|
||||
|
||||
t.test("resolve_sha returns missing for an unknown prefix", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
local full, status = r:resolve_sha("0000deadbeef")
|
||||
t.eq(full, nil)
|
||||
@@ -130,29 +128,29 @@ t.test("resolve_sha returns missing for an unknown prefix", function()
|
||||
end)
|
||||
|
||||
t.test("resolve_sha caches by prefix", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
local blob = vim.trim(git(dir, "rev-parse", "HEAD:a").stdout)
|
||||
local blob = h.git(dir, "rev-parse", "HEAD:a").stdout
|
||||
local short = blob:sub(1, 7)
|
||||
local _, _ = r:resolve_sha(short)
|
||||
t.truthy(r._cache["resolve:" .. short], "result should be cached")
|
||||
end)
|
||||
|
||||
t.test("watcher cleans up after a slash-branch dir is removed", function()
|
||||
local dir = make_repo({ a = "x" })
|
||||
local dir = h.make_repo({ a = "x" })
|
||||
local r = assert(require("git.repo").resolve(dir))
|
||||
git(dir, "branch", "feat/foo")
|
||||
h.git(dir, "branch", "feat/foo")
|
||||
-- Wait for the dynamic watcher on .git/refs/heads/feat to be added.
|
||||
local feat_path = dir .. "/.git/refs/heads/feat"
|
||||
vim.wait(2000, function()
|
||||
t.wait_for(function()
|
||||
return r._watchers[feat_path] ~= nil
|
||||
end)
|
||||
end, "watcher to be installed on feat/ subdir", 2000)
|
||||
t.truthy(r._watchers[feat_path], "feat/ subdir should be watched")
|
||||
-- Remove the branch; the feat/ directory becomes empty and is
|
||||
-- pruned by git, triggering the deleted-self event.
|
||||
git(dir, "branch", "-D", "feat/foo")
|
||||
vim.wait(2000, function()
|
||||
h.git(dir, "branch", "-D", "feat/foo")
|
||||
t.wait_for(function()
|
||||
return r._watchers[feat_path] == nil
|
||||
end)
|
||||
end, "watcher on feat/ subdir to close", 2000)
|
||||
t.falsy(r._watchers[feat_path], "watcher should self-close")
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user