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