robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local tt =require ( 'test.functional.testterm' ) 4local Screen =require ( 'test.functional.ui.screen' ) 5 6local describe , it , before_each , after_each , finally = 7t . describe , t . it , t . before_each , t . after_each , t . finally 8local uv =vim . uv 9local eq , eval , expect , exec =t . eq , n . eval , n . expect , n . exec 10local assert_alive =n . assert_alive 11local clear =n . clear 12local command =n . command 13local feed =n . feed 14local fn =n . fn 15local neq =t . neq 16local nvim_prog =n . nvim_prog 17local ok =t . ok 18local rmdir =n . rmdir 19local new_pipename =n . new_pipename 20local pesc =vim . pesc 21local is_os =t . is_os 22local skip =t . skip 23local set_session =n . set_session 24local async_meths =n . async_meths 25local expect_msg_seq =n . expect_msg_seq 26local pcall_err =t . pcall_err 27local mkdir =t . mkdir 28local poke_eventloop =n . poke_eventloop 29local api =n . api 30local retry =t . retry 31local write_file =t . write_file 32local expect_exitcode =tt . expect_exitcode 33 34describe ( ':recover' , function () 35before_each ( clear ) 36 37it ( 'fails if given a non-existent swapfile' , function () 38local swapname ='bogus_swapfile' 39local swapname2 ='bogus_swapfile.swp' 40eq ( 41'Vim(recover):E305: No swap file found for ' .. swapname , 42pcall_err ( command , 'recover ' .. swapname ) 43) -- Should not segfault. #2117 44-- Also check filename ending with ".swp". #9504 45eq ( 'Vim(recover):E306: Cannot open ' .. swapname2 , pcall_err ( command , 'recover ' .. swapname2 )) -- Should not segfault. #2117 46assert_alive () 47end ) 48end ) 49 50describe ( "preserve and (R)ecover with custom 'directory'" , function () 51local swapdir =uv . cwd () .. '/Xtest_recover_dir' 52local testfile ='Xtest_recover_file1' 53-- Put swapdir at the start of the 'directory' list. #1836 54-- Note: `set swapfile` *must* go after `set directory`: otherwise it may 55-- attempt to create a swapfile in different directory. 56local init =[[ 57set directory^=]] .. swapdir : gsub ( [[\]] , [[\\]] ) .. [[// 58set swapfile fileformat=unix undolevels=-1 59]] 60 61local nvim0 --- @type test.Session 62before_each ( function () 63nvim0 =n . new_session ( false ) 64set_session ( nvim0 ) 65rmdir ( swapdir ) 66mkdir ( swapdir ) 67end ) 68after_each ( function () 69command ( '%bwipeout!' ) 70rmdir ( swapdir ) 71end ) 72 73--- @return string 74local function setup_swapname () 75exec ( init ) 76command ( 'edit! ' .. testfile ) 77feed ( 'isometext<esc>' ) 78exec ( 'redir => g:swapname | silent swapname | redir END' ) 79return eval ( 'g:swapname' ): match ( '[^\n]*$' ) 80end 81 82local function test_recover ( swappath1 ) 83-- Start another Nvim instance. 84local nvim2 = 85n . new_session ( false , { args ={ '-u' , 'NONE' , '-i' , 'NONE' , '--embed' } , merge =false } ) 86set_session ( nvim2 ) 87 88exec ( init ) 89 90-- Use the "SwapExists" event to choose the (R)ecover choice at the dialog. 91command ( 'autocmd SwapExists * let v:swapchoice = "r"' ) 92command ( 'silent edit! ' .. testfile ) 93exec ( 'redir => g:swapname | silent swapname | redir END' ) 94 95local swappath2 =eval ( 'g:swapname' ) 96 97expect ( 'sometext' ) 98-- swapfile from session 1 should end in .swp 99eq ( testfile .. '.swp' , string . match ( swappath1 , '[^%%]+$' )) 100-- swapfile from session 2 should end in .swo 101eq ( testfile .. '.swo' , string . match ( swappath2 , '[^%%]+$' )) 102-- Verify that :swapname was not truncated (:help 'shortmess'). 103ok ( nil == string . find ( swappath1 , '%.%.%.' )) 104ok ( nil == string . find ( swappath2 , '%.%.%.' )) 105end 106 107it ( 'with :preserve and SIGKILL' , function () 108local swappath1 =setup_swapname () 109command ( 'preserve' ) 110neq ( nil , uv . fs_stat ( swappath1 )) 111eq ( 0 , vim . uv . kill ( eval ( 'getpid()' ), 'sigkill' )) 112test_recover ( swappath1 ) 113end ) 114 115it ( 'closing stdio channel without :preserve #22096' , function () 116local swappath1 =setup_swapname () 117nvim0 : close () 118neq ( nil , uv . fs_stat ( swappath1 )) 119test_recover ( swappath1 ) 120end ) 121 122it ( 'killing TUI process without :preserve #22096' , function () 123-- Windows(#38669): inner server could attach to the outer Nvim terminal's console 124-- and die abruptly when the outer terminal job closed, leaving an unreadable swapfile 125skip ( is_os ( 'win' ), 'unreadable swapfile on Windows after TUI process exit' ) 126 127local screen0 =Screen . new () 128local child_server =new_pipename () 129fn . jobstart ( { nvim_prog , '-u' , 'NONE' , '-i' , 'NONE' , '--listen' , child_server } , { 130term =true , 131env ={ VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ) } , 132} ) 133screen0 : expect ( { any =pesc ( '[No Name]' ) } ) -- Wait for the child process to start. 134local child_session =n . connect ( child_server ) 135set_session ( child_session ) 136local swappath1 =setup_swapname () 137set_session ( nvim0 ) 138-- n.exec_lua([[vim.uv.kill(vim.fn.jobpid(vim.bo.channel), 'sigterm')]]) 139command ( 'call chanclose(&channel)' ) -- Kill the child process. 140-- Wait for the child process to stop. 141screen0 : expect ( { any ='%[Process exited 1%]' } ) 142neq ( nil , uv . fs_stat ( swappath1 )) 143test_recover ( swappath1 ) 144end ) 145 146for _ , ext in ipairs ( { false , true } ) do 147it ( 'manual :recover with multiple swapfiles' .. ( ext and 'with ext_messages' or '' ), function () 148local swappath1 =setup_swapname () 149eq ( '.swp' , swappath1 : match ( '%.[^.]+$' )) 150nvim0 : close () 151neq ( nil , uv . fs_stat ( swappath1 )) 152local swappath2 =swappath1 : gsub ( '%.swp$' , '.swo' ) 153eq ( true , uv . fs_copyfile ( swappath1 , swappath2 )) 154clear () 155exec ( init ) 156local screen , msg =Screen . new ( 256 , 40 , { ext_messages =ext } ), nil 157feed ( ':recover! ' .. testfile .. '<CR>' ) 158if ext then 159screen : expect ( { 160cmdline ={ 161{ 162content ={ { '' } } , 163pos =0 , 164-- Default vim.ui.select prompt. 165prompt ='Type number and <Enter> (q or empty cancels): ' , 166} , 167} , 168condition =function () 169msg =msg or screen . messages [ 1 ] 170-- Concatenate all chunks (each chunk is { 'text' } or { hl_id, 'text', 'group' }). 171local text ='' 172for _ , chunk in ipairs ( msg . content ) do 173text =text .. ( # chunk >= 2 and chunk [ 2 ] or chunk [ 1 ]) 174end 175-- New ui.select-driven prompt; rich info from format_item. 176eq ( true , text : match ( 'Select a swapfile:' ) ~= nil ) 177eq ( true , text : match ( '%.swo' ) ~= nil ) 178eq ( true , text : match ( '%.swp' ) ~= nil ) 179eq ( true , text : match ( 'host name:' ) ~= nil ) 180eq ( 'confirm' , msg . kind ) 181screen . messages ={} 182end , 183} ) 184else 185screen : expect ( { 186any ={ 187vim . pesc ( 'Select a swapfile:' ), 188'\n1:.*%.swo' , 189'\n2:.*%.swp' , 190'host name:' , 191vim . pesc ( 'Type number and <Enter> (q or empty cancels): ^' ), 192} , 193none =vim . pesc ( '{18:^@}' ), 194} ) 195end 196feed ( '2<CR>' ) 197if ext then 198screen : expect ( { 199any ={ 'sometext' } , 200condition =function () 201eq ( 'wmsg' , screen . messages [ 1 ]. kind ) 202eq ( true , screen . messages [ 1 ]. content [ 1 ][ 2 ]: match ( 'Using.*Original' ) ~= nil ) 203eq ( 'wmsg' , screen . messages [ 1 ]. kind ) 204eq ( true , screen . messages [ 2 ]. content [ 1 ][ 2 ]: match ( 'Recovery.*You might.*You may' ) ~= nil ) 205screen . messages ={} 206end , 207} ) 208else 209screen : expect ( { 210any ={ 211vim . pesc ( '\nRecovery completed.' ), 212vim . pesc ( '\n{6:Press ENTER or type command to continue}^' ), 213} , 214} ) 215end 216feed ( '<CR>' ) 217expect ( 'sometext' ) 218end ) 219end 220end ) 221 222describe ( 'swapfile detection' , function () 223local swapdir =uv . cwd () .. '/Xtest_swapdialog_dir' 224local nvim0 --- @type test.Session 225-- Put swapdir at the start of the 'directory' list. #1836 226-- Note: `set swapfile` *must* go after `set directory`: otherwise it may 227-- attempt to create a swapfile in different directory. 228local init =[[ 229set directory^=]] .. swapdir : gsub ( [[\]] , [[\\]] ) .. [[// 230set swapfile fileformat=unix nomodified undolevels=-1 nohidden 231]] 232before_each ( function () 233nvim0 =n . new_session ( false ) 234set_session ( nvim0 ) 235rmdir ( swapdir ) 236mkdir ( swapdir ) 237end ) 238after_each ( function () 239set_session ( nvim0 ) 240command ( '%bwipeout!' ) 241rmdir ( swapdir ) 242end ) 243 244it ( 'redrawing during prompt does not break treesitter' , function () 245local testfile ='Xtest_swapredraw.lua' 246finally ( function () 247os . remove ( testfile ) 248end ) 249write_file ( 250testfile , 251[[ 252vim.o.foldmethod = 'expr' 253vim.o.foldexpr = 'v:lua.vim.treesitter.foldexpr()' 254vim.defer_fn(function() 255vim.api.nvim__redraw({ valid = false }) 256end, 500) 257pcall(vim.cmd.edit, 'Xtest_swapredraw.lua')]] 258) 259exec ( init ) 260command ( 'edit! ' .. testfile ) 261command ( 'preserve' ) 262local args2 ={ '--clean' , '--embed' , '--cmd' , n . runtime_set } 263local nvim2 =n . new_session ( true , { args =args2 , merge =false } ) 264set_session ( nvim2 ) 265local screen2 =Screen . new ( 100 , 40 ) 266screen2 : add_extra_attr_ids ( { 267[ 100 ] ={ foreground =Screen . colors . NvimLightGrey2 } , 268[ 101 ] ={ foreground =Screen . colors . NvimLightGreen } , 269[ 102 ] ={ 270foreground =Screen . colors . NvimLightGrey4 , 271background =Screen . colors . NvimDarkGrey1 , 272} , 273[ 104 ] ={ foreground =Screen . colors . NvimLightCyan } , 274[ 105 ] ={ foreground =Screen . colors . NvimDarkGrey4 } , 275[ 106 ] ={ 276foreground =Screen . colors . NvimLightGrey2 , 277background =Screen . colors . NvimDarkGrey4 , 278} , 279[ 107 ] ={ foreground =Screen . colors . NvimLightGrey2 , bold =true } , 280[ 108 ] ={ foreground =Screen . colors . NvimLightBlue } , 281} ) 282exec ( init ) 283command ( 'autocmd! nvim.swapfile' ) -- Delete the default handler (which skips the dialog). 284feed ( ':edit ' .. testfile .. '<CR>' ) 285eq ( 'r?' , api . nvim_get_mode (). mode ) 286feed ( 'E:source<CR>' ) 287eq ( 'r?' , api . nvim_get_mode (). mode ) 288screen2 : sleep ( 1000 ) 289feed ( 'E' ) 290screen2 : expect ( [[ 291{100:^vim.o.foldmethod} {100:=} {101:'expr'} | 292{100:vim.o.foldexpr} {100:=} {101:'v:lua.vim.treesitter.foldexpr()'} | 293{102:+-- 3 lines: vim.defer_fn(function()·······························································}| 294{104:pcall}{100:(vim.cmd.edit,} {101:'Xtest_swapredraw.lua'}{100:)} | 295{105:~ }|*34 296{106:Xtest_swapredraw.lua 1,1 All}| 297| 298]] ) 299nvim2 : close () 300end ) 301 302it ( 'always show swapfile dialog #8840 #9027' , function () 303local testfile ='Xtest_swapdialog_file1' 304 305local expected_no_dialog ='^' .. ( ' ' ): rep ( 256 ) .. '|\n' 306for _ =1 , 37 do 307expected_no_dialog =expected_no_dialog .. '~' .. ( ' ' ): rep ( 255 ) .. '|\n' 308end 309expected_no_dialog =expected_no_dialog .. testfile .. ( ' ' ): rep ( 216 ) .. '0,0-1 All|\n' 310expected_no_dialog =expected_no_dialog .. ( ' ' ): rep ( 256 ) .. '|\n' 311 312exec ( init ) 313command ( 'edit! ' .. testfile ) 314feed ( 'isometext<esc>' ) 315command ( 'preserve' ) 316 317-- Start another Nvim instance. 318local nvim2 = 319n . new_session ( true , { args ={ '-u' , 'NONE' , '-i' , 'NONE' , '--embed' } , merge =false } ) 320set_session ( nvim2 ) 321local screen2 =Screen . new ( 256 , 40 ) 322screen2 . _default_attr_ids =nil 323exec ( init ) 324command ( 'autocmd! nvim.swapfile' ) -- Delete the default handler (which skips the dialog). 325 326-- With shortmess+=F 327command ( 'set shortmess+=F' ) 328feed ( ':edit ' .. testfile .. '<CR>' ) 329screen2 : expect { 330any =[[E325: ATTENTION.*]] 331.. '\n' 332.. [[Found a swap file by the name ".*]] 333.. [[Xtest_swapdialog_dir[/\].*]] 334.. testfile 335.. [[%.swp"]] , 336} 337feed ( 'e' ) -- Chose "Edit" at the swap dialog. 338screen2 : expect ( expected_no_dialog ) 339 340-- With :silent and shortmess+=F 341feed ( ':silent edit %<CR>' ) 342screen2 : expect { 343any =[[Found a swap file by the name ".*]] 344.. [[Xtest_swapdialog_dir[/\].*]] 345.. testfile 346.. [[%.swp"]] , 347} 348feed ( 'e' ) -- Chose "Edit" at the swap dialog. 349screen2 : expect ( expected_no_dialog ) 350 351-- With :silent! and shortmess+=F 352feed ( ':silent! edit %<CR>' ) 353screen2 : expect { 354any =[[Found a swap file by the name ".*]] 355.. [[Xtest_swapdialog_dir[/\].*]] 356.. testfile 357.. [[%.swp"]] , 358} 359feed ( 'e' ) -- Chose "Edit" at the swap dialog. 360screen2 : expect ( expected_no_dialog ) 361 362-- With API (via eval/Vimscript) call and shortmess+=F 363feed ( ':call nvim_command("edit %")<CR>' ) 364screen2 : expect { 365any =[[Found a swap file by the name ".*]] 366.. [[Xtest_swapdialog_dir[/\].*]] 367.. testfile 368.. [[%.swp"]] , 369} 370feed ( 'e' ) -- Chose "Edit" at the swap dialog. 371screen2 : expect ( { any =pesc ( 'E5555: API call: Vim(edit):E325: ATTENTION' ) } ) 372feed ( '<c-c>' ) 373screen2 : expect ( expected_no_dialog ) 374 375-- With API call and shortmess+=F 376async_meths . nvim_command ( 'edit %' ) 377screen2 : expect { 378any =[[Found a swap file by the name ".*]] 379.. [[Xtest_swapdialog_dir[/\].*]] 380.. testfile 381.. [[%.swp"]] , 382} 383feed ( 'e' ) -- Chose "Edit" at the swap dialog. 384expect_msg_seq ( { 385ignore ={ 'redraw' } , 386seqs ={ 387{ { 'notification' , 'nvim_error_event' , { 0 , 'Vim(edit):E325: ATTENTION' } } } , 388} , 389} ) 390feed ( '<cr>' ) 391 392nvim2 : close () 393end ) 394 395it ( 'default SwapExists handler selects "(E)dit" and skips prompt' , function () 396exec ( init ) 397command ( 'edit Xfile1' ) 398command ( "put ='some text...'" ) 399command ( 'preserve' ) -- Make sure the swap file exists. 400local nvimpid =fn . getpid () 401 402local nvim1 =n . new_session ( true ) 403set_session ( nvim1 ) 404local screen =Screen . new ( 75 , 18 ) 405exec ( init ) 406 407feed ( ':edit Xfile1\n' ) 408local msg_expected =( 'W325: Ignoring swapfile from Nvim process %d' ): format ( nvimpid ) 409screen : expect ( { any =vim . pesc ( msg_expected ) } ) 410eq ( msg_expected , n . exec_capture ( 'messages' )) 411command ( 'bwipe!' ) 412 413-- With smaller screen, the message should be truncated, but only in display. 414screen : try_resize ( 40 , 18 ) 415feed ( ':edit Xfile1\n' ) 416screen : expect ( [[ 417^ | 418{1:~ }|*16 419{19:W325: Ignoring swapfile from Nvim proces}| 420]] ) 421eq (( '\n' .. msg_expected ): rep ( 2 ): sub ( 2 ), n . exec_capture ( 'messages' )) 422command ( 'bwipe!' ) 423 424-- Also test with 'ruler'. 425screen : try_resize ( 60 , 18 ) 426command ( 'set ruler' ) 427feed ( ':edit Xfile1\n' ) 428screen : expect ( [[ 429^ | 430{1:~ }|*16 431{19:W325: Ignoring swapfile from Nvim process }0,0-1 All| 432]] ) 433eq (( '\n' .. msg_expected ): rep ( 3 ): sub ( 2 ), n . exec_capture ( 'messages' )) 434command ( 'bwipe!' ) 435 436nvim1 : close () 437end ) 438 439-- ui2 opens windows to show a message, which chdirs in and out of the target window's context. 440-- readfile() aliases the buffer name across that. 441it ( 'ui2 message from the SwapExists handler does not abort the read #41454' , function () 442exec ( init ) 443write_file ( 'Xfile1' , 'some text...\n' ) 444command ( 'edit Xfile1' ) 445command ( 'preserve' ) -- Make sure the swap file exists. 446 447local initfile ='Xtest_swapdialog_init.lua' 448write_file ( 449initfile , 450( 'vim.opt.directory:prepend(%q)\nrequire("vim._core.ui2").enable()\n' ): format ( swapdir .. '//' ) 451) 452finally ( function () 453os . remove ( initfile ) 454os . remove ( 'Xfile1' ) 455end ) 456 457local nvim1 =n . new_session ( true ) 458set_session ( nvim1 ) 459local screen =Screen . new ( 75 , 10 ) 460-- ui2 needs a UI attached while the file argument is read, i.e. a real TTY. 461fn . jobstart ( { nvim_prog , '-i' , 'NONE' , '--noplugin' , '-u' , initfile , 'Xfile1' } , { 462term =true , 463env ={ VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ) } , 464} ) 465screen : expect ( { 466grid =[[ 467^some text... | 468~ |*6 469Xfile1 1,1 All| 470{MATCH:W325: Ignoring swapfile from Nvim process %d+ *}| 471| 472]] , 473attr_ids ={} , 474} ) 475 476nvim1 : close () 477end ) 478 479it ( 'attention message kind' , function () 480exec ( init ) 481command ( 'edit Xfile1' ) 482command ( 'preserve' ) -- Make sure the swap file exists. 483 484local screen =Screen . new ( nil , nil , { ext_messages =true } ) 485local nvim =n . new_session ( true ) 486set_session ( nvim ) 487screen : attach () 488exec ( init ) 489command ( 'edit Xfile1' ) 490command ( 'autocmd! nvim.swapfile' ) -- Delete the default handler (which skips the dialog). 491command ( 'edit Xfile2 | bunload 1' ) -- Unload to get non-prompt attention message. 492command ( 'silent! call bufload("Xfile1")' ) 493screen : expect ( { 494condition =function () 495for _ , msg in pairs ( screen . messages ) do 496local ok =msg . content [ 1 ][ 2 ]: match ( 'W325' ) 497eq ( true , ok and msg . kind == 'echomsg' or msg . kind == 'wmsg' ) 498eq ( true , ( ok or msg . content [ 1 ][ 2 ]: match ( 'Found a swap.*If you did' )) ~= nil ) 499end 500screen . messages ={} 501end , 502cmdline ={ 503{ content ={ { '' } } , hl ='MoreMsg' , pos =0 , prompt ='Press any key to continue' } , 504} , 505} ) 506nvim : close () 507end ) 508 509-- oldtest: Test_swap_prompt_splitwin() 510it ( 'selecting "q" in the attention prompt' , function () 511exec ( init ) 512command ( 'edit Xfile1' ) 513command ( 'preserve' ) -- Make sure the swap file exists. 514 515local screen =Screen . new ( 75 , 18 ) 516local nvim1 =n . new_session ( true ) 517set_session ( nvim1 ) 518screen : attach () 519exec ( init ) 520command ( 'autocmd! nvim.swapfile' ) -- Delete the default handler (which skips the dialog). 521feed ( ':split Xfile1\n' ) 522-- The default SwapExists handler does _not_ skip this prompt. 523screen : expect ( { 524any =pesc ( '{6:[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort: }^' ), 525} ) 526feed ( 'q' ) 527screen : expect ( [[ 528^ | 529{1:~ }|*16 530| 531]] ) 532feed ( ':<CR>' ) 533screen : expect ( [[ 534^ | 535{1:~ }|*16 536: | 537]] ) 538nvim1 : close () 539 540local nvim2 =n . new_session ( true ) 541set_session ( nvim2 ) 542screen : attach () 543exec ( init ) 544command ( 'autocmd! nvim.swapfile' ) -- Delete the default handler (which skips the dialog). 545command ( 'set more' ) 546command ( 'au bufadd * let foo_w = wincol()' ) 547feed ( ':e Xfile1<CR>' ) 548screen : expect ( { any =pesc ( '{6:-- More --}^' ) } ) 549feed ( '<Space>' ) 550screen : expect ( { 551any =pesc ( '{6:[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort: }^' ), 552} ) 553feed ( 'q' ) 554command ( [[echo 'hello']] ) 555screen : expect ( [[ 556^ | 557{1:~ }|*16 558hello | 559]] ) 560nvim2 : close () 561end ) 562 563--- @param swapexists boolean Enable the default SwapExists handler. 564--- @param on_swapfile_running fun(screen: any) Called after swapfile ("STILL RUNNING") prompt. 565local function test_swapfile_after_reboot ( swapexists , on_swapfile_running ) 566local screen =Screen . new ( 75 , 30 ) 567 568exec ( init ) 569if not swapexists then 570command ( 'autocmd! nvim.swapfile' ) -- Delete the default handler (which skips the dialog). 571end 572command ( 'set nohidden' ) 573 574exec ( [=[ 575" Make a copy of the current swap file to "Xswap". 576" Return the name of the swap file. 577func CopySwapfile() 578preserve 579" get the name of the swap file 580let swname = split(execute("swapname"))[0] 581let swname = substitute(swname, '[[:blank:][:cntrl:]]*\(.\{-}\)[[:blank:][:cntrl:]]*$', '\1', '') 582" make a copy of the swap file in Xswap 583set binary 584exe 'sp ' . fnameescape(swname) 585w! Xswap 586set nobinary 587return swname 588endfunc 589]=] ) 590 591-- Edit a file and grab its swapfile. 592exec ( [[ 593edit Xswaptest 594call setline(1, ['a', 'b', 'c']) 595]] ) 596local swname =fn . CopySwapfile () 597 598-- Forget we edited this file 599exec ( [[ 600new 601only! 602bwipe! Xswaptest 603]] ) 604 605os . rename ( 'Xswap' , swname ) 606 607feed ( ':edit Xswaptest<CR>' ) 608on_swapfile_running ( screen ) 609 610feed ( 'e' ) 611 612-- Forget we edited this file 613exec ( [[ 614new 615only! 616bwipe! Xswaptest 617]] ) 618 619-- pretend that the swapfile was created before boot 620local atime =os . time () - uv . uptime () - 10 621uv . fs_utime ( swname , atime , atime ) 622 623feed ( ':edit Xswaptest<CR>' ) 624screen : expect ( { 625any =table . concat ( { 626'{9:E325: ATTENTION}' , 627pesc ( '{6:[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort: }^' ), 628} , '.*' ), 629} ) 630 631feed ( 'e' ) 632end 633 634-- oldtest: Test_nocatch_process_still_running() 635it ( 'swapfile created before boot vim-patch:8.2.2586' , function () 636test_swapfile_after_reboot ( false , function ( screen ) 637screen : expect ( { 638any =table . concat ( { 639'{9:E325: ATTENTION}' , 640'{6: process ID: %d* %(STILL RUNNING%)}' , 641'{6:While opening file "Xswaptest"}' , 642pesc ( '{6:[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort: }^' ), 643} , '.*' ), 644} ) 645end ) 646end ) 647 648it ( 'swapfile created before boot + default SwapExists handler' , function () 649test_swapfile_after_reboot ( true , function ( screen ) 650screen : expect ( { any ='W325: Ignoring swapfile from Nvim process' } ) 651end ) 652end ) 653end ) 654 655describe ( 'quitting swapfile dialog on startup stops TUI properly' , function () 656local swapdir =uv . cwd () .. '/Xtest_swapquit_dir' 657local testfile ='Xtest_swapquit_file1' 658local otherfile ='Xtest_swapquit_file2' 659-- Put swapdir at the start of the 'directory' list. #1836 660-- Note: `set swapfile` *must* go after `set directory`: otherwise it may 661-- attempt to create a swapfile in different directory. 662local init_dir =[[set directory^=]] .. swapdir : gsub ( [[\]] , [[\\]] ) .. [[//]] 663local init_set =[[set swapfile fileformat=unix nomodified undolevels=-1 nohidden]] 664 665before_each ( function () 666clear ( { args ={ '--cmd' , init_dir , '--cmd' , init_set } } ) 667rmdir ( swapdir ) 668mkdir ( swapdir ) 669write_file ( 670testfile , 671[[ 672first 673second 674third 675 676]] 677) 678command ( 'edit! ' .. testfile ) 679feed ( 'Gisometext<esc>' ) 680poke_eventloop () 681clear () -- Leaves a swap file behind 682api . nvim_ui_attach ( 80 , 30 , {} ) 683end ) 684after_each ( function () 685rmdir ( swapdir ) 686os . remove ( testfile ) 687os . remove ( otherfile ) 688end ) 689 690it ( '(Q)uit at first file argument' , function () 691local chan =fn . jobstart ( 692{ nvim_prog , '-u' , 'NONE' , '-i' , 'NONE' , '--cmd' , init_dir , '--cmd' , init_set , testfile } , 693{ 694term =true , 695env ={ VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ) } , 696} 697) 698retry ( nil , nil , function () 699eq ( 700'[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:' , 701eval ( "getline('$')->trim(' ', 2)" ) 702) 703end ) 704api . nvim_chan_send ( chan , 'q' ) 705expect_exitcode ( 1 ) 706end ) 707 708it ( '(A)bort at second file argument with -p' , function () 709local chan =fn . jobstart ( { 710nvim_prog , 711'-u' , 712'NONE' , 713'-i' , 714'NONE' , 715'--cmd' , 716init_dir , 717'--cmd' , 718init_set , 719'-p' , 720otherfile , 721testfile , 722} , { 723term =true , 724env ={ VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ) } , 725} ) 726retry ( nil , nil , function () 727eq ( 728'[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:' , 729eval ( "getline('$')->trim(' ', 2)" ) 730) 731end ) 732api . nvim_chan_send ( chan , 'a' ) 733expect_exitcode ( 1 ) 734end ) 735 736it ( '(Q)uit at file opened by -t' , function () 737write_file ( 738otherfile , 739( [[ 740!_TAG_FILE_ENCODING utf-8 // 741first %s /^ \zsfirst$/ 742second %s /^ \zssecond$/ 743third %s /^ \zsthird$/]] ): format ( testfile , testfile , testfile ) 744) 745local chan =fn . jobstart ( { 746nvim_prog , 747'-u' , 748'NONE' , 749'-i' , 750'NONE' , 751'--cmd' , 752init_dir , 753'--cmd' , 754init_set , 755'--cmd' , 756'set tags=' .. otherfile , 757'-tsecond' , 758} , { 759term =true , 760env ={ VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ) } , 761} ) 762retry ( nil , nil , function () 763eq ( 764'[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:' , 765eval ( "getline('$')->trim(' ', 2)" ) 766) 767end ) 768api . nvim_chan_send ( chan , 'q' ) 769expect_exitcode ( 1 ) 770end ) 771end ) 772 773describe ( 'swapfile' , function () 774it ( 'when using device path #31606' , function () 775t . skip ( not is_os ( 'win' ), 'N/A: Windows feature' ) 776local cwd =vim . fs . normalize ( vim . uv . cwd ()) 777local drive , path =cwd : sub ( 1 , 1 ), cwd : sub ( 3 ) 778local swapdir =( '%s/Xtest_swap_dir' ): format ( cwd ) 779local dos_path =( '%s/file.txt' ): format ( cwd ) 780local device_quest_path =( '//?/%s' ): format ( dos_path ) 781clear ( { 782args ={ 783'--embed' , 784'--headless' , 785'--cmd' , 786( 'set directory=%s//' ): format ( swapdir ), 787'--' , 788device_quest_path , 789} , 790merge =false , 791} ) 792local escaped_name =( '%s/%s.swp' ): format ( swapdir , dos_path : gsub ( '[:/]' , '%%' )) 793eq ( escaped_name , fn . swapname ( '%' )) 794 795command ( 'bw!' ) 796api . nvim_buf_set_name ( 0 , dos_path ) 797eq ( escaped_name , fn . swapname ( '%' )) 798 799command ( 'bw!' ) 800local device_dot_path =( '//./%s' ): format ( dos_path ) 801api . nvim_buf_set_name ( 0 , device_dot_path ) 802eq ( escaped_name , fn . swapname ( '%' )) 803 804command ( 'bw!' ) 805local device_unc_path =( '//?/UNC/localhost/%s$%s/file.txt' ): format ( drive , path ) 806api . nvim_buf_set_name ( 0 , device_unc_path ) 807eq (( '%s/%%%s.swp' ): format ( swapdir , device_unc_path : sub ( 8 ): gsub ( '/' , '%%' )), fn . swapname ( '%' )) 808 809command ( 'bw!' ) 810-- Volume GUID form, like \\?\Volume{d4857377-2ac4-45d8-a4cb-9b2e447fd02e}\ 811local guid =vim . fs . normalize ( vim . trim ( vim . fn . system (( 'mountvol %s: /L' ): format ( drive )))) 812local device_guid_path =( '%s%s/file.txt' ): format ( guid , path : sub ( 2 )) 813api . nvim_buf_set_name ( 0 , device_guid_path ) 814eq ( escaped_name , fn . swapname ( '%' )) 815end ) 816end )