robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8634ec3827
master
1" Test signal handling. 2 3source check.vim 4source term_util.vim 5 6CheckUnix 7 8source shared.vim 9 10" Check whether a signal is available on this system. 11func HasSignal ( signal ) 12let signals = system ( 'kill -l' ) 13return signals =~# '\<' .. a: signal .. '\>' 14endfunc 15 16" Test signal WINCH (window resize signal) 17func Test_signal_WINCH () 18throw 'skipped: Nvim cannot avoid terminal resize' 19CheckNotGui 20if ! HasSignal ( 'WINCH' ) 21throw 'Skipped: WINCH signal not supported' 22endif 23 24" We do not actually want to change the size of the terminal. 25let old_WS = '' 26if exists ( '&t_WS' ) 27let old_WS = & t_WS 28let & t_WS = '' 29endif 30 31let old_lines = & lines 32let old_columns = & columns 33let new_lines = & lines - 2 34let new_columns = & columns - 2 35 36exe 'set lines=' .. new_lines 37exe 'set columns=' .. new_columns 38call assert_equal ( new_lines , & lines ) 39call assert_equal ( new_columns , & columns ) 40 41" Send signal and wait for signal to be processed. 42" 'lines' and 'columns' should have been restored 43" after handing signal WINCH. 44exe 'silent !kill -s WINCH ' .. getpid () 45call WaitForAssert ({ ->assert_equal ( old_lines , & lines )}) 46call assert_equal ( old_columns , & columns ) 47 48if old_WS != '' 49let & t_WS = old_WS 50endif 51endfunc 52 53" Test signal PWR, which should update the swap file. 54func Test_signal_PWR () 55if ! HasSignal ( 'PWR' ) 56throw 'Skipped: PWR signal not supported' 57endif 58 59" Set a very large 'updatetime' and 'updatecount', so that we can be sure 60" that swap file is updated as a result of sending PWR signal, and not 61" because of exceeding 'updatetime' or 'updatecount' when changing buffer. 62set updatetime = 100000 updatecount = 100000 63new Xtest_signal_PWR 64let swap_name = swapname ( '%' ) 65call setline ( 1 , '123' ) 66preserve 67let swap_content = readfile ( swap_name , 'b' ) 68 69" Update the buffer and check that the swap file is not yet updated, 70" since we set 'updatetime' and 'updatecount' to large values. 71call setline ( 1 , 'abc' ) 72call assert_equal ( swap_content , readfile ( swap_name , 'b' )) 73 74" Sending PWR signal should update the swap file. 75exe 'silent !kill -s PWR ' .. getpid () 76call WaitForAssert ({ ->assert_notequal ( swap_content , readfile ( swap_name , 'b' ))}) 77 78bwipe ! 79set updatetime & updatecount & 80endfunc 81 82" Test signal INT. Handler sets got_int. It should be like typing CTRL-C. 83func Test_signal_INT () 84CheckRunVimInTerminal 85if ! HasSignal ( 'INT' ) 86throw 'Skipped: INT signal not supported' 87endif 88 89let buf = RunVimInTerminal ( '' , { 'rows' : 6 }) 90let pid_vim = term_getjob ( buf ) ->job_info (). process 91 92" Check that an endless loop in Vim is interrupted by signal INT. 93call term_sendkeys ( buf , ":call setline(1, 'running')\n" ) 94call term_sendkeys ( buf , ":while 1 | endwhile\n" ) 95call WaitForAssert ({ ->assert_equal ( ':while 1 | endwhile' , term_getline ( buf , 6 ))}) 96exe 'silent !kill -s INT ' .. pid_vim 97sleep 50m 98call term_sendkeys ( buf , ":call setline(1, 'INTERRUPTED')\n" ) 99call WaitForAssert ({ ->assert_equal ( 'INTERRUPTED' , term_getline ( buf , 1 ))}) 100 101call StopVimInTerminal ( buf ) 102endfunc 103 104" Test signal TSTP. Handler sets got_tstp. 105func Test_signal_TSTP () 106CheckRunVimInTerminal 107if ! HasSignal ( 'TSTP' ) 108throw 'Skipped: TSTP signal not supported' 109endif 110 111" If test fails once, it can leave temporary files and trying to rerun 112" the test would then fail again if they are not deleted first. 113call delete ( '.Xsig_TERM.swp' ) 114call delete ( 'XsetupAucmd' ) 115call delete ( 'XautoOut1' ) 116call delete ( 'XautoOut2' ) 117let lines =<< trim END 118au VimSuspend * call writefile(["VimSuspend triggered"], "XautoOut1", "as") 119au VimResume * call writefile(["VimResume triggered"], "XautoOut2", "as") 120END 121call writefile ( lines , 'XsetupAucmd' , 'D' ) 122 123let buf = RunVimInTerminal ( '-S XsetupAucmd Xsig_TERM' , { 'rows' : 6 }) 124let pid_vim = term_getjob ( buf ) ->job_info (). process 125 126call term_sendkeys ( buf , ":call setline(1, 'foo')\n" ) 127call WaitForAssert ({ ->assert_equal ( 'foo' , term_getline ( buf , 1 ))}) 128 129call assert_false ( filereadable ( 'Xsig_TERM' )) 130 131" After TSTP the file is not saved (same function as ^Z) 132exe 'silent !kill -s TSTP ' .. pid_vim 133call WaitForAssert ({ ->assert_true ( filereadable ( '.Xsig_TERM.swp' ))}) 134sleep 100m 135 136" We resume after the suspend. Sleep a bit for the signal to take effect, 137" also when running under valgrind. 138exe 'silent !kill -s CONT ' .. pid_vim 139call WaitForAssert ({ ->assert_true ( filereadable ( 'XautoOut2' ))}) 140sleep 10m 141 142call StopVimInTerminal ( buf ) 143 144let result = readfile ( 'XautoOut1' ) 145call assert_equal ([ "VimSuspend triggered" ], result ) 146let result = readfile ( 'XautoOut2' ) 147call assert_equal ([ "VimResume triggered" ], result ) 148 149 %bwipe ! 150call delete ( '.Xsig_TERM.swp' ) 151call delete ( 'XautoOut1' ) 152call delete ( 'XautoOut2' ) 153endfunc 154 155" Test a deadly signal. 156" 157" There are several deadly signals: SISEGV, SIBUS, SIGTERM... 158" Test uses signal SIGTERM as it does not create a core 159" dump file unlike SIGSEGV, SIGBUS, etc. See "man 7 signals. 160" 161" Vim should exit with a deadly signal and unsaved changes 162" should be recoverable from the swap file preserved as a 163" result of the deadly signal handler. 164func Test_deadly_signal_TERM () 165if ! HasSignal ( 'TERM' ) 166throw 'Skipped: TERM signal not supported' 167endif 168CheckRunVimInTerminal 169 170" If test fails once, it can leave temporary files and trying to rerun 171" the test would then fail again if they are not deleted first. 172call delete ( '.Xsig_TERM.swp' ) 173call delete ( 'XsetupAucmd' ) 174call delete ( 'XautoOut' ) 175let lines =<< trim END 176au VimLeave * call writefile(["VimLeave triggered"], "XautoOut", "as") 177au VimLeavePre * call writefile(["VimLeavePre triggered"], "XautoOut", "as") 178END 179call writefile ( lines , 'XsetupAucmd' , 'D' ) 180 181let buf = RunVimInTerminal ( '-S XsetupAucmd Xsig_TERM' , { 'rows' : 6 }) 182let pid_vim = term_getjob ( buf ) ->job_info (). process 183 184call term_sendkeys ( buf , ":call setline(1, 'foo')\n" ) 185call WaitForAssert ({ ->assert_equal ( 'foo' , term_getline ( buf , 1 ))}) 186 187call assert_false ( filereadable ( 'Xsig_TERM' )) 188exe 'silent !kill -s TERM ' .. pid_vim 189call WaitForAssert ({ ->assert_true ( filereadable ( '.Xsig_TERM.swp' ))}) 190 191" Don't call StopVimInTerminal() as it expects job to be still running. 192call WaitForAssert ({ ->assert_equal ( "finished" , term_getstatus ( buf ))}) 193 194new 195silent recover .Xsig_TERM.swp 196call assert_equal ([ 'foo' ], getline ( 1 , '$' )) 197 198let result = readfile ( 'XautoOut' ) 199call assert_equal ([ "VimLeavePre triggered" , "VimLeave triggered" ], result ) 200 201 %bwipe ! 202call delete ( '.Xsig_TERM.swp' ) 203call delete ( 'XautoOut' ) 204endfunc 205 206" vim: ts=8 sw=2 sts=2 tw=80 fdm=marker