blob: 9ac0f2755b530ceba2f8e09789dd399589021654 [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 Moolenaarb00fdf62017-09-21 22:16:21 +020035 au BufWinEnter * if &buftype == 'terminal' | let b:done = 'yes' | endif
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 Moolenaar2dc9d262017-09-08 14:39:30 +020042 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
43 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020044 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020045 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020046 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020047 call assert_match('%aR[^\n]*running]', execute('ls'))
48
Bram Moolenaar94053a52017-08-01 21:44:33 +020049 call Stop_shell_in_terminal(buf)
50 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020051 call assert_equal('n', mode())
52 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020053
Bram Moolenaar94053a52017-08-01 21:44:33 +020054 " closing window wipes out the terminal buffer a with finished job
55 close
56 call assert_equal("", bufname(buf))
57
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020058 au! BufWinEnter
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020059 unlet g:job
60endfunc
61
62func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020063 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020064 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020065 call term_wait(buf)
66
67 setlocal modifiable
68 exe "normal Axxx\<Esc>"
69 call assert_fails(buf . 'bwipe', 'E517')
70 undo
71
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020072 exe buf . 'bwipe'
73 unlet g:job
74endfunc
75
Bram Moolenaar94053a52017-08-01 21:44:33 +020076func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020077 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020078 call assert_fails(buf . 'bwipe', 'E517')
79 exe buf . 'bwipe!'
Bram Moolenaar94053a52017-08-01 21:44:33 +020080 call WaitFor('job_status(g:job) == "dead"')
81 call assert_equal('dead', job_status(g:job))
82 call assert_equal("", bufname(buf))
83
84 unlet g:job
85endfunc
86
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020087func Test_terminal_split_quit()
88 let buf = Run_shell_in_terminal({})
89 call term_wait(buf)
90 split
91 quit!
92 call term_wait(buf)
93 sleep 50m
94 call assert_equal('run', job_status(g:job))
95
96 quit!
97 call WaitFor('job_status(g:job) == "dead"')
98 call assert_equal('dead', job_status(g:job))
99
100 exe buf . 'bwipe'
101 unlet g:job
102endfunc
103
Bram Moolenaar94053a52017-08-01 21:44:33 +0200104func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200105 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200106 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200107 quit
108 for nr in range(1, winnr('$'))
109 call assert_notequal(winbufnr(nr), buf)
110 endfor
111 call assert_true(bufloaded(buf))
112 call assert_true(buflisted(buf))
113
114 exe 'split ' . buf . 'buf'
115 call Stop_shell_in_terminal(buf)
116 exe buf . 'bwipe'
117
118 unlet g:job
119endfunc
120
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200121func! s:Nasty_exit_cb(job, st)
122 exe g:buf . 'bwipe!'
123 let g:buf = 0
124endfunc
125
Bram Moolenaar9d189612017-09-09 18:11:00 +0200126func Get_cat_123_cmd()
127 if has('win32')
128 return 'cmd /c "cls && color 2 && echo 123"'
129 else
130 call writefile(["\<Esc>[32m123"], 'Xtext')
131 return "cat Xtext"
132 endif
133endfunc
134
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200135func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200136 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200137 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
138 let g:job = term_getjob(g:buf)
139
140 call WaitFor('job_status(g:job) == "dead"')
141 call WaitFor('g:buf == 0')
142 unlet g:buf
143 unlet g:job
144 call delete('Xtext')
145endfunc
146
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200147func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200148 let l = term_scrape(a:buf, 0)
149 call assert_true(len(l) == 0)
150 let l = term_scrape(a:buf, 999)
151 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200152 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200153 call assert_true(len(l) > 0)
154 call assert_equal('1', l[0].chars)
155 call assert_equal('2', l[1].chars)
156 call assert_equal('3', l[2].chars)
157 call assert_equal('#00e000', l[0].fg)
158 if &background == 'light'
159 call assert_equal('#ffffff', l[0].bg)
160 else
161 call assert_equal('#000000', l[0].bg)
162 endif
163
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200164 let l = term_getline(a:buf, -1)
165 call assert_equal('', l)
166 let l = term_getline(a:buf, 0)
167 call assert_equal('', l)
168 let l = term_getline(a:buf, 999)
169 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200170 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200171 call assert_equal('123', l)
172endfunc
173
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200174func Test_terminal_scrape_123()
175 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200176 let buf = term_start(cmd)
177
178 let termlist = term_list()
179 call assert_equal(1, len(termlist))
180 call assert_equal(buf, termlist[0])
181
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200182 " Nothing happens with invalid buffer number
183 call term_wait(1234)
184
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200185 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200186 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200187 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100188 call WaitFor({-> len(term_scrape(buf, 1)) == 3})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200189 call Check_123(buf)
190
191 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100192 let job = term_getjob(buf)
193 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200194 call term_wait(buf)
195 call Check_123(buf)
196
197 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200198 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200199endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200200
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200201func Test_terminal_scrape_multibyte()
202 if !has('multi_byte')
203 return
204 endif
205 call writefile(["léttまrs"], 'Xtext')
206 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200207 " Run cmd with UTF-8 codepage to make the type command print the expected
208 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100209 let buf = term_start("cmd /K chcp 65001")
210 call term_sendkeys(buf, "type Xtext\<CR>")
211 call term_sendkeys(buf, "exit\<CR>")
212 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200213 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100214 let buf = term_start("cat Xtext")
215 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200216 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200217
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100218 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
219 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200220 call assert_true(len(l) >= 7)
221 call assert_equal('l', l[0].chars)
222 call assert_equal('é', l[1].chars)
223 call assert_equal(1, l[1].width)
224 call assert_equal('t', l[2].chars)
225 call assert_equal('t', l[3].chars)
226 call assert_equal('ま', l[4].chars)
227 call assert_equal(2, l[4].width)
228 call assert_equal('r', l[5].chars)
229 call assert_equal('s', l[6].chars)
230
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100231 let job = term_getjob(buf)
232 call WaitFor({-> job_status(job) == "dead"})
233 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200234
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100235 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200236 call delete('Xtext')
237endfunc
238
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200239func Test_terminal_scroll()
240 call writefile(range(1, 200), 'Xtext')
241 if has('win32')
242 let cmd = 'cmd /c "type Xtext"'
243 else
244 let cmd = "cat Xtext"
245 endif
246 let buf = term_start(cmd)
247
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100248 let job = term_getjob(buf)
249 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200250 call term_wait(buf)
251 if has('win32')
252 " TODO: this should not be needed
253 sleep 100m
254 endif
255
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200256 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200257 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200258 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200259 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200260 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200261 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200262 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200263
264 exe buf . 'bwipe'
265 call delete('Xtext')
266endfunc
267
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200268func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200269 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200270
Bram Moolenaarb2412082017-08-20 18:09:14 +0200271 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200272 let size = term_getsize('')
273 bwipe!
274 call assert_equal(5, size[0])
275
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200276 call term_start(cmd, {'term_rows': 6})
277 let size = term_getsize('')
278 bwipe!
279 call assert_equal(6, size[0])
280
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200281 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200282 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200283 let size = term_getsize('')
284 bwipe!
285 call assert_equal([5, 33], size)
286
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200287 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
288 let size = term_getsize('')
289 bwipe!
290 call assert_equal([6, 36], size)
291
Bram Moolenaarb2412082017-08-20 18:09:14 +0200292 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200293 let size = term_getsize('')
294 bwipe!
295 call assert_equal(20, size[1])
296
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200297 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
298 let size = term_getsize('')
299 bwipe!
300 call assert_equal(26, size[1])
301
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200302 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200303 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200304 let size = term_getsize('')
305 bwipe!
306 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200307
308 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
309 let size = term_getsize('')
310 bwipe!
311 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200312
313 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200314endfunc
315
316func Test_terminal_curwin()
317 let cmd = Get_cat_123_cmd()
318 call assert_equal(1, winnr('$'))
319
320 split dummy
321 exe 'terminal ++curwin ' . cmd
322 call assert_equal(2, winnr('$'))
323 bwipe!
324
325 split dummy
326 call term_start(cmd, {'curwin': 1})
327 call assert_equal(2, winnr('$'))
328 bwipe!
329
330 split dummy
331 call setline(1, 'change')
332 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
333 call assert_equal(2, winnr('$'))
334 exe 'terminal! ++curwin ' . cmd
335 call assert_equal(2, winnr('$'))
336 bwipe!
337
338 split dummy
339 call setline(1, 'change')
340 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
341 call assert_equal(2, winnr('$'))
342 bwipe!
343
344 split dummy
345 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200346 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200347endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200348
Bram Moolenaarff546792017-11-21 14:47:57 +0100349func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200350 if s:python != ''
351 let cmd = s:python . " test_short_sleep.py"
352 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200353 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200354 echo 'This will take five seconds...'
355 let waittime = 2000
356 if has('win32')
357 let cmd = $windir . '\system32\timeout.exe 1'
358 else
359 let cmd = 'sleep 1'
360 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200361 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100362 return [cmd, waittime]
363endfunc
364
365func Test_terminal_finish_open_close()
366 call assert_equal(1, winnr('$'))
367
368 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200369
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200370 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200371 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200372 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200373 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200374
375 call term_start(cmd, {'term_finish': 'close'})
376 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200377 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200378 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200379 call assert_equal(1, winnr('$'))
380
381 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200382 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200383 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200384 call assert_equal(2, winnr('$'))
385 bwipe
386
387 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200388 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200389 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200390 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200391 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200392
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200393 exe 'terminal ++hidden ++open ' . cmd
394 call assert_equal(1, winnr('$'))
395 call WaitFor("winnr('$') == 2", waittime)
396 call assert_equal(2, winnr('$'))
397 bwipe
398
399 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
400 call assert_equal(1, winnr('$'))
401 call WaitFor("winnr('$') == 2", waittime)
402 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200403 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200404
405 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
406 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
407 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
408 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
409
410 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200411 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200412 call WaitFor("winnr('$') == 2", waittime)
413 call assert_equal(2, winnr('$'))
414 call assert_equal(4, winheight(0))
415 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200416endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200417
418func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200419 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200420 return
421 endif
422 call mkdir('Xdir')
423 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200424 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
425 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200426
427 exe buf . 'bwipe'
428 call delete('Xdir', 'rf')
429endfunc
430
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100431func Test_terminal_servername()
432 if !has('clientserver')
433 return
434 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100435 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100436 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100437 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100438 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100439 call term_sendkeys(buf, "echo %VIM_SERVERNAME%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100440 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100441 call term_sendkeys(buf, "echo $VIM_SERVERNAME\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100442 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100443 call term_wait(buf)
444 call Stop_shell_in_terminal(buf)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100445 call WaitFor('getline(2) == v:servername')
446 call assert_equal(v:servername, getline(2))
447
Bram Moolenaar012eb662018-03-13 17:55:27 +0100448 exe buf . 'bwipe'
449 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100450endfunc
451
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200452func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100453 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200454 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100455 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100456 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100457 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100458 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100459 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100460 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100461 call term_wait(buf)
462 call Stop_shell_in_terminal(buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200463 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200464 call assert_equal('correct', getline(2))
465
Bram Moolenaar012eb662018-03-13 17:55:27 +0100466 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200467endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200468
469" must be last, we can't go back from GUI to terminal
470func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200471 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200472 return
473 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200474
475 " Ignore the "failed to create input context" error.
476 call test_ignore_error('E285:')
477
Bram Moolenaar679653e2017-08-13 14:13:19 +0200478 gui -f
479
480 call assert_equal(1, winnr('$'))
481 let buf = Run_shell_in_terminal({'term_finish': 'close'})
482 call Stop_shell_in_terminal(buf)
483 call term_wait(buf)
484
485 " closing window wipes out the terminal buffer a with finished job
486 call WaitFor("winnr('$') == 1")
487 call assert_equal(1, winnr('$'))
488 call assert_equal("", bufname(buf))
489
490 unlet g:job
491endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200492
493func Test_terminal_list_args()
494 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
495 call assert_fails(buf . 'bwipe', 'E517')
496 exe buf . 'bwipe!'
497 call assert_equal("", bufname(buf))
498endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200499
500func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100501 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200502 if has('mac')
503 " The shell or something else has a problem dealing with more than 1000
504 " characters at the same time.
505 let len = 1000
506 else
507 let len = 5000
508 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200509
510 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100511 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200512 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100513 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200514
515 " On MS-Windows there is an extra empty line below "done". Find "done" in
516 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100517 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100518 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100519 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200520 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100521 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200522 endif
523 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200524
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100525 let g:job = term_getjob(buf)
526 call Stop_shell_in_terminal(buf)
527 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200528 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200529 bwipe
530endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200531
532func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200533 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200534 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200535 endif
536 new
537 call setline(1, ['one', 'two', 'three'])
538 %term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200539 call WaitFor('getline("$") =~ "3"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200540 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200541 call assert_equal(['3', '3', '14'], nrs)
542 bwipe
543
Bram Moolenaardada6d22017-09-02 17:18:35 +0200544 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200545 call setline(1, ['one', 'two', 'three', 'four'])
546 2,3term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200547 call WaitFor('getline("$") =~ "2"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200548 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200549 call assert_equal(['2', '2', '10'], nrs)
550 bwipe
551
Bram Moolenaardada6d22017-09-02 17:18:35 +0200552 if executable('python')
553 new
554 call setline(1, ['print("hello")'])
555 1term ++eof=exit() python
556 " MS-Windows echoes the input, Unix doesn't.
557 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
558 if getline(1) =~ 'hello'
559 call assert_equal('hello', getline(1))
560 else
561 call assert_equal('hello', getline(line('$') - 1))
562 endif
563 bwipe
564
565 if has('win32')
566 new
567 call setline(1, ['print("hello")'])
568 1term ++eof=<C-Z> python
569 call WaitFor('getline("$") =~ "Z"')
570 call assert_equal('hello', getline(line('$') - 1))
571 bwipe
572 endif
573 endif
574
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200575 bwipe!
576endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200577
578func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200579 " Todo: make this work in the GUI
580 if !has('gui_running')
581 return
582 endif
583 let buf = term_start('NONE', {})
584 call assert_notequal(0, buf)
585
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200586 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200587 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200588 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200589 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200590 else
591 call system('echo "look here" > ' . pty)
592 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100593 call WaitFor({-> term_getline(buf, 1) =~ "look here"})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200594
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200595 call assert_match('look here', term_getline(buf, 1))
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200596 bwipe!
597endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200598
599func Test_terminal_special_chars()
600 " this file name only works on Unix
601 if !has('unix')
602 return
603 endif
604 call mkdir('Xdir with spaces')
605 call writefile(['x'], 'Xdir with spaces/quoted"file')
606 term ls Xdir\ with\ spaces/quoted\"file
607 call WaitFor('term_getline("", 1) =~ "quoted"')
608 call assert_match('quoted"file', term_getline('', 1))
609 call term_wait('')
610
611 call delete('Xdir with spaces', 'rf')
612 bwipe
613endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200614
615func Test_terminal_wrong_options()
616 call assert_fails('call term_start(&shell, {
617 \ "in_io": "file",
618 \ "in_name": "xxx",
619 \ "out_io": "file",
620 \ "out_name": "xxx",
621 \ "err_io": "file",
622 \ "err_name": "xxx"
623 \ })', 'E474:')
624 call assert_fails('call term_start(&shell, {
625 \ "out_buf": bufnr("%")
626 \ })', 'E474:')
627 call assert_fails('call term_start(&shell, {
628 \ "err_buf": bufnr("%")
629 \ })', 'E474:')
630endfunc
631
632func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200633 " TODO: this should work on MS-Window
634 if has('unix')
635 let cmd = Get_cat_123_cmd()
636 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
637 call term_wait(buf)
638 call WaitFor('len(readfile("Xfile")) > 0')
639 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200640 let g:job = term_getjob(buf)
641 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200642 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200643 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200644 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200645
646 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200647 call writefile(['one line'], 'Xfile')
648 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
649 call term_wait(buf)
650 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
651 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200652 let g:job = term_getjob(buf)
653 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200654 bwipe
655 call delete('Xfile')
656 endif
657endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200658
659func TerminalTmap(remap)
660 let buf = Run_shell_in_terminal({})
661 call assert_equal('t', mode())
662
663 if a:remap
664 tmap 123 456
665 else
666 tnoremap 123 456
667 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100668 " don't use abcde, it's an existing command
669 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200670 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100671 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200672 call feedkeys("123", 'tx')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100673 call WaitFor({-> term_getline(buf, term_getcursor(buf)[0]) =~ 'abxde\|456'})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200674 let lnum = term_getcursor(buf)[0]
675 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100676 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200677 else
678 call assert_match('456', term_getline(buf, lnum))
679 endif
680
681 call term_sendkeys(buf, "\r")
682 call Stop_shell_in_terminal(buf)
683 call term_wait(buf)
684
685 tunmap 123
686 tunmap 456
687 call assert_equal('', maparg('123', 't'))
688 close
689 unlet g:job
690endfunc
691
692func Test_terminal_tmap()
693 call TerminalTmap(1)
694 call TerminalTmap(0)
695endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200696
697func Test_terminal_wall()
698 let buf = Run_shell_in_terminal({})
699 wall
700 call Stop_shell_in_terminal(buf)
701 call term_wait(buf)
702 exe buf . 'bwipe'
703 unlet g:job
704endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200705
Bram Moolenaar7a760922018-02-19 23:10:02 +0100706func Test_terminal_wqall()
707 let buf = Run_shell_in_terminal({})
708 call assert_fails('wqall', 'E948')
709 call Stop_shell_in_terminal(buf)
710 call term_wait(buf)
711 exe buf . 'bwipe'
712 unlet g:job
713endfunc
714
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200715func Test_terminal_composing_unicode()
716 let save_enc = &encoding
717 set encoding=utf-8
718
719 if has('win32')
720 let cmd = "cmd /K chcp 65001"
721 let lnum = [3, 6, 9]
722 else
723 let cmd = &shell
724 let lnum = [1, 3, 5]
725 endif
726
727 enew
728 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100729 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200730 call term_wait(buf, 50)
731
732 " ascii + composing
733 let txt = "a\u0308bc"
734 call term_sendkeys(buf, "echo " . txt . "\r")
735 call term_wait(buf, 50)
736 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
737 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
738 let l = term_scrape(buf, lnum[0] + 1)
739 call assert_equal("a\u0308", l[0].chars)
740 call assert_equal("b", l[1].chars)
741 call assert_equal("c", l[2].chars)
742
743 " multibyte + composing
744 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
745 call term_sendkeys(buf, "echo " . txt . "\r")
746 call term_wait(buf, 50)
747 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
748 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
749 let l = term_scrape(buf, lnum[1] + 1)
750 call assert_equal("\u304b\u3099", l[0].chars)
751 call assert_equal("\u304e", l[1].chars)
752 call assert_equal("\u304f\u3099", l[2].chars)
753 call assert_equal("\u3052", l[3].chars)
754 call assert_equal("\u3053\u3099", l[4].chars)
755
756 " \u00a0 + composing
757 let txt = "abc\u00a0\u0308"
758 call term_sendkeys(buf, "echo " . txt . "\r")
759 call term_wait(buf, 50)
760 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
761 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
762 let l = term_scrape(buf, lnum[2] + 1)
763 call assert_equal("\u00a0\u0308", l[3].chars)
764
765 call term_sendkeys(buf, "exit\r")
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100766 call WaitFor('job_status(g:job) == "dead"')
767 call assert_equal('dead', job_status(g:job))
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200768 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100769 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200770 let &encoding = save_enc
771endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100772
773func Test_terminal_aucmd_on_close()
774 fun Nop()
775 let s:called = 1
776 endfun
777
778 aug repro
779 au!
780 au BufWinLeave * call Nop()
781 aug END
782
783 let [cmd, waittime] = s:get_sleep_cmd()
784
785 call assert_equal(1, winnr('$'))
786 new
787 call setline(1, ['one', 'two'])
788 exe 'term ++close ' . cmd
789 wincmd p
790 call WaitFor("winnr('$') == 2", waittime)
791 call assert_equal(1, s:called)
792 bwipe!
793
794 unlet s:called
795 au! repro
796 delfunc Nop
797endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100798
799func Test_terminal_term_start_empty_command()
800 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
801 call assert_fails(cmd, 'E474')
802 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
803 call assert_fails(cmd, 'E474')
804 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
805 call assert_fails(cmd, 'E474')
806 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
807 call assert_fails(cmd, 'E474')
808endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100809
810func Test_terminal_response_to_control_sequence()
811 if !has('unix')
812 return
813 endif
814
815 let buf = Run_shell_in_terminal({})
Bram Moolenaar012eb662018-03-13 17:55:27 +0100816 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100817
Bram Moolenaar012eb662018-03-13 17:55:27 +0100818 call writefile(["\x1b[6n"], 'Xescape')
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100819 call term_sendkeys(buf, "cat Xescape\<cr>")
820
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100821 " wait for the response of control sequence from libvterm (and send it to tty)
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100822 sleep 200m
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100823 call term_wait(buf)
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100824
825 " Wait for output from tty to display, below an empty line.
826 " It should show \e3;1R, but only 1R may show up
827 call assert_match('\<\d\+R', term_getline(buf, 3))
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100828
829 call term_sendkeys(buf, "\<c-c>")
830 call term_wait(buf)
831 call Stop_shell_in_terminal(buf)
832
833 exe buf . 'bwipe'
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100834 call delete('Xescape')
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100835 unlet g:job
836endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100837
838" Run Vim in a terminal, then start a terminal in that Vim with a kill
839" argument, check that :qall works.
840func Test_terminal_qall_kill_arg()
841 if !CanRunVimInTerminal()
842 return
843 endif
844 let buf = RunVimInTerminal('', {})
845
846 " Open a terminal window and wait for the prompt to appear
847 call term_sendkeys(buf, ":term ++kill=kill\<CR>")
848 call WaitFor({-> term_getline(buf, 10) =~ '\[running]'})
849 call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'})
850
851 " make Vim exit, it will kill the shell
852 call term_sendkeys(buf, "\<C-W>:qall\<CR>")
853 call WaitFor({-> term_getstatus(buf) == "finished"})
854
855 " close the terminal window where Vim was running
856 quit
857endfunc
858
859" Run Vim in a terminal, then start a terminal in that Vim with a kill
860" argument, check that :qall works.
861func Test_terminal_qall_kill_func()
862 if !CanRunVimInTerminal()
863 return
864 endif
865 let buf = RunVimInTerminal('', {})
866
867 " Open a terminal window and wait for the prompt to appear
868 call term_sendkeys(buf, ":term\<CR>")
869 call WaitFor({-> term_getline(buf, 10) =~ '\[running]'})
870 call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'})
871
872 " set kill using term_setkill()
873 call term_sendkeys(buf, "\<C-W>:call term_setkill(bufnr('%'), 'kill')\<CR>")
874
875 " make Vim exit, it will kill the shell
876 call term_sendkeys(buf, "\<C-W>:qall\<CR>")
877 call WaitFor({-> term_getstatus(buf) == "finished"})
878
879 " close the terminal window where Vim was running
880 quit
881endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100882
883" Run Vim in a terminal, then start a terminal in that Vim without a kill
884" argument, check that :confirm qall works.
885func Test_terminal_qall_prompt()
886 if !CanRunVimInTerminal()
887 return
888 endif
889 let buf = RunVimInTerminal('', {})
890
891 " Open a terminal window and wait for the prompt to appear
892 call term_sendkeys(buf, ":term\<CR>")
893 call WaitFor({-> term_getline(buf, 10) =~ '\[running]'})
894 call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'})
895
896 " make Vim exit, it will prompt to kill the shell
897 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
898 call WaitFor({-> term_getline(buf, 20) =~ 'ancel:'})
899 call term_sendkeys(buf, "y")
900 call WaitFor({-> term_getstatus(buf) == "finished"})
901
902 " close the terminal window where Vim was running
903 quit
904endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100905
Bram Moolenaar012eb662018-03-13 17:55:27 +0100906func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100907 augroup repro
908 au!
909 au TerminalOpen * let s:called += 1
910 augroup END
911
912 let s:called = 0
913
914 " Open a terminal window with :terminal
915 terminal
916 call assert_equal(1, s:called)
917 bwipe!
918
919 " Open a terminal window with term_start()
920 call term_start(&shell)
921 call assert_equal(2, s:called)
922 bwipe!
923
924 " Open a hidden terminal buffer with :terminal
925 terminal ++hidden
926 call assert_equal(3, s:called)
927 for buf in term_list()
928 exe buf . "bwipe!"
929 endfor
930
931 " Open a hidden terminal buffer with term_start()
932 let buf = term_start(&shell, {'hidden': 1})
933 call assert_equal(4, s:called)
934 exe buf . "bwipe!"
935
936 unlet s:called
937 au! repro
938endfunction