blob: 7a2be899f6b2c0bf8a6981c829d6bc01ca8368f4 [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()
83 if !HasSignal('INT')
84 throw 'Skipped: INT signal not supported'
85 endif
86
87 " Skip the rest of the test when running with valgrind as signal INT is not
88 " received somehow by Vim when running with valgrind.
89 let cmd = GetVimCommand()
90 if cmd =~ 'valgrind'
91 throw 'Skipped: cannot test signal INT with valgrind'
92 endif
93
94 if !CanRunVimInTerminal()
95 throw 'Skipped: cannot run vim in terminal'
96 endif
97 let buf = RunVimInTerminal('', {'rows': 6})
98 let pid_vim = term_getjob(buf)->job_info().process
99
100 " Check that an endless loop in Vim is interrupted by signal INT.
101 call term_sendkeys(buf, ":while 1 | endwhile\n")
102 call WaitForAssert({-> assert_equal(':while 1 | endwhile', term_getline(buf, 6))})
103 exe 'silent !kill -s INT ' .. pid_vim
104 call term_sendkeys(buf, ":call setline(1, 'INTERUPTED')\n")
105 call WaitForAssert({-> assert_equal('INTERUPTED', term_getline(buf, 1))})
106
107 call StopVimInTerminal(buf)
108endfunc