1
1
local eq = assert .are .same
2
2
local status = require (" neogit.status" )
3
3
local harness = require (" tests.util.git_harness" )
4
+ local system = require (" tests.util.util" ).system
4
5
local _ = require (" tests.mocks.input" )
5
6
local in_prepared_repo = harness .in_prepared_repo
6
7
local get_git_status = harness .get_git_status
@@ -17,10 +18,7 @@ local function find(text)
17
18
for index , line in ipairs (vim .api .nvim_buf_get_lines (0 , 0 , - 1 , true )) do
18
19
if line :match (text ) then
19
20
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 }
24
22
end
25
23
end
26
24
end
@@ -307,4 +305,105 @@ describe("status buffer", function()
307
305
end )
308
306
)
309
307
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 )
310
409
end )
0 commit comments