blob: 4a639bd87cd557f1b9f7cac2d5f05779816f28c4 [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 Moolenaar05aafed2017-08-11 19:12:11 +020037 let buf = Run_shell_in_terminal({})
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020038 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020039 call assert_match('^/dev/', job_info(g:job).tty_out)
40 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020041 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020042 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
43 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020044 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020045 call assert_equal('t', mode())
46 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 Moolenaar20e6cd02017-08-01 20:25:22 +020057 unlet g:job
58endfunc
59
60func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020061 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020062 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020063 call term_wait(buf)
64
65 setlocal modifiable
66 exe "normal Axxx\<Esc>"
67 call assert_fails(buf . 'bwipe', 'E517')
68 undo
69
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020070 exe buf . 'bwipe'
71 unlet g:job
72endfunc
73
Bram Moolenaar94053a52017-08-01 21:44:33 +020074func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020075 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020076 call assert_fails(buf . 'bwipe', 'E517')
77 exe buf . 'bwipe!'
Bram Moolenaar94053a52017-08-01 21:44:33 +020078 call WaitFor('job_status(g:job) == "dead"')
79 call assert_equal('dead', job_status(g:job))
80 call assert_equal("", bufname(buf))
81
82 unlet g:job
83endfunc
84
85func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020086 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +020087 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +020088 quit
89 for nr in range(1, winnr('$'))
90 call assert_notequal(winbufnr(nr), buf)
91 endfor
92 call assert_true(bufloaded(buf))
93 call assert_true(buflisted(buf))
94
95 exe 'split ' . buf . 'buf'
96 call Stop_shell_in_terminal(buf)
97 exe buf . 'bwipe'
98
99 unlet g:job
100endfunc
101
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200102func! s:Nasty_exit_cb(job, st)
103 exe g:buf . 'bwipe!'
104 let g:buf = 0
105endfunc
106
Bram Moolenaar9d189612017-09-09 18:11:00 +0200107func Get_cat_123_cmd()
108 if has('win32')
109 return 'cmd /c "cls && color 2 && echo 123"'
110 else
111 call writefile(["\<Esc>[32m123"], 'Xtext')
112 return "cat Xtext"
113 endif
114endfunc
115
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200116func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200117 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200118 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
119 let g:job = term_getjob(g:buf)
120
121 call WaitFor('job_status(g:job) == "dead"')
122 call WaitFor('g:buf == 0')
123 unlet g:buf
124 unlet g:job
125 call delete('Xtext')
126endfunc
127
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200128func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200129 let l = term_scrape(a:buf, 0)
130 call assert_true(len(l) == 0)
131 let l = term_scrape(a:buf, 999)
132 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200133 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200134 call assert_true(len(l) > 0)
135 call assert_equal('1', l[0].chars)
136 call assert_equal('2', l[1].chars)
137 call assert_equal('3', l[2].chars)
138 call assert_equal('#00e000', l[0].fg)
139 if &background == 'light'
140 call assert_equal('#ffffff', l[0].bg)
141 else
142 call assert_equal('#000000', l[0].bg)
143 endif
144
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200145 let l = term_getline(a:buf, -1)
146 call assert_equal('', l)
147 let l = term_getline(a:buf, 0)
148 call assert_equal('', l)
149 let l = term_getline(a:buf, 999)
150 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200151 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200152 call assert_equal('123', l)
153endfunc
154
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200155func Test_terminal_scrape_123()
156 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200157 let buf = term_start(cmd)
158
159 let termlist = term_list()
160 call assert_equal(1, len(termlist))
161 call assert_equal(buf, termlist[0])
162
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200163 " Nothing happens with invalid buffer number
164 call term_wait(1234)
165
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200166 call term_wait(buf)
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200167 let g:buf = buf
Bram Moolenaar17833372017-09-04 22:23:19 +0200168 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200169 " "cls" to happen, after that we have one line with three characters.
170 call WaitFor('len(term_scrape(g:buf, 1)) == 3')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200171 call Check_123(buf)
172
173 " Must still work after the job ended.
174 let g:job = term_getjob(buf)
175 call WaitFor('job_status(g:job) == "dead"')
176 call term_wait(buf)
177 call Check_123(buf)
178
179 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200180 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200181endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200182
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200183func Test_terminal_scrape_multibyte()
184 if !has('multi_byte')
185 return
186 endif
187 call writefile(["léttrs"], 'Xtext')
188 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200189 " Run cmd with UTF-8 codepage to make the type command print the expected
190 " multibyte characters.
191 let g:buf = term_start("cmd /K chcp 65001")
192 call term_sendkeys(g:buf, "type Xtext\<CR>")
193 call term_sendkeys(g:buf, "exit\<CR>")
194 let g:line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200195 else
Bram Moolenaar36783932017-08-14 23:07:30 +0200196 let g:buf = term_start("cat Xtext")
197 let g:line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200198 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200199
Bram Moolenaara038cb52017-09-11 20:45:23 +0200200 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 +0200201 let l = term_scrape(g:buf, g:line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200202 call assert_true(len(l) >= 7)
203 call assert_equal('l', l[0].chars)
204 call assert_equal('é', l[1].chars)
205 call assert_equal(1, l[1].width)
206 call assert_equal('t', l[2].chars)
207 call assert_equal('t', l[3].chars)
208 call assert_equal('ま', l[4].chars)
209 call assert_equal(2, l[4].width)
210 call assert_equal('r', l[5].chars)
211 call assert_equal('s', l[6].chars)
212
Bram Moolenaarc0870612017-08-14 22:01:16 +0200213 let g:job = term_getjob(g:buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200214 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaarc0870612017-08-14 22:01:16 +0200215 call term_wait(g:buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200216
Bram Moolenaarc0870612017-08-14 22:01:16 +0200217 exe g:buf . 'bwipe'
218 unlet g:buf
Bram Moolenaar36783932017-08-14 23:07:30 +0200219 unlet g:line
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200220 call delete('Xtext')
221endfunc
222
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200223func Test_terminal_scroll()
224 call writefile(range(1, 200), 'Xtext')
225 if has('win32')
226 let cmd = 'cmd /c "type Xtext"'
227 else
228 let cmd = "cat Xtext"
229 endif
230 let buf = term_start(cmd)
231
232 let g:job = term_getjob(buf)
233 call WaitFor('job_status(g:job) == "dead"')
234 call term_wait(buf)
235 if has('win32')
236 " TODO: this should not be needed
237 sleep 100m
238 endif
239
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200240 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200241 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200242 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200243 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200244 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200245 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200246 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200247
248 exe buf . 'bwipe'
249 call delete('Xtext')
250endfunc
251
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200252func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200253 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200254
Bram Moolenaarb2412082017-08-20 18:09:14 +0200255 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200256 let size = term_getsize('')
257 bwipe!
258 call assert_equal(5, size[0])
259
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200260 call term_start(cmd, {'term_rows': 6})
261 let size = term_getsize('')
262 bwipe!
263 call assert_equal(6, size[0])
264
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200265 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200266 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200267 let size = term_getsize('')
268 bwipe!
269 call assert_equal([5, 33], size)
270
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200271 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
272 let size = term_getsize('')
273 bwipe!
274 call assert_equal([6, 36], size)
275
Bram Moolenaarb2412082017-08-20 18:09:14 +0200276 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200277 let size = term_getsize('')
278 bwipe!
279 call assert_equal(20, size[1])
280
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200281 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
282 let size = term_getsize('')
283 bwipe!
284 call assert_equal(26, size[1])
285
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200286 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200287 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200288 let size = term_getsize('')
289 bwipe!
290 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200291
292 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
293 let size = term_getsize('')
294 bwipe!
295 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200296
297 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200298endfunc
299
300func Test_terminal_curwin()
301 let cmd = Get_cat_123_cmd()
302 call assert_equal(1, winnr('$'))
303
304 split dummy
305 exe 'terminal ++curwin ' . cmd
306 call assert_equal(2, winnr('$'))
307 bwipe!
308
309 split dummy
310 call term_start(cmd, {'curwin': 1})
311 call assert_equal(2, winnr('$'))
312 bwipe!
313
314 split dummy
315 call setline(1, 'change')
316 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
317 call assert_equal(2, winnr('$'))
318 exe 'terminal! ++curwin ' . cmd
319 call assert_equal(2, winnr('$'))
320 bwipe!
321
322 split dummy
323 call setline(1, 'change')
324 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
325 call assert_equal(2, winnr('$'))
326 bwipe!
327
328 split dummy
329 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200330 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200331endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200332
Bram Moolenaar37c45832017-08-12 16:01:04 +0200333func Test_finish_open_close()
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200334 call assert_equal(1, winnr('$'))
335
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200336 if s:python != ''
337 let cmd = s:python . " test_short_sleep.py"
338 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200339 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200340 echo 'This will take five seconds...'
341 let waittime = 2000
342 if has('win32')
343 let cmd = $windir . '\system32\timeout.exe 1'
344 else
345 let cmd = 'sleep 1'
346 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200347 endif
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200348
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200349 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200350 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200351 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200352 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200353 call assert_equal(1, winnr('$'))
354
355 call term_start(cmd, {'term_finish': 'close'})
356 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200357 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200358 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200359 call assert_equal(1, winnr('$'))
360
361 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200362 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200363 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200364 call assert_equal(2, winnr('$'))
365 bwipe
366
367 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200368 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200369 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200370 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200371 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200372
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200373 exe 'terminal ++hidden ++open ' . cmd
374 call assert_equal(1, winnr('$'))
375 call WaitFor("winnr('$') == 2", waittime)
376 call assert_equal(2, winnr('$'))
377 bwipe
378
379 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
380 call assert_equal(1, winnr('$'))
381 call WaitFor("winnr('$') == 2", waittime)
382 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200383 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200384
385 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
386 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
387 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
388 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
389
390 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200391 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200392 call WaitFor("winnr('$') == 2", waittime)
393 call assert_equal(2, winnr('$'))
394 call assert_equal(4, winheight(0))
395 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200396endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200397
398func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200399 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200400 return
401 endif
402 call mkdir('Xdir')
403 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200404 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
405 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200406
407 exe buf . 'bwipe'
408 call delete('Xdir', 'rf')
409endfunc
410
411func Test_terminal_env()
412 if !has('unix')
413 return
414 endif
Bram Moolenaarc0870612017-08-14 22:01:16 +0200415 let g:buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200416 " Wait for the shell to display a prompt
Bram Moolenaarc0870612017-08-14 22:01:16 +0200417 call WaitFor('term_getline(g:buf, 1) != ""')
418 call term_sendkeys(g:buf, "echo $TESTENV\r")
419 call term_wait(g:buf)
420 call Stop_shell_in_terminal(g:buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200421 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200422 call assert_equal('correct', getline(2))
423
Bram Moolenaarc0870612017-08-14 22:01:16 +0200424 exe g:buf . 'bwipe'
425 unlet g:buf
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200426endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200427
428" must be last, we can't go back from GUI to terminal
429func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200430 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200431 return
432 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200433
434 " Ignore the "failed to create input context" error.
435 call test_ignore_error('E285:')
436
Bram Moolenaar679653e2017-08-13 14:13:19 +0200437 gui -f
438
439 call assert_equal(1, winnr('$'))
440 let buf = Run_shell_in_terminal({'term_finish': 'close'})
441 call Stop_shell_in_terminal(buf)
442 call term_wait(buf)
443
444 " closing window wipes out the terminal buffer a with finished job
445 call WaitFor("winnr('$') == 1")
446 call assert_equal(1, winnr('$'))
447 call assert_equal("", bufname(buf))
448
449 unlet g:job
450endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200451
452func Test_terminal_list_args()
453 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
454 call assert_fails(buf . 'bwipe', 'E517')
455 exe buf . 'bwipe!'
456 call assert_equal("", bufname(buf))
457endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200458
459func Test_terminal_noblock()
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200460 let g:buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200461 if has('mac')
462 " The shell or something else has a problem dealing with more than 1000
463 " characters at the same time.
464 let len = 1000
465 else
466 let len = 5000
467 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200468
469 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200470 call term_sendkeys(g:buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200471 endfor
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200472 call term_sendkeys(g:buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200473
474 " On MS-Windows there is an extra empty line below "done". Find "done" in
475 " the last-but-one or the last-but-two line.
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200476 let g:lnum = term_getsize(g:buf)[0] - 1
Bram Moolenaareef05312017-08-20 20:21:23 +0200477 call WaitFor('term_getline(g:buf, g:lnum) =~ "done" || term_getline(g:buf, g:lnum - 1) =~ "done"', 3000)
478 let line = term_getline(g:buf, g:lnum)
479 if line !~ 'done'
480 let line = term_getline(g:buf, g:lnum - 1)
481 endif
482 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200483
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200484 let g:job = term_getjob(g:buf)
485 call Stop_shell_in_terminal(g:buf)
486 call term_wait(g:buf)
487 unlet g:buf
488 unlet g:job
489 unlet g:lnum
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200490 bwipe
491endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200492
493func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200494 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200495 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200496 endif
497 new
498 call setline(1, ['one', 'two', 'three'])
499 %term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200500 call WaitFor('getline("$") =~ "3"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200501 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200502 call assert_equal(['3', '3', '14'], nrs)
503 bwipe
504
Bram Moolenaardada6d22017-09-02 17:18:35 +0200505 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200506 call setline(1, ['one', 'two', 'three', 'four'])
507 2,3term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200508 call WaitFor('getline("$") =~ "2"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200509 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200510 call assert_equal(['2', '2', '10'], nrs)
511 bwipe
512
Bram Moolenaardada6d22017-09-02 17:18:35 +0200513 if executable('python')
514 new
515 call setline(1, ['print("hello")'])
516 1term ++eof=exit() python
517 " MS-Windows echoes the input, Unix doesn't.
518 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
519 if getline(1) =~ 'hello'
520 call assert_equal('hello', getline(1))
521 else
522 call assert_equal('hello', getline(line('$') - 1))
523 endif
524 bwipe
525
526 if has('win32')
527 new
528 call setline(1, ['print("hello")'])
529 1term ++eof=<C-Z> python
530 call WaitFor('getline("$") =~ "Z"')
531 call assert_equal('hello', getline(line('$') - 1))
532 bwipe
533 endif
534 endif
535
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200536 bwipe!
537endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200538
539func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200540 " Todo: make this work in the GUI
541 if !has('gui_running')
542 return
543 endif
544 let buf = term_start('NONE', {})
545 call assert_notequal(0, buf)
546
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200547 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200548 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200549 if has('win32')
550 silent exe '!cmd /c "echo look here > ' . pty . '"'
551 else
552 call system('echo "look here" > ' . pty)
553 endif
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200554 call term_wait(buf)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200555
556 let result = term_getline(buf, 1)
557 if has('win32')
558 let result = substitute(result, '\s\+$', '', '')
559 endif
560 call assert_equal('look here', result)
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200561 bwipe!
562endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200563
564func Test_terminal_special_chars()
565 " this file name only works on Unix
566 if !has('unix')
567 return
568 endif
569 call mkdir('Xdir with spaces')
570 call writefile(['x'], 'Xdir with spaces/quoted"file')
571 term ls Xdir\ with\ spaces/quoted\"file
572 call WaitFor('term_getline("", 1) =~ "quoted"')
573 call assert_match('quoted"file', term_getline('', 1))
574 call term_wait('')
575
576 call delete('Xdir with spaces', 'rf')
577 bwipe
578endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200579
580func Test_terminal_wrong_options()
581 call assert_fails('call term_start(&shell, {
582 \ "in_io": "file",
583 \ "in_name": "xxx",
584 \ "out_io": "file",
585 \ "out_name": "xxx",
586 \ "err_io": "file",
587 \ "err_name": "xxx"
588 \ })', 'E474:')
589 call assert_fails('call term_start(&shell, {
590 \ "out_buf": bufnr("%")
591 \ })', 'E474:')
592 call assert_fails('call term_start(&shell, {
593 \ "err_buf": bufnr("%")
594 \ })', 'E474:')
595endfunc
596
597func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200598 " TODO: this should work on MS-Window
599 if has('unix')
600 let cmd = Get_cat_123_cmd()
601 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
602 call term_wait(buf)
603 call WaitFor('len(readfile("Xfile")) > 0')
604 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200605 let g:job = term_getjob(buf)
606 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200607 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200608 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200609 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200610
611 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200612 call writefile(['one line'], 'Xfile')
613 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
614 call term_wait(buf)
615 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
616 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200617 let g:job = term_getjob(buf)
618 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200619 bwipe
620 call delete('Xfile')
621 endif
622endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200623
624func TerminalTmap(remap)
625 let buf = Run_shell_in_terminal({})
626 call assert_equal('t', mode())
627
628 if a:remap
629 tmap 123 456
630 else
631 tnoremap 123 456
632 endif
633 tmap 456 abcde
634 call assert_equal('456', maparg('123', 't'))
635 call assert_equal('abcde', maparg('456', 't'))
636 call feedkeys("123", 'tx')
637 call term_wait(buf)
638 let lnum = term_getcursor(buf)[0]
639 if a:remap
640 call assert_match('abcde', term_getline(buf, lnum))
641 else
642 call assert_match('456', term_getline(buf, lnum))
643 endif
644
645 call term_sendkeys(buf, "\r")
646 call Stop_shell_in_terminal(buf)
647 call term_wait(buf)
648
649 tunmap 123
650 tunmap 456
651 call assert_equal('', maparg('123', 't'))
652 close
653 unlet g:job
654endfunc
655
656func Test_terminal_tmap()
657 call TerminalTmap(1)
658 call TerminalTmap(0)
659endfunc