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