-
Notifications
You must be signed in to change notification settings - Fork 183
/
test_files.lua
4159 lines (3309 loc) · 116 KB
/
test_files.lua
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
--stylua: ignore start
local load_module = function(config) child.mini_load('files', config) end
local unload_module = function() child.mini_unload('files') 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
--stylua: ignore end
-- Tweak `expect_screenshot()` to test only on Neovim>=0.9 (as it introduced
-- titles). Use `expect_screenshot_orig()` for original testing.
local expect_screenshot_orig = child.expect_screenshot
child.expect_screenshot = function(...)
if child.fn.has('nvim-0.9') == 0 then return end
expect_screenshot_orig(...)
end
-- Test paths helpers
local test_dir = 'tests/dir-files'
local join_path = function(...) return table.concat({ ... }, '/') end
local full_path = function(...) return (vim.fn.fnamemodify(join_path(...), ':p'):gsub('\\', '/'):gsub('(.)/$', '%1')) end
local short_path = function(...) return (vim.fn.fnamemodify(join_path(...), ':~'):gsub('\\', '/'):gsub('(.)/$', '%1')) end
local make_test_path = function(...) return full_path(join_path(test_dir, ...)) end
local make_temp_dir = function(name, children)
-- Make temporary directory and make sure it is removed after test is done
local temp_dir = make_test_path(name or 'temp')
vim.fn.mkdir(temp_dir, 'p')
MiniTest.finally(function() vim.fn.delete(temp_dir, 'rf') end)
-- Create children
for _, path in ipairs(children) do
local path_ext = temp_dir .. '/' .. path
if vim.endswith(path, '/') then
vim.fn.mkdir(path_ext)
else
vim.fn.writefile({}, path_ext)
end
end
return temp_dir
end
-- Common validators and helpers
local validate_directory = function(...) eq(child.fn.isdirectory(join_path(...)), 1) end
local validate_no_directory = function(...) eq(child.fn.isdirectory(join_path(...)), 0) end
local validate_file = function(...) eq(child.fn.filereadable(join_path(...)), 1) end
local validate_no_file = function(...) eq(child.fn.filereadable(join_path(...)), 0) end
local validate_file_content = function(path, lines) eq(child.fn.readfile(path), lines) end
local validate_cur_line = function(x) eq(get_cursor()[1], x) end
local validate_n_wins = function(n) eq(#child.api.nvim_tabpage_list_wins(0), n) end
local validate_fs_entry = function(x)
eq(type(x), 'table')
eq(x.fs_type == 'file' or x.fs_type == 'directory', true)
eq(type(x.name), 'string')
eq(type(x.path), 'string')
end
local validate_confirm_args = function(ref_msg_pattern)
local args = child.lua_get('_G.confirm_args')
expect.match(args[1], ref_msg_pattern)
if args[2] ~= nil then eq(args[2], '&Yes\n&No') end
if args[3] ~= nil then eq(args[3], 1) end
if args[4] ~= nil then eq(args[4], 'Question') end
end
local make_plain_pattern = function(...) return table.concat(vim.tbl_map(vim.pesc, { ... }), '.*') end
local is_file_in_buffer = function(buf_id, path)
return string.find(child.api.nvim_buf_get_name(buf_id):gsub('\\', '/'), vim.pesc(path:gsub('\\', '/')) .. '$') ~= nil
end
local is_file_in_window = function(win_id, path) return is_file_in_buffer(child.api.nvim_win_get_buf(win_id), path) end
-- Common test wrappers
local forward_lua = function(fun_str)
local lua_cmd = fun_str .. '(...)'
return function(...) return child.lua_get(lua_cmd, { ... }) end
end
local open = forward_lua('MiniFiles.open')
local close = forward_lua('MiniFiles.close')
local go_in = forward_lua('MiniFiles.go_in')
local go_out = forward_lua('MiniFiles.go_out')
local trim_left = forward_lua('MiniFiles.trim_left')
local trim_right = forward_lua('MiniFiles.trim_right')
-- Extmark helper
local get_extmarks_hl = function()
local ns_id = child.api.nvim_get_namespaces()['MiniFilesHighlight']
local extmarks = child.api.nvim_buf_get_extmarks(0, ns_id, 0, -1, { details = true })
return vim.tbl_map(function(x) return x[4].hl_group end, extmarks)
end
-- Common mocks
local mock_win_functions = function() child.cmd('source tests/dir-files/mock-win-functions.lua') end
local mock_confirm = function(user_choice)
local lua_cmd = string.format(
[[vim.fn.confirm = function(...)
_G.confirm_args = { ... }
return %d
end]],
user_choice
)
child.lua(lua_cmd)
end
local mock_stdpath_data = function()
local data_dir = make_test_path('data')
local lua_cmd = string.format(
[[
_G.stdpath_orig = vim.fn.stpath
vim.fn.stdpath = function(what)
if what == 'data' then return %s end
return _G.stdpath_orig(what)
end]],
vim.inspect(data_dir)
)
child.lua(lua_cmd)
return data_dir
end
-- Data =======================================================================
local test_dir_path = 'tests/dir-files/common'
local test_file_path = 'tests/dir-files/common/a-file'
local test_fs_entries = {
-- Intentionally in proper order
{ fs_type = 'directory', name = '.a-dir', path = full_path('.a-dir') },
{ fs_type = 'directory', name = 'a-dir', path = full_path('a-dir') },
{ fs_type = 'directory', name = 'b-dir', path = full_path('b-dir') },
{ fs_type = 'file', name = '.a-file', path = full_path('.a-file') },
{ fs_type = 'file', name = 'a-file', path = full_path('a-file') },
{ fs_type = 'file', name = 'A-file-2', path = full_path('A-file-2') },
{ fs_type = 'file', name = 'b-file', path = full_path('b-file') },
}
-- Time constants
local track_lost_focus_delay = 1000
local small_time = helpers.get_time_const(10)
-- Output test set ============================================================
local T = new_set({
hooks = {
pre_case = function()
child.setup()
mock_win_functions()
child.set_size(15, 80)
load_module()
-- Mock `vim.notify()`
child.lua([[
_G.notify_log = {}
vim.notify = function(...) table.insert(_G.notify_log, { ... }) end
]])
-- Make more robust screenshots
child.o.laststatus = 0
-- - Hide intro
child.cmd('vsplit')
child.cmd('quit')
end,
post_case = function() vim.fn.delete(make_test_path('data'), 'rf') end,
post_once = child.stop,
},
})
-- Unit tests =================================================================
T['setup()'] = new_set()
T['setup()']['creates side effects'] = function()
-- Global variable
eq(child.lua_get('type(_G.MiniFiles)'), 'table')
-- Autocommand group
eq(child.fn.exists('#MiniFiles'), 1)
-- Highlight groups
child.cmd('hi clear')
load_module()
local validate_hl_group = function(name, ref) expect.match(child.cmd_capture('hi ' .. name), ref) end
validate_hl_group('MiniFilesBorder', 'links to FloatBorder')
validate_hl_group('MiniFilesBorderModified', 'links to DiagnosticFloatingWarn')
validate_hl_group('MiniFilesCursorLine', 'links to CursorLine')
validate_hl_group('MiniFilesDirectory', 'links to Directory')
eq(child.fn.hlexists('MiniFilesFile'), 1)
validate_hl_group('MiniFilesNormal', 'links to NormalFloat')
validate_hl_group('MiniFilesTitle', 'links to FloatTitle')
validate_hl_group('MiniFilesTitleFocused', 'links to FloatTitle')
end
T['setup()']['creates `config` field'] = function()
eq(child.lua_get('type(_G.MiniFiles.config)'), 'table')
-- Check default values
local expect_config = function(field, value) eq(child.lua_get('MiniFiles.config.' .. field), value) end
expect_config('content.filter', vim.NIL)
expect_config('content.prefix', vim.NIL)
expect_config('content.sort', vim.NIL)
expect_config('mappings.close', 'q')
expect_config('mappings.go_in', 'l')
expect_config('mappings.go_in_plus', 'L')
expect_config('mappings.go_out', 'h')
expect_config('mappings.go_out_plus', 'H')
expect_config('mappings.reset', '<BS>')
expect_config('mappings.reveal_cwd', '@')
expect_config('mappings.show_help', 'g?')
expect_config('mappings.synchronize', '=')
expect_config('mappings.trim_left', '<')
expect_config('mappings.trim_right', '>')
expect_config('options.use_as_default_explorer', true)
expect_config('options.permanent_delete', true)
expect_config('windows.max_number', math.huge)
expect_config('windows.preview', false)
expect_config('windows.width_focus', 50)
expect_config('windows.width_nofocus', 15)
expect_config('windows.width_preview', 25)
end
T['setup()']['respects `config` argument'] = function()
unload_module()
load_module({ mappings = { close = 'gc' } })
eq(child.lua_get('MiniFiles.config.mappings.close'), 'gc')
end
T['setup()']['validates `config` argument'] = function()
unload_module()
local expect_config_error = function(config, name, target_type)
expect.error(load_module, vim.pesc(name) .. '.*' .. vim.pesc(target_type), config)
end
expect_config_error('a', 'config', 'table')
expect_config_error({ content = 'a' }, 'content', 'table')
expect_config_error({ content = { filter = 1 } }, 'content.filter', 'function')
expect_config_error({ content = { prefix = 1 } }, 'content.prefix', 'function')
expect_config_error({ content = { sort = 1 } }, 'content.sort', 'function')
expect_config_error({ mappings = 'a' }, 'mappings', 'table')
expect_config_error({ mappings = { close = 1 } }, 'mappings.close', 'string')
expect_config_error({ mappings = { go_in = 1 } }, 'mappings.go_in', 'string')
expect_config_error({ mappings = { go_in_plus = 1 } }, 'mappings.go_in_plus', 'string')
expect_config_error({ mappings = { go_out = 1 } }, 'mappings.go_out', 'string')
expect_config_error({ mappings = { go_out_plus = 1 } }, 'mappings.go_out_plus', 'string')
expect_config_error({ mappings = { reset = 1 } }, 'mappings.reset', 'string')
expect_config_error({ mappings = { reveal_cwd = 1 } }, 'mappings.reveal_cwd', 'string')
expect_config_error({ mappings = { show_help = 1 } }, 'mappings.show_help', 'string')
expect_config_error({ mappings = { synchronize = 1 } }, 'mappings.synchronize', 'string')
expect_config_error({ mappings = { trim_left = 1 } }, 'mappings.trim_left', 'string')
expect_config_error({ mappings = { trim_right = 1 } }, 'mappings.trim_right', 'string')
expect_config_error({ options = 'a' }, 'options', 'table')
expect_config_error({ options = { use_as_default_explorer = 1 } }, 'options.use_as_default_explorer', 'boolean')
expect_config_error({ options = { permanent_delete = 1 } }, 'options.permanent_delete', 'boolean')
expect_config_error({ windows = 'a' }, 'windows', 'table')
expect_config_error({ windows = { max_number = 'a' } }, 'windows.max_number', 'number')
expect_config_error({ windows = { preview = 1 } }, 'windows.preview', 'boolean')
expect_config_error({ windows = { width_focus = 'a' } }, 'windows.width_focus', 'number')
expect_config_error({ windows = { width_nofocus = 'a' } }, 'windows.width_nofocus', 'number')
expect_config_error({ windows = { width_preview = 'a' } }, 'windows.width_preview', 'number')
end
T['open()'] = new_set()
T['open()']['works with directory path'] = function()
-- Works with relative path
open(test_dir_path)
child.expect_screenshot()
close()
validate_n_wins(1)
-- Works with absolute path
open(vim.fn.fnamemodify(test_dir_path, ':p'))
child.expect_screenshot()
close()
validate_n_wins(1)
-- Works with trailing slash
open(test_dir_path .. '/')
child.expect_screenshot()
end
T['open()']['works with file path'] = function()
-- Works with relative path
open(test_file_path)
-- Should focus on file entry
child.expect_screenshot()
close()
-- Works with absolute path
open(vim.fn.fnamemodify(test_file_path, ':p'))
child.expect_screenshot()
end
T['open()']['works per tabpage'] = function()
open(test_dir_path)
child.expect_screenshot()
child.cmd('tabedit')
open(test_dir_path .. '/a-dir')
child.expect_screenshot()
child.cmd('tabnext')
child.expect_screenshot()
end
T['open()']['handles problematic entry names'] = function()
local temp_dir = make_temp_dir('temp', { '%a bad-file-name', 'a bad-dir.name/' })
open(temp_dir)
child.expect_screenshot()
end
T['open()']['handles backslash on Unix'] = function()
if child.lua_get('vim.loop.os_uname().sysname') == 'Windows_NT' then MiniTest.skip('Test is not for Windows.') end
local temp_dir = make_temp_dir('temp', { '\\', 'hello\\', 'wo\\rld' })
open(temp_dir)
child.expect_screenshot()
end
T['open()']['handles undo just after open'] = function()
open(test_dir_path)
type_keys('u')
eq(#get_lines() > 1, true)
eq(child.cmd_capture('1messages'), 'Already at oldest change')
end
T['open()']['uses icon provider'] = function()
-- 'mini.icons'
child.lua('require("mini.icons").setup()')
open(make_test_path('real'))
--stylua: ignore
eq(get_extmarks_hl(), {
'MiniIconsAzure', 'MiniFilesFile',
'MiniIconsYellow', 'MiniFilesFile',
'MiniIconsAzure', 'MiniFilesFile',
'MiniIconsCyan', 'MiniFilesFile',
'MiniIconsGrey', 'MiniFilesFile',
})
go_out()
--stylua: ignore
eq(get_extmarks_hl(), {
'MiniIconsAzure', 'MiniFilesDirectory',
'MiniIconsAzure', 'MiniFilesDirectory',
'MiniIconsAzure', 'MiniFilesDirectory',
'MiniIconsAzure', 'MiniFilesDirectory',
'MiniIconsAzure', 'MiniFilesFile',
'MiniIconsAzure', 'MiniFilesFile',
})
child.expect_screenshot()
close()
-- Should still prefer 'mini.icons' if 'nvim-web-devicons' is available
-- - Mock 'nvim-web-devicons'
child.cmd('set rtp+=tests/dir-files')
open(make_test_path('real'))
child.expect_screenshot()
close()
-- Should fall back to 'nvim-web-devicons'
child.lua('_G.MiniIcons = nil')
open(make_test_path('real'))
child.expect_screenshot()
end
T['open()']['uses `MiniIcons.get()` with full path'] = function()
child.set_size(15, 60)
child.lua([[vim.filetype.add({ pattern = { ['.*/common/.*%-file'] = 'make' } })]])
child.lua('require("mini.icons").setup()')
open(test_dir_path)
child.expect_screenshot()
end
T['open()']['history'] = new_set()
T['open()']['history']['opens from history by default'] = function()
open(test_dir_path)
type_keys('j')
go_in()
type_keys('2j')
child.expect_screenshot()
close()
validate_n_wins(1)
open(test_dir_path)
-- Should be exactly the same, including cursors
child.expect_screenshot()
end
T['open()']['history']['handles external changes between calls'] = function()
local temp_dir = make_temp_dir('temp', { 'subdir/' })
open(temp_dir)
go_in()
child.expect_screenshot()
close()
child.fn.delete(join_path(temp_dir, 'subdir'), 'rf')
open(temp_dir)
child.expect_screenshot()
end
T['open()']['history']['respects `use_latest`'] = function()
open(test_dir_path)
type_keys('j')
go_in()
type_keys('2j')
child.expect_screenshot()
close()
validate_n_wins(1)
open(test_dir_path, false)
-- Should be as if opened first time
child.expect_screenshot()
end
T['open()']['history']['prefers global config before taking from history'] = function()
child.lua([[
_G.filter_starts_from_a = function(fs_entry) return vim.startswith(fs_entry.name, 'a') end
_G.filter_starts_from_b = function(fs_entry) return vim.startswith(fs_entry.name, 'b') end
]])
local lua_cmd = string.format(
'MiniFiles.open(%s, false, { content = { filter = _G.filter_starts_from_a } })',
vim.inspect(test_dir_path)
)
child.lua(lua_cmd)
child.expect_screenshot()
close()
child.lua('MiniFiles.config.content.filter = _G.filter_starts_from_b')
open(test_dir_path, true)
child.expect_screenshot()
end
T['open()']['history']['stores whole branch and not only visible windows'] = function()
child.set_size(15, 60)
open(test_dir_path)
go_in()
child.expect_screenshot()
close()
child.set_size(15, 80)
-- Should show two windows
open(test_dir_path, true)
child.expect_screenshot()
end
T['open()']['history']['is shared across tabpages'] = function()
-- Prepare history
open(test_dir_path)
go_in()
child.expect_screenshot()
close()
-- Open in new tabpage
child.cmd('tabedit')
open(test_dir_path, true)
child.expect_screenshot()
go_out()
close()
child.cmd('tabnext')
open(test_dir_path, true)
child.expect_screenshot()
end
T['open()']['history']['updates target window on every call'] = function()
-- Prepare windows
local win_id_1 = child.api.nvim_get_current_win()
child.cmd('wincmd v')
local win_id_2 = child.api.nvim_get_current_win()
eq(win_id_1 ~= win_id_2, true)
-- Put explorer in history which opened in current window
open(test_file_path)
go_in()
close()
eq(is_file_in_window(win_id_1, test_file_path), false)
eq(is_file_in_window(win_id_2, test_file_path), true)
child.api.nvim_win_set_buf(win_id_2, child.api.nvim_win_get_buf(win_id_1))
-- New `open()` call should register new target window
child.api.nvim_set_current_win(win_id_1)
open(test_file_path)
go_in()
close()
eq(is_file_in_window(win_id_1, test_file_path), true)
eq(is_file_in_window(win_id_2, test_file_path), false)
end
T['open()']['focuses on file entry when opened from history'] = function()
local path = make_test_path('common/a-dir/ab-file')
-- If in branch, just focus on entry
open(path)
type_keys('j')
go_out()
child.expect_screenshot()
close()
open(path)
child.expect_screenshot()
-- If not in branch, reset
go_out()
trim_right()
child.expect_screenshot()
close()
open(path)
child.expect_screenshot()
end
T['open()']['normalizes before first refresh when focused on file'] = function()
-- Prepare explorer state to be opened from history
open(make_test_path('common'))
go_in()
validate_n_wins(3)
close()
-- Mock `nvim_open_win()`
child.lua([[
_G.init_nvim_open_win = vim.api.nvim_open_win
_G.open_win_count = 0
vim.api.nvim_open_win = function(...)
_G.open_win_count = _G.open_win_count + 1
return init_nvim_open_win(...)
end
]])
-- Test. Opening file in 'common' directory makes previous two-window view
-- not synchronized with cursor (pointing at file while right window is for
-- previously opened directory). Make sure that it is made one window prior
-- to rendering, otherwise it might result in flickering.
open(make_test_path('common/a-file'))
child.expect_screenshot()
eq(child.lua_get('_G.open_win_count'), 1)
end
T['open()']['normalizes before first refresh when focused on directory with `windows.preview`'] = function()
-- Prepare explorer state to be opened from history
open(test_dir_path)
validate_n_wins(2)
close()
-- Mock `nvim_open_win()`
child.lua([[
_G.init_nvim_open_win = vim.api.nvim_open_win
_G.open_win_count = 0
vim.api.nvim_open_win = function(...)
_G.open_win_count = _G.open_win_count + 1
return init_nvim_open_win(...)
end
]])
-- Test. It should preview right away without extra window manipulations.
open(test_dir_path, true, { windows = { preview = true } })
child.expect_screenshot()
eq(child.lua_get('_G.open_win_count'), 2)
end
T['open()']['respects `content.filter`'] = function()
child.lua([[
_G.filter_arg = {}
MiniFiles.config.content.filter = function(fs_entry)
_G.filter_arg = fs_entry
-- Show only directories
return fs_entry.fs_type == 'directory'
end
]])
open(test_dir_path)
child.expect_screenshot()
validate_fs_entry(child.lua_get('_G.filter_arg'))
-- Local value from argument should take precedence
child.lua([[_G.filter_starts_from_a = function(fs_entry) return vim.startswith(fs_entry.name, 'a') end ]])
local lua_cmd = string.format(
[[MiniFiles.open(%s, false, { content = { filter = _G.filter_starts_from_a } })]],
vim.inspect(test_dir_path)
)
child.lua(lua_cmd)
child.expect_screenshot()
end
T['open()']['respects `content.prefix`'] = function()
child.set_size(15, 60)
child.lua([[
_G.prefix_arg = {}
MiniFiles.config.content.prefix = function(fs_entry)
_G.prefix_arg = fs_entry
if fs_entry.fs_type == 'directory' then
return '-', 'Comment'
else
return '|', 'Special'
end
end
]])
open(test_dir_path)
child.expect_screenshot()
validate_fs_entry(child.lua_get('_G.prefix_arg'))
-- Local value from argument should take precedence
child.lua([[_G.prefix_2 = function(fs_entry) return '|', 'Special' end ]])
local lua_cmd =
string.format([[MiniFiles.open(%s, false, { content = { prefix = _G.prefix_2 } })]], vim.inspect(test_dir_path))
child.lua(lua_cmd)
child.expect_screenshot()
end
T['open()']['`content.prefix` can be used to not show prefix'] = function()
child.lua([[MiniFiles.config.content.prefix = function() return '', '' end]])
open(test_dir_path)
go_in()
child.expect_screenshot()
end
T['open()']['`content.prefix` can return `nil`'] = function()
child.set_size(15, 60)
local validate = function(return_expr)
local lua_cmd = string.format([[MiniFiles.config.content.prefix = function() return %s end]], return_expr)
child.lua(lua_cmd)
open(test_dir_path)
child.expect_screenshot()
close()
end
validate()
validate([['', nil]])
validate([[nil, '']])
end
T['open()']['respects `content.sort`'] = function()
child.lua([[
_G.sort_arg = {}
MiniFiles.config.content.sort = function(fs_entries)
_G.sort_arg = fs_entries
-- Sort alphabetically without paying attention to file system type
local res = vim.deepcopy(fs_entries)
table.sort(res, function(a, b) return a.name < b.name end)
return res
end
]])
open(test_dir_path)
child.expect_screenshot()
local sort_arg = child.lua_get('_G.sort_arg')
eq(type(sort_arg), 'table')
for _, val in pairs(sort_arg) do
validate_fs_entry(val)
end
-- Local value from argument should take precedence
child.lua([[
_G.sort_rev_alpha = function(fs_entries)
local res = vim.deepcopy(fs_entries)
table.sort(res, function(a, b) return a.name > b.name end)
return res
end
]])
local lua_cmd =
string.format([[MiniFiles.open(%s, false, { content = { sort = _G.sort_rev_alpha } })]], vim.inspect(test_dir_path))
child.lua(lua_cmd)
child.expect_screenshot()
end
T['open()']['`content.sort` can be used to also filter items'] = function()
child.lua([[
MiniFiles.config.content.sort = function(fs_entries)
-- Sort alphabetically without paying attention to file system type
local res = vim.tbl_filter(function(x) return x.fs_type == 'directory' end, fs_entries)
table.sort(res, function(a, b) return a.name > b.name end)
return res
end
]])
open(test_dir_path)
child.expect_screenshot()
end
T['open()']['respects `mappings`'] = function()
child.lua([[MiniFiles.config.mappings.go_in = '<CR>']])
open(test_dir_path)
type_keys('<CR>')
child.expect_screenshot()
close()
-- Local value from argument should take precedence
open(test_dir_path, false, { mappings = { go_in = 'K' } })
type_keys('K')
child.expect_screenshot()
end
T['open()']['does not create mapping for empty string'] = function()
local has_map = function(lhs, pattern) return child.cmd_capture('nmap ' .. lhs):find(pattern) ~= nil end
-- Supplying empty string should mean "don't create keymap"
child.lua('MiniFiles.config.mappings.go_in = ""')
open()
eq(has_map('q', 'Close'), true)
eq(has_map('l', 'Go in'), false)
end
T['open()']['respects `windows.max_number`'] = function()
child.lua('MiniFiles.config.windows.max_number = 1')
open(test_dir_path)
go_in()
child.expect_screenshot()
close()
-- Local value from argument should take precedence
open(test_dir_path, false, { windows = { max_number = 2 } })
go_in()
child.expect_screenshot()
end
T['open()']['respects `windows.preview`'] = function()
child.lua('MiniFiles.config.windows.preview = true')
open(test_dir_path)
child.expect_screenshot()
close()
-- Local value from argument should take precedence
open(test_dir_path, false, { windows = { preview = false } })
child.expect_screenshot()
end
T['open()']['respects `windows.width_focus` and `windows.width_nofocus`'] = function()
child.lua('MiniFiles.config.windows.width_focus = 40')
child.lua('MiniFiles.config.windows.width_nofocus = 10')
open(test_dir_path)
go_in()
child.expect_screenshot()
close()
-- Local value from argument should take precedence
open(test_dir_path, false, { windows = { width_focus = 30, width_nofocus = 20 } })
go_in()
child.expect_screenshot()
end
T['open()']['respects `windows.width_preview`'] = function()
child.lua('MiniFiles.config.windows.preview = true')
child.lua('MiniFiles.config.windows.width_focus = 20')
child.lua('MiniFiles.config.windows.width_nofocus = 10')
child.lua('MiniFiles.config.windows.width_preview = 15')
local test_path = make_test_path('nested')
-- Preview window is to the right of focused one (if preview is active)
open(test_path)
child.expect_screenshot()
go_in()
child.expect_screenshot()
go_in()
child.expect_screenshot()
go_out()
child.expect_screenshot()
go_out()
child.expect_screenshot()
close()
-- Local value from argument should take precedence
open(test_path, false, { windows = { width_focus = 20, width_preview = 30 } })
child.expect_screenshot()
end
T['open()']['properly closes currently opened explorer'] = function()
local path_1, path_2 = make_test_path('common'), make_test_path('common/a-dir')
open(path_1)
go_in()
validate_n_wins(3)
-- Should properly close current opened explorer (at least save to history)
open(path_2)
close()
open(path_1, true)
child.expect_screenshot()
end
T['open()']['properly closes currently opened explorer with modified buffers'] = function()
child.cmd('au User MiniFilesExplorerClose lua _G.had_close_event = true')
child.set_size(100, 100)
local path_1, path_2 = make_test_path('common'), make_test_path('common/a-dir')
open(path_1)
type_keys('o', 'hello')
-- Should mention modified buffers and ask for confirmation
mock_confirm(1)
open(path_2)
validate_confirm_args('modified buffer.*close without sync')
-- Should trigger proper event for closing explorer
eq(child.lua_get('_G.had_close_event'), true)
end
T['open()']['tracks lost focus'] = function()
child.lua('MiniFiles.config.windows.preview = true')
local validate = function(loose_focus)
open(test_dir_path)
loose_focus()
-- Tracking is done by checking every second
sleep(track_lost_focus_delay + small_time)
validate_n_wins(1)
eq(#child.api.nvim_list_bufs(), 1)
end
local init_win_id = child.api.nvim_get_current_win()
validate(function() child.api.nvim_set_current_win(init_win_id) end)
validate(function() child.cmd('quit') end)
validate(function()
go_in()
type_keys('ZZ')
end)
-- Should still be possible to open same explorer afterwards
open(test_dir_path)
validate_n_wins(3)
end
T['open()']['validates input'] = function()
-- `path` should be a real path
expect.error(function() open('aaa') end, 'path.*not a valid path.*aaa')
end
T['open()']['respects `vim.b.minifiles_config`'] = function()
child.lua([[_G.filter_starts_from_a = function(fs_entry) return vim.startswith(fs_entry.name, 'a') end ]])
child.lua('vim.b.minifiles_config = { content = { filter = _G.filter_starts_from_a } }')
open(test_dir_path)
child.expect_screenshot()
end
T['refresh()'] = new_set()
local refresh = forward_lua('MiniFiles.refresh')
T['refresh()']['works'] = function()
open(test_dir_path)
refresh({ windows = { width_focus = 30 } })
child.expect_screenshot()
end
T['refresh()']['preserves explorer options'] = function()
open(test_dir_path, false, { windows = { width_focus = 45, width_nofocus = 10 } })
go_in()
child.expect_screenshot()
-- Current explorer options should be preserved
refresh({ windows = { width_focus = 30 } })
child.expect_screenshot()
end
T['refresh()']['does not update buffers with `nil` `filter` and `sort`'] = function()
local temp_dir = make_temp_dir('temp', { 'subdir/' })
open(temp_dir)
local buf_id_1 = child.api.nvim_get_current_buf()
go_in()
local buf_id_2 = child.api.nvim_get_current_buf()
child.expect_screenshot()
local changedtick_1 = child.api.nvim_buf_get_var(buf_id_1, 'changedtick')
local changedtick_2 = child.api.nvim_buf_get_var(buf_id_2, 'changedtick')
-- Should not update buffers
refresh()
eq(child.api.nvim_buf_get_var(buf_id_1, 'changedtick'), changedtick_1)
eq(child.api.nvim_buf_get_var(buf_id_2, 'changedtick'), changedtick_2)
-- Should not update even if there are external changes (there is
-- `synchronize()` for that)
vim.fn.mkdir(join_path(temp_dir, 'subdir', 'subsubdir'))
refresh()
child.expect_screenshot()
eq(child.api.nvim_buf_get_var(buf_id_1, 'changedtick'), changedtick_1)
eq(child.api.nvim_buf_get_var(buf_id_2, 'changedtick'), changedtick_2)
end
T['refresh()']['updates buffers with non-empty `content`'] = function()
child.lua([[
_G.hide_dotfiles = function(fs_entry) return not vim.startswith(fs_entry.name, '.') end
_G.hide_prefix = function() return '', '' end
_G.sort_rev_alpha = function(fs_entries)
local res = vim.deepcopy(fs_entries)
table.sort(res, function(a, b) return a.name > b.name end)
return res
end
]])
open(test_dir_path)
child.expect_screenshot()
child.lua('MiniFiles.refresh({ content = { filter = _G.hide_dotfiles } })')
child.expect_screenshot()
child.lua('MiniFiles.refresh({ content = { prefix = _G.hide_prefix } })')
child.expect_screenshot()
child.lua('MiniFiles.refresh({ content = { sort = _G.sort_rev_alpha } })')
child.expect_screenshot()
end
T['refresh()']['handles presence of modified buffers'] = function()
child.set_size(10, 60)
child.lua([[_G.hide_dotfiles = function(fs_entry) return not vim.startswith(fs_entry.name, '.') end ]])
local temp_dir = make_temp_dir('temp', { 'file', '.file' })
open(temp_dir)
-- On confirm should update buffers without synchronization
type_keys('o', 'new-file', '<Esc>')
child.expect_screenshot()
mock_confirm(1)
child.lua('MiniFiles.refresh({ content = { filter = _G.hide_dotfiles }, windows = { width_focus = 30 } })')
child.expect_screenshot()
validate_confirm_args('modified buffer.*Confirm buffer updates without sync')
-- On no confirm should not update buffers, but still apply other changes
type_keys('o', 'new-file-2', '<Esc>')
if child.fn.has('nvim-0.10') == 1 then child.expect_screenshot() end
mock_confirm(2)
child.lua('MiniFiles.refresh({ content = { filter = function() return true end }, windows = { width_focus = 15 } })')
if child.fn.has('nvim-0.10') == 1 then child.expect_screenshot() end
end
T['refresh()']['works when no explorer is opened'] = function() expect.no_error(refresh) end
-- More extensive testing is done in 'File Manipulation'
T['synchronize()'] = new_set()
local synchronize = forward_lua('MiniFiles.synchronize')
T['synchronize()']['can update external file system changes'] = function()
local temp_dir = make_temp_dir('temp', { 'subdir/' })
open(temp_dir)
validate_cur_line(1)
vim.fn.mkdir(join_path(temp_dir, 'aaa'))
synchronize()
child.expect_screenshot()
-- Cursor should be "sticked" to current entry
validate_cur_line(2)
end
T['synchronize()']['can apply file system actions'] = function()