blob: 3ca638af30f6a7cb970f1b1a7e8b65dbb0847b99 [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
3if !exists('*term_start')
4 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")
39 call assert_match("^/dev/", job_info(g:job).tty)
40 call assert_match("^/dev/", term_gettty(''))
41 else
Bram Moolenaar5be8dd02017-08-03 20:52:19 +020042 call assert_match("^winpty://", job_info(g:job).tty)
43 call assert_match("^winpty://", term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020044 endif
Bram Moolenaar94053a52017-08-01 21:44:33 +020045 call Stop_shell_in_terminal(buf)
46 call term_wait(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020047
Bram Moolenaar94053a52017-08-01 21:44:33 +020048 " closing window wipes out the terminal buffer a with finished job
49 close
50 call assert_equal("", bufname(buf))
51
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020052 unlet g:job
53endfunc
54
55func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020056 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020057 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020058 call term_wait(buf)
59
60 setlocal modifiable
61 exe "normal Axxx\<Esc>"
62 call assert_fails(buf . 'bwipe', 'E517')
63 undo
64
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020065 exe buf . 'bwipe'
66 unlet g:job
67endfunc
68
Bram Moolenaar94053a52017-08-01 21:44:33 +020069func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020070 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020071 call assert_fails(buf . 'bwipe', 'E517')
72 exe buf . 'bwipe!'
Bram Moolenaar94053a52017-08-01 21:44:33 +020073 call WaitFor('job_status(g:job) == "dead"')
74 call assert_equal('dead', job_status(g:job))
75 call assert_equal("", bufname(buf))
76
77 unlet g:job
78endfunc
79
80func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020081 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020082 quit
83 for nr in range(1, winnr('$'))
84 call assert_notequal(winbufnr(nr), buf)
85 endfor
86 call assert_true(bufloaded(buf))
87 call assert_true(buflisted(buf))
88
89 exe 'split ' . buf . 'buf'
90 call Stop_shell_in_terminal(buf)
91 exe buf . 'bwipe'
92
93 unlet g:job
94endfunc
95
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020096func! s:Nasty_exit_cb(job, st)
97 exe g:buf . 'bwipe!'
98 let g:buf = 0
99endfunc
100
101func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200102 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200103 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
104 let g:job = term_getjob(g:buf)
105
106 call WaitFor('job_status(g:job) == "dead"')
107 call WaitFor('g:buf == 0')
108 unlet g:buf
109 unlet g:job
110 call delete('Xtext')
111endfunc
112
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200113func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200114 let l = term_scrape(a:buf, 0)
115 call assert_true(len(l) == 0)
116 let l = term_scrape(a:buf, 999)
117 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200118 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200119 call assert_true(len(l) > 0)
120 call assert_equal('1', l[0].chars)
121 call assert_equal('2', l[1].chars)
122 call assert_equal('3', l[2].chars)
123 call assert_equal('#00e000', l[0].fg)
124 if &background == 'light'
125 call assert_equal('#ffffff', l[0].bg)
126 else
127 call assert_equal('#000000', l[0].bg)
128 endif
129
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200130 let l = term_getline(a:buf, -1)
131 call assert_equal('', l)
132 let l = term_getline(a:buf, 0)
133 call assert_equal('', l)
134 let l = term_getline(a:buf, 999)
135 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200136 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200137 call assert_equal('123', l)
138endfunc
139
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200140func Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200141 if has('win32')
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200142 return 'cmd /c "cls && color 2 && echo 123"'
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200143 else
144 call writefile(["\<Esc>[32m123"], 'Xtext')
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200145 return "cat Xtext"
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200146 endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200147endfunc
148
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200149func Test_terminal_scrape_123()
150 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200151 let buf = term_start(cmd)
152
153 let termlist = term_list()
154 call assert_equal(1, len(termlist))
155 call assert_equal(buf, termlist[0])
156
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200157 " Nothing happens with invalid buffer number
158 call term_wait(1234)
159
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200160 call term_wait(buf)
Bram Moolenaar620d0642017-08-03 21:08:05 +0200161 if has('win32')
162 " TODO: this should not be needed
163 sleep 100m
164 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200165 call Check_123(buf)
166
167 " Must still work after the job ended.
168 let g:job = term_getjob(buf)
169 call WaitFor('job_status(g:job) == "dead"')
170 call term_wait(buf)
171 call Check_123(buf)
172
173 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200174 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200175endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200176
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200177func Test_terminal_scrape_multibyte()
178 if !has('multi_byte')
179 return
180 endif
181 call writefile(["léttまrs"], 'Xtext')
182 if has('win32')
183 let cmd = 'cmd /c "type Xtext"'
184 else
185 let cmd = "cat Xtext"
186 endif
187 let buf = term_start(cmd)
188
189 call term_wait(buf)
190 if has('win32')
191 " TODO: this should not be needed
192 sleep 100m
193 endif
194
195 let l = term_scrape(buf, 1)
196 call assert_true(len(l) >= 7)
197 call assert_equal('l', l[0].chars)
198 call assert_equal('é', l[1].chars)
199 call assert_equal(1, l[1].width)
200 call assert_equal('t', l[2].chars)
201 call assert_equal('t', l[3].chars)
202 call assert_equal('ま', l[4].chars)
203 call assert_equal(2, l[4].width)
204 call assert_equal('r', l[5].chars)
205 call assert_equal('s', l[6].chars)
206
207 let g:job = term_getjob(buf)
208 call WaitFor('job_status(g:job) == "dead"')
209 call term_wait(buf)
210
211 exe buf . 'bwipe'
212 call delete('Xtext')
213endfunc
214
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200215func Test_terminal_scroll()
216 call writefile(range(1, 200), 'Xtext')
217 if has('win32')
218 let cmd = 'cmd /c "type Xtext"'
219 else
220 let cmd = "cat Xtext"
221 endif
222 let buf = term_start(cmd)
223
224 let g:job = term_getjob(buf)
225 call WaitFor('job_status(g:job) == "dead"')
226 call term_wait(buf)
227 if has('win32')
228 " TODO: this should not be needed
229 sleep 100m
230 endif
231
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200232 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200233 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200234 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200235 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200236 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200237 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200238 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200239
240 exe buf . 'bwipe'
241 call delete('Xtext')
242endfunc
243
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200244func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200245 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200246
247 exe '5terminal ' . cmd
248 let size = term_getsize('')
249 bwipe!
250 call assert_equal(5, size[0])
251
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200252 call term_start(cmd, {'term_rows': 6})
253 let size = term_getsize('')
254 bwipe!
255 call assert_equal(6, size[0])
256
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200257 vsplit
258 exe '5,33terminal ' . cmd
259 let size = term_getsize('')
260 bwipe!
261 call assert_equal([5, 33], size)
262
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200263 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
264 let size = term_getsize('')
265 bwipe!
266 call assert_equal([6, 36], size)
267
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200268 exe 'vertical 20terminal ' . cmd
269 let size = term_getsize('')
270 bwipe!
271 call assert_equal(20, size[1])
272
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200273 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
274 let size = term_getsize('')
275 bwipe!
276 call assert_equal(26, size[1])
277
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200278 split
279 exe 'vertical 6,20terminal ' . cmd
280 let size = term_getsize('')
281 bwipe!
282 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200283
284 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
285 let size = term_getsize('')
286 bwipe!
287 call assert_equal([7, 27], size)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200288endfunc
289
290func Test_terminal_curwin()
291 let cmd = Get_cat_123_cmd()
292 call assert_equal(1, winnr('$'))
293
294 split dummy
295 exe 'terminal ++curwin ' . cmd
296 call assert_equal(2, winnr('$'))
297 bwipe!
298
299 split dummy
300 call term_start(cmd, {'curwin': 1})
301 call assert_equal(2, winnr('$'))
302 bwipe!
303
304 split dummy
305 call setline(1, 'change')
306 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
307 call assert_equal(2, winnr('$'))
308 exe 'terminal! ++curwin ' . cmd
309 call assert_equal(2, winnr('$'))
310 bwipe!
311
312 split dummy
313 call setline(1, 'change')
314 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
315 call assert_equal(2, winnr('$'))
316 bwipe!
317
318 split dummy
319 bwipe!
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200320
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200321endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200322
Bram Moolenaar37c45832017-08-12 16:01:04 +0200323func Test_finish_open_close()
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200324 call assert_equal(1, winnr('$'))
325
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200326 if s:python != ''
327 let cmd = s:python . " test_short_sleep.py"
328 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200329 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200330 echo 'This will take five seconds...'
331 let waittime = 2000
332 if has('win32')
333 let cmd = $windir . '\system32\timeout.exe 1'
334 else
335 let cmd = 'sleep 1'
336 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200337 endif
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200338
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200339 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200340 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200341 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200342 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200343 call assert_equal(1, winnr('$'))
344
345 call term_start(cmd, {'term_finish': 'close'})
346 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200347 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200348 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200349 call assert_equal(1, winnr('$'))
350
351 exe 'terminal ++open ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200352 close
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200353 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200354 call assert_equal(2, winnr('$'))
355 bwipe
356
357 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200358 close
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200359 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200360 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200361 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200362
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200363 exe 'terminal ++hidden ++open ' . cmd
364 call assert_equal(1, winnr('$'))
365 call WaitFor("winnr('$') == 2", waittime)
366 call assert_equal(2, winnr('$'))
367 bwipe
368
369 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
370 call assert_equal(1, winnr('$'))
371 call WaitFor("winnr('$') == 2", waittime)
372 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200373 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200374
375 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
376 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
377 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
378 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
379
380 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
381 close
382 call WaitFor("winnr('$') == 2", waittime)
383 call assert_equal(2, winnr('$'))
384 call assert_equal(4, winheight(0))
385 bwipe
386
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200387endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200388
389func Test_terminal_cwd()
390 if !has('unix')
391 return
392 endif
393 call mkdir('Xdir')
394 let buf = term_start('pwd', {'cwd': 'Xdir'})
395 sleep 100m
396 call term_wait(buf)
397 call assert_equal(getcwd() . '/Xdir', getline(1))
398
399 exe buf . 'bwipe'
400 call delete('Xdir', 'rf')
401endfunc
402
403func Test_terminal_env()
404 if !has('unix')
405 return
406 endif
407 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
408 call term_wait(buf)
409 call term_sendkeys(buf, "echo $TESTENV\r")
410 call term_wait(buf)
411 call Stop_shell_in_terminal(buf)
412 call term_wait(buf)
413 call assert_equal('correct', getline(2))
414
415 exe buf . 'bwipe'
416endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200417
418" must be last, we can't go back from GUI to terminal
419func Test_zz_terminal_in_gui()
420 if !has('gui')
421 return
422 endif
423 gui -f
424
425 call assert_equal(1, winnr('$'))
426 let buf = Run_shell_in_terminal({'term_finish': 'close'})
427 call Stop_shell_in_terminal(buf)
428 call term_wait(buf)
429
430 " closing window wipes out the terminal buffer a with finished job
431 call WaitFor("winnr('$') == 1")
432 call assert_equal(1, winnr('$'))
433 call assert_equal("", bufname(buf))
434
435 unlet g:job
436endfunc