robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f0a0182285
master
1-- Other ShaDa tests 2local t =require ( 'test.testutil' ) 3local n =require ( 'test.functional.testnvim' )() 4local t_shada =require ( 'test.functional.shada.testutil' ) 5local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 6local uv =vim . uv 7local paths =t . paths 8local pcall_err =t . pcall_err 9 10local api , nvim_command , fn , eq =n . api , n . command , n . fn , t . eq 11local write_file , set_session =t . write_file , n . set_session 12local is_os =t . is_os 13local skip =t . skip 14local feed =n . feed 15 16local reset , clear , get_shada_rw =t_shada . reset , t_shada . clear , t_shada . get_shada_rw 17local read_shada_file =t_shada . read_shada_file 18 19local wshada , _ , shada_fname , clean =get_shada_rw ( 'Xtest-functional-shada-shada.shada' ) 20 21local dirname ='Xtest-functional-shada-shada.d' 22local dirshada =dirname .. '/main.shada' 23 24describe ( 'ShaDa support code' , function () 25before_each ( reset ) 26after_each ( function () 27clear () 28clean () 29uv . fs_rmdir ( dirname ) 30end ) 31 32it ( 'preserves `s` item size limit with unknown entries' , function () 33wshada ( 34'\100\000\207\000\000\000\000\000\000\004\000\218\003\253' 35.. ( '-' ): rep ( 1024 - 3 ) 36.. '\100\000\207\000\000\000\000\000\000\004\001\218\003\254' 37.. ( '-' ): rep ( 1025 - 3 ) 38) 39n . command ( 'wshada ' .. shada_fname ) 40local found =0 41for _ , v in ipairs ( read_shada_file ( shada_fname )) do 42if v . type == 100 then 43found =found + 1 44end 45end 46eq ( 2 , found ) 47n . command ( 'set shada-=s10 shada+=s1' ) 48n . command ( 'wshada ' .. shada_fname ) 49found =0 50for _ , v in ipairs ( read_shada_file ( shada_fname )) do 51if v . type == 100 then 52found =found + 1 53end 54end 55eq ( 1 , found ) 56end ) 57 58it ( 'preserves `s` item size limit with instance history entries' , function () 59local hist1 =( '-' ): rep ( 1024 - 5 ) 60local hist2 =( '-' ): rep ( 1025 - 5 ) 61nvim_command ( 'set shada-=s10 shada+=s1' ) 62fn . histadd ( ':' , hist1 ) 63fn . histadd ( ':' , hist2 ) 64n . command ( 'wshada ' .. shada_fname ) 65local found =0 66for _ , v in ipairs ( read_shada_file ( shada_fname )) do 67if v . type == 4 then 68found =found + 1 69eq ( hist1 , v . value [ 2 ]) 70end 71end 72eq ( 1 , found ) 73end ) 74 75it ( 'leaves .tmp.a in-place when there is error in original ShaDa' , function () 76wshada ( 'Some text file' ) 77eq ( 78'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier' , 79t . pcall_err ( n . command , 'wshada ' .. shada_fname ) 80) 81eq ( 1 , read_shada_file ( shada_fname .. '.tmp.a' )[ 1 ]. type ) 82end ) 83 84it ( 85'does not leave .tmp.a in-place when there is error in original ShaDa, but writing with bang' , 86function () 87wshada ( 'Some text file' ) 88n . command ( 'wshada! ' .. shada_fname ) 89eq ( 1 , read_shada_file ( shada_fname )[ 1 ]. type ) 90eq ( nil , uv . fs_stat ( shada_fname .. '.tmp.a' )) 91end 92) 93 94it ( 'leaves .tmp.b in-place when there is error in original ShaDa and it has .tmp.a' , function () 95wshada ( 'Some text file' ) 96eq ( 97'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier' , 98t . pcall_err ( n . command , 'wshada ' .. shada_fname ) 99) 100eq ( 101'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier' , 102t . pcall_err ( n . command , 'wshada ' .. shada_fname ) 103) 104eq ( 1 , read_shada_file ( shada_fname .. '.tmp.a' )[ 1 ]. type ) 105eq ( 1 , read_shada_file ( shada_fname .. '.tmp.b' )[ 1 ]. type ) 106end ) 107 108it ( 109'leaves .tmp.z in-place when there is error in original ShaDa and it has .tmp.a … .tmp.x' , 110function () 111wshada ( 'Some text file' ) 112local i =( 'a' ): byte () 113while i < ( 'z' ): byte () do 114write_file ( shada_fname .. ( '.tmp.%c' ): format ( i ), 'Some text file' , true ) 115i =i + 1 116end 117eq ( 118'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier' , 119t . pcall_err ( n . command , 'wshada ' .. shada_fname ) 120) 121eq ( 1 , read_shada_file ( shada_fname .. '.tmp.z' )[ 1 ]. type ) 122end 123) 124 125it ( 'errors out when there are .tmp.a … .tmp.z ShaDa files' , function () 126wshada ( '' ) 127local i =( 'a' ): byte () 128while i <= ( 'z' ): byte () do 129write_file ( shada_fname .. ( '.tmp.%c' ): format ( i ), '' , true ) 130i =i + 1 131end 132eq ( 133'Vim(wshada):E138: All Xtest-functional-shada-shada.shada.tmp.X files exist, cannot write ShaDa file!' , 134t . pcall_err ( n . command , 'wshada ' .. shada_fname ) 135) 136end ) 137 138it ( 'reads correctly various timestamps' , function () 139local msgpack ={ 140'\100' , -- Positive fixnum 100 141'\204\255' , -- uint 8 255 142'\205\010\003' , -- uint 16 2563 143'\206\255\010\030\004' , -- uint 32 4278853124 144'\207\005\100\060\250\255\010\030\004' , -- uint 64 388502516579048964 145} 146local s ='\100' 147local e ='\001\192' 148wshada ( s .. table . concat ( msgpack , e .. s ) .. e ) 149n . command ( 'wshada ' .. shada_fname ) 150local found =0 151local typ =vim . mpack . decode ( s ) 152for _ , v in ipairs ( read_shada_file ( shada_fname )) do 153if v . type == typ then 154found =found + 1 155eq ( vim . mpack . decode ( msgpack [ found ]), v . timestamp ) 156end 157end 158eq ( # msgpack , found ) 159end ) 160 161local marklike ={ [ 7 ] =true , [ 8 ] =true , [ 10 ] =true , [ 11 ] =true } 162local find_file =function ( fname ) 163local found ={} 164for _ , v in ipairs ( read_shada_file ( shada_fname )) do 165if marklike [ v . type ] and v . value . f == fname then 166found [ v . type ] =( found [ v . type ] or 0 ) + 1 167elseif v . type == 9 then 168for _ , b in ipairs ( v . value ) do 169if b . f == fname then 170found [ v . type ] =( found [ v . type ] or 0 ) + 1 171end 172end 173end 174end 175return found 176end 177 178it ( 'correctly uses shada-r option' , function () 179nvim_command ( 'set shellslash' ) 180api . nvim_set_var ( '__home' , paths . test_source_path ) 181nvim_command ( 'let $HOME = __home' ) 182nvim_command ( 'unlet __home' ) 183nvim_command ( 'edit ~/README.md' ) 184nvim_command ( 'normal! GmAggmaAabc' ) 185nvim_command ( 'undo' ) 186nvim_command ( 'set shada+=%' ) 187nvim_command ( 'wshada! ' .. shada_fname ) 188local readme_fname =fn . resolve ( paths . test_source_path ) .. '/README.md' 189eq ( { [ 7 ] =2 , [ 8 ] =2 , [ 9 ] =1 , [ 10 ] =4 , [ 11 ] =1 } , find_file ( readme_fname )) 190nvim_command ( 'set shada+=r~' ) 191nvim_command ( 'wshada! ' .. shada_fname ) 192eq ( {} , find_file ( readme_fname )) 193nvim_command ( 'set shada-=r~' ) 194nvim_command ( 'wshada! ' .. shada_fname ) 195eq ( { [ 7 ] =2 , [ 8 ] =2 , [ 9 ] =1 , [ 10 ] =4 , [ 11 ] =1 } , find_file ( readme_fname )) 196nvim_command ( 'set shada+=r' .. fn . escape ( fn . escape ( paths . test_source_path , '$~' ), ' "\\,' )) 197nvim_command ( 'wshada! ' .. shada_fname ) 198eq ( {} , find_file ( readme_fname )) 199end ) 200 201it ( 'correctly ignores case with shada-r option' , function () 202nvim_command ( 'set shellslash' ) 203local pwd =fn . getcwd () 204local relfname ='абв/test' 205local fname =pwd .. '/' .. relfname 206api . nvim_set_var ( '__fname' , fname ) 207nvim_command ( 'silent! edit `=__fname`' ) 208fn . setline ( 1 , { 'a' , 'b' , 'c' , 'd' } ) 209nvim_command ( 'normal! GmAggmaAabc' ) 210nvim_command ( 'undo' ) 211nvim_command ( 'set shada+=%' ) 212nvim_command ( 'wshada! ' .. shada_fname ) 213eq ( { [ 7 ] =2 , [ 8 ] =2 , [ 9 ] =1 , [ 10 ] =4 , [ 11 ] =2 } , find_file ( fname )) 214nvim_command ( 'set shada+=r' .. pwd .. '/АБВ' ) 215nvim_command ( 'wshada! ' .. shada_fname ) 216eq ( {} , find_file ( fname )) 217end ) 218 219it ( "does not store 'nobuflisted' buffer" , function () 220nvim_command ( 'set shellslash' ) 221local fname =fn . getcwd () .. '/file' 222api . nvim_set_var ( '__fname' , fname ) 223nvim_command ( 'edit `=__fname`' ) 224api . nvim_set_option_value ( 'buflisted' , false , {} ) 225nvim_command ( 'wshada! ' .. shada_fname ) 226eq ( {} , find_file ( fname )) 227-- Set 'buflisted', then check again. 228api . nvim_set_option_value ( 'buflisted' , true , {} ) 229nvim_command ( 'wshada! ' .. shada_fname ) 230eq ( { [ 7 ] =1 , [ 8 ] =1 , [ 10 ] =1 } , find_file ( fname )) 231end ) 232 233it ( 'is able to set &shada after &viminfo' , function () 234api . nvim_set_option_value ( 'viminfo' , "'10" , {} ) 235eq ( "'10" , api . nvim_get_option_value ( 'viminfo' , {} )) 236eq ( "'10" , api . nvim_get_option_value ( 'shada' , {} )) 237api . nvim_set_option_value ( 'shada' , '' , {} ) 238eq ( '' , api . nvim_get_option_value ( 'viminfo' , {} )) 239eq ( '' , api . nvim_get_option_value ( 'shada' , {} )) 240end ) 241 242it ( 'is able to set all& after setting &shada' , function () 243api . nvim_set_option_value ( 'shada' , "'10" , {} ) 244eq ( "'10" , api . nvim_get_option_value ( 'viminfo' , {} )) 245eq ( "'10" , api . nvim_get_option_value ( 'shada' , {} )) 246nvim_command ( 'set all&' ) 247eq ( "!,'100,<50,s10,h,r/tmp/,r/private/" , api . nvim_get_option_value ( 'viminfo' , {} )) 248eq ( "!,'100,<50,s10,h,r/tmp/,r/private/" , api . nvim_get_option_value ( 'shada' , {} )) 249end ) 250 251it ( 'is able to set &shada after &viminfo using :set' , function () 252nvim_command ( "set viminfo='10" ) 253eq ( "'10" , api . nvim_get_option_value ( 'viminfo' , {} )) 254eq ( "'10" , api . nvim_get_option_value ( 'shada' , {} )) 255nvim_command ( 'set shada=' ) 256eq ( '' , api . nvim_get_option_value ( 'viminfo' , {} )) 257eq ( '' , api . nvim_get_option_value ( 'shada' , {} )) 258end ) 259 260it ( 'setting &shada gives proper error message on missing number' , function () 261eq ( 262[[Vim(set):E526: Missing number after <">: shada="]] , 263t . pcall_err ( n . command , [[set shada=\"]] ) 264) 265for _ , c in ipairs ( { "'" , '/' , ':' , '<' , '@' , 's' } ) do 266eq ( 267( [[Vim(set):E526: Missing number after <%s>: shada=%s]] ): format ( c , c ), 268t . pcall_err ( n . command , ( [[set shada=%s]] ): format ( c )) 269) 270end 271end ) 272 273it ( '":wshada/:rshada [filename]" works when shadafile=NONE' , function () 274nvim_command ( 'set shadafile=NONE' ) 275nvim_command ( 'wshada ' .. shada_fname ) 276eq ( 1 , read_shada_file ( shada_fname )[ 1 ]. type ) 277 278wshada ( 'Some text file' ) 279eq ( 280'Vim(rshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier' , 281t . pcall_err ( n . command , 'rshada ' .. shada_fname ) 282) 283end ) 284 285it ( ':wshada/:rshada without arguments is no-op when shadafile=NONE' , function () 286nvim_command ( 'set shadafile=NONE' ) 287nvim_command ( 'wshada' ) 288nvim_command ( 'rshada' ) 289end ) 290 291it ( 'does not crash when ShaDa file directory is not writable' , function () 292skip ( is_os ( 'win' )) 293 294fn . mkdir ( dirname , '' , '0' ) 295eq ( 0 , fn . filewritable ( dirname )) 296reset { shadafile =dirshada , args ={ '--cmd' , 'set shada=' } } 297api . nvim_set_option_value ( 'shada' , "'10" , {} ) 298eq ( 299'Vim(wshada):E886: System error while opening ShaDa file ' 300.. 'Xtest-functional-shada-shada.d/main.shada for reading to merge ' 301.. 'before writing it: permission denied' , 302t . pcall_err ( n . command , 'wshada' ) 303) 304api . nvim_set_option_value ( 'shada' , '' , {} ) 305end ) 306 307it ( 'deduplicates items on case-insensitive systems' , function () 308local file =( '%s/nonexistent/héllo' ): format ( dirname ) 309nvim_command (( 'edit %s' ): format ( file )) 310file =api . nvim_buf_get_name ( 0 ) 311 312local file_upper =fn . toupper ( file ) 313if t . is_os ( 'win' ) then 314file_upper =file_upper : sub ( 3 ) -- drop drive letter 315end 316 317nvim_command (( 'edit %s' ): format ( file_upper )) 318feed ( 'i1<Esc>' ) 319nvim_command (( 'wshada! %s' ): format ( dirshada )) 320nvim_command ( 'bw!' ) 321 322-- path_cmp() ignores a single trailing slash 323local file_slash =( '%s/' ): format ( file ) 324nvim_command (( 'edit %s' ): format ( file_slash )) 325feed ( 'i123<Esc>' ) 326nvim_command ( 'mark a' ) 327nvim_command (( 'wshada %s' ): format ( dirshada )) 328nvim_command ( 'bw!' ) 329nvim_command (( 'rshada! %s' ): format ( dirshada )) 330local oldfiles =api . nvim_get_vvar ( 'oldfiles' ) 331-- Both filenames appear in shada file, but iteration order is unspecified. 332if t . is_os ( 'win' ) or t . is_os ( 'mac' ) then 333eq ( 1 , # oldfiles ) 334t . ok ( oldfiles [ 1 ] == file or oldfiles [ 1 ] == file_slash ) 335else 336eq ( 2 , # oldfiles ) 337t . ok ( oldfiles [ 1 ] == file or oldfiles [ 1 ] == file_upper ) 338end 339end ) 340end ) 341 342describe ( 'ShaDa support code' , function () 343it ( 'does not write NONE file' , function () 344local session =n . new_session ( false , { 345merge =false , 346args ={ '-u' , 'NONE' , '-i' , 'NONE' , '--embed' , '--headless' , '--cmd' , 'qall' } , 347} ) 348session : close () 349eq ( nil , uv . fs_stat ( 'NONE' )) 350eq ( nil , uv . fs_stat ( 'NONE.tmp.a' )) 351end ) 352 353it ( 'does not read NONE file' , function () 354write_file ( 'NONE' , '\005\001\015\131\161na\162rX\194\162rc\145\196\001-' ) 355local session =n . new_session ( 356false , 357{ merge =false , args ={ '-u' , 'NONE' , '-i' , 'NONE' , '--embed' , '--headless' } } 358) 359set_session ( session ) 360eq ( '' , fn . getreg ( 'a' )) 361session : close () 362os . remove ( 'NONE' ) 363end ) 364end )