blob: 58737edc8367d1544d200d77387bf580af186a2a [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 Moolenaar1dd98332018-03-16 22:54:53 +0100370 " shell terminal closes automatically
371 terminal
372 let buf = bufnr('%')
373 call assert_equal(2, winnr('$'))
374 " Wait for the shell to display a prompt
375 call WaitFor({-> term_getline(buf, 1) != ""})
376 call Stop_shell_in_terminal(buf)
377 call WaitFor("winnr('$') == 1", waittime)
378
379 " shell terminal that does not close automatically
380 terminal ++noclose
381 let buf = bufnr('%')
382 call assert_equal(2, winnr('$'))
383 " Wait for the shell to display a prompt
384 call WaitFor({-> term_getline(buf, 1) != ""})
385 call Stop_shell_in_terminal(buf)
386 call assert_equal(2, winnr('$'))
387 quit
388 call assert_equal(1, winnr('$'))
389
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200390 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200391 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200392 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200393 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200394
395 call term_start(cmd, {'term_finish': 'close'})
396 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200397 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200398 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200399 call assert_equal(1, winnr('$'))
400
401 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200402 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200403 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200404 call assert_equal(2, winnr('$'))
405 bwipe
406
407 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200408 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200409 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200410 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200411 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200412
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200413 exe 'terminal ++hidden ++open ' . cmd
414 call assert_equal(1, winnr('$'))
415 call WaitFor("winnr('$') == 2", waittime)
416 call assert_equal(2, winnr('$'))
417 bwipe
418
419 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
420 call assert_equal(1, winnr('$'))
421 call WaitFor("winnr('$') == 2", waittime)
422 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200423 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200424
425 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
426 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
427 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
428 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
429
430 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200431 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200432 call WaitFor("winnr('$') == 2", waittime)
433 call assert_equal(2, winnr('$'))
434 call assert_equal(4, winheight(0))
435 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200436endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200437
438func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200439 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200440 return
441 endif
442 call mkdir('Xdir')
443 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200444 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
445 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200446
447 exe buf . 'bwipe'
448 call delete('Xdir', 'rf')
449endfunc
450
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100451func Test_terminal_servername()
452 if !has('clientserver')
453 return
454 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100455 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100456 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100457 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100458 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100459 call term_sendkeys(buf, "echo %VIM_SERVERNAME%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100460 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100461 call term_sendkeys(buf, "echo $VIM_SERVERNAME\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100462 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100463 call term_wait(buf)
464 call Stop_shell_in_terminal(buf)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100465 call WaitFor('getline(2) == v:servername')
466 call assert_equal(v:servername, getline(2))
467
Bram Moolenaar012eb662018-03-13 17:55:27 +0100468 exe buf . 'bwipe'
469 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100470endfunc
471
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200472func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100473 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200474 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100475 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100476 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100477 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100478 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100479 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100480 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100481 call term_wait(buf)
482 call Stop_shell_in_terminal(buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200483 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200484 call assert_equal('correct', getline(2))
485
Bram Moolenaar012eb662018-03-13 17:55:27 +0100486 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200487endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200488
489" must be last, we can't go back from GUI to terminal
490func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200491 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200492 return
493 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200494
495 " Ignore the "failed to create input context" error.
496 call test_ignore_error('E285:')
497
Bram Moolenaar679653e2017-08-13 14:13:19 +0200498 gui -f
499
500 call assert_equal(1, winnr('$'))
501 let buf = Run_shell_in_terminal({'term_finish': 'close'})
502 call Stop_shell_in_terminal(buf)
503 call term_wait(buf)
504
505 " closing window wipes out the terminal buffer a with finished job
506 call WaitFor("winnr('$') == 1")
507 call assert_equal(1, winnr('$'))
508 call assert_equal("", bufname(buf))
509
510 unlet g:job
511endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200512
513func Test_terminal_list_args()
514 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
515 call assert_fails(buf . 'bwipe', 'E517')
516 exe buf . 'bwipe!'
517 call assert_equal("", bufname(buf))
518endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200519
520func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100521 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200522 if has('mac')
523 " The shell or something else has a problem dealing with more than 1000
524 " characters at the same time.
525 let len = 1000
526 else
527 let len = 5000
528 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200529
530 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100531 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200532 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100533 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200534
535 " On MS-Windows there is an extra empty line below "done". Find "done" in
536 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100537 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100538 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100539 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200540 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100541 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200542 endif
543 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200544
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100545 let g:job = term_getjob(buf)
546 call Stop_shell_in_terminal(buf)
547 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200548 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200549 bwipe
550endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200551
552func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200553 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200554 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200555 endif
556 new
557 call setline(1, ['one', 'two', 'three'])
558 %term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200559 call WaitFor('getline("$") =~ "3"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200560 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200561 call assert_equal(['3', '3', '14'], nrs)
562 bwipe
563
Bram Moolenaardada6d22017-09-02 17:18:35 +0200564 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200565 call setline(1, ['one', 'two', 'three', 'four'])
566 2,3term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200567 call WaitFor('getline("$") =~ "2"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200568 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200569 call assert_equal(['2', '2', '10'], nrs)
570 bwipe
571
Bram Moolenaardada6d22017-09-02 17:18:35 +0200572 if executable('python')
573 new
574 call setline(1, ['print("hello")'])
575 1term ++eof=exit() python
576 " MS-Windows echoes the input, Unix doesn't.
577 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
578 if getline(1) =~ 'hello'
579 call assert_equal('hello', getline(1))
580 else
581 call assert_equal('hello', getline(line('$') - 1))
582 endif
583 bwipe
584
585 if has('win32')
586 new
587 call setline(1, ['print("hello")'])
588 1term ++eof=<C-Z> python
589 call WaitFor('getline("$") =~ "Z"')
590 call assert_equal('hello', getline(line('$') - 1))
591 bwipe
592 endif
593 endif
594
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200595 bwipe!
596endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200597
598func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200599 " Todo: make this work in the GUI
600 if !has('gui_running')
601 return
602 endif
603 let buf = term_start('NONE', {})
604 call assert_notequal(0, buf)
605
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200606 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200607 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200608 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200609 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200610 else
611 call system('echo "look here" > ' . pty)
612 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100613 call WaitFor({-> term_getline(buf, 1) =~ "look here"})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200614
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200615 call assert_match('look here', term_getline(buf, 1))
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200616 bwipe!
617endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200618
619func Test_terminal_special_chars()
620 " this file name only works on Unix
621 if !has('unix')
622 return
623 endif
624 call mkdir('Xdir with spaces')
625 call writefile(['x'], 'Xdir with spaces/quoted"file')
626 term ls Xdir\ with\ spaces/quoted\"file
627 call WaitFor('term_getline("", 1) =~ "quoted"')
628 call assert_match('quoted"file', term_getline('', 1))
629 call term_wait('')
630
631 call delete('Xdir with spaces', 'rf')
632 bwipe
633endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200634
635func Test_terminal_wrong_options()
636 call assert_fails('call term_start(&shell, {
637 \ "in_io": "file",
638 \ "in_name": "xxx",
639 \ "out_io": "file",
640 \ "out_name": "xxx",
641 \ "err_io": "file",
642 \ "err_name": "xxx"
643 \ })', 'E474:')
644 call assert_fails('call term_start(&shell, {
645 \ "out_buf": bufnr("%")
646 \ })', 'E474:')
647 call assert_fails('call term_start(&shell, {
648 \ "err_buf": bufnr("%")
649 \ })', 'E474:')
650endfunc
651
652func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200653 " TODO: this should work on MS-Window
654 if has('unix')
655 let cmd = Get_cat_123_cmd()
656 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
657 call term_wait(buf)
658 call WaitFor('len(readfile("Xfile")) > 0')
659 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200660 let g:job = term_getjob(buf)
661 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200662 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200663 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200664 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200665
666 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200667 call writefile(['one line'], 'Xfile')
668 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
669 call term_wait(buf)
670 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
671 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200672 let g:job = term_getjob(buf)
673 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200674 bwipe
675 call delete('Xfile')
676 endif
677endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200678
679func TerminalTmap(remap)
680 let buf = Run_shell_in_terminal({})
681 call assert_equal('t', mode())
682
683 if a:remap
684 tmap 123 456
685 else
686 tnoremap 123 456
687 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100688 " don't use abcde, it's an existing command
689 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200690 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100691 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200692 call feedkeys("123", 'tx')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100693 call WaitFor({-> term_getline(buf, term_getcursor(buf)[0]) =~ 'abxde\|456'})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200694 let lnum = term_getcursor(buf)[0]
695 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100696 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200697 else
698 call assert_match('456', term_getline(buf, lnum))
699 endif
700
701 call term_sendkeys(buf, "\r")
702 call Stop_shell_in_terminal(buf)
703 call term_wait(buf)
704
705 tunmap 123
706 tunmap 456
707 call assert_equal('', maparg('123', 't'))
708 close
709 unlet g:job
710endfunc
711
712func Test_terminal_tmap()
713 call TerminalTmap(1)
714 call TerminalTmap(0)
715endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200716
717func Test_terminal_wall()
718 let buf = Run_shell_in_terminal({})
719 wall
720 call Stop_shell_in_terminal(buf)
721 call term_wait(buf)
722 exe buf . 'bwipe'
723 unlet g:job
724endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200725
Bram Moolenaar7a760922018-02-19 23:10:02 +0100726func Test_terminal_wqall()
727 let buf = Run_shell_in_terminal({})
728 call assert_fails('wqall', 'E948')
729 call Stop_shell_in_terminal(buf)
730 call term_wait(buf)
731 exe buf . 'bwipe'
732 unlet g:job
733endfunc
734
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200735func Test_terminal_composing_unicode()
736 let save_enc = &encoding
737 set encoding=utf-8
738
739 if has('win32')
740 let cmd = "cmd /K chcp 65001"
741 let lnum = [3, 6, 9]
742 else
743 let cmd = &shell
744 let lnum = [1, 3, 5]
745 endif
746
747 enew
748 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100749 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200750 call term_wait(buf, 50)
751
752 " ascii + composing
753 let txt = "a\u0308bc"
754 call term_sendkeys(buf, "echo " . txt . "\r")
755 call term_wait(buf, 50)
756 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
757 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
758 let l = term_scrape(buf, lnum[0] + 1)
759 call assert_equal("a\u0308", l[0].chars)
760 call assert_equal("b", l[1].chars)
761 call assert_equal("c", l[2].chars)
762
763 " multibyte + composing
764 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
765 call term_sendkeys(buf, "echo " . txt . "\r")
766 call term_wait(buf, 50)
767 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
768 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
769 let l = term_scrape(buf, lnum[1] + 1)
770 call assert_equal("\u304b\u3099", l[0].chars)
771 call assert_equal("\u304e", l[1].chars)
772 call assert_equal("\u304f\u3099", l[2].chars)
773 call assert_equal("\u3052", l[3].chars)
774 call assert_equal("\u3053\u3099", l[4].chars)
775
776 " \u00a0 + composing
777 let txt = "abc\u00a0\u0308"
778 call term_sendkeys(buf, "echo " . txt . "\r")
779 call term_wait(buf, 50)
780 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
781 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
782 let l = term_scrape(buf, lnum[2] + 1)
783 call assert_equal("\u00a0\u0308", l[3].chars)
784
785 call term_sendkeys(buf, "exit\r")
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100786 call WaitFor('job_status(g:job) == "dead"')
787 call assert_equal('dead', job_status(g:job))
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200788 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100789 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200790 let &encoding = save_enc
791endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100792
793func Test_terminal_aucmd_on_close()
794 fun Nop()
795 let s:called = 1
796 endfun
797
798 aug repro
799 au!
800 au BufWinLeave * call Nop()
801 aug END
802
803 let [cmd, waittime] = s:get_sleep_cmd()
804
805 call assert_equal(1, winnr('$'))
806 new
807 call setline(1, ['one', 'two'])
808 exe 'term ++close ' . cmd
809 wincmd p
810 call WaitFor("winnr('$') == 2", waittime)
811 call assert_equal(1, s:called)
812 bwipe!
813
814 unlet s:called
815 au! repro
816 delfunc Nop
817endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100818
819func Test_terminal_term_start_empty_command()
820 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
821 call assert_fails(cmd, 'E474')
822 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
823 call assert_fails(cmd, 'E474')
824 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
825 call assert_fails(cmd, 'E474')
826 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
827 call assert_fails(cmd, 'E474')
828endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100829
830func Test_terminal_response_to_control_sequence()
831 if !has('unix')
832 return
833 endif
834
835 let buf = Run_shell_in_terminal({})
Bram Moolenaar012eb662018-03-13 17:55:27 +0100836 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100837
Bram Moolenaar012eb662018-03-13 17:55:27 +0100838 call writefile(["\x1b[6n"], 'Xescape')
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100839 call term_sendkeys(buf, "cat Xescape\<cr>")
840
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100841 " wait for the response of control sequence from libvterm (and send it to tty)
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100842 sleep 200m
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100843 call term_wait(buf)
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100844
845 " Wait for output from tty to display, below an empty line.
846 " It should show \e3;1R, but only 1R may show up
847 call assert_match('\<\d\+R', term_getline(buf, 3))
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100848
849 call term_sendkeys(buf, "\<c-c>")
850 call term_wait(buf)
851 call Stop_shell_in_terminal(buf)
852
853 exe buf . 'bwipe'
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100854 call delete('Xescape')
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100855 unlet g:job
856endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100857
858" Run Vim in a terminal, then start a terminal in that Vim with a kill
859" argument, check that :qall works.
860func Test_terminal_qall_kill_arg()
861 if !CanRunVimInTerminal()
862 return
863 endif
864 let buf = RunVimInTerminal('', {})
865
866 " Open a terminal window and wait for the prompt to appear
867 call term_sendkeys(buf, ":term ++kill=kill\<CR>")
868 call WaitFor({-> term_getline(buf, 10) =~ '\[running]'})
869 call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'})
870
871 " make Vim exit, it will kill the shell
872 call term_sendkeys(buf, "\<C-W>:qall\<CR>")
873 call WaitFor({-> term_getstatus(buf) == "finished"})
874
875 " close the terminal window where Vim was running
876 quit
877endfunc
878
879" Run Vim in a terminal, then start a terminal in that Vim with a kill
880" argument, check that :qall works.
881func Test_terminal_qall_kill_func()
882 if !CanRunVimInTerminal()
883 return
884 endif
885 let buf = RunVimInTerminal('', {})
886
887 " Open a terminal window and wait for the prompt to appear
888 call term_sendkeys(buf, ":term\<CR>")
889 call WaitFor({-> term_getline(buf, 10) =~ '\[running]'})
890 call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'})
891
892 " set kill using term_setkill()
893 call term_sendkeys(buf, "\<C-W>:call term_setkill(bufnr('%'), 'kill')\<CR>")
894
895 " make Vim exit, it will kill the shell
896 call term_sendkeys(buf, "\<C-W>:qall\<CR>")
897 call WaitFor({-> term_getstatus(buf) == "finished"})
898
899 " close the terminal window where Vim was running
900 quit
901endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100902
903" Run Vim in a terminal, then start a terminal in that Vim without a kill
904" argument, check that :confirm qall works.
905func Test_terminal_qall_prompt()
906 if !CanRunVimInTerminal()
907 return
908 endif
909 let buf = RunVimInTerminal('', {})
910
911 " Open a terminal window and wait for the prompt to appear
912 call term_sendkeys(buf, ":term\<CR>")
913 call WaitFor({-> term_getline(buf, 10) =~ '\[running]'})
914 call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'})
915
916 " make Vim exit, it will prompt to kill the shell
917 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
918 call WaitFor({-> term_getline(buf, 20) =~ 'ancel:'})
919 call term_sendkeys(buf, "y")
920 call WaitFor({-> term_getstatus(buf) == "finished"})
921
922 " close the terminal window where Vim was running
923 quit
924endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100925
Bram Moolenaar012eb662018-03-13 17:55:27 +0100926func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100927 augroup repro
928 au!
929 au TerminalOpen * let s:called += 1
930 augroup END
931
932 let s:called = 0
933
934 " Open a terminal window with :terminal
935 terminal
936 call assert_equal(1, s:called)
937 bwipe!
938
939 " Open a terminal window with term_start()
940 call term_start(&shell)
941 call assert_equal(2, s:called)
942 bwipe!
943
944 " Open a hidden terminal buffer with :terminal
945 terminal ++hidden
946 call assert_equal(3, s:called)
947 for buf in term_list()
948 exe buf . "bwipe!"
949 endfor
950
951 " Open a hidden terminal buffer with term_start()
952 let buf = term_start(&shell, {'hidden': 1})
953 call assert_equal(4, s:called)
954 exe buf . "bwipe!"
955
956 unlet s:called
957 au! repro
958endfunction