Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/mini/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ H.track_dir_edit = function(data)
if vim.b.minifiles_processed_dir then
-- Smartly delete directory buffer if already visited
local alt_buf = vim.fn.bufnr('#')
if alt_buf ~= data.buf and vim.fn.buflisted(alt_buf) == 1 then vim.api.nvim_win_set_buf(0, alt_buf) end
if alt_buf ~= data.buf and vim.fn.buflisted(alt_buf) == 1 then return vim.api.nvim_win_set_buf(0, alt_buf) end
return vim.api.nvim_buf_delete(data.buf, { force = true })
end

Expand Down
19 changes: 19 additions & 0 deletions tests/dir-extra/mocks/mock-win-functions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local mock_title = function(x)
if type(x) ~= 'string' then return x end
-- Make sure that full path title is same on any machine
return x:gsub('^.*/tests/dir%-files', 'MOCK_ROOT/tests/dir-files')
end

_G.nvim_open_win_orig = vim.api.nvim_open_win

vim.api.nvim_open_win = function(buf_id, enter, config)
config.title = mock_title(config.title)
return nvim_open_win_orig(buf_id, enter, config)
end

_G.nvim_win_set_config_orig = vim.api.nvim_win_set_config

vim.api.nvim_win_set_config = function(win_id, config)
config.title = mock_title(config.title)
return nvim_win_set_config_orig(win_id, config)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--|---------|---------|---------|---------|
01|local a = 1
02|local t = {
03| x = math.max(a, 2),
04| y = math.min(a, 2),
05|┌> ▏─────────────────────┐
06|│ real-files │
07|│ │
08|│ │
09|│ │
10|│ │
11|│ │
12|│ │
13|│ │
14|└ Visit paths (cwd) ─────┘
15|

--|---------|---------|---------|---------|
01|0000011111211111111111111111111111111111
02|0000011111311111111111111111111111111111
03|1141113333566651512551111111111111111111
04|1141113333566651512551111111111111111111
05|7889777777777777777777777711111111111111
06|7::;;;;;;;;;;;;;;;;;;;;;;7<<<<<<<<<<<<<<
07|7========================7<<<<<<<<<<<<<<
08|7========================7<<<<<<<<<<<<<<
09|7========================7<<<<<<<<<<<<<<
10|7========================7<<<<<<<<<<<<<<
11|7========================7<<<<<<<<<<<<<<
12|7========================7<<<<<<<<<<<<<<
13|7========================7<<<<<<<<<<<<<<
14|7>>>>>>>>>>>>>>>>>>>777777<<<<<<<<<<<<<<
15|1111111111111111111111111111111111111111
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--|---------|---------|---------|---------|
01|┌…s/mini.nvim/tests/dir-extra/real-file┐
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails since it clips the final s in what seems to be your local directory where 'mini.nvim' is located. The GitHub runner (and possibly other local runs) will probably have different letter here.

This is the drawback of using screenshot testing. The "mock window title" approach could have also been more robust here, but if there is a reasonably concise way to not do a screenshot testing, it should usually be taken.

02|│ a.lua │
03|│ b.txt │
04|│ c.gif │
05|│ LICENSE │
06|│ Makefile │
07|└──────────────────────────────────────┘
08|~
09|~
10|~
11|~
12|~
13|~
14|~
15|

--|---------|---------|---------|---------|
01|0111111111111111111111111111111111111110
02|0222222222222222222222222222222222222220
03|0333333333333333333333333333333333333330
04|0333333333333333333333333333333333333330
05|0333333333333333333333333333333333333330
06|0333333333333333333333333333333333333330
07|0000000000000000000000000000000000000000
08|4444444444444444444444444444444444444444
09|4444444444444444444444444444444444444444
10|4444444444444444444444444444444444444444
11|4444444444444444444444444444444444444444
12|4444444444444444444444444444444444444444
13|4444444444444444444444444444444444444444
14|4444444444444444444444444444444444444444
15|5555555555555555555555555555555555555555
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--|---------|---------|---------|---------|---------|---------|---------|---------|
01|┌MOCK_ROOT/tests/dir-files/common ─────────────────┐
02|│ .a-dir │
03|│ a-dir │
04|│ b-dir │
05|│ .a-file │
06|│ a-file │
07|│ A-file-2 │
08|│ b-file │
09|└──────────────────────────────────────────────────┘
10|~
11|~
12|~
13|~
14|~
15| 1,1 All

--|---------|---------|---------|---------|---------|---------|---------|---------|
01|01111111111111111111111111111111110000000000000000002222222222222222222222222222
02|03333333344444444444444444444444444444444444444444405555555555555555555555555555
03|06666666777777777777777777777777777777777777777777705555555555555555555555555555
04|06666666777777777777777777777777777777777777777777705555555555555555555555555555
05|07777777777777777777777777777777777777777777777777705555555555555555555555555555
06|07777777777777777777777777777777777777777777777777705555555555555555555555555555
07|07777777777777777777777777777777777777777777777777705555555555555555555555555555
08|07777777777777777777777777777777777777777777777777705555555555555555555555555555
09|00000000000000000000000000000000000000000000000000005555555555555555555555555555
10|55555555555555555555555555555555555555555555555555555555555555555555555555555555
11|55555555555555555555555555555555555555555555555555555555555555555555555555555555
12|55555555555555555555555555555555555555555555555555555555555555555555555555555555
13|55555555555555555555555555555555555555555555555555555555555555555555555555555555
14|55555555555555555555555555555555555555555555555555555555555555555555555555555555
15|88888888888888888888888888888888888888888888888888888888888888888888888888888888
30 changes: 29 additions & 1 deletion tests/test_extra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ local get_extra_picker_extmarks = function(from, to)
end

-- Common mocks
local mock_win_functions = function() child.cmd('source tests/dir-extra/mocks/mock-win-functions.lua') end

local mock_fn_executable = function(available_executables)
local lua_cmd = string.format(
'vim.fn.executable = function(x) return vim.tbl_contains(%s, x) and 1 or 0 end',
Expand Down Expand Up @@ -3780,7 +3782,13 @@ local setup_visits = function()
child.fn.chdir(dir)
end

T['pickers']['visit_paths()'] = new_set({ hooks = { pre_case = setup_visits } })
T['pickers']['visit_paths()'] =
new_set({ hooks = {
pre_case = function()
mock_win_functions()
setup_visits()
end,
} })

local pick_visit_paths = forward_lua_notify('MiniExtra.pickers.visit_paths')

Expand Down Expand Up @@ -3880,6 +3888,26 @@ T['pickers']['visit_paths()']["checks for present 'mini.visits'"] = function()
expect.error(function() child.lua('MiniExtra.pickers.visit_paths()') end, '`pickers%.visit_paths`.*mini%.visits')
end

T['pickers']['visit_paths()']["can open visited directory in 'mini.files'"] = function()
child.lua([[require('mini.files').setup()]])

-- NOTE: 'mini.visits' uses forward slashes in index
local dir = test_dir_absolute:gsub('\\', '/')
local visit_index = {
[dir] = {
[dir .. '/real-files'] = { count = 10, latest = 10 },
},
}
child.lua([[require('mini.visits').set_index(...)]], { visit_index })
child.fn.chdir(dir)

child.cmd('edit real-files/a.lua')
pick_visit_paths()
child.expect_screenshot()
type_keys('<CR>')
child.expect_screenshot()
end

T['pickers']['visit_labels()'] = new_set({ hooks = { pre_case = setup_visits } })

local pick_visit_labels = forward_lua_notify('MiniExtra.pickers.visit_labels')
Expand Down
34 changes: 34 additions & 0 deletions tests/test_files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5740,6 +5740,40 @@ T['Default explorer']['works in `:edit .`'] = function()
child.expect_screenshot()
end

T['Default explorer']['deletes directory buffer if already visited(no alternate)'] = function()
-- Starting with empty buffer having id 1
child.cmd('edit ' .. test_dir_path)
close()

local bufs = child.api.nvim_list_bufs()
eq(#bufs, 1)

-- Assert that buffer with id 1 has been deleted, otherwise it would be named to directory test_dir_path
local is_still_one = bufs[1] == 1
eq(is_still_one, false)
Comment on lines +5744 to +5753
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Using explicit buffer ids is usually a bad idea. It can fail if there is some change in how initial state of the child process is done.
  • If you want to check that the name is not for the directory, then should do so more directly.
  • There is no need to check if the initial buffer was deleted, since this is not the goal (more like a side effect).
Suggested change
-- Starting with empty buffer having id 1
child.cmd('edit ' .. test_dir_path)
close()
local bufs = child.api.nvim_list_bufs()
eq(#bufs, 1)
-- Assert that buffer with id 1 has been deleted, otherwise it would be named to directory test_dir_path
local is_still_one = bufs[1] == 1
eq(is_still_one, false)
local buf_name_init = child.api.nvim_buf_get_name(0)
child.cmd('edit ' .. test_dir_path)
close()
-- Should not leave buffer named after opened directory
eq(child.api.nvim_buf_get_name(0), buf_name_init)
eq(#child.api.nvim_list_bufs(), 1)

end

T['Default explorer']['deletes directory buffer if already visited'] = function()
-- Starting with listed buffer having id 1
child.cmd('edit ' .. test_file_path)
child.cmd('edit ' .. test_dir_path)
close()

local bufs = child.api.nvim_list_bufs()
eq(#bufs, 1)
eq(bufs[1], 1)
Comment on lines +5757 to +5764
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar notes about using explicit buffer ids. Although I think this test might be redundant as there is already the other "handles close without opening file".

Suggested change
-- Starting with listed buffer having id 1
child.cmd('edit ' .. test_file_path)
child.cmd('edit ' .. test_dir_path)
close()
local bufs = child.api.nvim_list_bufs()
eq(#bufs, 1)
eq(bufs[1], 1)
local buf_id_init = child.api.nvim_get_current_buf()
child.cmd('edit ' .. test_file_path)
child.cmd('edit ' .. test_dir_path)
close()
eq(child.api.nvim_list_bufs(), { buf_id_init })

end

T['Default explorer']['does not crash on external `vim.api.nvim_set_current_win`'] = function()
child.cmd('edit ' .. test_file_path)
child.cmd('edit ' .. test_dir_path)
child.lua([[
local win_id = vim.api.nvim_list_wins()[1] -- win id 1000
vim.api.nvim_set_current_win(win_id)
]])
child.expect_screenshot()
Comment on lines +5768 to +5774
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • There rarely should be need to use child.lua('vim.api.nvim_xxx()') approach. Usually if it involves data structures that can not be transmitted between test runner and child instances. Usually it involves functions and userdata types (like libuv timers, tree-sitter nodes, etc.).

    Whenever possible, use child.api.nvim_xxx() directly.

Suggested change
child.cmd('edit ' .. test_file_path)
child.cmd('edit ' .. test_dir_path)
child.lua([[
local win_id = vim.api.nvim_list_wins()[1] -- win id 1000
vim.api.nvim_set_current_win(win_id)
]])
child.expect_screenshot()
child.cmd('edit ' .. test_file_path)
child.cmd('edit ' .. test_dir_path)
local win_id = child.api.nvim_list_wins()[1]
child.api.nvim_set_current_win(win_id)
child.expect_screenshot()

end

T['Default explorer']['works in `:vsplit .`'] = function()
child.cmd('vsplit ' .. test_dir_path)
child.expect_screenshot()
Expand Down
Loading