blob: 0e04abd5c13865d567f673c277d4e7a1a22d658a [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
8
Bram Moolenaarb81bc772017-08-11 22:45:01 +02009let s:python = PythonProg()
10
Bram Moolenaar94053a52017-08-01 21:44:33 +020011" Open a terminal with a shell, assign the job to g:job and return the buffer
12" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020013func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010014 if has('win32')
15 let buf = term_start([&shell,'/k'], a:options)
16 else
17 let buf = term_start(&shell, a:options)
18 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020019
20 let termlist = term_list()
21 call assert_equal(1, len(termlist))
22 call assert_equal(buf, termlist[0])
23
24 let g:job = term_getjob(buf)
25 call assert_equal(v:t_job, type(g:job))
26
Bram Moolenaar35422f42017-08-05 16:33:56 +020027 let string = string({'job': term_getjob(buf)})
28 call assert_match("{'job': 'process \\d\\+ run'}", string)
29
Bram Moolenaar94053a52017-08-01 21:44:33 +020030 return buf
31endfunc
32
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020033func Test_terminal_basic()
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020034 au BufWinEnter * if &buftype == 'terminal' | let b:done = 'yes' | endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +020035 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020036
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020037 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020038 call assert_match('^/dev/', job_info(g:job).tty_out)
39 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020040 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020041 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
42 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020043 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020044 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020045 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020046 call assert_match('%aR[^\n]*running]', execute('ls'))
47
Bram Moolenaar94053a52017-08-01 21:44:33 +020048 call Stop_shell_in_terminal(buf)
49 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020050 call assert_equal('n', mode())
51 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020052
Bram Moolenaar94053a52017-08-01 21:44:33 +020053 " closing window wipes out the terminal buffer a with finished job
54 close
55 call assert_equal("", bufname(buf))
56
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020057 au! BufWinEnter
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020058 unlet g:job
59endfunc
60
61func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020062 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020064 call term_wait(buf)
65
66 setlocal modifiable
67 exe "normal Axxx\<Esc>"
68 call assert_fails(buf . 'bwipe', 'E517')
69 undo
70
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020071 exe buf . 'bwipe'
72 unlet g:job
73endfunc
74
Bram Moolenaar94053a52017-08-01 21:44:33 +020075func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020076 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020077 call assert_fails(buf . 'bwipe', 'E517')
78 exe buf . 'bwipe!'
Bram Moolenaar94053a52017-08-01 21:44:33 +020079 call WaitFor('job_status(g:job) == "dead"')
80 call assert_equal('dead', job_status(g:job))
81 call assert_equal("", bufname(buf))
82
83 unlet g:job
84endfunc
85
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020086func Test_terminal_split_quit()
87 let buf = Run_shell_in_terminal({})
88 call term_wait(buf)
89 split
90 quit!
91 call term_wait(buf)
92 sleep 50m
93 call assert_equal('run', job_status(g:job))
94
95 quit!
96 call WaitFor('job_status(g:job) == "dead"')
97 call assert_equal('dead', job_status(g:job))
98
99 exe buf . 'bwipe'
100 unlet g:job
101endfunc
102
Bram Moolenaar94053a52017-08-01 21:44:33 +0200103func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200104 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200105 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200106 quit
107 for nr in range(1, winnr('$'))
108 call assert_notequal(winbufnr(nr), buf)
109 endfor
110 call assert_true(bufloaded(buf))
111 call assert_true(buflisted(buf))
112
113 exe 'split ' . buf . 'buf'
114 call Stop_shell_in_terminal(buf)
115 exe buf . 'bwipe'
116
117 unlet g:job
118endfunc
119
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200120func! s:Nasty_exit_cb(job, st)
121 exe g:buf . 'bwipe!'
122 let g:buf = 0
123endfunc
124
Bram Moolenaar9d189612017-09-09 18:11:00 +0200125func Get_cat_123_cmd()
126 if has('win32')
127 return 'cmd /c "cls && color 2 && echo 123"'
128 else
129 call writefile(["\<Esc>[32m123"], 'Xtext')
130 return "cat Xtext"
131 endif
132endfunc
133
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200134func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200135 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200136 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
137 let g:job = term_getjob(g:buf)
138
139 call WaitFor('job_status(g:job) == "dead"')
140 call WaitFor('g:buf == 0')
141 unlet g:buf
142 unlet g:job
143 call delete('Xtext')
144endfunc
145
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200146func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200147 let l = term_scrape(a:buf, 0)
148 call assert_true(len(l) == 0)
149 let l = term_scrape(a:buf, 999)
150 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200151 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200152 call assert_true(len(l) > 0)
153 call assert_equal('1', l[0].chars)
154 call assert_equal('2', l[1].chars)
155 call assert_equal('3', l[2].chars)
156 call assert_equal('#00e000', l[0].fg)
157 if &background == 'light'
158 call assert_equal('#ffffff', l[0].bg)
159 else
160 call assert_equal('#000000', l[0].bg)
161 endif
162
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200163 let l = term_getline(a:buf, -1)
164 call assert_equal('', l)
165 let l = term_getline(a:buf, 0)
166 call assert_equal('', l)
167 let l = term_getline(a:buf, 999)
168 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200169 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200170 call assert_equal('123', l)
171endfunc
172
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200173func Test_terminal_scrape_123()
174 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200175 let buf = term_start(cmd)
176
177 let termlist = term_list()
178 call assert_equal(1, len(termlist))
179 call assert_equal(buf, termlist[0])
180
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200181 " Nothing happens with invalid buffer number
182 call term_wait(1234)
183
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200184 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200185 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200186 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100187 call WaitFor({-> len(term_scrape(buf, 1)) == 3})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200188 call Check_123(buf)
189
190 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100191 let job = term_getjob(buf)
192 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200193 call term_wait(buf)
194 call Check_123(buf)
195
196 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200197 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200198endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200199
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200200func Test_terminal_scrape_multibyte()
201 if !has('multi_byte')
202 return
203 endif
204 call writefile(["léttまrs"], 'Xtext')
205 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200206 " Run cmd with UTF-8 codepage to make the type command print the expected
207 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100208 let buf = term_start("cmd /K chcp 65001")
209 call term_sendkeys(buf, "type Xtext\<CR>")
210 call term_sendkeys(buf, "exit\<CR>")
211 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200212 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100213 let buf = term_start("cat Xtext")
214 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200215 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200216
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100217 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
218 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200219 call assert_true(len(l) >= 7)
220 call assert_equal('l', l[0].chars)
221 call assert_equal('é', l[1].chars)
222 call assert_equal(1, l[1].width)
223 call assert_equal('t', l[2].chars)
224 call assert_equal('t', l[3].chars)
225 call assert_equal('ま', l[4].chars)
226 call assert_equal(2, l[4].width)
227 call assert_equal('r', l[5].chars)
228 call assert_equal('s', l[6].chars)
229
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100230 let job = term_getjob(buf)
231 call WaitFor({-> job_status(job) == "dead"})
232 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200233
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100234 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200235 call delete('Xtext')
236endfunc
237
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200238func Test_terminal_scroll()
239 call writefile(range(1, 200), 'Xtext')
240 if has('win32')
241 let cmd = 'cmd /c "type Xtext"'
242 else
243 let cmd = "cat Xtext"
244 endif
245 let buf = term_start(cmd)
246
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100247 let job = term_getjob(buf)
248 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200249 call term_wait(buf)
250 if has('win32')
251 " TODO: this should not be needed
252 sleep 100m
253 endif
254
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200255 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200256 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200257 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200258 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200259 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200260 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200261 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200262
263 exe buf . 'bwipe'
264 call delete('Xtext')
265endfunc
266
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200267func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200268 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200269
Bram Moolenaarb2412082017-08-20 18:09:14 +0200270 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200271 let size = term_getsize('')
272 bwipe!
273 call assert_equal(5, size[0])
274
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200275 call term_start(cmd, {'term_rows': 6})
276 let size = term_getsize('')
277 bwipe!
278 call assert_equal(6, size[0])
279
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200280 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200281 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200282 let size = term_getsize('')
283 bwipe!
284 call assert_equal([5, 33], size)
285
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200286 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
287 let size = term_getsize('')
288 bwipe!
289 call assert_equal([6, 36], size)
290
Bram Moolenaarb2412082017-08-20 18:09:14 +0200291 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200292 let size = term_getsize('')
293 bwipe!
294 call assert_equal(20, size[1])
295
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200296 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
297 let size = term_getsize('')
298 bwipe!
299 call assert_equal(26, size[1])
300
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200301 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200302 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200303 let size = term_getsize('')
304 bwipe!
305 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200306
307 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
308 let size = term_getsize('')
309 bwipe!
310 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200311
312 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200313endfunc
314
315func Test_terminal_curwin()
316 let cmd = Get_cat_123_cmd()
317 call assert_equal(1, winnr('$'))
318
319 split dummy
320 exe 'terminal ++curwin ' . cmd
321 call assert_equal(2, winnr('$'))
322 bwipe!
323
324 split dummy
325 call term_start(cmd, {'curwin': 1})
326 call assert_equal(2, winnr('$'))
327 bwipe!
328
329 split dummy
330 call setline(1, 'change')
331 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
332 call assert_equal(2, winnr('$'))
333 exe 'terminal! ++curwin ' . cmd
334 call assert_equal(2, winnr('$'))
335 bwipe!
336
337 split dummy
338 call setline(1, 'change')
339 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
340 call assert_equal(2, winnr('$'))
341 bwipe!
342
343 split dummy
344 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200345 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200346endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200347
Bram Moolenaarff546792017-11-21 14:47:57 +0100348func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200349 if s:python != ''
350 let cmd = s:python . " test_short_sleep.py"
351 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200352 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200353 echo 'This will take five seconds...'
354 let waittime = 2000
355 if has('win32')
356 let cmd = $windir . '\system32\timeout.exe 1'
357 else
358 let cmd = 'sleep 1'
359 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200360 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100361 return [cmd, waittime]
362endfunc
363
364func Test_terminal_finish_open_close()
365 call assert_equal(1, winnr('$'))
366
367 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200368
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200369 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200370 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200371 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200372 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200373
374 call term_start(cmd, {'term_finish': 'close'})
375 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200376 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200377 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200378 call assert_equal(1, winnr('$'))
379
380 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200381 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200382 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200383 call assert_equal(2, winnr('$'))
384 bwipe
385
386 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200387 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200388 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200389 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200390 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200391
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200392 exe 'terminal ++hidden ++open ' . cmd
393 call assert_equal(1, winnr('$'))
394 call WaitFor("winnr('$') == 2", waittime)
395 call assert_equal(2, winnr('$'))
396 bwipe
397
398 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
399 call assert_equal(1, winnr('$'))
400 call WaitFor("winnr('$') == 2", waittime)
401 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200402 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200403
404 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
405 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
406 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
407 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
408
409 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200410 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200411 call WaitFor("winnr('$') == 2", waittime)
412 call assert_equal(2, winnr('$'))
413 call assert_equal(4, winheight(0))
414 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200415endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200416
417func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200418 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200419 return
420 endif
421 call mkdir('Xdir')
422 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200423 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
424 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200425
426 exe buf . 'bwipe'
427 call delete('Xdir', 'rf')
428endfunc
429
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100430func Test_terminal_servername()
431 if !has('clientserver')
432 return
433 endif
434 let g:buf = Run_shell_in_terminal({})
435 " Wait for the shell to display a prompt
436 call WaitFor('term_getline(g:buf, 1) != ""')
437 if has('win32')
438 call term_sendkeys(g:buf, "echo %VIM_SERVERNAME%\r")
439 else
440 call term_sendkeys(g:buf, "echo $VIM_SERVERNAME\r")
441 endif
442 call term_wait(g:buf)
443 call Stop_shell_in_terminal(g:buf)
444 call WaitFor('getline(2) == v:servername')
445 call assert_equal(v:servername, getline(2))
446
447 exe g:buf . 'bwipe'
448 unlet g:buf
449endfunc
450
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200451func Test_terminal_env()
Bram Moolenaarc0870612017-08-14 22:01:16 +0200452 let g:buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200453 " Wait for the shell to display a prompt
Bram Moolenaarc0870612017-08-14 22:01:16 +0200454 call WaitFor('term_getline(g:buf, 1) != ""')
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100455 if has('win32')
456 call term_sendkeys(g:buf, "echo %TESTENV%\r")
457 else
458 call term_sendkeys(g:buf, "echo $TESTENV\r")
459 endif
Bram Moolenaarc0870612017-08-14 22:01:16 +0200460 call term_wait(g:buf)
461 call Stop_shell_in_terminal(g:buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200462 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200463 call assert_equal('correct', getline(2))
464
Bram Moolenaarc0870612017-08-14 22:01:16 +0200465 exe g:buf . 'bwipe'
466 unlet g:buf
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 Moolenaare738a1a2017-09-16 17:42:41 +0200593 let g:buf = buf
594 call WaitFor('term_getline(g:buf, 1) =~ "look here"')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200595
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200596 call assert_match('look here', term_getline(buf, 1))
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200597 bwipe!
598endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200599
600func Test_terminal_special_chars()
601 " this file name only works on Unix
602 if !has('unix')
603 return
604 endif
605 call mkdir('Xdir with spaces')
606 call writefile(['x'], 'Xdir with spaces/quoted"file')
607 term ls Xdir\ with\ spaces/quoted\"file
608 call WaitFor('term_getline("", 1) =~ "quoted"')
609 call assert_match('quoted"file', term_getline('', 1))
610 call term_wait('')
611
612 call delete('Xdir with spaces', 'rf')
613 bwipe
614endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200615
616func Test_terminal_wrong_options()
617 call assert_fails('call term_start(&shell, {
618 \ "in_io": "file",
619 \ "in_name": "xxx",
620 \ "out_io": "file",
621 \ "out_name": "xxx",
622 \ "err_io": "file",
623 \ "err_name": "xxx"
624 \ })', 'E474:')
625 call assert_fails('call term_start(&shell, {
626 \ "out_buf": bufnr("%")
627 \ })', 'E474:')
628 call assert_fails('call term_start(&shell, {
629 \ "err_buf": bufnr("%")
630 \ })', 'E474:')
631endfunc
632
633func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200634 " TODO: this should work on MS-Window
635 if has('unix')
636 let cmd = Get_cat_123_cmd()
637 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
638 call term_wait(buf)
639 call WaitFor('len(readfile("Xfile")) > 0')
640 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200641 let g:job = term_getjob(buf)
642 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200643 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200644 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200645 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200646
647 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200648 call writefile(['one line'], 'Xfile')
649 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
650 call term_wait(buf)
651 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
652 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200653 let g:job = term_getjob(buf)
654 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200655 bwipe
656 call delete('Xfile')
657 endif
658endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200659
660func TerminalTmap(remap)
661 let buf = Run_shell_in_terminal({})
662 call assert_equal('t', mode())
663
664 if a:remap
665 tmap 123 456
666 else
667 tnoremap 123 456
668 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100669 " don't use abcde, it's an existing command
670 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200671 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100672 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200673 call feedkeys("123", 'tx')
Bram Moolenaar1514e8f2017-09-16 17:35:13 +0200674 let g:buf = buf
Bram Moolenaar461fe502017-12-05 12:30:03 +0100675 call WaitFor("term_getline(g:buf,term_getcursor(g:buf)[0]) =~ 'abxde\\|456'")
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200676 let lnum = term_getcursor(buf)[0]
677 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100678 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200679 else
680 call assert_match('456', term_getline(buf, lnum))
681 endif
682
683 call term_sendkeys(buf, "\r")
684 call Stop_shell_in_terminal(buf)
685 call term_wait(buf)
686
687 tunmap 123
688 tunmap 456
689 call assert_equal('', maparg('123', 't'))
690 close
691 unlet g:job
692endfunc
693
694func Test_terminal_tmap()
695 call TerminalTmap(1)
696 call TerminalTmap(0)
697endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200698
699func Test_terminal_wall()
700 let buf = Run_shell_in_terminal({})
701 wall
702 call Stop_shell_in_terminal(buf)
703 call term_wait(buf)
704 exe buf . 'bwipe'
705 unlet g:job
706endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200707
Bram Moolenaar7a760922018-02-19 23:10:02 +0100708func Test_terminal_wqall()
709 let buf = Run_shell_in_terminal({})
710 call assert_fails('wqall', 'E948')
711 call Stop_shell_in_terminal(buf)
712 call term_wait(buf)
713 exe buf . 'bwipe'
714 unlet g:job
715endfunc
716
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200717func Test_terminal_composing_unicode()
718 let save_enc = &encoding
719 set encoding=utf-8
720
721 if has('win32')
722 let cmd = "cmd /K chcp 65001"
723 let lnum = [3, 6, 9]
724 else
725 let cmd = &shell
726 let lnum = [1, 3, 5]
727 endif
728
729 enew
730 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100731 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200732 call term_wait(buf, 50)
733
734 " ascii + composing
735 let txt = "a\u0308bc"
736 call term_sendkeys(buf, "echo " . txt . "\r")
737 call term_wait(buf, 50)
738 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
739 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
740 let l = term_scrape(buf, lnum[0] + 1)
741 call assert_equal("a\u0308", l[0].chars)
742 call assert_equal("b", l[1].chars)
743 call assert_equal("c", l[2].chars)
744
745 " multibyte + composing
746 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
747 call term_sendkeys(buf, "echo " . txt . "\r")
748 call term_wait(buf, 50)
749 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
750 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
751 let l = term_scrape(buf, lnum[1] + 1)
752 call assert_equal("\u304b\u3099", l[0].chars)
753 call assert_equal("\u304e", l[1].chars)
754 call assert_equal("\u304f\u3099", l[2].chars)
755 call assert_equal("\u3052", l[3].chars)
756 call assert_equal("\u3053\u3099", l[4].chars)
757
758 " \u00a0 + composing
759 let txt = "abc\u00a0\u0308"
760 call term_sendkeys(buf, "echo " . txt . "\r")
761 call term_wait(buf, 50)
762 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
763 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
764 let l = term_scrape(buf, lnum[2] + 1)
765 call assert_equal("\u00a0\u0308", l[3].chars)
766
767 call term_sendkeys(buf, "exit\r")
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100768 call WaitFor('job_status(g:job) == "dead"')
769 call assert_equal('dead', job_status(g:job))
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200770 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100771 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200772 let &encoding = save_enc
773endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100774
775func Test_terminal_aucmd_on_close()
776 fun Nop()
777 let s:called = 1
778 endfun
779
780 aug repro
781 au!
782 au BufWinLeave * call Nop()
783 aug END
784
785 let [cmd, waittime] = s:get_sleep_cmd()
786
787 call assert_equal(1, winnr('$'))
788 new
789 call setline(1, ['one', 'two'])
790 exe 'term ++close ' . cmd
791 wincmd p
792 call WaitFor("winnr('$') == 2", waittime)
793 call assert_equal(1, s:called)
794 bwipe!
795
796 unlet s:called
797 au! repro
798 delfunc Nop
799endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100800
801func Test_terminal_term_start_empty_command()
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({}, {'curwin' : 1, 'term_finish' : 'close'})"
807 call assert_fails(cmd, 'E474')
808 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
809 call assert_fails(cmd, 'E474')
810endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100811
812func Test_terminal_response_to_control_sequence()
813 if !has('unix')
814 return
815 endif
816
817 let buf = Run_shell_in_terminal({})
818 call term_wait(buf)
819
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100820 new
821 call setline(1, "\x1b[6n")
822 write! Xescape
823 bwipe
824 call term_sendkeys(buf, "cat Xescape\<cr>")
825
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100826 " wait for the response of control sequence from libvterm (and send it to tty)
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100827 sleep 200m
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100828 call term_wait(buf)
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100829
830 " Wait for output from tty to display, below an empty line.
831 " It should show \e3;1R, but only 1R may show up
832 call assert_match('\<\d\+R', term_getline(buf, 3))
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100833
834 call term_sendkeys(buf, "\<c-c>")
835 call term_wait(buf)
836 call Stop_shell_in_terminal(buf)
837
838 exe buf . 'bwipe'
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100839 call delete('Xescape')
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100840 unlet g:job
841endfunc