blob: 17167e89ffb033a9bda178d2a3d305560b78a0c9 [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
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020085func Test_terminal_split_quit()
86 let buf = Run_shell_in_terminal({})
87 call term_wait(buf)
88 split
89 quit!
90 call term_wait(buf)
91 sleep 50m
92 call assert_equal('run', job_status(g:job))
93
94 quit!
95 call WaitFor('job_status(g:job) == "dead"')
96 call assert_equal('dead', job_status(g:job))
97
98 exe buf . 'bwipe'
99 unlet g:job
100endfunc
101
Bram Moolenaar94053a52017-08-01 21:44:33 +0200102func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200103 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200104 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200105 quit
106 for nr in range(1, winnr('$'))
107 call assert_notequal(winbufnr(nr), buf)
108 endfor
109 call assert_true(bufloaded(buf))
110 call assert_true(buflisted(buf))
111
112 exe 'split ' . buf . 'buf'
113 call Stop_shell_in_terminal(buf)
114 exe buf . 'bwipe'
115
116 unlet g:job
117endfunc
118
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200119func! s:Nasty_exit_cb(job, st)
120 exe g:buf . 'bwipe!'
121 let g:buf = 0
122endfunc
123
Bram Moolenaar9d189612017-09-09 18:11:00 +0200124func Get_cat_123_cmd()
125 if has('win32')
126 return 'cmd /c "cls && color 2 && echo 123"'
127 else
128 call writefile(["\<Esc>[32m123"], 'Xtext')
129 return "cat Xtext"
130 endif
131endfunc
132
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200133func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200134 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200135 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
136 let g:job = term_getjob(g:buf)
137
138 call WaitFor('job_status(g:job) == "dead"')
139 call WaitFor('g:buf == 0')
140 unlet g:buf
141 unlet g:job
142 call delete('Xtext')
143endfunc
144
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200145func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200146 let l = term_scrape(a:buf, 0)
147 call assert_true(len(l) == 0)
148 let l = term_scrape(a:buf, 999)
149 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200150 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200151 call assert_true(len(l) > 0)
152 call assert_equal('1', l[0].chars)
153 call assert_equal('2', l[1].chars)
154 call assert_equal('3', l[2].chars)
155 call assert_equal('#00e000', l[0].fg)
156 if &background == 'light'
157 call assert_equal('#ffffff', l[0].bg)
158 else
159 call assert_equal('#000000', l[0].bg)
160 endif
161
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200162 let l = term_getline(a:buf, -1)
163 call assert_equal('', l)
164 let l = term_getline(a:buf, 0)
165 call assert_equal('', l)
166 let l = term_getline(a:buf, 999)
167 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200168 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200169 call assert_equal('123', l)
170endfunc
171
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200172func Test_terminal_scrape_123()
173 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200174 let buf = term_start(cmd)
175
176 let termlist = term_list()
177 call assert_equal(1, len(termlist))
178 call assert_equal(buf, termlist[0])
179
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200180 " Nothing happens with invalid buffer number
181 call term_wait(1234)
182
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200183 call term_wait(buf)
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200184 let g:buf = 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.
187 call WaitFor('len(term_scrape(g:buf, 1)) == 3')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200188 call Check_123(buf)
189
190 " Must still work after the job ended.
191 let g:job = term_getjob(buf)
192 call WaitFor('job_status(g:job) == "dead"')
193 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éttrs"], '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.
208 let g:buf = term_start("cmd /K chcp 65001")
209 call term_sendkeys(g:buf, "type Xtext\<CR>")
210 call term_sendkeys(g:buf, "exit\<CR>")
211 let g:line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200212 else
Bram Moolenaar36783932017-08-14 23:07:30 +0200213 let g:buf = term_start("cat Xtext")
214 let g:line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200215 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200216
Bram Moolenaara038cb52017-09-11 20:45:23 +0200217 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 +0200218 let l = term_scrape(g:buf, g: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 Moolenaarc0870612017-08-14 22:01:16 +0200230 let g:job = term_getjob(g:buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200231 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaarc0870612017-08-14 22:01:16 +0200232 call term_wait(g:buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200233
Bram Moolenaarc0870612017-08-14 22:01:16 +0200234 exe g:buf . 'bwipe'
235 unlet g:buf
Bram Moolenaar36783932017-08-14 23:07:30 +0200236 unlet g:line
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200237 call delete('Xtext')
238endfunc
239
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200240func Test_terminal_scroll()
241 call writefile(range(1, 200), 'Xtext')
242 if has('win32')
243 let cmd = 'cmd /c "type Xtext"'
244 else
245 let cmd = "cat Xtext"
246 endif
247 let buf = term_start(cmd)
248
249 let g:job = term_getjob(buf)
250 call WaitFor('job_status(g:job) == "dead"')
251 call term_wait(buf)
252 if has('win32')
253 " TODO: this should not be needed
254 sleep 100m
255 endif
256
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200257 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200258 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200259 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200260 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200261 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200262 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200263 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200264
265 exe buf . 'bwipe'
266 call delete('Xtext')
267endfunc
268
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200269func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200270 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200271
Bram Moolenaarb2412082017-08-20 18:09:14 +0200272 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200273 let size = term_getsize('')
274 bwipe!
275 call assert_equal(5, size[0])
276
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200277 call term_start(cmd, {'term_rows': 6})
278 let size = term_getsize('')
279 bwipe!
280 call assert_equal(6, size[0])
281
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200282 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200283 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200284 let size = term_getsize('')
285 bwipe!
286 call assert_equal([5, 33], size)
287
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200288 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
289 let size = term_getsize('')
290 bwipe!
291 call assert_equal([6, 36], size)
292
Bram Moolenaarb2412082017-08-20 18:09:14 +0200293 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200294 let size = term_getsize('')
295 bwipe!
296 call assert_equal(20, size[1])
297
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200298 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
299 let size = term_getsize('')
300 bwipe!
301 call assert_equal(26, size[1])
302
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200303 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200304 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200305 let size = term_getsize('')
306 bwipe!
307 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200308
309 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
310 let size = term_getsize('')
311 bwipe!
312 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200313
314 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200315endfunc
316
317func Test_terminal_curwin()
318 let cmd = Get_cat_123_cmd()
319 call assert_equal(1, winnr('$'))
320
321 split dummy
322 exe 'terminal ++curwin ' . cmd
323 call assert_equal(2, winnr('$'))
324 bwipe!
325
326 split dummy
327 call term_start(cmd, {'curwin': 1})
328 call assert_equal(2, winnr('$'))
329 bwipe!
330
331 split dummy
332 call setline(1, 'change')
333 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
334 call assert_equal(2, winnr('$'))
335 exe 'terminal! ++curwin ' . cmd
336 call assert_equal(2, winnr('$'))
337 bwipe!
338
339 split dummy
340 call setline(1, 'change')
341 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
342 call assert_equal(2, winnr('$'))
343 bwipe!
344
345 split dummy
346 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200347 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200348endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200349
Bram Moolenaar37c45832017-08-12 16:01:04 +0200350func Test_finish_open_close()
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200351 call assert_equal(1, winnr('$'))
352
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200353 if s:python != ''
354 let cmd = s:python . " test_short_sleep.py"
355 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200356 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200357 echo 'This will take five seconds...'
358 let waittime = 2000
359 if has('win32')
360 let cmd = $windir . '\system32\timeout.exe 1'
361 else
362 let cmd = 'sleep 1'
363 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200364 endif
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200365
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200366 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200367 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200368 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200369 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200370 call assert_equal(1, winnr('$'))
371
372 call term_start(cmd, {'term_finish': 'close'})
373 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200374 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200375 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200376 call assert_equal(1, winnr('$'))
377
378 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200379 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200380 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200381 call assert_equal(2, winnr('$'))
382 bwipe
383
384 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200385 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200386 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200387 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200388 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200389
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200390 exe 'terminal ++hidden ++open ' . cmd
391 call assert_equal(1, winnr('$'))
392 call WaitFor("winnr('$') == 2", waittime)
393 call assert_equal(2, winnr('$'))
394 bwipe
395
396 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
397 call assert_equal(1, winnr('$'))
398 call WaitFor("winnr('$') == 2", waittime)
399 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200400 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200401
402 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
403 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
404 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
405 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
406
407 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200408 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200409 call WaitFor("winnr('$') == 2", waittime)
410 call assert_equal(2, winnr('$'))
411 call assert_equal(4, winheight(0))
412 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200413endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200414
415func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200416 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200417 return
418 endif
419 call mkdir('Xdir')
420 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200421 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
422 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200423
424 exe buf . 'bwipe'
425 call delete('Xdir', 'rf')
426endfunc
427
428func Test_terminal_env()
429 if !has('unix')
430 return
431 endif
Bram Moolenaarc0870612017-08-14 22:01:16 +0200432 let g:buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200433 " Wait for the shell to display a prompt
Bram Moolenaarc0870612017-08-14 22:01:16 +0200434 call WaitFor('term_getline(g:buf, 1) != ""')
435 call term_sendkeys(g:buf, "echo $TESTENV\r")
436 call term_wait(g:buf)
437 call Stop_shell_in_terminal(g:buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200438 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200439 call assert_equal('correct', getline(2))
440
Bram Moolenaarc0870612017-08-14 22:01:16 +0200441 exe g:buf . 'bwipe'
442 unlet g:buf
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200443endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200444
445" must be last, we can't go back from GUI to terminal
446func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200447 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200448 return
449 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200450
451 " Ignore the "failed to create input context" error.
452 call test_ignore_error('E285:')
453
Bram Moolenaar679653e2017-08-13 14:13:19 +0200454 gui -f
455
456 call assert_equal(1, winnr('$'))
457 let buf = Run_shell_in_terminal({'term_finish': 'close'})
458 call Stop_shell_in_terminal(buf)
459 call term_wait(buf)
460
461 " closing window wipes out the terminal buffer a with finished job
462 call WaitFor("winnr('$') == 1")
463 call assert_equal(1, winnr('$'))
464 call assert_equal("", bufname(buf))
465
466 unlet g:job
467endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200468
469func Test_terminal_list_args()
470 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
471 call assert_fails(buf . 'bwipe', 'E517')
472 exe buf . 'bwipe!'
473 call assert_equal("", bufname(buf))
474endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200475
476func Test_terminal_noblock()
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200477 let g:buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200478 if has('mac')
479 " The shell or something else has a problem dealing with more than 1000
480 " characters at the same time.
481 let len = 1000
482 else
483 let len = 5000
484 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200485
486 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200487 call term_sendkeys(g:buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200488 endfor
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200489 call term_sendkeys(g:buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200490
491 " On MS-Windows there is an extra empty line below "done". Find "done" in
492 " the last-but-one or the last-but-two line.
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200493 let g:lnum = term_getsize(g:buf)[0] - 1
Bram Moolenaareef05312017-08-20 20:21:23 +0200494 call WaitFor('term_getline(g:buf, g:lnum) =~ "done" || term_getline(g:buf, g:lnum - 1) =~ "done"', 3000)
495 let line = term_getline(g:buf, g:lnum)
496 if line !~ 'done'
497 let line = term_getline(g:buf, g:lnum - 1)
498 endif
499 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200500
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200501 let g:job = term_getjob(g:buf)
502 call Stop_shell_in_terminal(g:buf)
503 call term_wait(g:buf)
504 unlet g:buf
505 unlet g:job
506 unlet g:lnum
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200507 bwipe
508endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200509
510func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200511 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200512 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200513 endif
514 new
515 call setline(1, ['one', 'two', 'three'])
516 %term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200517 call WaitFor('getline("$") =~ "3"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200518 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200519 call assert_equal(['3', '3', '14'], nrs)
520 bwipe
521
Bram Moolenaardada6d22017-09-02 17:18:35 +0200522 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200523 call setline(1, ['one', 'two', 'three', 'four'])
524 2,3term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200525 call WaitFor('getline("$") =~ "2"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200526 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200527 call assert_equal(['2', '2', '10'], nrs)
528 bwipe
529
Bram Moolenaardada6d22017-09-02 17:18:35 +0200530 if executable('python')
531 new
532 call setline(1, ['print("hello")'])
533 1term ++eof=exit() python
534 " MS-Windows echoes the input, Unix doesn't.
535 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
536 if getline(1) =~ 'hello'
537 call assert_equal('hello', getline(1))
538 else
539 call assert_equal('hello', getline(line('$') - 1))
540 endif
541 bwipe
542
543 if has('win32')
544 new
545 call setline(1, ['print("hello")'])
546 1term ++eof=<C-Z> python
547 call WaitFor('getline("$") =~ "Z"')
548 call assert_equal('hello', getline(line('$') - 1))
549 bwipe
550 endif
551 endif
552
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200553 bwipe!
554endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200555
556func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200557 " Todo: make this work in the GUI
558 if !has('gui_running')
559 return
560 endif
561 let buf = term_start('NONE', {})
562 call assert_notequal(0, buf)
563
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200564 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200565 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200566 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200567 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200568 else
569 call system('echo "look here" > ' . pty)
570 endif
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200571 let g:buf = buf
572 call WaitFor('term_getline(g:buf, 1) =~ "look here"')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200573
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200574 call assert_match('look here', term_getline(buf, 1))
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200575 bwipe!
576endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200577
578func Test_terminal_special_chars()
579 " this file name only works on Unix
580 if !has('unix')
581 return
582 endif
583 call mkdir('Xdir with spaces')
584 call writefile(['x'], 'Xdir with spaces/quoted"file')
585 term ls Xdir\ with\ spaces/quoted\"file
586 call WaitFor('term_getline("", 1) =~ "quoted"')
587 call assert_match('quoted"file', term_getline('', 1))
588 call term_wait('')
589
590 call delete('Xdir with spaces', 'rf')
591 bwipe
592endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200593
594func Test_terminal_wrong_options()
595 call assert_fails('call term_start(&shell, {
596 \ "in_io": "file",
597 \ "in_name": "xxx",
598 \ "out_io": "file",
599 \ "out_name": "xxx",
600 \ "err_io": "file",
601 \ "err_name": "xxx"
602 \ })', 'E474:')
603 call assert_fails('call term_start(&shell, {
604 \ "out_buf": bufnr("%")
605 \ })', 'E474:')
606 call assert_fails('call term_start(&shell, {
607 \ "err_buf": bufnr("%")
608 \ })', 'E474:')
609endfunc
610
611func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200612 " TODO: this should work on MS-Window
613 if has('unix')
614 let cmd = Get_cat_123_cmd()
615 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
616 call term_wait(buf)
617 call WaitFor('len(readfile("Xfile")) > 0')
618 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200619 let g:job = term_getjob(buf)
620 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200621 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200622 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200623 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200624
625 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200626 call writefile(['one line'], 'Xfile')
627 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
628 call term_wait(buf)
629 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
630 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200631 let g:job = term_getjob(buf)
632 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200633 bwipe
634 call delete('Xfile')
635 endif
636endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200637
638func TerminalTmap(remap)
639 let buf = Run_shell_in_terminal({})
640 call assert_equal('t', mode())
641
642 if a:remap
643 tmap 123 456
644 else
645 tnoremap 123 456
646 endif
647 tmap 456 abcde
648 call assert_equal('456', maparg('123', 't'))
649 call assert_equal('abcde', maparg('456', 't'))
650 call feedkeys("123", 'tx')
Bram Moolenaar1514e8f2017-09-16 17:35:13 +0200651 let g:buf = buf
652 call WaitFor("term_getline(g:buf,term_getcursor(g:buf)[0]) =~ 'abcde\\|456'")
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200653 let lnum = term_getcursor(buf)[0]
654 if a:remap
655 call assert_match('abcde', term_getline(buf, lnum))
656 else
657 call assert_match('456', term_getline(buf, lnum))
658 endif
659
660 call term_sendkeys(buf, "\r")
661 call Stop_shell_in_terminal(buf)
662 call term_wait(buf)
663
664 tunmap 123
665 tunmap 456
666 call assert_equal('', maparg('123', 't'))
667 close
668 unlet g:job
669endfunc
670
671func Test_terminal_tmap()
672 call TerminalTmap(1)
673 call TerminalTmap(0)
674endfunc