xref: /vim-8.2.3635/src/testdir/test_signals.vim (revision a7251494)
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)
12  let signals = system('kill -l')
13  return signals =~# '\<' .. a:signal .. '\>'
14endfunc
15
16" Test signal WINCH (window resize signal)
17func Test_signal_WINCH()
18  CheckNotGui
19  if !HasSignal('WINCH')
20    throw 'Skipped: WINCH signal not supported'
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
35  exe 'set lines=' .. new_lines
36  exe 'set columns=' .. new_columns
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.
43  exe 'silent !kill -s WINCH ' .. getpid()
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
50endfunc
51
52" Test signal PWR, which should update the swap file.
53func Test_signal_PWR()
54  if !HasSignal('PWR')
55    throw 'Skipped: PWR signal not supported'
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&
79endfunc
80
81" Test signal INT. Handler sets got_int. It should be like typing CTRL-C.
82func Test_signal_INT()
83  CheckRunVimInTerminal
84  if !HasSignal('INT')
85    throw 'Skipped: INT signal not supported'
86  endif
87
88  " Skip the test when running with valgrind as signal INT is not received
89  " somehow by Vim when running with valgrind.
90  let cmd = GetVimCommand()
91  if cmd =~ 'valgrind'
92    throw 'Skipped: cannot test signal INT with valgrind'
93  endif
94
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)
106endfunc
107
108" Test a deadly signal.
109"
110" There are several deadly signals: SISEGV, SIBUS, SIGTERM...
111" Test uses signal SIGTERM as it does not create a core
112" dump file unlike SIGSEGV, SIGBUS, etc. See "man 7 signals.
113"
114" Vim should exit with a deadly signal and unsaved changes
115" should be recoverable from the swap file preserved as a
116" result of the deadly signal handler.
117func Test_deadly_signal_TERM()
118  if !HasSignal('TERM')
119    throw 'Skipped: TERM signal not supported'
120  endif
121  CheckRunVimInTerminal
122
123  " If test fails once, it can leave temporary files and trying to rerun
124  " the test would then fail again if they are not deleted first.
125  call delete('.Xsig_TERM.swp')
126  call delete('XsetupAucmd')
127  call delete('XautoOut')
128  let lines =<< trim END
129    au VimLeave * call writefile(["VimLeave triggered"], "XautoOut", "as")
130    au VimLeavePre * call writefile(["VimLeavePre triggered"], "XautoOut", "as")
131  END
132  call writefile(lines, 'XsetupAucmd')
133
134  let buf = RunVimInTerminal('-S XsetupAucmd Xsig_TERM', {'rows': 6})
135  let pid_vim = term_getjob(buf)->job_info().process
136
137  call term_sendkeys(buf, ":call setline(1, 'foo')\n")
138  call WaitForAssert({-> assert_equal('foo', term_getline(buf, 1))})
139
140  call assert_false(filereadable('Xsig_TERM'))
141  exe 'silent !kill -s TERM '  .. pid_vim
142  call WaitForAssert({-> assert_true(filereadable('.Xsig_TERM.swp'))})
143
144  " Don't call StopVimInTerminal() as it expects job to be still running.
145  call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})
146
147  new
148  silent recover .Xsig_TERM.swp
149  call assert_equal(['foo'], getline(1, '$'))
150
151  let result = readfile('XautoOut')
152  call assert_equal(["VimLeavePre triggered", "VimLeave triggered"], result)
153
154  %bwipe!
155  call delete('.Xsig_TERM.swp')
156  call delete('XsetupAucmd')
157  call delete('XautoOut')
158endfunc
159
160" vim: ts=8 sw=2 sts=2 tw=80 fdm=marker
161