robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ce9a9b4700
master
1" Tests for the swap feature 2 3source check.vim 4source shared.vim 5source term_util.vim 6 7func s: swapname () 8return trim ( execute ( 'swapname' )) 9endfunc 10 11" Tests for 'directory' option. 12func Test_swap_directory () 13CheckUnix 14 15let content = [ 'start of testfile' , 16 \'line 2 Abcdefghij' , 17 \'line 3 Abcdefghij' , 18 \'end of testfile' ] 19call writefile ( content , 'Xtest1' , 'D' ) 20 21" '.', swap file in the same directory as file 22set dir = .,~ 23 24" Verify that the swap file doesn't exist in the current directory 25call assert_equal ([], glob ( ".Xtest1*.swp" , 1 , 1 , 1 )) 26edit Xtest1 27let swfname = s: swapname () 28call assert_equal ([ swfname ], glob ( swfname , 1 , 1 , 1 )) 29 30" './dir', swap file in a directory relative to the file 31set dir = ./Xtest2,.,~ 32 33call mkdir ( "Xtest2" , 'R' ) 34edit Xtest1 35call assert_equal ([], glob ( swfname , 1 , 1 , 1 )) 36let swfname = "Xtest2/Xtest1.swp" 37call assert_equal ( swfname , s: swapname ()) 38call assert_equal ([ swfname ], glob ( "Xtest2/*" , 1 , 1 , 1 )) 39 40" 'dir', swap file in directory relative to the current dir 41set dir = Xtest.je,~ 42 43call mkdir ( "Xtest.je" , 'R' ) 44call writefile ( content , 'Xtest2/Xtest3' ) 45edit Xtest2/Xtest3 46call assert_equal ([ "Xtest2/Xtest3" ], glob ( "Xtest2/*" , 1 , 1 , 1 )) 47let swfname = "Xtest.je/Xtest3.swp" 48call assert_equal ( swfname , s: swapname ()) 49call assert_equal ([ swfname ], glob ( "Xtest.je/*" , 1 , 1 , 1 )) 50 51set dir & 52endfunc 53 54func Test_swap_group () 55CheckUnix 56 57let groups = split ( system ( 'groups' )) 58if len ( groups ) <= 1 59throw 'Skipped: need at least two groups, got ' . string ( groups ) 60endif 61 62try 63call delete ( 'Xtest' ) 64split Xtest 65call setline ( 1 , 'just some text' ) 66wq 67if system ( 'ls -l Xtest' ) !~ ' ' . groups [ 0 ] . ' \d' 68throw 'Skipped: test file does not have the first group' 69else 70silent !chmod640 Xtest 71call system ( 'chgrp ' . groups [ 1 ] . ' Xtest' ) 72if system ( 'ls -l Xtest' ) !~ ' ' . groups [ 1 ] . ' \d' 73throw 'Skipped: cannot set second group on test file' 74else 75split Xtest 76let swapname = substitute ( execute ( 'swapname' ), '[[:space:]]' , '' , 'g' ) 77call assert_match ( 'Xtest' , swapname ) 78" Group of swapfile must now match original file. 79call assert_match ( ' ' . groups [ 1 ] . ' \d' , system ( 'ls -l ' . swapname )) 80 81bwipe ! 82endif 83endif 84finally 85call delete ( 'Xtest' ) 86endtry 87endfunc 88 89func Test_missing_dir () 90call mkdir ( 'Xswapdir' ) 91exe 'set directory=' . getcwd () . '/Xswapdir' 92 93call assert_equal ( '' , glob ( 'foo' )) 94call assert_equal ( '' , glob ( 'bar' )) 95edit foo/x.txt 96" This should not give a warning for an existing swap file. 97split bar/x.txt 98only 99 100" Delete the buffer so that swap file is removed before we try to delete the 101" directory. That fails on MS-Windows. 102 %bdelete ! 103set directory & 104call delete ( 'Xswapdir' , 'rf' ) 105endfunc 106 107func Test_swapinfo () 108new Xswapinfo 109call setline ( 1 , [ 'one' , 'two' , 'three' ]) 110w 111let fname = s: swapname () 112call assert_match ( 'Xswapinfo' , fname ) 113 114" Check the tail appears in the list from swapfilelist(). The path depends 115" on the system. 116let tail = fnamemodify ( fname , ":t" ) ->fnameescape () 117let nr = 0 118for name in swapfilelist () 119if name =~ tail .. '$' 120let nr += 1 121endif 122endfor 123call assert_equal ( 1 , nr , 'not found in ' .. string ( swapfilelist ())) 124 125let info = fname ->swapinfo () 126let ver = printf ( 'VIM %d.%d' , v: version / 100 , v: version % 100 ) 127call assert_equal ( ver , info . version ) 128 129call assert_match ( '\w' , info . user ) 130" host name is truncated to 39 bytes in the swap file 131call assert_equal ( hostname ()[: 38 ], info . host ) 132call assert_match ( 'Xswapinfo' , info . fname ) 133call assert_match ( 0 , info . dirty ) 134call assert_equal ( getpid (), info . pid ) 135call assert_match ( '^\d*$' , info . mtime ) 136if has_key ( info , 'inode' ) 137call assert_match ( '\d' , info . inode ) 138endif 139bwipe ! 140call delete ( fname ) 141call delete ( 'Xswapinfo' ) 142 143let info = swapinfo ( 'doesnotexist' ) 144call assert_equal ( 'Cannot open file' , info . error ) 145 146call writefile ([ 'burp' ], 'Xnotaswapfile' , 'D' ) 147let info = swapinfo ( 'Xnotaswapfile' ) 148call assert_equal ( 'Cannot read file' , info . error ) 149call delete ( 'Xnotaswapfile' ) 150 151call writefile ([ repeat ( 'x' , 10000 )], 'Xnotaswapfile' ) 152let info = swapinfo ( 'Xnotaswapfile' ) 153call assert_equal ( 'Not a swap file' , info . error ) 154endfunc 155 156func Test_swapname () 157edit Xtest1 158let expected = s: swapname () 159call assert_equal ( expected , swapname ( '%' )) 160 161new Xtest2 162let buf = bufnr ( '%' ) 163let expected = s: swapname () 164wincmd p 165call assert_equal ( expected , buf ->swapname ()) 166 167new Xtest3 168setlocal noswapfile 169call assert_equal ( '' , swapname ( '%' )) 170 171bwipe ! 172call delete ( 'Xtest1' ) 173call delete ( 'Xtest2' ) 174call delete ( 'Xtest3' ) 175endfunc 176 177func Test_swapfile_delete () 178autocmd ! SwapExists 179function s: swap_exists () 180let v: swapchoice = s: swap_choice 181let s: swapname = v: swapname 182let s: filename = expand ( '<afile>' ) 183endfunc 184augroup test_swapfile_delete 185autocmd ! 186autocmd SwapExists * call s: swap_exists () 187augroup END 188 189 190" Create a valid swapfile by editing a file. 191split XswapfileText 192call setline ( 1 , [ 'one' , 'two' , 'three' ]) 193write " file is written, not modified 194" read the swapfile as a Blob 195let swapfile_name = swapname ( '%' ) 196let swapfile_bytes = readfile ( swapfile_name , 'B' ) 197 198" Close the file and recreate the swap file. 199" Now editing the file will run into the process still existing 200quit 201call writefile ( swapfile_bytes , swapfile_name , 'D' ) 202let s: swap_choice = 'e' 203let s: swapname = '' 204split XswapfileText 205quit 206call assert_equal ( fnamemodify ( swapfile_name , ':t' ), fnamemodify ( s: swapname , ':t' )) 207 208" This test won't work as root because root can successfully run kill(1, 0) 209if ! IsRoot () 210" Write the swapfile with a modified PID, now it will be automatically 211" deleted. Process 0x3fffffff most likely does not exist. 212let swapfile_bytes [ 24 : 27 ] = 0 zffffff3f 213call writefile ( swapfile_bytes , swapfile_name ) 214let s: swapname = '' 215split XswapfileText 216quit 217call assert_equal ( '' , s: swapname ) 218endif 219 220" Now set the modified flag, the swap file will not be deleted 221let swapfile_bytes [ 28 + 80 + 899 ] = 0x55 222call writefile ( swapfile_bytes , swapfile_name ) 223let s: swapname = '' 224split XswapfileText 225quit 226call assert_equal ( fnamemodify ( swapfile_name , ':t' ), fnamemodify ( s: swapname , ':t' )) 227 228call delete ( 'XswapfileText' ) 229augroup test_swapfile_delete 230autocmd ! 231augroup END 232augroup ! test_swapfile_delete 233endfunc 234 235func Test_swap_recover () 236autocmd ! SwapExists 237augroup test_swap_recover 238autocmd ! 239autocmd SwapExists * let v: swapchoice = 'r' 240augroup END 241 242call mkdir ( 'Xswap' , 'R' ) 243let $ Xswap = 'foo' " Check for issue #4369. 244set dir = Xswap// 245" Create a valid swapfile by editing a file. 246split Xswap/text 247call setline ( 1 , [ 'one' , 'two' , 'three' ]) 248write " file is written, not modified 249" read the swapfile as a Blob 250let swapfile_name = swapname ( '%' ) 251let swapfile_bytes = readfile ( swapfile_name , 'B' ) 252 253" Close the file and recreate the swap file. 254quit 255call writefile ( swapfile_bytes , swapfile_name , 'D' ) 256" Edit the file again. This triggers recovery. 257try 258split Xswap/text 259catch 260" E308 should be caught, not E305. 261call assert_exception ( 'E308:' ) " Original file may have been changed 262endtry 263" The file should be recovered. 264call assert_equal ([ 'one' , 'two' , 'three' ], getline ( 1 , 3 )) 265quit ! 266 267unlet $ Xswap 268set dir & 269augroup test_swap_recover 270autocmd ! 271augroup END 272augroup ! test_swap_recover 273endfunc 274 275func Test_swap_recover_ext () 276autocmd ! SwapExists 277augroup test_swap_recover_ext 278autocmd ! 279autocmd SwapExists * let v: swapchoice = 'r' 280augroup END 281 282" Create a valid swapfile by editing a file with a special extension. 283split Xtest.scr 284call setline ( 1 , [ 'one' , 'two' , 'three' ]) 285write " file is written, not modified 286write " write again to make sure the swapfile is created 287" read the swapfile as a Blob 288let swapfile_name = swapname ( '%' ) 289let swapfile_bytes = readfile ( swapfile_name , 'B' ) 290 291" Close and delete the file and recreate the swap file. 292quit 293call delete ( 'Xtest.scr' ) 294call writefile ( swapfile_bytes , swapfile_name , 'D' ) 295" Edit the file again. This triggers recovery. 296try 297split Xtest.scr 298catch 299" E308 should be caught, not E306. 300call assert_exception ( 'E308:' ) " Original file may have been changed 301endtry 302" The file should be recovered. 303call assert_equal ([ 'one' , 'two' , 'three' ], getline ( 1 , 3 )) 304quit ! 305 306call delete ( 'Xtest.scr' ) 307augroup test_swap_recover_ext 308autocmd ! 309augroup END 310augroup ! test_swap_recover_ext 311endfunc 312 313" Test for closing a split window automatically when a swap file is detected 314" and 'Q' is selected in the confirmation prompt. 315func Test_swap_split_win () 316autocmd ! SwapExists 317augroup test_swap_splitwin 318autocmd ! 319autocmd SwapExists * let v: swapchoice = 'q' 320augroup END 321 322" Create a valid swapfile by editing a file with a special extension. 323split Xtest.scr 324call setline ( 1 , [ 'one' , 'two' , 'three' ]) 325write " file is written, not modified 326write " write again to make sure the swapfile is created 327" read the swapfile as a Blob 328let swapfile_name = swapname ( '%' ) 329let swapfile_bytes = readfile ( swapfile_name , 'B' ) 330 331" Close and delete the file and recreate the swap file. 332quit 333call delete ( 'Xtest.scr' ) 334call writefile ( swapfile_bytes , swapfile_name , 'D' ) 335" Split edit the file again. This should fail to open the window 336try 337split Xtest.scr 338catch 339" E308 should be caught, not E306. 340call assert_exception ( 'E308:' ) " Original file may have been changed 341endtry 342call assert_equal ( 1 , winnr ( '$' )) 343 344call delete ( 'Xtest.scr' ) 345 346augroup test_swap_splitwin 347autocmd ! 348augroup END 349augroup ! test_swap_splitwin 350endfunc 351 352" Test for selecting 'q' in the attention prompt 353func Test_swap_prompt_splitwin () 354CheckRunVimInTerminal 355 356call writefile ([ 'foo bar' ], 'Xfile1' , 'D' ) 357edit Xfile1 358preserve " should help to make sure the swap file exists 359 360let buf = RunVimInTerminal ( '' , { 'rows' : 20 }) 361call term_sendkeys ( buf , ":set nomore\n" ) 362call term_sendkeys ( buf , ":set noruler\n" ) 363 364call term_sendkeys ( buf , ":split Xfile1\n" ) 365call TermWait ( buf ) 366call WaitForAssert ({ -> assert_match ( '^\[O\]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort: $' , term_getline ( buf , 20 ))}) 367call term_sendkeys ( buf , "q" ) 368call TermWait ( buf ) 369call term_sendkeys ( buf , ":\<CR>" ) 370call WaitForAssert ({ -> assert_match ( '^:$' , term_getline ( buf , 20 ))}) 371call term_sendkeys ( buf , ":echomsg winnr('$')\<CR>" ) 372call TermWait ( buf ) 373call WaitForAssert ({ -> assert_match ( '^1$' , term_getline ( buf , 20 ))}) 374call StopVimInTerminal ( buf ) 375 376" This caused Vim to crash when typing "q" at the swap file prompt. 377let buf = RunVimInTerminal ( '-c "au bufadd * let foo_w = wincol()"' , { 'rows' : 18 }) 378call term_sendkeys ( buf , ":e Xfile1\<CR>" ) 379call WaitForAssert ({ -> assert_match ( 'More' , term_getline ( buf , 18 ))}) 380call term_sendkeys ( buf , " " ) 381call WaitForAssert ({ -> assert_match ( '^\[O\]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:' , term_getline ( buf , 18 ))}) 382call term_sendkeys ( buf , "q" ) 383call TermWait ( buf ) 384" check that Vim is still running 385call term_sendkeys ( buf , ":echo 'hello'\<CR>" ) 386call WaitForAssert ({ -> assert_match ( '^hello' , term_getline ( buf , 18 ))}) 387call term_sendkeys ( buf , ":%bwipe!\<CR>" ) 388call StopVimInTerminal ( buf ) 389 390% bwipe ! 391endfunc 392 393func Test_swap_symlink () 394CheckUnix 395 396call writefile ([ 'text' ], 'Xtestfile' , 'D' ) 397silent !ln -s -f Xtestfile Xtestlink 398 399set dir = . 400 401" Test that swap file uses the name of the file when editing through a 402" symbolic link (so that editing the file twice is detected) 403edit Xtestlink 404call assert_match ( 'Xtestfile\.swp$' , s: swapname ()) 405bwipe ! 406 407call mkdir ( 'Xswapdir' , 'R' ) 408exe 'set dir=' . getcwd () . '/Xswapdir//' 409 410" Check that this also works when 'directory' ends with '//' 411edit Xtestlink 412call assert_match ( 'Xswapdir[/\\]%.*testdir%Xtestfile\.swp$' , s: swapname ()) 413bwipe ! 414 415set dir & 416call delete ( 'Xtestlink' ) 417endfunc 418 419func s: get_unused_pid ( base ) 420if has ( 'job' ) 421" Execute 'echo' as a temporary job, and return its pid as an unused pid. 422if has ( 'win32' ) 423let cmd = 'cmd /D /c echo' 424else 425let cmd = 'echo' 426endif 427let j = job_start ( cmd ) 428while job_status ( j ) ==# 'run' 429sleep 10m 430endwhile 431if job_status ( j ) ==# 'dead' 432return job_info ( j ). process 433endif 434elseif has ( 'nvim' ) 435let j = jobstart ( 'echo' ) 436let pid = jobpid ( j ) 437if jobwait ([ j ])[ 0 ] >= 0 438return pid 439endif 440endif 441" Must add four for MS-Windows to see it as a different one. 442return a: base + 4 443endfunc 444 445func s: blob_to_pid ( b ) 446return a: b [ 3 ] * 16777216 + a: b [ 2 ] * 65536 + a: b [ 1 ] * 256 + a: b [ 0 ] 447endfunc 448 449func s: pid_to_blob ( i ) 450let b = 0 z 451let b [ 0 ] = and ( a: i , 0xff ) 452let b [ 1 ] = and ( a: i / 256 , 0xff ) 453let b [ 2 ] = and ( a: i / 65536 , 0xff ) 454let b [ 3 ] = and ( a: i / 16777216 , 0xff ) 455return b 456endfunc 457 458func Test_swap_auto_delete () 459" Create a valid swapfile by editing a file with a special extension. 460split Xtest.scr 461call setline ( 1 , [ 'one' , 'two' , 'three' ]) 462write " file is written, not modified 463write " write again to make sure the swapfile is created 464" read the swapfile as a Blob 465let swapfile_name = swapname ( '%' ) 466let swapfile_bytes = readfile ( swapfile_name , 'B' ) 467 468" Forget about the file, recreate the swap file, then edit it again. The 469" swap file should be automatically deleted. 470bwipe ! 471" Change the process ID to avoid the "still running" warning. 472let swapfile_bytes [ 24 : 27 ] = s: pid_to_blob ( s: get_unused_pid ( 473\ s: blob_to_pid ( swapfile_bytes [ 24 : 27 ]))) 474call writefile ( swapfile_bytes , swapfile_name , 'D' ) 475edit Xtest.scr 476" will end up using the same swap file after deleting the existing one 477call assert_equal ( swapfile_name , swapname ( '%' )) 478bwipe ! 479 480" create the swap file again, but change the host name so that it won't be 481" deleted 482autocmd ! SwapExists 483augroup test_swap_recover_ext 484autocmd ! 485autocmd SwapExists * let v: swapchoice = 'e' 486augroup END 487 488" change the host name 489let swapfile_bytes [ 28 + 40 ] = swapfile_bytes [ 28 + 40 ] + 2 490call writefile ( swapfile_bytes , swapfile_name ) 491edit Xtest.scr 492call assert_equal ( 1 , filereadable ( swapfile_name )) 493" will use another same swap file name 494call assert_notequal ( swapfile_name , swapname ( '%' )) 495bwipe ! 496 497call delete ( 'Xtest.scr' ) 498augroup test_swap_recover_ext 499autocmd ! 500augroup END 501augroup ! test_swap_recover_ext 502endfunc 503 504" Test for renaming a buffer when the swap file is deleted out-of-band 505func Test_missing_swap_file () 506CheckUnix 507new Xfile2 508call delete ( swapname ( '' )) 509call assert_fails ( 'file Xfile3' , 'E301:' ) 510call assert_equal ( 'Xfile3' , bufname ()) 511call assert_true ( bufexists ( 'Xfile2' )) 512call assert_true ( bufexists ( 'Xfile3' )) 513% bw ! 514endfunc 515 516" Test for :preserve command 517func Test_preserve () 518new Xfile4 519setlocal noswapfile 520call assert_fails ( 'preserve' , 'E313:' ) 521bw ! 522endfunc 523 524" Test for the v:swapchoice variable 525func Test_swapchoice () 526call writefile ([ 'aaa' , 'bbb' ], 'Xfile5' , 'D' ) 527edit Xfile5 528preserve 529let swapfname = swapname ( '' ) 530let b = readblob ( swapfname ) 531bw ! 532call writefile ( b , swapfname , 'D' ) 533 534autocmd ! SwapExists 535 536" Test for v:swapchoice = 'o' (readonly) 537augroup test_swapchoice 538autocmd ! 539autocmd SwapExists * let v: swapchoice = 'o' 540augroup END 541edit Xfile5 542call assert_true ( & readonly ) 543call assert_equal ([ 'aaa' , 'bbb' ], getline ( 1 , '$' )) 544% bw ! 545call assert_true ( filereadable ( swapfname )) 546 547" Test for v:swapchoice = 'a' (abort) 548augroup test_swapchoice 549autocmd ! 550autocmd SwapExists * let v: swapchoice = 'a' 551augroup END 552try 553edit Xfile5 554catch /^Vim:Interrupt$/ 555endtry 556call assert_equal ( '' , @% ) 557call assert_true ( bufexists ( 'Xfile5' )) 558% bw ! 559call assert_true ( filereadable ( swapfname )) 560 561" Test for v:swapchoice = 'd' (delete) 562augroup test_swapchoice 563autocmd ! 564autocmd SwapExists * let v: swapchoice = 'd' 565augroup END 566edit Xfile5 567call assert_equal ( 'Xfile5' , @% ) 568% bw ! 569call assert_false ( filereadable ( swapfname )) 570 571call delete ( swapfname ) 572augroup test_swapchoice 573autocmd ! 574augroup END 575augroup ! test_swapchoice 576endfunc 577 578func Test_no_swap_file () 579call assert_equal ( "\nNo swap file" , execute ( 'swapname' )) 580endfunc 581 582" vim: shiftwidth=2 sts=2 expandtab