Skip to content

Commit 46255b9

Browse files
committed
test(feat): add tests for recent commits
1 parent c5e19cf commit 46255b9

File tree

1 file changed

+111
-4
lines changed

1 file changed

+111
-4
lines changed

tests/specs/neogit/status_spec.lua

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local eq = assert.are.same
22
local status = require("neogit.status")
33
local harness = require("tests.util.git_harness")
4+
local system = require("tests.util.util").system
45
local _ = require("tests.mocks.input")
56
local in_prepared_repo = harness.in_prepared_repo
67
local get_git_status = harness.get_git_status
@@ -17,10 +18,7 @@ local function find(text)
1718
for index, line in ipairs(vim.api.nvim_buf_get_lines(0, 0, -1, true)) do
1819
if line:match(text) then
1920
vim.api.nvim_win_set_cursor(0, { index, 0 })
20-
-- print(">" .. tostring(index) .. " " .. line)
21-
break
22-
-- else
23-
-- print(tostring(index) .. " " .. line)
21+
return unpack { line, index }
2422
end
2523
end
2624
end
@@ -307,4 +305,113 @@ describe("status buffer", function()
307305
end)
308306
)
309307
end)
308+
309+
describe("recent commits", function()
310+
local recent_commit_pattern = "Recent commits %(%d+%)"
311+
312+
local function refresh_status_buffer()
313+
act("<c-r>")
314+
require("plenary.async").util.block_on(status.reset)
315+
end
316+
317+
local function create_new_commits(message, number_of_commits)
318+
system("git stash && git stash clear && git clean -ffdx")
319+
local commit_commands =
320+
string.format("printf 'Some Content\\n' >> a-file && git add a-file && git commit -m '%s'", message)
321+
322+
if number_of_commits == 1 then
323+
system(commit_commands)
324+
else
325+
local loop_cmd = string.format(
326+
[[
327+
COUNT=1
328+
while [ "$COUNT" -ne %s ]; do
329+
%s
330+
COUNT=$((COUNT + 1))
331+
done
332+
]],
333+
number_of_commits,
334+
commit_commands
335+
)
336+
system(loop_cmd)
337+
end
338+
refresh_status_buffer()
339+
end
340+
341+
describe("count", function()
342+
it(
343+
"has the correct number of recent commits",
344+
in_prepared_repo(function()
345+
local line = find(recent_commit_pattern)
346+
local recent_commit_count = tonumber(string.match(line, "%d+"))
347+
local repo_commit_count = tonumber(system("git rev-list master --count"))
348+
assert.are.equal(recent_commit_count, repo_commit_count)
349+
end)
350+
)
351+
352+
it(
353+
"has the correct number when there are more commits than recent_commit_count",
354+
in_prepared_repo(function()
355+
create_new_commits("A commit to increase commit number", 50)
356+
local line = find(recent_commit_pattern)
357+
local recent_commit_count = tonumber(string.match(line, "%d+"))
358+
local repo_commit_count = tonumber(system("git rev-list master --count"))
359+
local config_commit_count = 10
360+
require("neogit.config").values.status.recent_commit_count = config_commit_count
361+
362+
-- Ensures the actual number of recent commits is less than the repo commits.
363+
-- The total number of repo commits SHOULD be more than the recent commit count.
364+
print("Recent Commit Count: " .. recent_commit_count)
365+
print("Repository Commit Count: " .. repo_commit_count)
366+
assert.True(recent_commit_count < repo_commit_count)
367+
-- Ensure the number of recent commits is equal to the number specified in the config
368+
print("Recent Commit Count: " .. recent_commit_count)
369+
print("Config Commit Count: " .. config_commit_count)
370+
assert.are.equal(recent_commit_count, config_commit_count)
371+
end)
372+
)
373+
end)
374+
describe("content", function()
375+
local function get_latest_recent_commit()
376+
-- Get the commit right under the "Recent commits" message
377+
local _, cursor_row = find(recent_commit_pattern)
378+
act("<tab>")
379+
vim.api.nvim_win_set_cursor(0, { cursor_row + 1, 0 })
380+
local commit_message_line = vim.api.nvim_buf_get_lines(0, cursor_row, cursor_row + 1, true)[1]
381+
-- Remove the leading commit hash
382+
local commit_message = string.gsub(commit_message_line, "^[a-z0-9]+ ", "")
383+
384+
return commit_message
385+
end
386+
387+
it(
388+
"has correct recent commit information with the default config",
389+
in_prepared_repo(function()
390+
local new_commit_message = "a commit"
391+
create_new_commits(new_commit_message, 1)
392+
393+
local commit_message = get_latest_recent_commit()
394+
print("Got commit message as: " .. commit_message)
395+
396+
assert.are.same(new_commit_message, commit_message)
397+
end)
398+
)
399+
400+
it(
401+
"has correct recent commit information with extra author info",
402+
in_prepared_repo(function()
403+
require("neogit.config").values.status.recent_commit_include_author_info = true
404+
local new_commit_message = "a commit"
405+
create_new_commits(new_commit_message, 1)
406+
407+
local commit_message = get_latest_recent_commit()
408+
print("Got commit message as: " .. commit_message)
409+
410+
new_commit_message =
411+
string.format("[%s] <%s> %s", "Neogit Test User", "test@neogit.git", new_commit_message)
412+
assert.are.same(new_commit_message, commit_message)
413+
end)
414+
)
415+
end)
416+
end)
310417
end)

0 commit comments

Comments
 (0)