blob: ee50b570b29f32ee0da89588ed14ad98f9e7ff6d [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarea5d6fa2017-08-18 21:07:11 +02003if !has('terminal')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02004 finish
5endif
6
7source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01008source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02009
Bram Moolenaarb81bc772017-08-11 22:45:01 +020010let s:python = PythonProg()
11
Bram Moolenaar94053a52017-08-01 21:44:33 +020012" Open a terminal with a shell, assign the job to g:job and return the buffer
13" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020014func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010015 if has('win32')
16 let buf = term_start([&shell,'/k'], a:options)
17 else
18 let buf = term_start(&shell, a:options)
19 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020020
21 let termlist = term_list()
22 call assert_equal(1, len(termlist))
23 call assert_equal(buf, termlist[0])
24
25 let g:job = term_getjob(buf)
26 call assert_equal(v:t_job, type(g:job))
27
Bram Moolenaar35422f42017-08-05 16:33:56 +020028 let string = string({'job': term_getjob(buf)})
29 call assert_match("{'job': 'process \\d\\+ run'}", string)
30
Bram Moolenaar94053a52017-08-01 21:44:33 +020031 return buf
32endfunc
33
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020035 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020036 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020037
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020038 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020039 call assert_match('^/dev/', job_info(g:job).tty_out)
40 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020041 else
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010042 " ConPTY works on anonymous pipe.
43 if !has('conpty')
44 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
45 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
46 endif
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020047 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020048 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020049 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020050 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020051 call assert_match('%aR[^\n]*running]', execute('ls R'))
52 call assert_notmatch('%[^\n]*running]', execute('ls F'))
53 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054
Bram Moolenaar94053a52017-08-01 21:44:33 +020055 call Stop_shell_in_terminal(buf)
56 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020057 call assert_equal('n', mode())
58 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020059 call assert_match('%aF[^\n]*finished]', execute('ls F'))
60 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
61 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020062
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 " closing window wipes out the terminal buffer a with finished job
64 close
65 call assert_equal("", bufname(buf))
66
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020067 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020068 unlet g:job
69endfunc
70
71func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020072 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020073 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020074 call term_wait(buf)
75
76 setlocal modifiable
77 exe "normal Axxx\<Esc>"
78 call assert_fails(buf . 'bwipe', 'E517')
79 undo
80
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020081 exe buf . 'bwipe'
82 unlet g:job
83endfunc
84
Bram Moolenaar94053a52017-08-01 21:44:33 +020085func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020086 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020087 call assert_fails(buf . 'bwipe', 'E517')
88 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +020089 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +020090 call assert_equal("", bufname(buf))
91
92 unlet g:job
93endfunc
94
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020095func Test_terminal_split_quit()
96 let buf = Run_shell_in_terminal({})
97 call term_wait(buf)
98 split
99 quit!
100 call term_wait(buf)
101 sleep 50m
102 call assert_equal('run', job_status(g:job))
103
104 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200105 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200106
107 exe buf . 'bwipe'
108 unlet g:job
109endfunc
110
Bram Moolenaar94053a52017-08-01 21:44:33 +0200111func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200112 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200113 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200114 quit
115 for nr in range(1, winnr('$'))
116 call assert_notequal(winbufnr(nr), buf)
117 endfor
118 call assert_true(bufloaded(buf))
119 call assert_true(buflisted(buf))
120
121 exe 'split ' . buf . 'buf'
122 call Stop_shell_in_terminal(buf)
123 exe buf . 'bwipe'
124
125 unlet g:job
126endfunc
127
Bram Moolenaar1e115362019-01-09 23:01:02 +0100128func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200129 exe g:buf . 'bwipe!'
130 let g:buf = 0
131endfunc
132
Bram Moolenaar9d189612017-09-09 18:11:00 +0200133func Get_cat_123_cmd()
134 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100135 if !has('conpty')
136 return 'cmd /c "cls && color 2 && echo 123"'
137 else
138 " When clearing twice, extra sequence is not output.
139 return 'cmd /c "cls && cls && color 2 && echo 123"'
140 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200141 else
142 call writefile(["\<Esc>[32m123"], 'Xtext')
143 return "cat Xtext"
144 endif
145endfunc
146
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200147func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200148 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200149 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
150 let g:job = term_getjob(g:buf)
151
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200152 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
153 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200154 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100155 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200156 call delete('Xtext')
157endfunc
158
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200159func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200160 let l = term_scrape(a:buf, 0)
161 call assert_true(len(l) == 0)
162 let l = term_scrape(a:buf, 999)
163 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200164 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200165 call assert_true(len(l) > 0)
166 call assert_equal('1', l[0].chars)
167 call assert_equal('2', l[1].chars)
168 call assert_equal('3', l[2].chars)
169 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar81df6352018-12-22 18:25:30 +0100170 if has('win32')
171 " On Windows 'background' always defaults to dark, even though the terminal
172 " may use a light background. Therefore accept both white and black.
173 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200174 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100175 if &background == 'light'
176 call assert_equal('#ffffff', l[0].bg)
177 else
178 call assert_equal('#000000', l[0].bg)
179 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200180 endif
181
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200182 let l = term_getline(a:buf, -1)
183 call assert_equal('', l)
184 let l = term_getline(a:buf, 0)
185 call assert_equal('', l)
186 let l = term_getline(a:buf, 999)
187 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200188 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200189 call assert_equal('123', l)
190endfunc
191
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200192func Test_terminal_scrape_123()
193 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200194 let buf = term_start(cmd)
195
196 let termlist = term_list()
197 call assert_equal(1, len(termlist))
198 call assert_equal(buf, termlist[0])
199
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200200 " Nothing happens with invalid buffer number
201 call term_wait(1234)
202
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200203 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200204 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200205 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200206 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200207 call Check_123(buf)
208
209 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100210 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200211 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200212 call term_wait(buf)
213 call Check_123(buf)
214
215 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200216 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200217endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200218
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200219func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200220 call writefile(["léttまrs"], 'Xtext')
221 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200222 " Run cmd with UTF-8 codepage to make the type command print the expected
223 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100224 let buf = term_start("cmd /K chcp 65001")
225 call term_sendkeys(buf, "type Xtext\<CR>")
226 call term_sendkeys(buf, "exit\<CR>")
227 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200228 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100229 let buf = term_start("cat Xtext")
230 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200231 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200232
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100233 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
234 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200235 call assert_true(len(l) >= 7)
236 call assert_equal('l', l[0].chars)
237 call assert_equal('é', l[1].chars)
238 call assert_equal(1, l[1].width)
239 call assert_equal('t', l[2].chars)
240 call assert_equal('t', l[3].chars)
241 call assert_equal('ま', l[4].chars)
242 call assert_equal(2, l[4].width)
243 call assert_equal('r', l[5].chars)
244 call assert_equal('s', l[6].chars)
245
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100246 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200247 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100248 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200249
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100250 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200251 call delete('Xtext')
252endfunc
253
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200254func Test_terminal_scroll()
255 call writefile(range(1, 200), 'Xtext')
256 if has('win32')
257 let cmd = 'cmd /c "type Xtext"'
258 else
259 let cmd = "cat Xtext"
260 endif
261 let buf = term_start(cmd)
262
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200264 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200265 call term_wait(buf)
266 if has('win32')
267 " TODO: this should not be needed
268 sleep 100m
269 endif
270
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200271 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200272 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200273 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200274 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200275 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200276 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200277 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200278
279 exe buf . 'bwipe'
280 call delete('Xtext')
281endfunc
282
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200283func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200284 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200285 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200286 call writefile(range(150), 'Xtext')
287 if has('win32')
288 call term_sendkeys(buf, "type Xtext\<CR>")
289 else
290 call term_sendkeys(buf, "cat Xtext\<CR>")
291 endif
292 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200293 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200294 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200295 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200296 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200297
298 call Stop_shell_in_terminal(buf)
299 call term_wait(buf)
300 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200301 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100302 call delete('Xtext')
303endfunc
304
305func Test_terminal_postponed_scrollback()
306 if !has('unix')
307 " tail -f only works on Unix
308 return
309 endif
310
311 call writefile(range(50), 'Xtext')
312 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100313 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100314 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100315 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100316 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
317 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100318 \ 'call feedkeys("\<C-W>N", "xt")',
319 \ ], 'XTest_postponed')
320 let buf = RunVimInTerminal('-S XTest_postponed', {})
321 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaar96baf022019-02-14 23:49:38 +0100322 call VerifyScreenDump(buf, 'Test_terminal_01', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100323
324 silent !echo 'one more line' >>Xtext
325 " Sceen will not change, move cursor to get a different dump
326 call term_sendkeys(buf, "k")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100327 call VerifyScreenDump(buf, 'Test_terminal_02', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100328
329 " Back to Terminal-Job mode, text will scroll and show the extra line.
330 call term_sendkeys(buf, "a")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100331 call VerifyScreenDump(buf, 'Test_terminal_03', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100332
333 call term_wait(buf)
334 call term_sendkeys(buf, "\<C-C>")
335 call term_wait(buf)
336 call term_sendkeys(buf, "exit\<CR>")
337 call term_wait(buf)
338 call term_sendkeys(buf, ":q\<CR>")
339 call StopVimInTerminal(buf)
340 call delete('XTest_postponed')
341 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200342endfunc
343
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100344" Run diff on two dumps with different size.
345func Test_terminal_dumpdiff_size()
346 call assert_equal(1, winnr('$'))
347 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
348 call assert_equal(2, winnr('$'))
349 call assert_match('Test_incsearch_search_01.dump', getline(10))
350 call assert_match(' +++++$', getline(11))
351 call assert_match('Test_popup_command_01.dump', getline(31))
352 call assert_equal(repeat('+', 75), getline(30))
353 quit
354endfunc
355
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200356func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200357 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200358
Bram Moolenaarb2412082017-08-20 18:09:14 +0200359 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200360 let size = term_getsize('')
361 bwipe!
362 call assert_equal(5, size[0])
363
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200364 call term_start(cmd, {'term_rows': 6})
365 let size = term_getsize('')
366 bwipe!
367 call assert_equal(6, size[0])
368
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200369 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200370 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200371 call assert_equal([5, 33], term_getsize(''))
372
373 call term_setsize('', 6, 0)
374 call assert_equal([6, 33], term_getsize(''))
375
376 call term_setsize('', 0, 35)
377 call assert_equal([6, 35], term_getsize(''))
378
379 call term_setsize('', 7, 30)
380 call assert_equal([7, 30], term_getsize(''))
381
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200382 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200383 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200384
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200385 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
386 let size = term_getsize('')
387 bwipe!
388 call assert_equal([6, 36], size)
389
Bram Moolenaarb2412082017-08-20 18:09:14 +0200390 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200391 let size = term_getsize('')
392 bwipe!
393 call assert_equal(20, size[1])
394
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200395 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
396 let size = term_getsize('')
397 bwipe!
398 call assert_equal(26, size[1])
399
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200400 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200401 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200402 let size = term_getsize('')
403 bwipe!
404 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200405
406 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
407 let size = term_getsize('')
408 bwipe!
409 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200410
411 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200412endfunc
413
414func Test_terminal_curwin()
415 let cmd = Get_cat_123_cmd()
416 call assert_equal(1, winnr('$'))
417
418 split dummy
419 exe 'terminal ++curwin ' . cmd
420 call assert_equal(2, winnr('$'))
421 bwipe!
422
423 split dummy
424 call term_start(cmd, {'curwin': 1})
425 call assert_equal(2, winnr('$'))
426 bwipe!
427
428 split dummy
429 call setline(1, 'change')
430 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
431 call assert_equal(2, winnr('$'))
432 exe 'terminal! ++curwin ' . cmd
433 call assert_equal(2, winnr('$'))
434 bwipe!
435
436 split dummy
437 call setline(1, 'change')
438 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
439 call assert_equal(2, winnr('$'))
440 bwipe!
441
442 split dummy
443 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200444 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200445endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200446
Bram Moolenaarff546792017-11-21 14:47:57 +0100447func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200448 if s:python != ''
449 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200450 " 500 was not enough for Travis
451 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200452 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200453 echo 'This will take five seconds...'
454 let waittime = 2000
455 if has('win32')
456 let cmd = $windir . '\system32\timeout.exe 1'
457 else
458 let cmd = 'sleep 1'
459 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200460 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100461 return [cmd, waittime]
462endfunc
463
464func Test_terminal_finish_open_close()
465 call assert_equal(1, winnr('$'))
466
467 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200468
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100469 " shell terminal closes automatically
470 terminal
471 let buf = bufnr('%')
472 call assert_equal(2, winnr('$'))
473 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200474 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100475 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200476 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100477
478 " shell terminal that does not close automatically
479 terminal ++noclose
480 let buf = bufnr('%')
481 call assert_equal(2, winnr('$'))
482 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200483 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100484 call Stop_shell_in_terminal(buf)
485 call assert_equal(2, winnr('$'))
486 quit
487 call assert_equal(1, winnr('$'))
488
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200489 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200490 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200491 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200492 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200493
494 call term_start(cmd, {'term_finish': 'close'})
495 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200496 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200497 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200498 call assert_equal(1, winnr('$'))
499
500 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200501 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200502 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200503 bwipe
504
505 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200506 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200507 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200508 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200509
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200510 exe 'terminal ++hidden ++open ' . cmd
511 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200512 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200513 bwipe
514
515 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
516 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200517 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200518 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200519
520 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
521 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
522 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
523 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
524
525 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200526 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200527 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200528 call assert_equal(4, winheight(0))
529 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200530endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200531
532func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200533 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200534 return
535 endif
536 call mkdir('Xdir')
537 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200538 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200539
540 exe buf . 'bwipe'
541 call delete('Xdir', 'rf')
542endfunc
543
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200544func Test_terminal_cwd_failure()
545 " Case 1: Provided directory is not actually a directory. Attempt to make
546 " the file executable as well.
547 call writefile([], 'Xfile')
548 call setfperm('Xfile', 'rwx------')
549 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
550 call delete('Xfile')
551
552 " Case 2: Directory does not exist.
553 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
554
555 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100556 " Skip this for root, it will be accessible anyway.
557 if $USER != 'root'
558 call mkdir('XdirNoAccess', '', '0600')
559 " return early if the directory permissions could not be set properly
560 if getfperm('XdirNoAccess')[2] == 'x'
561 call delete('XdirNoAccess', 'rf')
562 return
563 endif
564 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
565 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200566 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200567endfunc
568
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100569func Test_terminal_servername()
570 if !has('clientserver')
571 return
572 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200573 call s:test_environment("VIM_SERVERNAME", v:servername)
574endfunc
575
576func Test_terminal_version()
577 call s:test_environment("VIM_TERMINAL", string(v:version))
578endfunc
579
580func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100581 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100582 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200583 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100584 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200585 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100586 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200587 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100588 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100589 call term_wait(buf)
590 call Stop_shell_in_terminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200591 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100592
Bram Moolenaar012eb662018-03-13 17:55:27 +0100593 exe buf . 'bwipe'
594 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100595endfunc
596
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200597func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100598 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200599 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200600 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100601 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100602 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100603 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100604 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100605 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100606 call term_wait(buf)
607 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200608 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200609
Bram Moolenaar012eb662018-03-13 17:55:27 +0100610 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200611endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200612
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200613func Test_terminal_list_args()
614 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
615 call assert_fails(buf . 'bwipe', 'E517')
616 exe buf . 'bwipe!'
617 call assert_equal("", bufname(buf))
618endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200619
620func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100621 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100622 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200623 " The shell or something else has a problem dealing with more than 1000
624 " characters at the same time.
625 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100626 " NPFS is used in Windows, nonblocking mode does not work properly.
627 elseif has('win32')
628 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200629 else
630 let len = 5000
631 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200632
633 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100634 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200635 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100636 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200637
638 " On MS-Windows there is an extra empty line below "done". Find "done" in
639 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100640 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100641 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100642 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200643 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100644 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200645 endif
646 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200647
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100648 let g:job = term_getjob(buf)
649 call Stop_shell_in_terminal(buf)
650 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200651 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200652 bwipe
653endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200654
655func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200656 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200657 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200658 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200659 if has('win32')
660 " TODO: enable once writing to stdin works on MS-Windows
661 return
662 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200663 new
664 call setline(1, ['one', 'two', 'three'])
665 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200666 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200667 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200668 call assert_equal(['3', '3', '14'], nrs)
669 bwipe
670
Bram Moolenaardada6d22017-09-02 17:18:35 +0200671 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200672 call setline(1, ['one', 'two', 'three', 'four'])
673 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200674 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200675 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200676 call assert_equal(['2', '2', '10'], nrs)
677 bwipe
678
Bram Moolenaardada6d22017-09-02 17:18:35 +0200679 if executable('python')
680 new
681 call setline(1, ['print("hello")'])
682 1term ++eof=exit() python
683 " MS-Windows echoes the input, Unix doesn't.
684 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
685 if getline(1) =~ 'hello'
686 call assert_equal('hello', getline(1))
687 else
688 call assert_equal('hello', getline(line('$') - 1))
689 endif
690 bwipe
691
692 if has('win32')
693 new
694 call setline(1, ['print("hello")'])
695 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200696 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200697 call assert_equal('hello', getline(line('$') - 1))
698 bwipe
699 endif
700 endif
701
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200702 bwipe!
703endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200704
705func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200706 let buf = term_start('NONE', {})
707 call assert_notequal(0, buf)
708
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200709 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200710 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100711 if has('gui_running') && !has('win32')
712 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200713 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100714 else
715 " Otherwise using a job works on all systems.
716 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200717 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200718 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200719
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200720 bwipe!
721endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200722
723func Test_terminal_special_chars()
724 " this file name only works on Unix
725 if !has('unix')
726 return
727 endif
728 call mkdir('Xdir with spaces')
729 call writefile(['x'], 'Xdir with spaces/quoted"file')
730 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200731 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200732 call term_wait('')
733
734 call delete('Xdir with spaces', 'rf')
735 bwipe
736endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200737
738func Test_terminal_wrong_options()
739 call assert_fails('call term_start(&shell, {
740 \ "in_io": "file",
741 \ "in_name": "xxx",
742 \ "out_io": "file",
743 \ "out_name": "xxx",
744 \ "err_io": "file",
745 \ "err_name": "xxx"
746 \ })', 'E474:')
747 call assert_fails('call term_start(&shell, {
748 \ "out_buf": bufnr("%")
749 \ })', 'E474:')
750 call assert_fails('call term_start(&shell, {
751 \ "err_buf": bufnr("%")
752 \ })', 'E474:')
753endfunc
754
755func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200756 let cmd = Get_cat_123_cmd()
757 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
758 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100759 " ConPTY may precede escape sequence. There are things that are not so.
760 if !has('conpty')
761 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
762 call assert_match('123', readfile('Xfile')[0])
763 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200764 let g:job = term_getjob(buf)
765 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
766 call delete('Xfile')
767 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200768
769 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200770 call writefile(['one line'], 'Xfile')
771 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
772 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200773 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200774 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200775 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200776 bwipe
777 call delete('Xfile')
778 endif
779endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200780
781func TerminalTmap(remap)
782 let buf = Run_shell_in_terminal({})
783 call assert_equal('t', mode())
784
785 if a:remap
786 tmap 123 456
787 else
788 tnoremap 123 456
789 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100790 " don't use abcde, it's an existing command
791 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200792 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100793 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200794 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200795 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200796 let lnum = term_getcursor(buf)[0]
797 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100798 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200799 else
800 call assert_match('456', term_getline(buf, lnum))
801 endif
802
803 call term_sendkeys(buf, "\r")
804 call Stop_shell_in_terminal(buf)
805 call term_wait(buf)
806
807 tunmap 123
808 tunmap 456
809 call assert_equal('', maparg('123', 't'))
810 close
811 unlet g:job
812endfunc
813
814func Test_terminal_tmap()
815 call TerminalTmap(1)
816 call TerminalTmap(0)
817endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200818
819func Test_terminal_wall()
820 let buf = Run_shell_in_terminal({})
821 wall
822 call Stop_shell_in_terminal(buf)
823 call term_wait(buf)
824 exe buf . 'bwipe'
825 unlet g:job
826endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200827
Bram Moolenaar7a760922018-02-19 23:10:02 +0100828func Test_terminal_wqall()
829 let buf = Run_shell_in_terminal({})
830 call assert_fails('wqall', 'E948')
831 call Stop_shell_in_terminal(buf)
832 call term_wait(buf)
833 exe buf . 'bwipe'
834 unlet g:job
835endfunc
836
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200837func Test_terminal_composing_unicode()
838 let save_enc = &encoding
839 set encoding=utf-8
840
841 if has('win32')
842 let cmd = "cmd /K chcp 65001"
843 let lnum = [3, 6, 9]
844 else
845 let cmd = &shell
846 let lnum = [1, 3, 5]
847 endif
848
849 enew
850 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100851 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200852 call term_wait(buf, 50)
853
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200854 if has('win32')
855 call assert_equal('cmd', job_info(g:job).cmd[0])
856 else
857 call assert_equal(&shell, job_info(g:job).cmd[0])
858 endif
859
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200860 " ascii + composing
861 let txt = "a\u0308bc"
862 call term_sendkeys(buf, "echo " . txt . "\r")
863 call term_wait(buf, 50)
864 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
865 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
866 let l = term_scrape(buf, lnum[0] + 1)
867 call assert_equal("a\u0308", l[0].chars)
868 call assert_equal("b", l[1].chars)
869 call assert_equal("c", l[2].chars)
870
871 " multibyte + composing
872 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
873 call term_sendkeys(buf, "echo " . txt . "\r")
874 call term_wait(buf, 50)
875 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
876 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
877 let l = term_scrape(buf, lnum[1] + 1)
878 call assert_equal("\u304b\u3099", l[0].chars)
879 call assert_equal("\u304e", l[1].chars)
880 call assert_equal("\u304f\u3099", l[2].chars)
881 call assert_equal("\u3052", l[3].chars)
882 call assert_equal("\u3053\u3099", l[4].chars)
883
884 " \u00a0 + composing
885 let txt = "abc\u00a0\u0308"
886 call term_sendkeys(buf, "echo " . txt . "\r")
887 call term_wait(buf, 50)
888 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
889 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
890 let l = term_scrape(buf, lnum[2] + 1)
891 call assert_equal("\u00a0\u0308", l[3].chars)
892
893 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200894 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200895 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100896 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200897 let &encoding = save_enc
898endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100899
900func Test_terminal_aucmd_on_close()
901 fun Nop()
902 let s:called = 1
903 endfun
904
905 aug repro
906 au!
907 au BufWinLeave * call Nop()
908 aug END
909
910 let [cmd, waittime] = s:get_sleep_cmd()
911
912 call assert_equal(1, winnr('$'))
913 new
914 call setline(1, ['one', 'two'])
915 exe 'term ++close ' . cmd
916 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200917 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100918 call assert_equal(1, s:called)
919 bwipe!
920
921 unlet s:called
922 au! repro
923 delfunc Nop
924endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100925
926func Test_terminal_term_start_empty_command()
927 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
928 call assert_fails(cmd, 'E474')
929 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
930 call assert_fails(cmd, 'E474')
931 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
932 call assert_fails(cmd, 'E474')
933 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
934 call assert_fails(cmd, 'E474')
935endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100936
937func Test_terminal_response_to_control_sequence()
938 if !has('unix')
939 return
940 endif
941
942 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200943 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100944
Bram Moolenaar086eb872018-03-25 21:24:12 +0200945 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200946 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100947
Bram Moolenaar086eb872018-03-25 21:24:12 +0200948 " Request the cursor position.
949 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100950
951 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200952 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100953
Bram Moolenaar086eb872018-03-25 21:24:12 +0200954 " End "cat" gently.
955 call term_sendkeys(buf, "\<CR>\<C-D>")
956
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100957 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100958 exe buf . 'bwipe'
959 unlet g:job
960endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100961
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100962" Run Vim, start a terminal in that Vim with the kill argument,
963" :qall works.
964func Run_terminal_qall_kill(line1, line2)
965 " 1. Open a terminal window and wait for the prompt to appear
966 " 2. set kill using term_setkill()
967 " 3. make Vim exit, it will kill the shell
968 let after = [
969 \ a:line1,
970 \ 'let buf = bufnr("%")',
971 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
972 \ ' sleep 10m',
973 \ 'endwhile',
974 \ a:line2,
975 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
976 \ 'qall',
977 \ ]
978 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100979 return
980 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100981 call assert_equal("done", readfile("Xdone")[0])
982 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100983endfunc
984
985" Run Vim in a terminal, then start a terminal in that Vim with a kill
986" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100987func Test_terminal_qall_kill_arg()
988 call Run_terminal_qall_kill('term ++kill=kill', '')
989endfunc
990
991" Run Vim, start a terminal in that Vim, set the kill argument with
992" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100993func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100994 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
995endfunc
996
997" Run Vim, start a terminal in that Vim without the kill argument,
998" check that :qall does not exit, :qall! does.
999func Test_terminal_qall_exit()
1000 let after = [
1001 \ 'term',
1002 \ 'let buf = bufnr("%")',
1003 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1004 \ ' sleep 10m',
1005 \ 'endwhile',
1006 \ 'set nomore',
1007 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
1008 \ 'qall',
1009 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
1010 \ 'cquit',
1011 \ ]
1012 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001013 return
1014 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001015 call assert_equal("done", readfile("Xdone")[0])
1016 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001017endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001018
1019" Run Vim in a terminal, then start a terminal in that Vim without a kill
1020" argument, check that :confirm qall works.
1021func Test_terminal_qall_prompt()
1022 if !CanRunVimInTerminal()
1023 return
1024 endif
1025 let buf = RunVimInTerminal('', {})
1026
1027 " Open a terminal window and wait for the prompt to appear
1028 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001029 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1030 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001031
1032 " make Vim exit, it will prompt to kill the shell
1033 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001034 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001035 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001036 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001037
1038 " close the terminal window where Vim was running
1039 quit
1040endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001041
Bram Moolenaar012eb662018-03-13 17:55:27 +01001042func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001043 augroup repro
1044 au!
1045 au TerminalOpen * let s:called += 1
1046 augroup END
1047
1048 let s:called = 0
1049
1050 " Open a terminal window with :terminal
1051 terminal
1052 call assert_equal(1, s:called)
1053 bwipe!
1054
1055 " Open a terminal window with term_start()
1056 call term_start(&shell)
1057 call assert_equal(2, s:called)
1058 bwipe!
1059
1060 " Open a hidden terminal buffer with :terminal
1061 terminal ++hidden
1062 call assert_equal(3, s:called)
1063 for buf in term_list()
1064 exe buf . "bwipe!"
1065 endfor
1066
1067 " Open a hidden terminal buffer with term_start()
1068 let buf = term_start(&shell, {'hidden': 1})
1069 call assert_equal(4, s:called)
1070 exe buf . "bwipe!"
1071
1072 unlet s:called
1073 au! repro
1074endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001075
1076func Check_dump01(off)
1077 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1078 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001079 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001080endfunc
1081
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001082func Test_terminal_dumpwrite_composing()
1083 if !CanRunVimInTerminal()
1084 return
1085 endif
1086 let save_enc = &encoding
1087 set encoding=utf-8
1088 call assert_equal(1, winnr('$'))
1089
1090 let text = " a\u0300 e\u0302 o\u0308"
1091 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001092 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001093 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001094 call term_dumpwrite(buf, 'Xdump')
1095 let dumpline = readfile('Xdump')[0]
1096 call assert_match('|à| |ê| |ö', dumpline)
1097
1098 call StopVimInTerminal(buf)
1099 call delete('Xcomposing')
1100 call delete('Xdump')
1101 let &encoding = save_enc
1102endfunc
1103
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001104" just testing basic functionality.
1105func Test_terminal_dumpload()
1106 call assert_equal(1, winnr('$'))
1107 call term_dumpload('dumps/Test_popup_command_01.dump')
1108 call assert_equal(2, winnr('$'))
1109 call assert_equal(20, line('$'))
1110 call Check_dump01(0)
1111 quit
1112endfunc
1113
1114func Test_terminal_dumpdiff()
1115 call assert_equal(1, winnr('$'))
1116 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1117 call assert_equal(2, winnr('$'))
1118 call assert_equal(62, line('$'))
1119 call Check_dump01(0)
1120 call Check_dump01(42)
1121 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1122 quit
1123endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001124
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001125func Test_terminal_dumpdiff_swap()
1126 call assert_equal(1, winnr('$'))
1127 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1128 call assert_equal(2, winnr('$'))
1129 call assert_equal(62, line('$'))
1130 call assert_match('Test_popup_command_01.dump', getline(21))
1131 call assert_match('Test_popup_command_03.dump', getline(42))
1132 call assert_match('Undo', getline(3))
1133 call assert_match('three four five', getline(45))
1134
1135 normal s
1136 call assert_match('Test_popup_command_03.dump', getline(21))
1137 call assert_match('Test_popup_command_01.dump', getline(42))
1138 call assert_match('three four five', getline(3))
1139 call assert_match('Undo', getline(45))
1140 quit
1141endfunc
1142
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001143func Test_terminal_dumpdiff_options()
1144 set laststatus=0
1145 call assert_equal(1, winnr('$'))
1146 let height = winheight(0)
1147 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1148 call assert_equal(2, winnr('$'))
1149 call assert_equal(height, winheight(winnr()))
1150 call assert_equal(33, winwidth(winnr()))
1151 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1152 quit
1153
1154 call assert_equal(1, winnr('$'))
1155 let width = winwidth(0)
1156 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1157 call assert_equal(2, winnr('$'))
1158 call assert_equal(width, winwidth(winnr()))
1159 call assert_equal(13, winheight(winnr()))
1160 call assert_equal('something else', bufname('%'))
1161 quit
1162
1163 call assert_equal(1, winnr('$'))
1164 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1165 call assert_equal(1, winnr('$'))
1166 bwipe
1167
1168 set laststatus&
1169endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001170
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001171func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001172 call assert_equal(1, winnr('$'))
1173
1174 " Use the title termcap entries to output the escape sequence.
1175 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001176 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001177 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001178 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001179 \ 'redraw',
1180 \ "set t_ts=",
1181 \ ], 'Xscript')
1182 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001183 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001184 call assert_equal('Xtextfile', expand('%:t'))
1185 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001186 return buf
1187endfunc
1188
1189func Test_terminal_api_drop_newwin()
1190 if !CanRunVimInTerminal()
1191 return
1192 endif
1193 let buf = Api_drop_common('')
1194 call assert_equal(0, &bin)
1195 call assert_equal('', &fenc)
1196
1197 call StopVimInTerminal(buf)
1198 call delete('Xscript')
1199 bwipe Xtextfile
1200endfunc
1201
1202func Test_terminal_api_drop_newwin_bin()
1203 if !CanRunVimInTerminal()
1204 return
1205 endif
1206 let buf = Api_drop_common(',{"bin":1}')
1207 call assert_equal(1, &bin)
1208
1209 call StopVimInTerminal(buf)
1210 call delete('Xscript')
1211 bwipe Xtextfile
1212endfunc
1213
1214func Test_terminal_api_drop_newwin_binary()
1215 if !CanRunVimInTerminal()
1216 return
1217 endif
1218 let buf = Api_drop_common(',{"binary":1}')
1219 call assert_equal(1, &bin)
1220
1221 call StopVimInTerminal(buf)
1222 call delete('Xscript')
1223 bwipe Xtextfile
1224endfunc
1225
1226func Test_terminal_api_drop_newwin_nobin()
1227 if !CanRunVimInTerminal()
1228 return
1229 endif
1230 set binary
1231 let buf = Api_drop_common(',{"nobin":1}')
1232 call assert_equal(0, &bin)
1233
1234 call StopVimInTerminal(buf)
1235 call delete('Xscript')
1236 bwipe Xtextfile
1237 set nobinary
1238endfunc
1239
1240func Test_terminal_api_drop_newwin_nobinary()
1241 if !CanRunVimInTerminal()
1242 return
1243 endif
1244 set binary
1245 let buf = Api_drop_common(',{"nobinary":1}')
1246 call assert_equal(0, &bin)
1247
1248 call StopVimInTerminal(buf)
1249 call delete('Xscript')
1250 bwipe Xtextfile
1251 set nobinary
1252endfunc
1253
1254func Test_terminal_api_drop_newwin_ff()
1255 if !CanRunVimInTerminal()
1256 return
1257 endif
1258 let buf = Api_drop_common(',{"ff":"dos"}')
1259 call assert_equal("dos", &ff)
1260
1261 call StopVimInTerminal(buf)
1262 call delete('Xscript')
1263 bwipe Xtextfile
1264endfunc
1265
1266func Test_terminal_api_drop_newwin_fileformat()
1267 if !CanRunVimInTerminal()
1268 return
1269 endif
1270 let buf = Api_drop_common(',{"fileformat":"dos"}')
1271 call assert_equal("dos", &ff)
1272
1273 call StopVimInTerminal(buf)
1274 call delete('Xscript')
1275 bwipe Xtextfile
1276endfunc
1277
1278func Test_terminal_api_drop_newwin_enc()
1279 if !CanRunVimInTerminal()
1280 return
1281 endif
1282 let buf = Api_drop_common(',{"enc":"utf-16"}')
1283 call assert_equal("utf-16", &fenc)
1284
1285 call StopVimInTerminal(buf)
1286 call delete('Xscript')
1287 bwipe Xtextfile
1288endfunc
1289
1290func Test_terminal_api_drop_newwin_encoding()
1291 if !CanRunVimInTerminal()
1292 return
1293 endif
1294 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1295 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001296
1297 call StopVimInTerminal(buf)
1298 call delete('Xscript')
1299 bwipe Xtextfile
1300endfunc
1301
1302func Test_terminal_api_drop_oldwin()
1303 if !CanRunVimInTerminal()
1304 return
1305 endif
1306 let firstwinid = win_getid()
1307 split Xtextfile
1308 let textfile_winid = win_getid()
1309 call assert_equal(2, winnr('$'))
1310 call win_gotoid(firstwinid)
1311
1312 " Use the title termcap entries to output the escape sequence.
1313 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001314 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001315 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1316 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1317 \ 'redraw',
1318 \ "set t_ts=",
1319 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001320 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001321 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001322 call assert_equal(textfile_winid, win_getid())
1323
1324 call StopVimInTerminal(buf)
1325 call delete('Xscript')
1326 bwipe Xtextfile
1327endfunc
1328
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001329func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001330 let g:called_bufnum = a:bufnum
1331 let g:called_arg = a:arg
1332endfunc
1333
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001334func WriteApiCall(funcname)
1335 " Use the title termcap entries to output the escape sequence.
1336 call writefile([
1337 \ 'set title',
1338 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1339 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1340 \ 'redraw',
1341 \ "set t_ts=",
1342 \ ], 'Xscript')
1343endfunc
1344
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001345func Test_terminal_api_call()
1346 if !CanRunVimInTerminal()
1347 return
1348 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001349
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001350 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001351 let buf = RunVimInTerminal('-S Xscript', {})
1352 call WaitFor({-> exists('g:called_bufnum')})
1353 call assert_equal(buf, g:called_bufnum)
1354 call assert_equal(['hello', 123], g:called_arg)
1355
1356 call StopVimInTerminal(buf)
1357 call delete('Xscript')
1358 unlet g:called_bufnum
1359 unlet g:called_arg
1360endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001361
1362func Test_terminal_api_call_fails()
1363 if !CanRunVimInTerminal()
1364 return
1365 endif
1366
1367 call WriteApiCall('TryThis')
1368 call ch_logfile('Xlog', 'w')
1369 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001370 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001371
1372 call StopVimInTerminal(buf)
1373 call delete('Xscript')
1374 call ch_logfile('', '')
1375 call delete('Xlog')
1376endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001377
Bram Moolenaara997b452018-04-17 23:24:06 +02001378let s:caught_e937 = 0
1379
1380func Tapi_Delete(bufnum, arg)
1381 try
1382 execute 'bdelete!' a:bufnum
1383 catch /E937:/
1384 let s:caught_e937 = 1
1385 endtry
1386endfunc
1387
1388func Test_terminal_api_call_fail_delete()
1389 if !CanRunVimInTerminal()
1390 return
1391 endif
1392
1393 call WriteApiCall('Tapi_Delete')
1394 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001395 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001396
1397 call StopVimInTerminal(buf)
1398 call delete('Xscript')
1399 call ch_logfile('', '')
1400endfunc
1401
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001402func Test_terminal_ansicolors_default()
1403 let colors = [
1404 \ '#000000', '#e00000',
1405 \ '#00e000', '#e0e000',
1406 \ '#0000e0', '#e000e0',
1407 \ '#00e0e0', '#e0e0e0',
1408 \ '#808080', '#ff4040',
1409 \ '#40ff40', '#ffff40',
1410 \ '#4040ff', '#ff40ff',
1411 \ '#40ffff', '#ffffff',
1412 \]
1413
1414 let buf = Run_shell_in_terminal({})
1415 call assert_equal(colors, term_getansicolors(buf))
1416 call Stop_shell_in_terminal(buf)
1417 call term_wait(buf)
1418
1419 exe buf . 'bwipe'
1420endfunc
1421
1422let s:test_colors = [
1423 \ '#616e64', '#0d0a79',
1424 \ '#6d610d', '#0a7373',
1425 \ '#690d0a', '#6d696e',
1426 \ '#0d0a6f', '#616e0d',
1427 \ '#0a6479', '#6d0d0a',
1428 \ '#617373', '#0d0a69',
1429 \ '#6d690d', '#0a6e6f',
1430 \ '#610d0a', '#6e6479',
1431 \]
1432
1433func Test_terminal_ansicolors_global()
1434 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1435 let buf = Run_shell_in_terminal({})
1436 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1437 call Stop_shell_in_terminal(buf)
1438 call term_wait(buf)
1439
1440 exe buf . 'bwipe'
1441 unlet g:terminal_ansi_colors
1442endfunc
1443
1444func Test_terminal_ansicolors_func()
1445 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1446 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1447 call assert_equal(s:test_colors, term_getansicolors(buf))
1448
1449 call term_setansicolors(buf, g:terminal_ansi_colors)
1450 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1451
1452 let colors = [
1453 \ 'ivory', 'AliceBlue',
1454 \ 'grey67', 'dark goldenrod',
1455 \ 'SteelBlue3', 'PaleVioletRed4',
1456 \ 'MediumPurple2', 'yellow2',
1457 \ 'RosyBrown3', 'OrangeRed2',
1458 \ 'white smoke', 'navy blue',
1459 \ 'grey47', 'gray97',
1460 \ 'MistyRose2', 'DodgerBlue4',
1461 \]
1462 call term_setansicolors(buf, colors)
1463
1464 let colors[4] = 'Invalid'
1465 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1466
1467 call Stop_shell_in_terminal(buf)
1468 call term_wait(buf)
1469 exe buf . 'bwipe'
1470endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001471
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001472func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001473 if !CanRunVimInTerminal()
1474 return
1475 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001476 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001477 let text = []
1478 for n in range(10)
1479 call add(text, repeat(n, 50))
1480 endfor
1481 call writefile(text, 'Xwinsize')
1482 let buf = RunVimInTerminal('Xwinsize', {})
1483 let win = bufwinid(buf)
1484 call assert_equal([6, 40], term_getsize(buf))
1485 call assert_equal(6, winheight(win))
1486 call assert_equal(40, winwidth(win))
1487
1488 " resizing the window doesn't resize the terminal.
1489 resize 10
1490 vertical resize 60
1491 call assert_equal([6, 40], term_getsize(buf))
1492 call assert_equal(10, winheight(win))
1493 call assert_equal(60, winwidth(win))
1494
1495 call StopVimInTerminal(buf)
1496 call delete('Xwinsize')
1497
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001498 call assert_fails('set termwinsize=40', 'E474')
1499 call assert_fails('set termwinsize=10+40', 'E474')
1500 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001501
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001502 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001503endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001504
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001505func Test_terminal_termwinsize_option_zero()
1506 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001507 let buf = Run_shell_in_terminal({})
1508 let win = bufwinid(buf)
1509 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1510 call Stop_shell_in_terminal(buf)
1511 call term_wait(buf)
1512 exe buf . 'bwipe'
1513
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001514 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001515 let buf = Run_shell_in_terminal({})
1516 let win = bufwinid(buf)
1517 call assert_equal([7, winwidth(win)], term_getsize(buf))
1518 call Stop_shell_in_terminal(buf)
1519 call term_wait(buf)
1520 exe buf . 'bwipe'
1521
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001522 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001523 let buf = Run_shell_in_terminal({})
1524 let win = bufwinid(buf)
1525 call assert_equal([winheight(win), 33], term_getsize(buf))
1526 call Stop_shell_in_terminal(buf)
1527 call term_wait(buf)
1528 exe buf . 'bwipe'
1529
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001530 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001531endfunc
1532
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001533func Test_terminal_termwinsize_mininmum()
1534 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001535 vsplit
1536 let buf = Run_shell_in_terminal({})
1537 let win = bufwinid(buf)
1538 call assert_inrange(10, 1000, winheight(win))
1539 call assert_inrange(50, 1000, winwidth(win))
1540 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1541
1542 resize 15
1543 vertical resize 60
1544 redraw
1545 call assert_equal([15, 60], term_getsize(buf))
1546 call assert_equal(15, winheight(win))
1547 call assert_equal(60, winwidth(win))
1548
1549 resize 7
1550 vertical resize 30
1551 redraw
1552 call assert_equal([10, 50], term_getsize(buf))
1553 call assert_equal(7, winheight(win))
1554 call assert_equal(30, winwidth(win))
1555
1556 call Stop_shell_in_terminal(buf)
1557 call term_wait(buf)
1558 exe buf . 'bwipe'
1559
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001560 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001561 let buf = Run_shell_in_terminal({})
1562 let win = bufwinid(buf)
1563 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1564 call Stop_shell_in_terminal(buf)
1565 call term_wait(buf)
1566 exe buf . 'bwipe'
1567
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001568 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001569endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001570
1571func Test_terminal_termwinkey()
1572 call assert_equal(1, winnr('$'))
1573 let thiswin = win_getid()
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001574 tabnew
1575 tabnext
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001576
1577 let buf = Run_shell_in_terminal({})
1578 let termwin = bufwinid(buf)
1579 set termwinkey=<C-L>
1580 call feedkeys("\<C-L>w", 'tx')
1581 call assert_equal(thiswin, win_getid())
1582 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001583 call assert_equal(termwin, win_getid())
1584
1585 let tnr = tabpagenr()
1586 call feedkeys("\<C-L>gt", "xt")
1587 call assert_notequal(tnr, tabpagenr())
1588 tabnext
1589 call assert_equal(tnr, tabpagenr())
1590 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001591
1592 let job = term_getjob(buf)
1593 call feedkeys("\<C-L>\<C-C>", 'tx')
1594 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001595
1596 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001597 tabnext
1598 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001599endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001600
1601func Test_terminal_out_err()
1602 if !has('unix')
1603 return
1604 endif
1605 call writefile([
1606 \ '#!/bin/sh',
1607 \ 'echo "this is standard error" >&2',
1608 \ 'echo "this is standard out" >&1',
1609 \ ], 'Xechoerrout.sh')
1610 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1611
1612 let outfile = 'Xtermstdout'
1613 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001614
1615 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1616 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001617 call assert_equal('this is standard error', term_getline(buf, 1))
1618
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001619 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001620 exe buf . 'bwipe'
1621 call delete('Xechoerrout.sh')
1622 call delete(outfile)
1623endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001624
1625func Test_terminwinscroll()
1626 if !has('unix')
1627 return
1628 endif
1629
1630 " Let the terminal output more than 'termwinscroll' lines, some at the start
1631 " will be dropped.
1632 exe 'set termwinscroll=' . &lines
1633 let buf = term_start('/bin/sh')
1634 for i in range(1, &lines)
1635 call feedkeys("echo " . i . "\<CR>", 'xt')
1636 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1637 endfor
1638 " Go to Terminal-Normal mode to update the buffer.
1639 call feedkeys("\<C-W>N", 'xt')
1640 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1641
1642 " Every "echo nr" must only appear once
1643 let lines = getline(1, line('$'))
1644 for i in range(&lines - len(lines) / 2 + 2, &lines)
1645 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1646 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1647 endfor
1648
1649 exe buf . 'bwipe!'
1650endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001651
Bram Moolenaar875cf872018-07-08 20:49:07 +02001652" Resizing the terminal window caused an ml_get error.
1653" TODO: This does not reproduce the original problem.
1654func Test_terminal_resize()
1655 set statusline=x
1656 terminal
1657 call assert_equal(2, winnr('$'))
1658
1659 " Fill the terminal with text.
1660 if has('win32')
1661 call feedkeys("dir\<CR>", 'xt')
1662 else
1663 call feedkeys("ls\<CR>", 'xt')
1664 endif
1665 " Go to Terminal-Normal mode for a moment.
1666 call feedkeys("\<C-W>N", 'xt')
1667 " Open a new window
1668 call feedkeys("i\<C-W>n", 'xt')
1669 call assert_equal(3, winnr('$'))
1670 redraw
1671
1672 close
1673 call assert_equal(2, winnr('$'))
1674 call feedkeys("exit\<CR>", 'xt')
1675 set statusline&
1676endfunc
1677
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001678" must be nearly the last, we can't go back from GUI to terminal
1679func Test_zz1_terminal_in_gui()
1680 if !CanRunGui()
1681 return
1682 endif
1683
1684 " Ignore the "failed to create input context" error.
1685 call test_ignore_error('E285:')
1686
1687 gui -f
1688
1689 call assert_equal(1, winnr('$'))
1690 let buf = Run_shell_in_terminal({'term_finish': 'close'})
1691 call Stop_shell_in_terminal(buf)
1692 call term_wait(buf)
1693
1694 " closing window wipes out the terminal buffer a with finished job
1695 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1696 call assert_equal("", bufname(buf))
1697
1698 unlet g:job
1699endfunc
1700
1701func Test_zz2_terminal_guioptions_bang()
1702 if !has('gui_running')
1703 return
1704 endif
1705 set guioptions+=!
1706
1707 let filename = 'Xtestscript'
1708 if has('win32')
1709 let filename .= '.bat'
1710 let prefix = ''
1711 let contents = ['@echo off', 'exit %1']
1712 else
1713 let filename .= '.sh'
1714 let prefix = './'
1715 let contents = ['#!/bin/sh', 'exit $1']
1716 endif
1717 call writefile(contents, filename)
1718 call setfperm(filename, 'rwxrwx---')
1719
1720 " Check if v:shell_error is equal to the exit status.
1721 let exitval = 0
1722 execute printf(':!%s%s %d', prefix, filename, exitval)
1723 call assert_equal(exitval, v:shell_error)
1724
1725 let exitval = 9
1726 execute printf(':!%s%s %d', prefix, filename, exitval)
1727 call assert_equal(exitval, v:shell_error)
1728
1729 set guioptions&
1730 call delete(filename)
1731endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001732
1733func Test_terminal_hidden()
1734 if !has('unix')
1735 return
1736 endif
1737 term ++hidden cat
1738 let bnr = bufnr('$')
1739 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1740 exe 'sbuf ' . bnr
1741 call assert_equal('terminal', &buftype)
1742 call term_sendkeys(bnr, "asdf\<CR>")
1743 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1744 call term_sendkeys(bnr, "\<C-D>")
1745 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
1746 bwipe!
1747endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001748
1749func Test_terminal_hidden_and_close()
1750 if !has('unix')
1751 return
1752 endif
1753 call assert_equal(1, winnr('$'))
1754 term ++hidden ++close ls
1755 let bnr = bufnr('$')
1756 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1757 call WaitForAssert({-> assert_false(bufexists(bnr))})
1758 call assert_equal(1, winnr('$'))
1759endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001760
1761func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001762 " This test does not pass through ConPTY.
1763 if has('conpty')
1764 return
1765 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001766 let contents = [
1767 \ [ 'One', '', 'X' ],
1768 \ [ 'Two', '', '' ],
1769 \ [ 'Three' ] + repeat([''], 30)
1770 \ ]
1771
1772 for c in contents
1773 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001774 if has('win32')
1775 term cmd /c type Xfile
1776 else
1777 term cat Xfile
1778 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001779 let bnr = bufnr('$')
1780 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1781 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001782 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001783 call assert_equal(c, getline(1, line('$')))
1784 quit
1785 endfor
1786
1787 call delete('Xfile')
1788endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001789
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001790func Test_terminal_no_job()
1791 let term = term_start('false', {'term_finish': 'close'})
1792 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1793endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001794
1795func Test_term_gettitle()
1796 if !has('title') || empty(&t_ts)
1797 return
1798 endif
1799 " TODO: this fails on Travis
1800 return
1801
1802 " term_gettitle() returns an empty string for a non-terminal buffer
1803 " or for a non-existing buffer.
1804 call assert_equal('', term_gettitle(bufnr('%')))
1805 call assert_equal('', term_gettitle(bufnr('$') + 1))
1806
1807 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
1808 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
1809
1810 call term_sendkeys(term, ":e Xfoo\r")
1811 call WaitForAssert({-> assert_match('Xfoo (.*[/\\]testdir) - VIM', term_gettitle(term)) })
1812
1813 call term_sendkeys(term, ":set titlestring=foo\r")
1814 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
1815
1816 exe term . 'bwipe!'
1817endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01001818
1819" When drawing the statusline the cursor position may not have been updated
1820" yet.
1821" 1. create a terminal, make it show 2 lines
1822" 2. 0.5 sec later: leave terminal window, execute "i"
1823" 3. 0.5 sec later: clear terminal window, now it's 1 line
1824" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1825" 4. 0.5 sec later: should be done, clean up
1826func Test_terminal_statusline()
1827 if !has('unix')
1828 return
1829 endif
1830 set statusline=x
1831 terminal
1832 let tbuf = bufnr('')
1833 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1834 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1835 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1836 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1837
1838 sleep 2
1839 exe tbuf . 'bwipe!'
1840 au! BufLeave
1841 set statusline=
1842endfunc