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