8000 mini.nvim/tests/test_git.lua at main · dmun/mini.nvim · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
3153 lines (2623 loc) · 104 KB

test_git.lua

File metadata and controls

3153 lines (2623 loc) · 104 KB
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('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 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
--stylua: ignore end
-- TODO: Remove after compatibility with Neovim=0.9 is dropped
local islist = vim.fn.has('nvim-0.10') == 1 and vim.islist or vim.tbl_islist
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'
local forward_lua = function(fun_str)
local lua_cmd = fun_str .. '(...)'
return function(...) return child.lua_get(lua_cmd, { ... }) end
end
-- 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 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(load_module, vim.pesc(name) .. '.*' .. vim.pesc(target_type), config)
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()
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
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.',
'',
'diff --git a/file b/file',
'new file mode 100644',
'index 0000000..f9264f7',
'--- /dev/null',
'+++ b/file',
'@@ -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) Could not find "before" file', '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:file')
validate_notifications({ { '(mini.git) Could not find "before" file', '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
vali 5586 date('both', 16, 'row', name_after, { 1, 0 })
validate('both', 17, 'row', name_after, { 2, 0 })
validate('both', 12, 'row', name_after, { 1, 0 })
validate('both', 13, 'row', name_after, { 1, 0 })
end
T['show_diff_source()']['uses correct working directory'] = function()
local root, repo = test_dir_absolute, git_repo_dir
local rev_parse_track = repo .. '\n' .. root
child.lua('_G.rev_parse_track = ' .. vim.inspect(rev_parse_track))
child.lua([[_G.stdio_queue = {
{ { 'out', _G.rev_parse_track } }, -- Get path to root and repo
{ { 'out', 'abc1234\nmain' } }, -- Get HEAD data
{ { 'out', 'A log-output' } }, -- Get file status data
{ { 'out', 'Line 1\nCurrent line 2\nLine 3' } } -- Show diff source
}
]])
edit(test_dir_absolute .. '/log-output')
child.fn.chdir(git_dir_path)
set_cursor(17, 0)
show_diff_source()
--stylua: ignore
local ref_git_spawn_log = {
{
args = { '-c', 'gc.auto=0', 'rev-parse', '--path-format=absolute', '--git-dir', '--show-toplevel' },
cwd = test_dir_absolute,
},
{
args = { '-c', 'gc.auto=0', 'rev-parse', 'HEAD', '--abbrev-ref', 'HEAD' },
cwd = root,
},
{
args = {
'-c', 'gc.auto=0', 'status', '--verbose', '--untracked-files=all', '--ignored', '--porcelain', '-z',
0