blob: 295ccf1c9682a967eb27ad170559a55ef0c0ec46 [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
33" Stops the shell started by Run_shell_in_terminal().
34func Stop_shell_in_terminal(buf)
35 call term_sendkeys(a:buf, "exit\r")
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020036 call WaitFor('job_status(g:job) == "dead"')
37 call assert_equal('dead', job_status(g:job))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020038endfunc
39
40func Test_terminal_basic()
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020041 au BufWinEnter * if &buftype == 'terminal' | let b:done = 'yes' | endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +020042 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020043
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020044 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020045 call assert_match('^/dev/', job_info(g:job).tty_out)
46 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020047 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020048 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
49 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020050 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020051 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020052 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020053 call assert_match('%aR[^\n]*running]', execute('ls'))
54
Bram Moolenaar94053a52017-08-01 21:44:33 +020055 call Stop_shell_in_terminal(buf)
56 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020057 call assert_equal('n', mode())
58 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020059
Bram Moolenaar94053a52017-08-01 21:44:33 +020060 " closing window wipes out the terminal buffer a with finished job
61 close
62 call assert_equal("", bufname(buf))
63
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020064 au! BufWinEnter
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020065 unlet g:job
66endfunc
67
68func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020069 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020070 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020071 call term_wait(buf)
72
73 setlocal modifiable
74 exe "normal Axxx\<Esc>"
75 call assert_fails(buf . 'bwipe', 'E517')
76 undo
77
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020078 exe buf . 'bwipe'
79 unlet g:job
80endfunc
81
Bram Moolenaar94053a52017-08-01 21:44:33 +020082func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020083 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020084 call assert_fails(buf . 'bwipe', 'E517')
85 exe buf . 'bwipe!'
Bram Moolenaar94053a52017-08-01 21:44:33 +020086 call WaitFor('job_status(g:job) == "dead"')
87 call assert_equal('dead', job_status(g:job))
88 call assert_equal("", bufname(buf))
89
90 unlet g:job
91endfunc
92
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020093func Test_terminal_split_quit()
94 let buf = Run_shell_in_terminal({})
95 call term_wait(buf)
96 split
97 quit!
98 call term_wait(buf)
99 sleep 50m
100 call assert_equal('run', job_status(g:job))
101
102 quit!
103 call WaitFor('job_status(g:job) == "dead"')
104 call assert_equal('dead', job_status(g:job))
105
106 exe buf . 'bwipe'
107 unlet g:job
108endfunc
109
Bram Moolenaar94053a52017-08-01 21:44:33 +0200110func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200111 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200112 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200113 quit
114 for nr in range(1, winnr('$'))
115 call assert_notequal(winbufnr(nr), buf)
116 endfor
117 call assert_true(bufloaded(buf))
118 call assert_true(buflisted(buf))
119
120 exe 'split ' . buf . 'buf'
121 call Stop_shell_in_terminal(buf)
122 exe buf . 'bwipe'
123
124 unlet g:job
125endfunc
126
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200127func! s:Nasty_exit_cb(job, st)
128 exe g:buf . 'bwipe!'
129 let g:buf = 0
130endfunc
131
Bram Moolenaar9d189612017-09-09 18:11:00 +0200132func Get_cat_123_cmd()
133 if has('win32')
134 return 'cmd /c "cls && color 2 && echo 123"'
135 else
136 call writefile(["\<Esc>[32m123"], 'Xtext')
137 return "cat Xtext"
138 endif
139endfunc
140
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200141func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200142 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200143 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
144 let g:job = term_getjob(g:buf)
145
146 call WaitFor('job_status(g:job) == "dead"')
147 call WaitFor('g:buf == 0')
148 unlet g:buf
149 unlet g:job
150 call delete('Xtext')
151endfunc
152
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200153func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200154 let l = term_scrape(a:buf, 0)
155 call assert_true(len(l) == 0)
156 let l = term_scrape(a:buf, 999)
157 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200158 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200159 call assert_true(len(l) > 0)
160 call assert_equal('1', l[0].chars)
161 call assert_equal('2', l[1].chars)
162 call assert_equal('3', l[2].chars)
163 call assert_equal('#00e000', l[0].fg)
164 if &background == 'light'
165 call assert_equal('#ffffff', l[0].bg)
166 else
167 call assert_equal('#000000', l[0].bg)
168 endif
169
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200170 let l = term_getline(a:buf, -1)
171 call assert_equal('', l)
172 let l = term_getline(a:buf, 0)
173 call assert_equal('', l)
174 let l = term_getline(a:buf, 999)
175 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200176 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200177 call assert_equal('123', l)
178endfunc
179
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200180func Test_terminal_scrape_123()
181 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200182 let buf = term_start(cmd)
183
184 let termlist = term_list()
185 call assert_equal(1, len(termlist))
186 call assert_equal(buf, termlist[0])
187
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200188 " Nothing happens with invalid buffer number
189 call term_wait(1234)
190
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200191 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200192 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200193 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100194 call WaitFor({-> len(term_scrape(buf, 1)) == 3})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200195 call Check_123(buf)
196
197 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100198 let job = term_getjob(buf)
199 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200200 call term_wait(buf)
201 call Check_123(buf)
202
203 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200204 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200205endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200206
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200207func Test_terminal_scrape_multibyte()
208 if !has('multi_byte')
209 return
210 endif
211 call writefile(["léttrs"], 'Xtext')
212 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200213 " Run cmd with UTF-8 codepage to make the type command print the expected
214 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100215 let buf = term_start("cmd /K chcp 65001")
216 call term_sendkeys(buf, "type Xtext\<CR>")
217 call term_sendkeys(buf, "exit\<CR>")
218 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200219 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100220 let buf = term_start("cat Xtext")
221 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200222 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200223
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100224 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
225 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200226 call assert_true(len(l) >= 7)
227 call assert_equal('l', l[0].chars)
228 call assert_equal('é', l[1].chars)
229 call assert_equal(1, l[1].width)
230 call assert_equal('t', l[2].chars)
231 call assert_equal('t', l[3].chars)
232 call assert_equal('ま', l[4].chars)
233 call assert_equal(2, l[4].width)
234 call assert_equal('r', l[5].chars)
235 call assert_equal('s', l[6].chars)
236
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100237 let job = term_getjob(buf)
238 call WaitFor({-> job_status(job) == "dead"})
239 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200240
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100241 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200242 call delete('Xtext')
243endfunc
244
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200245func Test_terminal_scroll()
246 call writefile(range(1, 200), 'Xtext')
247 if has('win32')
248 let cmd = 'cmd /c "type Xtext"'
249 else
250 let cmd = "cat Xtext"
251 endif
252 let buf = term_start(cmd)
253
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100254 let job = term_getjob(buf)
255 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200256 call term_wait(buf)
257 if has('win32')
258 " TODO: this should not be needed
259 sleep 100m
260 endif
261
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200262 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200263 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200264 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200265 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200266 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200267 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200268 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200269
270 exe buf . 'bwipe'
271 call delete('Xtext')
272endfunc
273
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200274func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200275 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200276
Bram Moolenaarb2412082017-08-20 18:09:14 +0200277 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200278 let size = term_getsize('')
279 bwipe!
280 call assert_equal(5, size[0])
281
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200282 call term_start(cmd, {'term_rows': 6})
283 let size = term_getsize('')
284 bwipe!
285 call assert_equal(6, size[0])
286
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200287 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200288 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200289 let size = term_getsize('')
290 bwipe!
291 call assert_equal([5, 33], size)
292
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200293 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
294 let size = term_getsize('')
295 bwipe!
296 call assert_equal([6, 36], size)
297
Bram Moolenaarb2412082017-08-20 18:09:14 +0200298 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200299 let size = term_getsize('')
300 bwipe!
301 call assert_equal(20, size[1])
302
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200303 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
304 let size = term_getsize('')
305 bwipe!
306 call assert_equal(26, size[1])
307
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200308 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200309 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200310 let size = term_getsize('')
311 bwipe!
312 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200313
314 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
315 let size = term_getsize('')
316 bwipe!
317 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200318
319 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200320endfunc
321
322func Test_terminal_curwin()
323 let cmd = Get_cat_123_cmd()
324 call assert_equal(1, winnr('$'))
325
326 split dummy
327 exe 'terminal ++curwin ' . cmd
328 call assert_equal(2, winnr('$'))
329 bwipe!
330
331 split dummy
332 call term_start(cmd, {'curwin': 1})
333 call assert_equal(2, winnr('$'))
334 bwipe!
335
336 split dummy
337 call setline(1, 'change')
338 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
339 call assert_equal(2, winnr('$'))
340 exe 'terminal! ++curwin ' . cmd
341 call assert_equal(2, winnr('$'))
342 bwipe!
343
344 split dummy
345 call setline(1, 'change')
346 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
347 call assert_equal(2, winnr('$'))
348 bwipe!
349
350 split dummy
351 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200352 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200353endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200354
Bram Moolenaarff546792017-11-21 14:47:57 +0100355func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200356 if s:python != ''
357 let cmd = s:python . " test_short_sleep.py"
358 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200359 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200360 echo 'This will take five seconds...'
361 let waittime = 2000
362 if has('win32')
363 let cmd = $windir . '\system32\timeout.exe 1'
364 else
365 let cmd = 'sleep 1'
366 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200367 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100368 return [cmd, waittime]
369endfunc
370
371func Test_terminal_finish_open_close()
372 call assert_equal(1, winnr('$'))
373
374 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200375
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200376 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200377 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200378 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200379 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200380
381 call term_start(cmd, {'term_finish': 'close'})
382 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200383 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200384 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200385 call assert_equal(1, winnr('$'))
386
387 exe 'terminal ++open ' . cmd
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('$'))
391 bwipe
392
393 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200394 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200395 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200396 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200397 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200398
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200399 exe 'terminal ++hidden ++open ' . cmd
400 call assert_equal(1, winnr('$'))
401 call WaitFor("winnr('$') == 2", waittime)
402 call assert_equal(2, winnr('$'))
403 bwipe
404
405 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
406 call assert_equal(1, winnr('$'))
407 call WaitFor("winnr('$') == 2", waittime)
408 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200409 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200410
411 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
412 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
413 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
414 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
415
416 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200417 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200418 call WaitFor("winnr('$') == 2", waittime)
419 call assert_equal(2, winnr('$'))
420 call assert_equal(4, winheight(0))
421 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200422endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200423
424func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200425 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200426 return
427 endif
428 call mkdir('Xdir')
429 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200430 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
431 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200432
433 exe buf . 'bwipe'
434 call delete('Xdir', 'rf')
435endfunc
436
437func Test_terminal_env()
Bram Moolenaarc0870612017-08-14 22:01:16 +0200438 let g:buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200439 " Wait for the shell to display a prompt
Bram Moolenaarc0870612017-08-14 22:01:16 +0200440 call WaitFor('term_getline(g:buf, 1) != ""')
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100441 if has('win32')
442 call term_sendkeys(g:buf, "echo %TESTENV%\r")
443 else
444 call term_sendkeys(g:buf, "echo $TESTENV\r")
445 endif
Bram Moolenaarc0870612017-08-14 22:01:16 +0200446 call term_wait(g:buf)
447 call Stop_shell_in_terminal(g:buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200448 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200449 call assert_equal('correct', getline(2))
450
Bram Moolenaarc0870612017-08-14 22:01:16 +0200451 exe g:buf . 'bwipe'
452 unlet g:buf
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200453endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200454
455" must be last, we can't go back from GUI to terminal
456func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200457 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200458 return
459 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200460
461 " Ignore the "failed to create input context" error.
462 call test_ignore_error('E285:')
463
Bram Moolenaar679653e2017-08-13 14:13:19 +0200464 gui -f
465
466 call assert_equal(1, winnr('$'))
467 let buf = Run_shell_in_terminal({'term_finish': 'close'})
468 call Stop_shell_in_terminal(buf)
469 call term_wait(buf)
470
471 " closing window wipes out the terminal buffer a with finished job
472 call WaitFor("winnr('$') == 1")
473 call assert_equal(1, winnr('$'))
474 call assert_equal("", bufname(buf))
475
476 unlet g:job
477endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200478
479func Test_terminal_list_args()
480 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
481 call assert_fails(buf . 'bwipe', 'E517')
482 exe buf . 'bwipe!'
483 call assert_equal("", bufname(buf))
484endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200485
486func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100487 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200488 if has('mac')
489 " The shell or something else has a problem dealing with more than 1000
490 " characters at the same time.
491 let len = 1000
492 else
493 let len = 5000
494 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200495
496 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100497 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200498 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100499 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200500
501 " On MS-Windows there is an extra empty line below "done". Find "done" in
502 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100503 let lnum = term_getsize(buf)[0] - 1
504 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 3000)
505 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200506 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100507 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200508 endif
509 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200510
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100511 let g:job = term_getjob(buf)
512 call Stop_shell_in_terminal(buf)
513 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200514 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200515 bwipe
516endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200517
518func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200519 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200520 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200521 endif
522 new
523 call setline(1, ['one', 'two', 'three'])
524 %term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200525 call WaitFor('getline("$") =~ "3"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200526 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200527 call assert_equal(['3', '3', '14'], nrs)
528 bwipe
529
Bram Moolenaardada6d22017-09-02 17:18:35 +0200530 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200531 call setline(1, ['one', 'two', 'three', 'four'])
532 2,3term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200533 call WaitFor('getline("$") =~ "2"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200534 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200535 call assert_equal(['2', '2', '10'], nrs)
536 bwipe
537
Bram Moolenaardada6d22017-09-02 17:18:35 +0200538 if executable('python')
539 new
540 call setline(1, ['print("hello")'])
541 1term ++eof=exit() python
542 " MS-Windows echoes the input, Unix doesn't.
543 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
544 if getline(1) =~ 'hello'
545 call assert_equal('hello', getline(1))
546 else
547 call assert_equal('hello', getline(line('$') - 1))
548 endif
549 bwipe
550
551 if has('win32')
552 new
553 call setline(1, ['print("hello")'])
554 1term ++eof=<C-Z> python
555 call WaitFor('getline("$") =~ "Z"')
556 call assert_equal('hello', getline(line('$') - 1))
557 bwipe
558 endif
559 endif
560
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200561 bwipe!
562endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200563
564func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200565 " Todo: make this work in the GUI
566 if !has('gui_running')
567 return
568 endif
569 let buf = term_start('NONE', {})
570 call assert_notequal(0, buf)
571
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200572 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200573 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200574 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200575 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200576 else
577 call system('echo "look here" > ' . pty)
578 endif
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200579 let g:buf = buf
580 call WaitFor('term_getline(g:buf, 1) =~ "look here"')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200581
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200582 call assert_match('look here', term_getline(buf, 1))
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200583 bwipe!
584endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200585
586func Test_terminal_special_chars()
587 " this file name only works on Unix
588 if !has('unix')
589 return
590 endif
591 call mkdir('Xdir with spaces')
592 call writefile(['x'], 'Xdir with spaces/quoted"file')
593 term ls Xdir\ with\ spaces/quoted\"file
594 call WaitFor('term_getline("", 1) =~ "quoted"')
595 call assert_match('quoted"file', term_getline('', 1))
596 call term_wait('')
597
598 call delete('Xdir with spaces', 'rf')
599 bwipe
600endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200601
602func Test_terminal_wrong_options()
603 call assert_fails('call term_start(&shell, {
604 \ "in_io": "file",
605 \ "in_name": "xxx",
606 \ "out_io": "file",
607 \ "out_name": "xxx",
608 \ "err_io": "file",
609 \ "err_name": "xxx"
610 \ })', 'E474:')
611 call assert_fails('call term_start(&shell, {
612 \ "out_buf": bufnr("%")
613 \ })', 'E474:')
614 call assert_fails('call term_start(&shell, {
615 \ "err_buf": bufnr("%")
616 \ })', 'E474:')
617endfunc
618
619func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200620 " TODO: this should work on MS-Window
621 if has('unix')
622 let cmd = Get_cat_123_cmd()
623 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
624 call term_wait(buf)
625 call WaitFor('len(readfile("Xfile")) > 0')
626 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200627 let g:job = term_getjob(buf)
628 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200629 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200630 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200631 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200632
633 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200634 call writefile(['one line'], 'Xfile')
635 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
636 call term_wait(buf)
637 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
638 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200639 let g:job = term_getjob(buf)
640 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200641 bwipe
642 call delete('Xfile')
643 endif
644endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200645
646func TerminalTmap(remap)
647 let buf = Run_shell_in_terminal({})
648 call assert_equal('t', mode())
649
650 if a:remap
651 tmap 123 456
652 else
653 tnoremap 123 456
654 endif
655 tmap 456 abcde
656 call assert_equal('456', maparg('123', 't'))
657 call assert_equal('abcde', maparg('456', 't'))
658 call feedkeys("123", 'tx')
Bram Moolenaar1514e8f2017-09-16 17:35:13 +0200659 let g:buf = buf
660 call WaitFor("term_getline(g:buf,term_getcursor(g:buf)[0]) =~ 'abcde\\|456'")
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200661 let lnum = term_getcursor(buf)[0]
662 if a:remap
663 call assert_match('abcde', term_getline(buf, lnum))
664 else
665 call assert_match('456', term_getline(buf, lnum))
666 endif
667
668 call term_sendkeys(buf, "\r")
669 call Stop_shell_in_terminal(buf)
670 call term_wait(buf)
671
672 tunmap 123
673 tunmap 456
674 call assert_equal('', maparg('123', 't'))
675 close
676 unlet g:job
677endfunc
678
679func Test_terminal_tmap()
680 call TerminalTmap(1)
681 call TerminalTmap(0)
682endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200683
684func Test_terminal_wall()
685 let buf = Run_shell_in_terminal({})
686 wall
687 call Stop_shell_in_terminal(buf)
688 call term_wait(buf)
689 exe buf . 'bwipe'
690 unlet g:job
691endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200692
693func Test_terminal_composing_unicode()
694 let save_enc = &encoding
695 set encoding=utf-8
696
697 if has('win32')
698 let cmd = "cmd /K chcp 65001"
699 let lnum = [3, 6, 9]
700 else
701 let cmd = &shell
702 let lnum = [1, 3, 5]
703 endif
704
705 enew
706 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100707 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200708 call term_wait(buf, 50)
709
710 " ascii + composing
711 let txt = "a\u0308bc"
712 call term_sendkeys(buf, "echo " . txt . "\r")
713 call term_wait(buf, 50)
714 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
715 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
716 let l = term_scrape(buf, lnum[0] + 1)
717 call assert_equal("a\u0308", l[0].chars)
718 call assert_equal("b", l[1].chars)
719 call assert_equal("c", l[2].chars)
720
721 " multibyte + composing
722 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
723 call term_sendkeys(buf, "echo " . txt . "\r")
724 call term_wait(buf, 50)
725 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
726 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
727 let l = term_scrape(buf, lnum[1] + 1)
728 call assert_equal("\u304b\u3099", l[0].chars)
729 call assert_equal("\u304e", l[1].chars)
730 call assert_equal("\u304f\u3099", l[2].chars)
731 call assert_equal("\u3052", l[3].chars)
732 call assert_equal("\u3053\u3099", l[4].chars)
733
734 " \u00a0 + composing
735 let txt = "abc\u00a0\u0308"
736 call term_sendkeys(buf, "echo " . txt . "\r")
737 call term_wait(buf, 50)
738 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
739 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
740 let l = term_scrape(buf, lnum[2] + 1)
741 call assert_equal("\u00a0\u0308", l[3].chars)
742
743 call term_sendkeys(buf, "exit\r")
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100744 call WaitFor('job_status(g:job) == "dead"')
745 call assert_equal('dead', job_status(g:job))
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200746 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100747 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200748 let &encoding = save_enc
749endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100750
751func Test_terminal_aucmd_on_close()
752 fun Nop()
753 let s:called = 1
754 endfun
755
756 aug repro
757 au!
758 au BufWinLeave * call Nop()
759 aug END
760
761 let [cmd, waittime] = s:get_sleep_cmd()
762
763 call assert_equal(1, winnr('$'))
764 new
765 call setline(1, ['one', 'two'])
766 exe 'term ++close ' . cmd
767 wincmd p
768 call WaitFor("winnr('$') == 2", waittime)
769 call assert_equal(1, s:called)
770 bwipe!
771
772 unlet s:called
773 au! repro
774 delfunc Nop
775endfunc