Repository navigation
Expand file tree
/
Copy pathtest_git.lua
More file actions
3191 lines (2656 loc) · 106 KB
/
Copy pathtest_git.lua
File metadata and controls
3191 lines (2656 loc) · 106 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local helpers = dofile('tests/helpers.lua')
local child = helpers.new_child_neovim()
local expect, eq = helpers.expect, helpers.expect.equality
local new_set = MiniTest.new_set
-- Helpers with child processes
local load_module = function(config) child.mini_load('git', config) end
local set_cursor = function(...) return child.set_cursor(...) end
local get_cursor = function(...) return child.get_cursor(...) end
local set_lines = function(...) return child.set_lines(...) end
local get_lines = function(...) return child.get_lines(...) end
local type_keys = function(...) return child.type_keys(...) end
local sleep = function(ms) helpers.sleep(ms, child) end
local forward_lua = function(fun_str) return helpers.forward_lua(child, fun_str) end
local new_buf = function() return child.api.nvim_create_buf(true, false) end
local new_scratch_buf = function() return child.api.nvim_create_buf(false, true) end
local get_buf = function() return child.api.nvim_get_current_buf() end
local set_buf = function(buf_id) child.api.nvim_set_current_buf(buf_id) end
local get_win = function() return child.api.nvim_get_current_win() end
local path_sep = package.config:sub(1, 1)
local test_dir = 'tests/dir-git'
local test_dir_absolute = vim.fn.fnamemodify(test_dir, ':p'):gsub('(.)[\\/]$', '%1')
local test_file_absolute = test_dir_absolute .. path_sep .. 'file'
local git_root_dir = test_dir_absolute .. path_sep .. 'git-repo'
local git_repo_dir = git_root_dir .. path_sep .. '.git-dir'
local git_dir_path = git_root_dir .. path_sep .. 'dir-in-git'
local git_file_path = git_root_dir .. path_sep .. 'file-in-git'
-- Time constants
local repo_watch_delay = 50
local small_time = helpers.get_time_const(10)
local micro_time = 1
-- Common wrappers
local edit = function(path)
child.cmd('edit ' .. child.fn.fnameescape(path))
-- Slow context needs a small delay to get things up to date
if helpers.is_slow() then sleep(small_time) end
end
local log_calls = function(fun_name)
--stylua: ignore
local lua_cmd = string.format(
[[local orig = %s
_G.call_log = _G.call_log or {}
%s = function(...) table.insert(_G.call_log, { %s, ... }); return orig(...) end]],
fun_name, fun_name, vim.inspect(fun_name)
)
child.lua(lua_cmd)
end
local validate_calls = function(ref) eq(child.lua_get('_G.call_log'), ref) end
local get_buf_data = forward_lua('require("mini.git").get_buf_data')
local is_buf_enabled = function(buf_id) return get_buf_data(buf_id) ~= vim.NIL end
local make_minigit_name = function(buf_id, name)
if buf_id == 0 then buf_id = get_buf() end
return 'minigit://' .. buf_id .. '/' .. name
end
local validate_minigit_name = function(buf_id, ref_name)
eq(child.api.nvim_buf_get_name(buf_id), make_minigit_name(buf_id, ref_name))
end
-- Common mocks
-- - Git mocks
local mock_change_git_index = function()
local index_path = git_repo_dir .. '/index'
child.fn.writefile({}, index_path .. '.lock')
sleep(micro_time)
child.fn.delete(index_path)
child.loop.fs_rename(index_path .. '.lock', index_path)
end
local mock_executable = function()
child.lua([[
_G.orig_executable = vim.fn.executable
vim.fn.executable = function(exec) return exec == 'git' and 1 or _G.orig_executable(exec) end
]])
end
local mock_init_track_stdio_queue = function()
child.lua([[
_G.init_track_stdio_queue = {
{ { 'out', _G.rev_parse_track } }, -- Get path to root and repo
{ { 'out', 'abc1234\nmain' } }, -- Get HEAD data
{ { 'out', '?? file-in-git' } }, -- Get file status data
}
]])
end
local mock_spawn = function()
local mock_file = test_dir_absolute .. '/mocks/spawn.lua'
local lua_cmd = string.format('dofile(%s)', vim.inspect(mock_file))
child.lua(lua_cmd)
end
local get_spawn_log = function() return child.lua_get('_G.spawn_log') end
local validate_git_spawn_log = function(ref_log)
local spawn_log = get_spawn_log()
local n = math.max(#spawn_log, #ref_log)
for i = 1, n do
local real, ref = spawn_log[i], ref_log[i]
if real == nil then
eq('Real spawn log does not have entry for present reference log entry', ref)
elseif ref == nil then
eq(real, 'Reference does not have entry for present spawn log entry')
elseif vim.islist(ref) then
eq(real, { executable = 'git', options = { args = ref, cwd = real.options.cwd } })
else
eq(real, { executable = 'git', options = ref })
end
end
end
local clear_spawn_log = function() child.lua('_G.spawn_log = {}') end
-- - Notifications
local mock_notify = function()
child.lua([[
_G.notify_log = {}
vim.notify = function(...) table.insert(_G.notify_log, { ... }) end
]])
end
local get_notify_log = function() return child.lua_get('_G.notify_log') end
local validate_notifications = function(ref_log, msg_pattern)
local notify_log = get_notify_log()
local n = math.max(#notify_log, #ref_log)
for i = 1, n do
local real, ref = notify_log[i], ref_log[i]
if real == nil then
eq('Real notify log does not have entry for present reference log entry', ref)
elseif ref == nil then
eq(real, 'Reference does not have entry for present notify log entry')
else
local expect_msg = msg_pattern and expect.match or eq
expect_msg(real[1], ref[1])
eq(real[2], child.lua_get('vim.log.levels.' .. ref[2]))
end
end
end
local clear_notify_log = function() return child.lua('_G.notify_log = {}') end
-- Output test set ============================================================
local T = new_set({
hooks = {
pre_case = function()
child.setup()
child.set_size(10, 15)
mock_spawn()
mock_notify()
mock_executable()
-- Populate child with frequently used paths
child.lua('_G.git_root_dir, _G.git_repo_dir = ' .. vim.inspect(git_root_dir) .. ', ' .. vim.inspect(git_repo_dir))
child.lua([[_G.rev_parse_track = _G.git_repo_dir .. '\n' .. _G.git_root_dir]])
end,
post_once = child.stop,
},
n_retry = helpers.get_n_retry(2),
})
-- Unit tests =================================================================
T['setup()'] = new_set()
T['setup()']['creates side effects'] = function()
load_module()
-- Global variable
eq(child.lua_get('type(_G.MiniGit)'), 'table')
-- Autocommand group
eq(child.fn.exists('#MiniGit'), 1)
-- User command
eq(child.fn.exists(':Git'), 2)
end
T['setup()']['creates `config` field'] = function()
load_module()
eq(child.lua_get('type(_G.MiniGit.config)'), 'table')
-- Check default values
local expect_config = function(field, value) eq(child.lua_get('MiniGit.config.' .. field), value) end
expect_config('job.git_executable', 'git')
expect_config('job.timeout', 30000)
expect_config('command.split', 'auto')
end
T['setup()']['respects `config` argument'] = function()
load_module({ command = { split = 'vertical' } })
eq(child.lua_get('MiniGit.config.command.split'), 'vertical')
end
T['setup()']['validates `config` argument'] = function()
local expect_config_error = function(config, name, target_type)
expect.error(function() load_module(config) end, vim.pesc(name) .. '.*' .. vim.pesc(target_type))
end
expect_config_error('a', 'config', 'table')
expect_config_error({ job = 'a' }, 'job', 'table')
expect_config_error({ job = { git_executable = 1 } }, 'job.git_executable', 'string')
expect_config_error({ job = { timeout = 'a' } }, 'job.timeout', 'number')
expect_config_error({ command = 'a' }, 'command', 'table')
expect_config_error({ command = { split = 1 } }, 'command.split', 'one of')
expect_config_error({ command = { split = 'xxx' } }, 'command.split', 'one of')
end
T['setup()']['warns about missing executable'] = function()
load_module({ job = { git_executable = 'no-git-is-available' } })
validate_notifications({ { '(mini.git) There is no `no-git-is-available` executable', 'WARN' } })
end
T['setup()']['auto enables in all existing loaded buffers'] = function()
mock_init_track_stdio_queue()
child.lua('_G.stdio_queue = _G.init_track_stdio_queue')
local buf_id_bad_1 = new_scratch_buf()
edit(git_root_dir .. '/file-in-git_symlink-source')
local buf_id_bad_2 = get_buf()
edit(git_root_dir .. '/dir-in-git/file-in-dir-in-git')
local buf_id_other_normal = get_buf()
edit(git_file_path)
local buf_id_cur_normal = get_buf()
-- Make enable-able buffer normal+listed but unloaded
child.api.nvim_buf_delete(buf_id_bad_2, { unload = true })
eq(child.api.nvim_buf_is_loaded(buf_id_bad_2), false)
child.api.nvim_set_option_value('buflisted', true, { buf = buf_id_bad_2 })
load_module()
eq(is_buf_enabled(buf_id_cur_normal), true)
eq(is_buf_enabled(buf_id_other_normal), true)
eq(is_buf_enabled(buf_id_bad_1), false)
eq(is_buf_enabled(buf_id_bad_2), false)
-- Should not force load unloaded buffers
eq(child.api.nvim_buf_is_loaded(buf_id_bad_2), false)
end
T['show_at_cursor()'] = new_set({ hooks = { pre_case = load_module } })
local show_at_cursor = forward_lua('MiniGit.show_at_cursor')
T['show_at_cursor()']['works on commit'] = function()
local buf_id = get_buf()
set_lines({ 'abc1234.def' })
set_cursor(1, 0)
child.lua([[_G.stdio_queue = { { { 'out', 'commit abc123456\nHello' } } }]])
show_at_cursor()
local ref_args = { '--no-pager', 'show', '--stat', '--patch', 'abc1234' }
local ref_git_spawn_log = { { args = ref_args, cwd = child.fn.getcwd() } }
validate_git_spawn_log(ref_git_spawn_log)
clear_spawn_log()
eq(child.api.nvim_tabpage_get_number(0), 2)
eq(get_lines(), { 'commit abc123456', 'Hello' })
eq(child.o.filetype, 'git')
-- Should use `<cword>`
set_buf(buf_id)
set_cursor(1, 0)
child.bo.iskeyword = child.bo.iskeyword .. ',.'
show_at_cursor()
-- - No `git show` calls because "abc1234.def" does not match commit pattern
validate_git_spawn_log({})
end
T['show_at_cursor()']['uses correct pattern to match commit'] = function()
log_calls('MiniGit.show_at_cursor')
set_lines({ 'abc1234', 'abc123', 'abC1234', 'abc123x' })
local validate = function(cword, ref_is_commit)
set_lines({ cword })
set_cursor(1, 0)
clear_spawn_log()
show_at_cursor()
local ref_args = { '--no-pager', 'show', '--stat', '--patch', cword }
local spawn_log = get_spawn_log()
local is_commit = spawn_log[1] ~= nil and vim.deep_equal(spawn_log[1].options.args, ref_args)
eq(is_commit, ref_is_commit)
end
validate('abc1234', true)
validate('abc123', false)
validate('abC1234', false)
validate('abc123x', false)
validate('abc1234x', false)
end
T['show_at_cursor()']['uses `opts` on commit'] = function()
set_lines({ 'abc1234' })
child.lua([[_G.stdio_queue = { { { 'out', 'commit abc123456\nHello' } } }]])
local init_win_id = get_win()
10BC8
div>
show_at_cursor({ split = 'vertical' })
eq(child.api.nvim_tabpage_get_number(0), 1)
eq(child.fn.winlayout(), { 'row', { { 'leaf', get_win() }, { 'leaf', init_win_id } } })
eq(get_lines(), { 'commit abc123456', 'Hello' })
eq(child.o.filetype, 'git')
end
T['show_at_cursor()']["works in 'mini.deps' confirmation buffer"] = function()
local deps_buf_id = new_scratch_buf()
set_buf(deps_buf_id)
local lines = child.fn.readfile(test_dir_absolute .. '/deps-confirm')
set_lines(lines)
child.bo.filetype = 'minideps-confirm'
child.api.nvim_buf_set_name(0, 'mini-deps://confirm-update')
local validate_no_show = function(l_num)
-- Assume not existing `cwd` (because it should be called during checking
-- if the buffer's directory is inside git repo, which is not)
child.lua([[vim.fn.isdirectory = function() return 0 end]])
-- Should not depend on column
set_cursor(l_num, l_num % 13)
show_at_cursor()
eq(get_buf(), deps_buf_id)
eq(#child.api.nvim_list_tabpages(), 1)
-- A spawn log as if nothing is detected at "show commit" step (entries
-- come from the next steps)
validate_git_spawn_log({})
clear_spawn_log()
validate_notifications({ { '(mini.git) Nothing Git-related to show at cursor', 'WARN' } })
clear_notify_log()
end
local validate_show = function(l_num, commit, plugin_name)
-- Prepare spawn mocks data
mock_spawn()
child.lua([[_G.stdio_queue = { { { 'out', 'commit abc123456\nHello' } } }]])
-- Assume `cwd` always exists (for easier `deps-confirm` mock text)
child.lua([[vim.fn.isdirectory = function() return 1 end]])
set_cursor(l_num, l_num % 13)
show_at_cursor()
-- Should show in commit info in separate tabpage
expect.no_equality(get_buf(), deps_buf_id)
eq(child.bo.filetype, 'git')
eq(get_lines(), { 'commit abc123456', 'Hello' })
eq(#child.api.nvim_list_tabpages(), 2)
eq(child.api.nvim_tabpage_get_number(0), 2)
eq(#child.api.nvim_tabpage_list_wins(0), 1)
local ref_git_spawn_log = {
{
args = { '--no-pager', 'show', '--stat', '--patch', commit },
cwd = '/home/user/.local/share/nvim/site/pack/deps/opt/' .. plugin_name,
},
}
validate_git_spawn_log(ref_git_spawn_log)
clear_spawn_log()
validate_notifications({})
clear_notify_log()
-- Clean up
child.cmd('quit')
end
for i = 1, 11 do
validate_no_show(i)
end
validate_show(12, 'aa339f6ab611da07183a7fe44daa482605392502', 'plugin_b')
validate_show(13, '093b29f2b409278e2ed69a90462fee54714b5a84', 'plugin_b')
validate_show(14, '093b29f2b409278e2ed69a90462fee54714b5a84', 'plugin_b')
validate_show(15, '093b29f2b409278e2ed69a90462fee54714b5a84', 'plugin_b')
validate_show(16, '093b29f2', 'plugin_b')
validate_show(17, '093b29f2', 'plugin_b')
validate_show(18, '093b29f2', 'plugin_b')
validate_show(19, 'bfe74a48', 'plugin_b')
validate_show(20, 'bfe74a48', 'plugin_b')
validate_show(21, 'bfe74a48', 'plugin_b')
validate_show(22, '3826d0c4', 'plugin_b')
validate_show(23, '3826d0c4', 'plugin_b')
validate_show(24, '3826d0c4', 'plugin_b')
validate_no_show(25)
validate_no_show(26)
validate_no_show(27)
validate_show(28, '3a3c6244553f13fdd92d312c82722b57ce6c4bec', 'plugin_c')
validate_show(29, 'fe3deb7f67ce0cc4ebfe2ea6c1c7ae1c7a939d73', 'plugin_c')
validate_show(30, 'fe3deb7f67ce0cc4ebfe2ea6c1c7ae1c7a939d73', 'plugin_c')
validate_show(31, 'fe3deb7f67ce0cc4ebfe2ea6c1c7ae1c7a939d73', 'plugin_c')
validate_show(32, 'fe3deb7', 'plugin_c')
validate_show(33, 'fe3deb7', 'plugin_c')
validate_show(34, 'fe3deb7', 'plugin_c')
validate_no_show(35)
validate_no_show(36)
validate_no_show(37)
validate_show(38, 'd231729b13da28fd1625c3d85f2315886ddeb05d', 'plugin_d')
end
T['show_at_cursor()']["uses `opts` in 'mini.deps' confirmation buffer"] = function()
local deps_buf_id = new_scratch_buf()
set_buf(deps_buf_id)
local lines = child.fn.readfile(test_dir_absolute .. '/deps-confirm')
set_lines(lines)
child.bo.filetype = 'minideps-confirm'
child.api.nvim_buf_set_name(0, 'mini-deps://confirm-update')
child.lua([[vim.fn.isdirectory = function() return 1 end]])
child.lua([[_G.stdio_queue = { { { 'out', 'commit abc123456\nHello' } } }]])
local init_win_id = get_win()
set_cursor(16, 0)
show_at_cursor({ split = 'vertical' })
expect.no_equality(get_buf(), deps_buf_id)
eq(child.api.nvim_tabpage_get_number(0), 1)
eq(child.fn.winlayout(), { 'row', { { 'leaf', get_win() }, { 'leaf', init_win_id } } })
eq(get_lines(), { 'commit abc123456', 'Hello' })
eq(child.o.filetype, 'git')
end
T['show_at_cursor()']['shows message if can not find commit'] = function()
local lines = child.fn.readfile(test_dir_absolute .. '/deps-confirm')
set_lines(lines)
set_cursor(16, 2)
-- Commit at cursor
show_at_cursor()
validate_notifications({ { '(mini.git) Can not show commit 093b29f2 in repo ' .. child.fn.getcwd(), 'WARN' } })
clear_notify_log()
-- 'mini.deps' confirmation buffer
child.bo.filetype = 'minideps-confirm'
child.api.nvim_buf_set_name(0, 'mini-deps://confirm-update')
show_at_cursor()
local path = '/home/user/.local/share/nvim/site/pack/deps/opt/plugin_b'
validate_notifications({ { '(mini.git) Can not show commit 093b29f2 in repo ' .. path, 'WARN' } })
end
T['show_at_cursor()']['works for diff source'] = function()
child.lua('MiniGit.show_diff_source = function() end')
log_calls('MiniGit.show_diff_source')
local log_output = child.fn.readfile(test_dir_absolute .. '/log-output')
set_lines(log_output)
set_cursor(17, 0)
local opts = { split = 'vertical', target = 'both' }
show_at_cursor(opts)
validate_calls({ { 'MiniGit.show_diff_source', opts } })
end
T['show_at_cursor()']['works for range history in tracked file'] = function()
child.lua('MiniGit.show_range_history = function() end')
log_calls('MiniGit.show_range_history')
mock_init_track_stdio_queue()
child.lua([[_G.stdio_queue = _G.init_track_stdio_queue]])
edit(git_file_path)
eq(is_buf_enabled(), true)
clear_spawn_log()
local opts = { line_start = 1, line_end = 2, split = 'vertical', log_args = { '--oneline' } }
show_at_cursor(opts)
validate_calls({ { 'MiniGit.show_range_history', opts } })
-- Should not use spawn to check if buffer is in repo (it is already tracked)
validate_git_spawn_log({})
end
T['show_at_cursor()']['works for range history in not tracked file'] = function()
child.lua('MiniGit.show_range_history = function() end')
log_calls('MiniGit.show_range_history')
-- Mock real path presence
child.lua([[
vim.loop.fs_realpath = function() return "/home/user/repo-root/source/of/symlink" end
vim.fn.isdirectory = function() return 1 end
]])
child.lua([[_G.stdio_queue = { { { 'out', '/home/user/repo-root' } } }]])
set_lines({ 'Line 1' })
-- Should try to find Git root at symlink's source
show_at_cursor()
validate_git_spawn_log({
{ args = { '--no-pager', 'rev-parse', '--show-toplevel' }, cwd = '/home/user/repo-root/source/of' },
})
validate_calls({ { 'MiniGit.show_range_history' } })
end
T['show_at_cursor()']['works for range history in `show_diff_source()` output'] = function()
child.lua('MiniGit.show_range_history = function() end')
log_calls('MiniGit.show_range_history')
child.lua([[_G.stdio_queue = { { { 'out', 'Line 1\nCurrent line 2\nLine 3' } } }]])
local log_output = child.fn.readfile(test_dir_absolute .. '/log-output')
set_lines(log_output)
set_cursor(17, 0)
child.lua('MiniGit.show_diff_source()')
local opts = { line_start = 1, line_end = 2, split = 'vertical', log_args = { '--oneline' } }
show_at_cursor(opts)
validate_calls({ { 'MiniGit.show_range_history', opts } })
end
T['show_at_cursor()']['works on nothing'] = function()
expect.no_error(show_at_cursor)
validate_notifications({ { '(mini.git) Nothing Git-related to show at cursor', 'WARN' } })
clear_notify_log()
-- Should work in non-path buffers
set_buf(new_scratch_buf())
child.api.nvim_buf_set_name(0, 'minigit://2/git show')
clear_spawn_log()
expect.no_error(show_at_cursor)
validate_notifications({ { '(mini.git) Nothing Git-related to show at cursor', 'WARN' } })
-- - Should not try to find git directory in "show range history" stage
validate_git_spawn_log({})
end
T['show_diff_source()'] = new_set({
hooks = {
pre_case = function()
-- Show log output
local log_output = child.fn.readfile(test_dir_absolute .. '/log-output')
set_lines(log_output)
load_module()
end,
},
})
local show_diff_source = forward_lua('MiniGit.show_diff_source')
T['show_diff_source()']['works'] = function()
child.lua([[_G.stdio_queue = {
{ { 'out', 'Line 1\nCurrent line 2\nLine 3' } }, -- Diff source
}]])
-- Show diff source
set_cursor(17, 0)
show_diff_source()
local ref_git_spawn_log = {
{
args = { '--no-pager', 'show', '5ed8432441b495fa9bd4ad2e4f635bae64e95cc2:dir/file-after' },
cwd = child.fn.getcwd(),
},
}
validate_git_spawn_log(ref_git_spawn_log)
eq(#child.api.nvim_list_tabpages(), 2)
eq(child.api.nvim_tabpage_get_number(0), 2)
validate_minigit_name(0, 'show 5ed8432441b495fa9bd4ad2e4f635bae64e95cc2:dir/file-after')
eq(get_lines(), { 'Line 1', 'Current line 2', 'Line 3' })
eq(get_cursor(), { 2, 0 })
end
T['show_diff_source()']['works in not diff file'] = function()
set_lines({ 'Not', 'a', 'patch' })
set_cursor(3, 0)
expect.no_error(show_diff_source)
validate_notifications({
{ '(mini.git) Could not find diff source. Ensure that cursor is inside a valid diff lines of git log.', 'WARN' },
})
end
T['show_diff_source()']['correctly identifies source'] = function()
local log_output = child.fn.readfile(test_dir_absolute .. '/log-output')
child.lua([[
_G.source_lines = {}
for i = 1, 500 do
table.insert(_G.source_lines, 'Line ' .. i)
end
_G.show_out = table.concat(_G.source_lines, '\n')
]])
local source_lines = child.lua_get('_G.source_lines')
local validate_ok = function(lnum, ref_commit, ref_path, ref_lnum)
mock_spawn()
child.lua([[_G.stdio_queue = { { { 'out', _G.show_out } } }]])
set_lines(log_output)
set_cursor(lnum, 0)
show_diff_source()
local ref_git_spawn_log =
{ { args = { '--no-pager', 'show', ref_commit .. ':' .. ref_path }, cwd = child.fn.getcwd() } }
validate_git_spawn_log(ref_git_spawn_log)
eq(get_lines(), source_lines)
eq(get_cursor(), { ref_lnum, 0 })
validate_minigit_name(0, 'show ' .. ref_commit .. ':' .. ref_path)
-- Clean up
child.cmd('%bwipeout!')
end
local validate_no_ok = function(lnum)
mock_spawn()
set_lines(log_output)
set_cursor(lnum, 0)
expect.no_error(show_diff_source)
eq(get_spawn_log(), {})
validate_notifications({
{ '(mini.git) Could not find diff source. Ensure that cursor is inside a valid diff lines of git log.', 'WARN' },
})
clear_notify_log()
end
local commit_after = '5ed8432441b495fa9bd4ad2e4f635bae64e95cc2'
local commit_before = commit_after .. '~'
-- Cursor should be placed inside valid hunk
validate_no_ok(1)
validate_no_ok(2)
validate_no_ok(3)
validate_no_ok(10)
validate_no_ok(11)
-- Should place on the first line for lines showing target files
validate_ok(12, commit_before, 'dir/file-before', 1)
validate_ok(13, commit_after, 'dir/file-after', 1)
-- Should work inside hunks and place cursor on the corresponding line.
-- Should (with default `target = 'auto'`) pick "before" if on the deleted
-- line, "after" otherwise.
validate_ok(14, commit_after, 'dir/file-after', 1)
validate_ok(15, commit_after, 'dir/file-after', 1)
validate_ok(16, commit_before, 'dir/file-before', 2)
validate_ok(17, commit_after, 'dir/file-after', 2)
validate_ok(18, commit_after, 'dir/file-after', 3)
validate_ok(19, commit_after, 'dir/file-after', 316)
validate_ok(20, commit_after, 'dir/file-after', 317)
validate_ok(21, commit_after, 'dir/file-after', 318)
validate_ok(22, commit_after, 'dir/file-after', 319)
validate_ok(23, commit_after, 'dir/file-after', 320)
validate_no_ok(24)
validate_no_ok(25)
-- Should get proper (nearest from above) file
validate_ok(26, commit_before, 'file', 1)
validate_ok(27, commit_after, 'file', 1)
validate_ok(28, commit_after, 'file', 282)
validate_ok(29, commit_after, 'file', 283)
validate_ok(30, commit_before, 'file', 284)
validate_ok(31, commit_before, 'file', 285)
validate_ok(32, commit_after, 'file', 284)
-- - Between log entries is also not a valid diff line
validate_no_ok(33)
validate_no_ok(34)
validate_no_ok(35)
-- Should get proper (nearest from above) commit
-- Should also work with `diff.mnemonicPrefix=true` Git config setting, which
-- can have source and destination prefixes be not only `a/` and `b/`
local commit_after_2 = '7264474d3bda16d0098a7f89a4143fe4db3d82cf'
local commit_before_2 = commit_after_2 .. '~'
validate_ok(42, commit_before_2, 'dir/file1', 1)
validate_ok(43, commit_after_2, 'dir/file1', 1)
validate_ok(44, commit_after_2, 'dir/file1', 246)
validate_ok(45, commit_before_2, 'dir/file1', 247)
validate_ok(46, commit_after_2, 'dir/file1', 247)
end
T['show_diff_source()']['correctly identifies source for `:Git diff` output'] = function()
local diff_output = {
'diff --git a/file b/file',
'index a357c5c..7ec2b3c 100644',
'--- a/file',
'+++ b/file',
'@@ -1,2 +1,2 @@',
' aaa',
'-uuu',
'+UUU',
}
child.lua('_G.diff_lines = ' .. vim.inspect(table.concat(diff_output, '\n')))
child.lua([[
_G.stdio_queue = {
-- Mock initial spawns for gathering subcommand data
{ { 'out', 'diff\nshow' } }, { { 'out', 'diff\nshow' } }, { { 'out', '' } },
{ { 'out', _G.diff_lines } }, -- :Git diff
{ { 'out', 'aaa\nuuu' } }, -- "Before" state
-- No spawn for "after"
{ { 'out', _G.diff_lines } }, -- :Git diff --cached
{ { 'out', 'aaa\nuuu' } }, -- "Before" state
{ { 'out', 'aaa\nUUU' } }, -- "After" state
{ { 'out', _G.diff_lines } }, -- :Git diff abc1234
{ { 'out', 'aaa\nuuu' } }, -- "Before" state
-- No spawn for "after"
}
]])
child.fn.chdir(test_dir_absolute)
local diff_win_id
local validate = function(lnum, ref_git_spawn_log, ref_lines, ref_cursor)
clear_spawn_log()
child.api.nvim_set_current_win(diff_win_id)
set_cursor(lnum, 0)
show_diff_source()
validate_git_spawn_log(ref_git_spawn_log)
eq(get_lines(), ref_lines)
eq(get_cursor(), ref_cursor)
child.cmd('bwipeout!')
end
-- Compare index (before) and work tree (after)
child.cmd('Git diff')
diff_win_id = get_win()
-- - "Before"
validate(7, { { '--no-pager', 'show', ':0:file' } }, { 'aaa', 'uuu' }, { 2, 0 })
-- - "After"
validate(8, {}, { 'aaa', 'uuu', '', 'xxx' }, { 2, 0 })
-- Compare HEAD (before) and index (after)
child.cmd('Git diff --cached')
diff_win_id = get_win()
-- - "Before"
validate(7, { { '--no-pager', 'show', 'HEAD:file' } }, { 'aaa', 'uuu' }, { 2, 0 })
-- - "After"
validate(8, { { '--no-pager', 'show', ':0:file' } }, { 'aaa', 'UUU' }, { 2, 0 })
-- Compare commit (before) and work tree (after)
child.cmd('Git diff abc1234')
diff_win_id = get_win()
-- - "Before"
validate(7, { { '--no-pager', 'show', 'abc1234:file' } }, { 'aaa', 'uuu' }, { 2, 0 })
-- - "After"
validate(8, {}, { 'aaa', 'uuu', '', 'xxx' }, { 2, 0 })
end
T['show_diff_source()']['works when there is no "before" file'] = function()
child.lua([[_G.stdio_queue = {
{ { 'out', 'Line 1\nCurrent line 2\nLine 3' } }, -- Diff source
}]])
set_lines({
'commit 5ed8432441b495fa9bd4ad2e4f635bae64e95cc2',
'Author: Neo McVim <neo.mcvim@gmail.com>',
'Date: Sat May 4 16:24:15 2024 +0300',
'',
'Add file with relative path "dev/null".',
'',
'diff --git a/dev/null b/dev/null',
'new file mode 100644',
'index 0000000..f9264f7',
'--- /dev/null',
'+++ b/dev/null',
'@@ -0,0 +1,2 @@',
'+Hello',
'+World',
})
-- Target "before" should do nothing while showing notification
local init_buf_id = get_buf()
set_cursor(13, 0)
show_diff_source({ target = 'before' })
eq(get_buf(), init_buf_id)
eq(child.api.nvim_buf_get_name(0), '')
validate_git_spawn_log({})
clear_spawn_log()
validate_notifications({ { '(mini.git) No "before" as file was created', 'WARN' } })
clear_notify_log()
-- Target "both" should show only "after" in a specified split
set_cursor(13, 0)
show_diff_source({ target = 'both' })
eq(child.api.nvim_tabpage_get_number(0), 2)
eq(#child.api.nvim_tabpage_list_wins(0), 1)
validate_minigit_name(0, 'show 5ed8432441b495fa9bd4ad2e4f635bae64e95cc2:dev/null')
validate_notifications({ { '(mini.git) No "before" as file was created', 'WARN' } })
end
T['show_diff_source()']['works when there is no "after" file'] = function()
child.lua([[_G.stdio_queue = {
{ { 'out', 'Line 1\nCurrent line 2\nLine 3' } }, -- Diff source
}]])
set_lines({
'commit 5ed8432441b495fa9bd4ad2e4f635bae64e95cc2',
'Author: Neo McVim <neo.mcvim@gmail.com>',
'Date: Sat May 4 16:24:15 2024 +0300',
'',
'Remove file with relative path "dev/null".',
'',
'diff --git a/dev/null b/dev/null',
'new file mode 100644',
'index 0000000..f9264f7',
'--- a/dev/null',
'+++ /dev/null',
'@@ -2 +0,0 @@',
'-Hello',
'-World',
})
-- Target "after" should do nothing while showing notification
local init_buf_id = get_buf()
set_cursor(13, 0)
show_diff_source({ target = 'after' })
eq(get_buf(), init_buf_id)
eq(child.api.nvim_buf_get_name(0), '')
validate_git_spawn_log({})
clear_spawn_log()
validate_notifications({ { '(mini.git) No "after" as file was deleted', 'WARN' } })
clear_notify_log()
-- Target "both" should show only "before" in a specified split
set_cursor(13, 0)
show_diff_source({ target = 'both' })
eq(child.api.nvim_tabpage_get_number(0), 2)
eq(#child.api.nvim_tabpage_list_wins(0), 1)
validate_minigit_name(0, 'show 5ed8432441b495fa9bd4ad2e4f635bae64e95cc2~:dev/null')
validate_notifications({ { '(mini.git) No "after" as file was deleted', 'WARN' } })
end
T['show_diff_source()']['does not depend on cursor column'] = function()
local buf_id = get_buf()
for i = 0, 10 do
set_buf(buf_id)
set_cursor(17, i)
show_diff_source()
eq(get_cursor(), { 2, 0 })
end
end
T['show_diff_source()']['tries to infer and set filetype'] = function()
child.lua([[_G.stdio_queue = { { { 'out', 'local a = 1\n-- This is a Lua comment' } } }]])
set_cursor(57, 0)
show_diff_source()
validate_minigit_name(0, 'show 7264474d3bda16d0098a7f89a4143fe4db3d82cf:file.lua')
eq(get_lines(), { 'local a = 1', '-- This is a Lua comment' })
eq(get_cursor(), { 1, 0 })
eq(child.bo.filetype, 'lua')
eq(child.wo.foldlevel, 0)
end
T['show_diff_source()']['respects `opts.split`'] = new_set(
{ parametrize = { { 'horizontal' }, { 'vertical' }, { 'tab' } } },
{
test = function(split)
child.lua([[_G.stdio_queue = {
{ { 'out', 'Line 1\nCurrent line 2\nLine 3' } }, -- Diff source
}]])
set_cursor(17, 0)
local init_win_id = get_win()
show_diff_source({ split = split })
local cur_win_id = get_win()
local ref_git_spawn_log = {
{
args = { '--no-pager', 'show', '5ed8432441b495fa9bd4ad2e4f635bae64e95cc2:dir/file-after' },
cwd = child.fn.getcwd(),
},
}
validate_git_spawn_log(ref_git_spawn_log)
validate_minigit_name(0, 'show 5ed8432441b495fa9bd4ad2e4f635bae64e95cc2:dir/file-after')
-- Validate proper split
eq(#child.api.nvim_list_tabpages(), split == 'tab' and 2 or 1)
eq(child.api.nvim_tabpage_get_number(0), split == 'tab' and 2 or 1)
local ref_layout = ({
horizontal = { 'col', { { 'leaf', cur_win_id }, { 'leaf', init_win_id } } },
vertical = { 'row', { { 'leaf', cur_win_id }, { 'leaf', init_win_id } } },
tab = { 'leaf', cur_win_id },
})[split]
eq(child.fn.winlayout(), ref_layout)
end,
}
)
T['show_diff_source()']['works with `opts.split = "auto"`'] = function()
child.lua([[_G.stdio_queue = {
{ { 'out', 'Line 1\nCurrent line 2\nLine 3' } }, -- Diff source
{ { 'out', 'Line 4\nCurrent line 5\nLine 6' } }, -- Diff source
}]])
local init_buf_id, init_win_id = get_buf(), get_win()
-- Should open in new tabpage if there is a non-minigit buffer visible
child.cmd('vertical split')
local buf_id = new_scratch_buf()
set_buf(buf_id)
child.api.nvim_buf_set_name(buf_id, make_minigit_name(buf_id, 'some mini.git buffer'))
eq(child.fn.winlayout()[1], 'row')
child.api.nvim_set_current_win(init_win_id)
set_cursor(17, 0)
show_diff_source({ split = 'auto' })
local win_id_1 = get_win()
eq(child.api.nvim_tabpage_get_number(0), 2)
eq(child.fn.winlayout(), { 'leaf', win_id_1 })
-- Should split vertically if there are only minigit buffers visible
set_buf(init_buf_id)
child.api.nvim_buf_set_name(0, make_minigit_name(0, 'log -L1,1:file'))
set_cursor(17, 0)
show_diff_source({ split = 'auto' })
eq(child.api.nvim_tabpage_get_number(0), 2)
eq(child.fn.winlayout(), { 'row', { { 'leaf', get_win() }, { 'leaf', win_id_1 } } })
end
T['show_diff_source()']['respects `opts.target`'] = function()
child.lua([[
local item = { { 'out', 'Line 1\nCurrent line 2\nLine 3' } }
_G.stdio_queue = {
item, -- 'before'
item, -- 'before'
item, -- 'after'
item, -- 'after'
item, item, -- 'both'
item, item, -- 'both'
item, item, -- 'both'
item, item, -- 'both'
}]])
local init_lines = get_lines()
local commit_after = '5ed8432441b495fa9bd4ad2e4f635bae64e95cc2'
local commit_before = commit_after .. '~'
local name_after = 'show ' .. commit_after .. ':dir/file-after'
local name_before = 'show ' .. commit_before .. ':dir/file-before'
local validate = function(target, lnum, layout_type, name, cursor)
child.cmd('%bwipeout!')
set_lines(init_lines)
set_cursor(lnum, 0)
show_diff_source({ target = target, split = 'tab' })
local layout = child.fn.winlayout()
eq(layout[1], layout_type)
validate_minigit_name(0, name)
eq(get_cursor(), cursor)
if layout_type == 'row' then
-- Current window with "after" file should be on the right
local all_wins, cur_win = child.api.nvim_tabpage_list_wins(0), get_win()
local other_win = all_wins[1] == cur_win and all_wins[2] or all_wins[1]
eq(layout, { 'row', { { 'leaf', other_win }, { 'leaf', cur_win } } })
-- Other window should contain "before" file
local other_buf = child.api.nvim_win_get_buf(other_win)
validate_minigit_name(other_buf, name_before)
end
end
-- "Before" should always show "before" file
validate('before', 17, 'leaf', name_before, { 2, 0 })
-- - Even when cursor is on "+++ b/yyy" line
validate('before', 13, 'leaf', name_before, { 1, 0 })
-- "After" should always show "after" file
validate('after', 16, 'leaf', name_after, { 1, 0 })
-- - Even when cursor is on "--- a/xxx" line
validate('after', 12, 'leaf', name_after, { 1, 0 })
-- "Both" should always show vertical split with "after" to the right