blob: 92dcd1a5771515f323dc30493cd56673529fb289 [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)
14 let buf = term_start(&shell, a:options)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020015
16 let termlist = term_list()
17 call assert_equal(1, len(termlist))
18 call assert_equal(buf, termlist[0])
19
20 let g:job = term_getjob(buf)
21 call assert_equal(v:t_job, type(g:job))
22
Bram Moolenaar35422f42017-08-05 16:33:56 +020023 let string = string({'job': term_getjob(buf)})
24 call assert_match("{'job': 'process \\d\\+ run'}", string)
25
Bram Moolenaar94053a52017-08-01 21:44:33 +020026 return buf
27endfunc
28
29" Stops the shell started by Run_shell_in_terminal().
30func Stop_shell_in_terminal(buf)
31 call term_sendkeys(a:buf, "exit\r")
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020032 call WaitFor('job_status(g:job) == "dead"')
33 call assert_equal('dead', job_status(g:job))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034endfunc
35
36func Test_terminal_basic()
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020037 au BufWinEnter * if &buftype == 'terminal' | let b:done = 'yes' | endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +020038 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020039
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020040 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020041 call assert_match('^/dev/', job_info(g:job).tty_out)
42 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020043 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020044 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
45 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020046 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020047 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020048 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020049 call assert_match('%aR[^\n]*running]', execute('ls'))
50
Bram Moolenaar94053a52017-08-01 21:44:33 +020051 call Stop_shell_in_terminal(buf)
52 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020053 call assert_equal('n', mode())
54 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020055
Bram Moolenaar94053a52017-08-01 21:44:33 +020056 " closing window wipes out the terminal buffer a with finished job
57 close
58 call assert_equal("", bufname(buf))
59
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020060 au! BufWinEnter
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020061 unlet g:job
62endfunc
63
64func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020065 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020066 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020067 call term_wait(buf)
68
69 setlocal modifiable
70 exe "normal Axxx\<Esc>"
71 call assert_fails(buf . 'bwipe', 'E517')
72 undo
73
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020074 exe buf . 'bwipe'
75 unlet g:job
76endfunc
77
Bram Moolenaar94053a52017-08-01 21:44:33 +020078func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020079 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020080 call assert_fails(buf . 'bwipe', 'E517')
81 exe buf . 'bwipe!'
Bram Moolenaar94053a52017-08-01 21:44:33 +020082 call WaitFor('job_status(g:job) == "dead"')
83 call assert_equal('dead', job_status(g:job))
84 call assert_equal("", bufname(buf))
85
86 unlet g:job
87endfunc
88
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020089func Test_terminal_split_quit()
90 let buf = Run_shell_in_terminal({})
91 call term_wait(buf)
92 split
93 quit!
94 call term_wait(buf)
95 sleep 50m
96 call assert_equal('run', job_status(g:job))
97
98 quit!
99 call WaitFor('job_status(g:job) == "dead"')
100 call assert_equal('dead', job_status(g:job))
101
102 exe buf . 'bwipe'
103 unlet g:job
104endfunc
105
Bram Moolenaar94053a52017-08-01 21:44:33 +0200106func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200107 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200108 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200109 quit
110 for nr in range(1, winnr('$'))
111 call assert_notequal(winbufnr(nr), buf)
112 endfor
113 call assert_true(bufloaded(buf))
114 call assert_true(buflisted(buf))
115
116 exe 'split ' . buf . 'buf'
117 call Stop_shell_in_terminal(buf)
118 exe buf . 'bwipe'
119
120 unlet g:job
121endfunc
122
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200123func! s:Nasty_exit_cb(job, st)
124 exe g:buf . 'bwipe!'
125 let g:buf = 0
126endfunc
127
Bram Moolenaar9d189612017-09-09 18:11:00 +0200128func Get_cat_123_cmd()
129 if has('win32')
130 return 'cmd /c "cls && color 2 && echo 123"'
131 else
132 call writefile(["\<Esc>[32m123"], 'Xtext')
133 return "cat Xtext"
134 endif
135endfunc
136
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200137func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200138 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200139 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
140 let g:job = term_getjob(g:buf)
141
142 call WaitFor('job_status(g:job) == "dead"')
143 call WaitFor('g:buf == 0')
144 unlet g:buf
145 unlet g:job
146 call delete('Xtext')
147endfunc
148
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200149func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200150 let l = term_scrape(a:buf, 0)
151 call assert_true(len(l) == 0)
152 let l = term_scrape(a:buf, 999)
153 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200154 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200155 call assert_true(len(l) > 0)
156 call assert_equal('1', l[0].chars)
157 call assert_equal('2', l[1].chars)
158 call assert_equal('3', l[2].chars)
159 call assert_equal('#00e000', l[0].fg)
160 if &background == 'light'
161 call assert_equal('#ffffff', l[0].bg)
162 else
163 call assert_equal('#000000', l[0].bg)
164 endif
165
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200166 let l = term_getline(a:buf, -1)
167 call assert_equal('', l)
168 let l = term_getline(a:buf, 0)
169 call assert_equal('', l)
170 let l = term_getline(a:buf, 999)
171 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200172 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200173 call assert_equal('123', l)
174endfunc
175
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200176func Test_terminal_scrape_123()
177 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200178 let buf = term_start(cmd)
179
180 let termlist = term_list()
181 call assert_equal(1, len(termlist))
182 call assert_equal(buf, termlist[0])
183
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200184 " Nothing happens with invalid buffer number
185 call term_wait(1234)
186
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200187 call term_wait(buf)
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200188 let g:buf = buf
Bram Moolenaar17833372017-09-04 22:23:19 +0200189 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200190 " "cls" to happen, after that we have one line with three characters.
191 call WaitFor('len(term_scrape(g:buf, 1)) == 3')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200192 call Check_123(buf)
193
194 " Must still work after the job ended.
195 let g:job = term_getjob(buf)
196 call WaitFor('job_status(g:job) == "dead"')
197 call term_wait(buf)
198 call Check_123(buf)
199
200 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200201 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200202endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200203
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200204func Test_terminal_scrape_multibyte()
205 if !has('multi_byte')
206 return
207 endif
208 call writefile(["léttrs"], 'Xtext')
209 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200210 " Run cmd with UTF-8 codepage to make the type command print the expected
211 " multibyte characters.
212 let g:buf = term_start("cmd /K chcp 65001")
213 call term_sendkeys(g:buf, "type Xtext\<CR>")
214 call term_sendkeys(g:buf, "exit\<CR>")
215 let g:line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200216 else
Bram Moolenaar36783932017-08-14 23:07:30 +0200217 let g:buf = term_start("cat Xtext")
218 let g:line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200219 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200220
Bram Moolenaara038cb52017-09-11 20:45:23 +0200221 call WaitFor('len(term_scrape(g:buf, g:line)) >= 7 && term_scrape(g:buf, g:line)[0].chars == "l"')
Bram Moolenaar36783932017-08-14 23:07:30 +0200222 let l = term_scrape(g:buf, g:line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200223 call assert_true(len(l) >= 7)
224 call assert_equal('l', l[0].chars)
225 call assert_equal('é', l[1].chars)
226 call assert_equal(1, l[1].width)
227 call assert_equal('t', l[2].chars)
228 call assert_equal('t', l[3].chars)
229 call assert_equal('ま', l[4].chars)
230 call assert_equal(2, l[4].width)
231 call assert_equal('r', l[5].chars)
232 call assert_equal('s', l[6].chars)
233
Bram Moolenaarc0870612017-08-14 22:01:16 +0200234 let g:job = term_getjob(g:buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200235 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaarc0870612017-08-14 22:01:16 +0200236 call term_wait(g:buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200237
Bram Moolenaarc0870612017-08-14 22:01:16 +0200238 exe g:buf . 'bwipe'
239 unlet g:buf
Bram Moolenaar36783932017-08-14 23:07:30 +0200240 unlet g:line
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200241 call delete('Xtext')
242endfunc
243
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200244func Test_terminal_scroll()
245 call writefile(range(1, 200), 'Xtext')
246 if has('win32')
247 let cmd = 'cmd /c "type Xtext"'
248 else
249 let cmd = "cat Xtext"
250 endif
251 let buf = term_start(cmd)
252
253 let g:job = term_getjob(buf)
254 call WaitFor('job_status(g:job) == "dead"')
255 call term_wait(buf)
256 if has('win32')
257 " TODO: this should not be needed
258 sleep 100m
259 endif
260
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200261 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200262 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200263 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200264 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200265 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200266 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200267 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200268
269 exe buf . 'bwipe'
270 call delete('Xtext')
271endfunc
272
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200273func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200274 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200275
Bram Moolenaarb2412082017-08-20 18:09:14 +0200276 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200277 let size = term_getsize('')
278 bwipe!
279 call assert_equal(5, size[0])
280
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200281 call term_start(cmd, {'term_rows': 6})
282 let size = term_getsize('')
283 bwipe!
284 call assert_equal(6, size[0])
285
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200286 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200287 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200288 let size = term_getsize('')
289 bwipe!
290 call assert_equal([5, 33], size)
291
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200292 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
293 let size = term_getsize('')
294 bwipe!
295 call assert_equal([6, 36], size)
296
Bram Moolenaarb2412082017-08-20 18:09:14 +0200297 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200298 let size = term_getsize('')
299 bwipe!
300 call assert_equal(20, size[1])
301
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200302 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
303 let size = term_getsize('')
304 bwipe!
305 call assert_equal(26, size[1])
306
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200307 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200308 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200309 let size = term_getsize('')
310 bwipe!
311 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200312
313 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
314 let size = term_getsize('')
315 bwipe!
316 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200317
318 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200319endfunc
320
321func Test_terminal_curwin()
322 let cmd = Get_cat_123_cmd()
323 call assert_equal(1, winnr('$'))
324
325 split dummy
326 exe 'terminal ++curwin ' . cmd
327 call assert_equal(2, winnr('$'))
328 bwipe!
329
330 split dummy
331 call term_start(cmd, {'curwin': 1})
332 call assert_equal(2, winnr('$'))
333 bwipe!
334
335 split dummy
336 call setline(1, 'change')
337 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
338 call assert_equal(2, winnr('$'))
339 exe 'terminal! ++curwin ' . cmd
340 call assert_equal(2, winnr('$'))
341 bwipe!
342
343 split dummy
344 call setline(1, 'change')
345 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
346 call assert_equal(2, winnr('$'))
347 bwipe!
348
349 split dummy
350 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200351 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200352endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200353
Bram Moolenaar37c45832017-08-12 16:01:04 +0200354func Test_finish_open_close()
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200355 call assert_equal(1, winnr('$'))
356
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200357 if s:python != ''
358 let cmd = s:python . " test_short_sleep.py"
359 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200360 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200361 echo 'This will take five seconds...'
362 let waittime = 2000
363 if has('win32')
364 let cmd = $windir . '\system32\timeout.exe 1'
365 else
366 let cmd = 'sleep 1'
367 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200368 endif
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200369
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200370 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200371 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200372 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200373 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200374 call assert_equal(1, winnr('$'))
375
376 call term_start(cmd, {'term_finish': 'close'})
377 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 call assert_equal(1, winnr('$'))
381
382 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200383 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200384 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200385 call assert_equal(2, winnr('$'))
386 bwipe
387
388 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200389 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200390 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200391 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200392 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200393
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200394 exe 'terminal ++hidden ++open ' . cmd
395 call assert_equal(1, winnr('$'))
396 call WaitFor("winnr('$') == 2", waittime)
397 call assert_equal(2, winnr('$'))
398 bwipe
399
400 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
401 call assert_equal(1, winnr('$'))
402 call WaitFor("winnr('$') == 2", waittime)
403 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200404 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200405
406 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
407 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
408 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
409 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
410
411 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200412 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200413 call WaitFor("winnr('$') == 2", waittime)
414 call assert_equal(2, winnr('$'))
415 call assert_equal(4, winheight(0))
416 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200417endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200418
419func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200420 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200421 return
422 endif
423 call mkdir('Xdir')
424 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200425 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
426 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200427
428 exe buf . 'bwipe'
429 call delete('Xdir', 'rf')
430endfunc
431
432func Test_terminal_env()
433 if !has('unix')
434 return
435 endif
Bram Moolenaarc0870612017-08-14 22:01:16 +0200436 let g:buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200437 " Wait for the shell to display a prompt
Bram Moolenaarc0870612017-08-14 22:01:16 +0200438 call WaitFor('term_getline(g:buf, 1) != ""')
439 call term_sendkeys(g:buf, "echo $TESTENV\r")
440 call term_wait(g:buf)
441 call Stop_shell_in_terminal(g:buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200442 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200443 call assert_equal('correct', getline(2))
444
Bram Moolenaarc0870612017-08-14 22:01:16 +0200445 exe g:buf . 'bwipe'
446 unlet g:buf
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200447endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200448
449" must be last, we can't go back from GUI to terminal
450func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200451 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200452 return
453 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200454
455 " Ignore the "failed to create input context" error.
456 call test_ignore_error('E285:')
457
Bram Moolenaar679653e2017-08-13 14:13:19 +0200458 gui -f
459
460 call assert_equal(1, winnr('$'))
461 let buf = Run_shell_in_terminal({'term_finish': 'close'})
462 call Stop_shell_in_terminal(buf)
463 call term_wait(buf)
464
465 " closing window wipes out the terminal buffer a with finished job
466 call WaitFor("winnr('$') == 1")
467 call assert_equal(1, winnr('$'))
468 call assert_equal("", bufname(buf))
469
470 unlet g:job
471endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200472
473func Test_terminal_list_args()
474 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
475 call assert_fails(buf . 'bwipe', 'E517')
476 exe buf . 'bwipe!'
477 call assert_equal("", bufname(buf))
478endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200479
480func Test_terminal_noblock()
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200481 let g:buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200482 if has('mac')
483 " The shell or something else has a problem dealing with more than 1000
484 " characters at the same time.
485 let len = 1000
486 else
487 let len = 5000
488 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200489
490 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200491 call term_sendkeys(g:buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200492 endfor
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200493 call term_sendkeys(g:buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200494
495 " On MS-Windows there is an extra empty line below "done". Find "done" in
496 " the last-but-one or the last-but-two line.
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200497 let g:lnum = term_getsize(g:buf)[0] - 1
Bram Moolenaareef05312017-08-20 20:21:23 +0200498 call WaitFor('term_getline(g:buf, g:lnum) =~ "done" || term_getline(g:buf, g:lnum - 1) =~ "done"', 3000)
499 let line = term_getline(g:buf, g:lnum)
500 if line !~ 'done'
501 let line = term_getline(g:buf, g:lnum - 1)
502 endif
503 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200504
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200505 let g:job = term_getjob(g:buf)
506 call Stop_shell_in_terminal(g:buf)
507 call term_wait(g:buf)
508 unlet g:buf
509 unlet g:job
510 unlet g:lnum
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200511 bwipe
512endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200513
514func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200515 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200516 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200517 endif
518 new
519 call setline(1, ['one', 'two', 'three'])
520 %term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200521 call WaitFor('getline("$") =~ "3"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200522 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200523 call assert_equal(['3', '3', '14'], nrs)
524 bwipe
525
Bram Moolenaardada6d22017-09-02 17:18:35 +0200526 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200527 call setline(1, ['one', 'two', 'three', 'four'])
528 2,3term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200529 call WaitFor('getline("$") =~ "2"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200530 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200531 call assert_equal(['2', '2', '10'], nrs)
532 bwipe
533
Bram Moolenaardada6d22017-09-02 17:18:35 +0200534 if executable('python')
535 new
536 call setline(1, ['print("hello")'])
537 1term ++eof=exit() python
538 " MS-Windows echoes the input, Unix doesn't.
539 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
540 if getline(1) =~ 'hello'
541 call assert_equal('hello', getline(1))
542 else
543 call assert_equal('hello', getline(line('$') - 1))
544 endif
545 bwipe
546
547 if has('win32')
548 new
549 call setline(1, ['print("hello")'])
550 1term ++eof=<C-Z> python
551 call WaitFor('getline("$") =~ "Z"')
552 call assert_equal('hello', getline(line('$') - 1))
553 bwipe
554 endif
555 endif
556
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200557 bwipe!
558endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200559
560func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200561 " Todo: make this work in the GUI
562 if !has('gui_running')
563 return
564 endif
565 let buf = term_start('NONE', {})
566 call assert_notequal(0, buf)
567
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200568 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200569 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200570 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200571 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200572 else
573 call system('echo "look here" > ' . pty)
574 endif
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200575 let g:buf = buf
576 call WaitFor('term_getline(g:buf, 1) =~ "look here"')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200577
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200578 call assert_match('look here', term_getline(buf, 1))
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200579 bwipe!
580endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200581
582func Test_terminal_special_chars()
583 " this file name only works on Unix
584 if !has('unix')
585 return
586 endif
587 call mkdir('Xdir with spaces')
588 call writefile(['x'], 'Xdir with spaces/quoted"file')
589 term ls Xdir\ with\ spaces/quoted\"file
590 call WaitFor('term_getline("", 1) =~ "quoted"')
591 call assert_match('quoted"file', term_getline('', 1))
592 call term_wait('')
593
594 call delete('Xdir with spaces', 'rf')
595 bwipe
596endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200597
598func Test_terminal_wrong_options()
599 call assert_fails('call term_start(&shell, {
600 \ "in_io": "file",
601 \ "in_name": "xxx",
602 \ "out_io": "file",
603 \ "out_name": "xxx",
604 \ "err_io": "file",
605 \ "err_name": "xxx"
606 \ })', 'E474:')
607 call assert_fails('call term_start(&shell, {
608 \ "out_buf": bufnr("%")
609 \ })', 'E474:')
610 call assert_fails('call term_start(&shell, {
611 \ "err_buf": bufnr("%")
612 \ })', 'E474:')
613endfunc
614
615func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200616 " TODO: this should work on MS-Window
617 if has('unix')
618 let cmd = Get_cat_123_cmd()
619 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
620 call term_wait(buf)
621 call WaitFor('len(readfile("Xfile")) > 0')
622 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200623 let g:job = term_getjob(buf)
624 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200625 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200626 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200627 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200628
629 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200630 call writefile(['one line'], 'Xfile')
631 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
632 call term_wait(buf)
633 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
634 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200635 let g:job = term_getjob(buf)
636 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200637 bwipe
638 call delete('Xfile')
639 endif
640endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200641
642func TerminalTmap(remap)
643 let buf = Run_shell_in_terminal({})
644 call assert_equal('t', mode())
645
646 if a:remap
647 tmap 123 456
648 else
649 tnoremap 123 456
650 endif
651 tmap 456 abcde
652 call assert_equal('456', maparg('123', 't'))
653 call assert_equal('abcde', maparg('456', 't'))
654 call feedkeys("123", 'tx')
Bram Moolenaar1514e8f2017-09-16 17:35:13 +0200655 let g:buf = buf
656 call WaitFor("term_getline(g:buf,term_getcursor(g:buf)[0]) =~ 'abcde\\|456'")
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200657 let lnum = term_getcursor(buf)[0]
658 if a:remap
659 call assert_match('abcde', term_getline(buf, lnum))
660 else
661 call assert_match('456', term_getline(buf, lnum))
662 endif
663
664 call term_sendkeys(buf, "\r")
665 call Stop_shell_in_terminal(buf)
666 call term_wait(buf)
667
668 tunmap 123
669 tunmap 456
670 call assert_equal('', maparg('123', 't'))
671 close
672 unlet g:job
673endfunc
674
675func Test_terminal_tmap()
676 call TerminalTmap(1)
677 call TerminalTmap(0)
678endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200679
680func Test_terminal_wall()
681 let buf = Run_shell_in_terminal({})
682 wall
683 call Stop_shell_in_terminal(buf)
684 call term_wait(buf)
685 exe buf . 'bwipe'
686 unlet g:job
687endfunc