difftool_spec.lua (4723B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local clear = n.clear 5 local command = n.command 6 local eq = t.eq 7 local fn = n.fn 8 9 local pathsep = n.get_pathsep() 10 local testdir_left = 'Xtest-difftool-left' 11 local testdir_right = 'Xtest-difftool-right' 12 13 setup(function() 14 n.mkdir_p(testdir_left) 15 n.mkdir_p(testdir_right) 16 t.write_file(testdir_left .. pathsep .. 'file1.txt', 'hello') 17 t.write_file(testdir_left .. pathsep .. 'file2.txt', 'foo') 18 t.write_file(testdir_right .. pathsep .. 'file1.txt', 'hello world') -- modified 19 t.write_file(testdir_right .. pathsep .. 'file3.txt', 'bar') -- added 20 end) 21 22 teardown(function() 23 n.rmdir(testdir_left) 24 n.rmdir(testdir_right) 25 end) 26 27 describe('nvim.difftool', function() 28 before_each(function() 29 clear() 30 command('packadd nvim.difftool') 31 end) 32 33 it('shows added, modified, and deleted files in quickfix', function() 34 command(('DiffTool %s %s'):format(testdir_left, testdir_right)) 35 local qflist = fn.getqflist() 36 local entries = {} 37 for _, item in ipairs(qflist) do 38 table.insert(entries, { text = item.text, rel = item.user_data and item.user_data.rel }) 39 end 40 41 -- Should show: 42 -- file1.txt as modified (M) 43 -- file2.txt as deleted (D) 44 -- file3.txt as added (A) 45 eq({ 46 { text = 'M', rel = 'file1.txt' }, 47 { text = 'D', rel = 'file2.txt' }, 48 { text = 'A', rel = 'file3.txt' }, 49 }, entries) 50 end) 51 52 it('has consistent split layout', function() 53 command('set nosplitright') 54 command(('DiffTool %s %s'):format(testdir_left, testdir_right)) 55 local wins = fn.getwininfo() 56 local left_win_col = wins[1].wincol 57 local right_win_col = wins[2].wincol 58 assert( 59 left_win_col < right_win_col, 60 'Left window should be to the left of right window even with nosplitright set' 61 ) 62 end) 63 64 it('handles symlinks', function() 65 -- Create a symlink in right dir pointing to file2.txt in left dir 66 local symlink_path = vim.fs.joinpath(testdir_right, 'file2.txt') 67 local target_path = vim.fs.joinpath('..', testdir_left, 'file2.txt') 68 assert(vim.uv.fs_symlink(target_path, symlink_path) == true) 69 finally(function() 70 os.remove(symlink_path) 71 end) 72 assert(fn.getftype(symlink_path) == 'link') 73 74 -- Run difftool 75 command(('DiffTool %s %s'):format(testdir_left, testdir_right)) 76 local qflist = fn.getqflist() 77 local entries = {} 78 for _, item in ipairs(qflist) do 79 table.insert(entries, { text = item.text, rel = item.user_data and item.user_data.rel }) 80 end 81 82 -- file2.txt should not be reported as added or deleted anymore 83 eq({ 84 { text = 'M', rel = 'file1.txt' }, 85 { text = 'A', rel = 'file3.txt' }, 86 }, entries) 87 end) 88 89 it('has autocmds when diff window is opened', function() 90 command(('DiffTool %s %s'):format(testdir_left, testdir_right)) 91 local autocmds = fn.nvim_get_autocmds({ group = 'nvim.difftool.events' }) 92 assert(#autocmds > 0) 93 end) 94 95 it('cleans up autocmds when diff window is closed', function() 96 command(('DiffTool %s %s'):format(testdir_left, testdir_right)) 97 command('q') 98 local ok = pcall(fn.nvim_get_autocmds, { group = 'nvim.difftool.events' }) 99 eq(false, ok) 100 end) 101 102 it('does not reset quickfix list when closing quickfix window', function() 103 command(('DiffTool %s %s'):format(testdir_left, testdir_right)) 104 local qflist_before = fn.getqflist() 105 assert(#qflist_before > 0, 'quickfix list should not be empty') 106 107 -- Close the quickfix window 108 command('cclose') 109 110 -- Quickfix list should still be intact 111 local qflist_after = fn.getqflist() 112 eq(#qflist_before, #qflist_after) 113 114 -- Autocmds should still be active 115 local autocmds = fn.nvim_get_autocmds({ group = 'nvim.difftool.events' }) 116 assert(#autocmds > 0, 'autocmds should still exist after closing quickfix window') 117 end) 118 119 it('opens difftool automatically when started with nvim -d', function() 120 -- Start Neovim with -d flag for directory diff 121 clear({ 122 args = { 123 '--cmd', 124 'packadd nvim.difftool', 125 '-d', 126 testdir_left, 127 testdir_right, 128 }, 129 }) 130 131 -- Wait for difftool to open 132 n.poke_eventloop() 133 134 -- Verify we have 3 windows (left, right, and quickfix) 135 eq(3, #fn.getwininfo()) 136 137 -- Verify quickfix list has the expected entries 138 local qflist = fn.getqflist() 139 local entries = {} 140 for _, item in ipairs(qflist) do 141 table.insert(entries, { text = item.text, rel = item.user_data and item.user_data.rel }) 142 end 143 eq({ 144 { text = 'M', rel = 'file1.txt' }, 145 { text = 'D', rel = 'file2.txt' }, 146 { text = 'A', rel = 'file3.txt' }, 147 }, entries) 148 end) 149 end)