test_signals.vim (6568B)
1 " Test signal handling. 2 3 source check.vim 4 source term_util.vim 5 6 CheckUnix 7 8 source shared.vim 9 10 " Check whether a signal is available on this system. 11 func HasSignal(signal) 12 let signals = system('kill -l') 13 return signals =~# '\<' .. a:signal .. '\>' 14 endfunc 15 16 " Test signal WINCH (window resize signal) 17 func Test_signal_WINCH() 18 throw 'skipped: Nvim cannot avoid terminal resize' 19 CheckNotGui 20 if !HasSignal('WINCH') 21 throw 'Skipped: WINCH signal not supported' 22 endif 23 24 " We do not actually want to change the size of the terminal. 25 let old_WS = '' 26 if exists('&t_WS') 27 let old_WS = &t_WS 28 let &t_WS = '' 29 endif 30 31 let old_lines = &lines 32 let old_columns = &columns 33 let new_lines = &lines - 2 34 let new_columns = &columns - 2 35 36 exe 'set lines=' .. new_lines 37 exe 'set columns=' .. new_columns 38 call assert_equal(new_lines, &lines) 39 call 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. 44 exe 'silent !kill -s WINCH ' .. getpid() 45 call WaitForAssert({-> assert_equal(old_lines, &lines)}) 46 call assert_equal(old_columns, &columns) 47 48 if old_WS != '' 49 let &t_WS = old_WS 50 endif 51 endfunc 52 53 " Test signal PWR, which should update the swap file. 54 func Test_signal_PWR() 55 if !HasSignal('PWR') 56 throw 'Skipped: PWR signal not supported' 57 endif 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. 62 set updatetime=100000 updatecount=100000 63 new Xtest_signal_PWR 64 let swap_name = swapname('%') 65 call setline(1, '123') 66 preserve 67 let 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. 71 call setline(1, 'abc') 72 call assert_equal(swap_content, readfile(swap_name, 'b')) 73 74 " Sending PWR signal should update the swap file. 75 exe 'silent !kill -s PWR ' .. getpid() 76 call WaitForAssert({-> assert_notequal(swap_content, readfile(swap_name, 'b'))}) 77 78 bwipe! 79 set updatetime& updatecount& 80 endfunc 81 82 " Test signal INT. Handler sets got_int. It should be like typing CTRL-C. 83 func Test_signal_INT() 84 CheckRunVimInTerminal 85 if !HasSignal('INT') 86 throw 'Skipped: INT signal not supported' 87 endif 88 89 let buf = RunVimInTerminal('', {'rows': 6}) 90 let pid_vim = term_getjob(buf)->job_info().process 91 92 " Check that an endless loop in Vim is interrupted by signal INT. 93 call term_sendkeys(buf, ":call setline(1, 'running')\n") 94 call term_sendkeys(buf, ":while 1 | endwhile\n") 95 call WaitForAssert({-> assert_equal(':while 1 | endwhile', term_getline(buf, 6))}) 96 exe 'silent !kill -s INT ' .. pid_vim 97 sleep 50m 98 call term_sendkeys(buf, ":call setline(1, 'INTERRUPTED')\n") 99 call WaitForAssert({-> assert_equal('INTERRUPTED', term_getline(buf, 1))}) 100 101 call StopVimInTerminal(buf) 102 endfunc 103 104 " Test signal TSTP. Handler sets got_tstp. 105 func Test_signal_TSTP() 106 CheckRunVimInTerminal 107 if !HasSignal('TSTP') 108 throw 'Skipped: TSTP signal not supported' 109 endif 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. 113 call delete('.Xsig_TERM.swp') 114 call delete('XsetupAucmd') 115 call delete('XautoOut1') 116 call delete('XautoOut2') 117 let lines =<< trim END 118 au VimSuspend * call writefile(["VimSuspend triggered"], "XautoOut1", "as") 119 au VimResume * call writefile(["VimResume triggered"], "XautoOut2", "as") 120 END 121 call writefile(lines, 'XsetupAucmd', 'D') 122 123 let buf = RunVimInTerminal('-S XsetupAucmd Xsig_TERM', {'rows': 6}) 124 let pid_vim = term_getjob(buf)->job_info().process 125 126 call term_sendkeys(buf, ":call setline(1, 'foo')\n") 127 call WaitForAssert({-> assert_equal('foo', term_getline(buf, 1))}) 128 129 call assert_false(filereadable('Xsig_TERM')) 130 131 " After TSTP the file is not saved (same function as ^Z) 132 exe 'silent !kill -s TSTP ' .. pid_vim 133 call WaitForAssert({-> assert_true(filereadable('.Xsig_TERM.swp'))}) 134 sleep 100m 135 136 " We resume after the suspend. Sleep a bit for the signal to take effect, 137 " also when running under valgrind. 138 exe 'silent !kill -s CONT ' .. pid_vim 139 call WaitForAssert({-> assert_true(filereadable('XautoOut2'))}) 140 sleep 10m 141 142 call StopVimInTerminal(buf) 143 144 let result = readfile('XautoOut1') 145 call assert_equal(["VimSuspend triggered"], result) 146 let result = readfile('XautoOut2') 147 call assert_equal(["VimResume triggered"], result) 148 149 %bwipe! 150 call delete('.Xsig_TERM.swp') 151 call delete('XautoOut1') 152 call delete('XautoOut2') 153 endfunc 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. 164 func Test_deadly_signal_TERM() 165 if !HasSignal('TERM') 166 throw 'Skipped: TERM signal not supported' 167 endif 168 CheckRunVimInTerminal 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. 172 call delete('.Xsig_TERM.swp') 173 call delete('XsetupAucmd') 174 call delete('XautoOut') 175 let lines =<< trim END 176 au VimLeave * call writefile(["VimLeave triggered"], "XautoOut", "as") 177 au VimLeavePre * call writefile(["VimLeavePre triggered"], "XautoOut", "as") 178 END 179 call writefile(lines, 'XsetupAucmd', 'D') 180 181 let buf = RunVimInTerminal('-S XsetupAucmd Xsig_TERM', {'rows': 6}) 182 let pid_vim = term_getjob(buf)->job_info().process 183 184 call term_sendkeys(buf, ":call setline(1, 'foo')\n") 185 call WaitForAssert({-> assert_equal('foo', term_getline(buf, 1))}) 186 187 call assert_false(filereadable('Xsig_TERM')) 188 exe 'silent !kill -s TERM ' .. pid_vim 189 call WaitForAssert({-> assert_true(filereadable('.Xsig_TERM.swp'))}) 190 191 " Don't call StopVimInTerminal() as it expects job to be still running. 192 call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))}) 193 194 new 195 silent recover .Xsig_TERM.swp 196 call assert_equal(['foo'], getline(1, '$')) 197 198 let result = readfile('XautoOut') 199 call assert_equal(["VimLeavePre triggered", "VimLeave triggered"], result) 200 201 %bwipe! 202 call delete('.Xsig_TERM.swp') 203 call delete('XautoOut') 204 endfunc 205 206 " vim: ts=8 sw=2 sts=2 tw=80 fdm=marker