Skip to content

Commit 2667ae0

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

File tree

1 file changed

+103
-4
lines changed

1 file changed

+103
-4
lines changed

tests/specs/neogit/status_spec.lua

Lines changed: 103 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,105 @@ 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, original_commit_count)
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+
system(
326+
string.format(
327+
"COUNT=0; while (( COUNT < %s )); do %s; (( COUNT++ )) done",
328+
number_of_commits,
329+
commit_commands
330+
)
331+
)
332+
end
333+
refresh_status_buffer()
334+
end
335+
336+
describe("count", function()
337+
it(
338+
"has the correct number of recent commits",
339+
in_prepared_repo(function()
340+
local line = find(recent_commit_pattern)
341+
local recent_commit_count = tonumber(string.match(line, "%d+"))
342+
local repo_commit_count = tonumber(system("git rev-list master --count"))
343+
assert.are.equal(recent_commit_count, repo_commit_count)
344+
end)
345+
)
346+
347+
it(
348+
"has the correct number when there are more commits than recent_commit_count",
349+
in_prepared_repo(function()
350+
create_new_commits("A commit to increase commit number", 50)
351+
local line = find(recent_commit_pattern)
352+
local recent_commit_count = tonumber(string.match(line, "%d+"))
353+
local repo_commit_count = tonumber(system("git rev-list master --count"))
354+
local config_commit_count = require("neogit.config").values.status.recent_commit_count
355+
356+
-- Ensures the actual number of recent commits is less than the repo commits.
357+
-- The total number of repo commits SHOULD be more than the recent commit count.
358+
print("Recent Commit Count: " .. recent_commit_count)
359+
print("Repository Commit Count: " .. repo_commit_count)
360+
assert.True(recent_commit_count < repo_commit_count)
361+
-- Ensure the number of recent commits is equal to the number specified in the config
362+
assert.are.equal(recent_commit_count, config_commit_count)
363+
end)
364+
)
365+
end)
366+
describe("content", function()
367+
local function get_latest_recent_commit()
368+
-- Get the commit right under the "Recent commits" message
369+
local _, cursor_row = find(recent_commit_pattern)
370+
act("<tab>")
371+
vim.api.nvim_win_set_cursor(0, { cursor_row + 1, 0 })
372+
local commit_message_line = vim.api.nvim_buf_get_lines(0, cursor_row, cursor_row + 1, true)[1]
373+
-- Remove the leading commit hash
374+
local commit_message = string.gsub(commit_message_line, "^[a-z0-9]+ ", "")
375+
376+
return commit_message
377+
end
378+
379+
it(
380+
"has correct recent commit information with the default config",
381+
in_prepared_repo(function()
382+
local new_commit_message = "a commit"
383+
create_new_commits(new_commit_message, 1)
384+
385+
local commit_message = get_latest_recent_commit()
386+
print("Got commit message as: " .. commit_message)
387+
388+
assert.are.same(new_commit_message, commit_message)
389+
end)
390+
)
391+
392+
it(
393+
"has correct recent commit information with extra author info",
394+
in_prepared_repo(function()
395+
require("neogit.config").values.status.recent_commit_include_author_info = true
396+
local new_commit_message = "a commit"
397+
create_new_commits(new_commit_message, 1)
398+
399+
local commit_message = get_latest_recent_commit()
400+
print("Got commit message as: " .. commit_message)
401+
402+
new_commit_message =
403+
string.format("[%s] <%s> %s", "Neogit Test User", "[email protected]", new_commit_message)
404+
assert.are.same(new_commit_message, commit_message)
405+
end)
406+
)
407+
end)
408+
end)
310409
end)

0 commit comments

Comments
 (0)