Skip to content

Commit 3523fad

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

File tree

1 file changed

+112
-4
lines changed

1 file changed

+112
-4
lines changed

tests/specs/neogit/status_spec.lua

Lines changed: 112 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,114 @@ 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+
print("Recent Commit Line: " .. line)
358+
local recent_commit_count = tonumber(string.match(line, "%d+"))
359+
local repo_commit_count = tonumber(system("git rev-list master --count"))
360+
local config_commit_count = 10
361+
require("neogit.config").values.status.recent_commit_count = config_commit_count
362+
363+
-- Ensures the actual number of recent commits is less than the repo commits.
364+
-- The total number of repo commits SHOULD be more than the recent commit count.
365+
print("Recent Commit Count: " .. recent_commit_count)
366+
print("Repository Commit Count: " .. repo_commit_count)
367+
assert.True(recent_commit_count < repo_commit_count)
368+
-- Ensure the number of recent commits is equal to the number specified in the config
369+
print("Recent Commit Count: " .. recent_commit_count)
370+
print("Config Commit Count: " .. config_commit_count)
371+
assert.are.equal(recent_commit_count, config_commit_count)
372+
end)
373+
)
374+
end)
375+
describe("content", function()
376+
local function get_latest_recent_commit()
377+
-- Get the commit right under the "Recent commits" message
378+
local _, cursor_row = find(recent_commit_pattern)
379+
act("<tab>")
380+
vim.api.nvim_win_set_cursor(0, { cursor_row + 1, 0 })
381+
local commit_message_line = vim.api.nvim_buf_get_lines(0, cursor_row, cursor_row + 1, true)[1]
382+
-- Remove the leading commit hash
383+
local commit_message = string.gsub(commit_message_line, "^[a-z0-9]+ ", "")
384+
385+
return commit_message
386+
end
387+
388+
it(
389+
"has correct recent commit information with the default config",
390+
in_prepared_repo(function()
391+
local new_commit_message = "a commit"
392+
create_new_commits(new_commit_message, 1)
393+
394+
local commit_message = get_latest_recent_commit()
395+
print("Got commit message as: " .. commit_message)
396+
397+
assert.are.same(new_commit_message, commit_message)
398+
end)
399+
)
400+
401+
it(
402+
"has correct recent commit information with extra author info",
403+
in_prepared_repo(function()
404+
require("neogit.config").values.status.recent_commit_include_author_info = true
405+
local new_commit_message = "a commit"
406+
create_new_commits(new_commit_message, 1)
407+
408+
local commit_message = get_latest_recent_commit()
409+
print("Got commit message as: " .. commit_message)
410+
411+
new_commit_message =
412+
string.format("[%s] <%s> %s", "Neogit Test User", "[email protected]", new_commit_message)
413+
assert.are.same(new_commit_message, commit_message)
414+
end)
415+
)
416+
end)
417+
end)
310418
end)

0 commit comments

Comments
 (0)