blob: ea25ae1091aac509fbb22925daa35e3574ab6a00 [file] [log] [blame]
Bram Moolenaardb77b842019-03-24 14:58:31 +01001" Test signal handling.
2
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02003source check.vim
Bram Moolenaarbad88042020-03-23 20:54:32 +01004source term_util.vim
5
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02006CheckUnix
Bram Moolenaardb77b842019-03-24 14:58:31 +01007
Bram Moolenaardb77b842019-03-24 14:58:31 +01008source shared.vim
9
Bram Moolenaar520e2452019-04-27 17:32:40 +020010" Check whether a signal is available on this system.
11func HasSignal(signal)
12 let signals = system('kill -l')
13 return signals =~# '\<' .. a:signal .. '\>'
14endfunc
15
Bram Moolenaardb77b842019-03-24 14:58:31 +010016" Test signal WINCH (window resize signal)
17func Test_signal_WINCH()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +020018 CheckNotGui
19 if !HasSignal('WINCH')
20 throw 'Skipped: WINCH signal not supported'
Bram Moolenaardb77b842019-03-24 14:58:31 +010021 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 Moolenaar520e2452019-04-27 17:32:40 +020035 exe 'set lines=' .. new_lines
36 exe 'set columns=' .. new_columns
Bram Moolenaardb77b842019-03-24 14:58:31 +010037 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 Moolenaar520e2452019-04-27 17:32:40 +020043 exe 'silent !kill -s WINCH ' .. getpid()
Bram Moolenaardb77b842019-03-24 14:58:31 +010044 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
50endfunc
Bram Moolenaar520e2452019-04-27 17:32:40 +020051
52" Test signal PWR, which should update the swap file.
53func Test_signal_PWR()
54 if !HasSignal('PWR')
Bram Moolenaarbad88042020-03-23 20:54:32 +010055 throw 'Skipped: PWR signal not supported'
Bram Moolenaar520e2452019-04-27 17:32:40 +020056 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&
79endfunc
Bram Moolenaarbad88042020-03-23 20:54:32 +010080
81" Test signal INT. Handler sets got_int. It should be like typing CTRL-C.
82func Test_signal_INT()
Bram Moolenaar494e9062020-05-31 21:28:02 +020083 CheckRunVimInTerminal
Bram Moolenaarbad88042020-03-23 20:54:32 +010084 if !HasSignal('INT')
85 throw 'Skipped: INT signal not supported'
86 endif
87
Bram Moolenaarbad88042020-03-23 20:54:32 +010088 let buf = RunVimInTerminal('', {'rows': 6})
89 let pid_vim = term_getjob(buf)->job_info().process
90
91 " Check that an endless loop in Vim is interrupted by signal INT.
Bram Moolenaar61e37842022-06-25 12:13:28 +010092 call term_sendkeys(buf, ":call setline(1, 'running')\n")
Bram Moolenaarbad88042020-03-23 20:54:32 +010093 call term_sendkeys(buf, ":while 1 | endwhile\n")
94 call WaitForAssert({-> assert_equal(':while 1 | endwhile', term_getline(buf, 6))})
95 exe 'silent !kill -s INT ' .. pid_vim
Bram Moolenaar61e37842022-06-25 12:13:28 +010096 sleep 50m
Dominique Pelle81b573d2022-03-22 21:14:55 +000097 call term_sendkeys(buf, ":call setline(1, 'INTERRUPTED')\n")
98 call WaitForAssert({-> assert_equal('INTERRUPTED', term_getline(buf, 1))})
Bram Moolenaarbad88042020-03-23 20:54:32 +010099
100 call StopVimInTerminal(buf)
101endfunc
Bram Moolenaar48a68712020-05-12 14:42:02 +0200102
dbivolaruab16ad32021-12-29 19:41:47 +0000103" Test signal TSTP. Handler sets got_tstp.
104func Test_signal_TSTP()
105 CheckRunVimInTerminal
106 if !HasSignal('TSTP')
107 throw 'Skipped: TSTP signal not supported'
108 endif
109
dbivolaruab16ad32021-12-29 19:41:47 +0000110 " If test fails once, it can leave temporary files and trying to rerun
111 " the test would then fail again if they are not deleted first.
112 call delete('.Xsig_TERM.swp')
113 call delete('XsetupAucmd')
K.Takataa9480db2022-09-06 10:56:19 +0100114 call delete('XautoOut1')
115 call delete('XautoOut2')
dbivolaruab16ad32021-12-29 19:41:47 +0000116 let lines =<< trim END
K.Takataa9480db2022-09-06 10:56:19 +0100117 au VimSuspend * call writefile(["VimSuspend triggered"], "XautoOut1", "as")
118 au VimResume * call writefile(["VimResume triggered"], "XautoOut2", "as")
dbivolaruab16ad32021-12-29 19:41:47 +0000119 END
120 call writefile(lines, 'XsetupAucmd')
121
122 let buf = RunVimInTerminal('-S XsetupAucmd Xsig_TERM', {'rows': 6})
123 let pid_vim = term_getjob(buf)->job_info().process
124
125 call term_sendkeys(buf, ":call setline(1, 'foo')\n")
126 call WaitForAssert({-> assert_equal('foo', term_getline(buf, 1))})
127
128 call assert_false(filereadable('Xsig_TERM'))
129
130 " After TSTP the file is not saved (same function as ^Z)
131 exe 'silent !kill -s TSTP ' .. pid_vim
132 call WaitForAssert({-> assert_true(filereadable('.Xsig_TERM.swp'))})
K.Takataa9480db2022-09-06 10:56:19 +0100133 sleep 100m
dbivolaruab16ad32021-12-29 19:41:47 +0000134
Bram Moolenaar61e37842022-06-25 12:13:28 +0100135 " We resume after the suspend. Sleep a bit for the signal to take effect,
136 " also when running under valgrind.
dbivolaruab16ad32021-12-29 19:41:47 +0000137 exe 'silent !kill -s CONT ' .. pid_vim
K.Takataa9480db2022-09-06 10:56:19 +0100138 call WaitForAssert({-> assert_true(filereadable('XautoOut2'))})
139 sleep 10m
dbivolaruab16ad32021-12-29 19:41:47 +0000140
141 call StopVimInTerminal(buf)
142
K.Takataa9480db2022-09-06 10:56:19 +0100143 let result = readfile('XautoOut1')
144 call assert_equal(["VimSuspend triggered"], result)
145 let result = readfile('XautoOut2')
146 call assert_equal(["VimResume triggered"], result)
dbivolaruab16ad32021-12-29 19:41:47 +0000147
148 %bwipe!
149 call delete('.Xsig_TERM.swp')
150 call delete('XsetupAucmd')
K.Takataa9480db2022-09-06 10:56:19 +0100151 call delete('XautoOut1')
152 call delete('XautoOut2')
dbivolaruab16ad32021-12-29 19:41:47 +0000153endfunc
154
Bram Moolenaar48a68712020-05-12 14:42:02 +0200155" 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()
165 if !HasSignal('TERM')
166 throw 'Skipped: TERM signal not supported'
167 endif
Bram Moolenaar494e9062020-05-31 21:28:02 +0200168 CheckRunVimInTerminal
Bram Moolenaard14fd522020-06-01 15:05:19 +0200169
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')
Bram Moolenaar129d6bf2020-05-16 16:08:35 +0200175 let lines =<< trim END
Bram Moolenaard14fd522020-06-01 15:05:19 +0200176 au VimLeave * call writefile(["VimLeave triggered"], "XautoOut", "as")
177 au VimLeavePre * call writefile(["VimLeavePre triggered"], "XautoOut", "as")
Bram Moolenaar129d6bf2020-05-16 16:08:35 +0200178 END
179 call writefile(lines, 'XsetupAucmd')
Bram Moolenaar48a68712020-05-12 14:42:02 +0200180
Bram Moolenaar129d6bf2020-05-16 16:08:35 +0200181 let buf = RunVimInTerminal('-S XsetupAucmd Xsig_TERM', {'rows': 6})
Bram Moolenaar48a68712020-05-12 14:42:02 +0200182 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
Bram Moolenaar48a68712020-05-12 14:42:02 +0200189 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
Bram Moolenaar129d6bf2020-05-16 16:08:35 +0200198 let result = readfile('XautoOut')
Bram Moolenaar67322bf2020-12-06 15:03:19 +0100199 call assert_equal(["VimLeavePre triggered", "VimLeave triggered"], result)
Bram Moolenaar129d6bf2020-05-16 16:08:35 +0200200
Bram Moolenaar48a68712020-05-12 14:42:02 +0200201 %bwipe!
202 call delete('.Xsig_TERM.swp')
Bram Moolenaar129d6bf2020-05-16 16:08:35 +0200203 call delete('XsetupAucmd')
204 call delete('XautoOut')
Bram Moolenaar48a68712020-05-12 14:42:02 +0200205endfunc
206
207" vim: ts=8 sw=2 sts=2 tw=80 fdm=marker