blob: a99818b8d75f8607546689c4b7aa67fe940dce5d [file] [log] [blame]
Bram Moolenaar1112c0f2020-07-01 21:53:50 +02001" Tests for the terminal window.
2" This is split in two, because it can take a lot of time.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02003" See test_terminal.vim and test_terminal3.vim for further tests.
Bram Moolenaar1112c0f2020-07-01 21:53:50 +02004
5source check.vim
6CheckFeature terminal
7
8source shared.vim
9source screendump.vim
10source mouse.vim
11source term_util.vim
12
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020013let $PROMPT_COMMAND=''
14
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020015func Test_terminal_termwinsize_option_fixed()
16 CheckRunVimInTerminal
17 set termwinsize=6x40
18 let text = []
19 for n in range(10)
20 call add(text, repeat(n, 50))
21 endfor
22 call writefile(text, 'Xwinsize')
23 let buf = RunVimInTerminal('Xwinsize', {})
24 let win = bufwinid(buf)
25 call assert_equal([6, 40], term_getsize(buf))
26 call assert_equal(6, winheight(win))
27 call assert_equal(40, winwidth(win))
28
29 " resizing the window doesn't resize the terminal.
30 resize 10
31 vertical resize 60
32 call assert_equal([6, 40], term_getsize(buf))
33 call assert_equal(10, winheight(win))
34 call assert_equal(60, winwidth(win))
35
36 call StopVimInTerminal(buf)
37 call delete('Xwinsize')
38
Bram Moolenaare2e40752020-09-04 21:18:46 +020039 call assert_fails('set termwinsize=40', 'E474:')
40 call assert_fails('set termwinsize=10+40', 'E474:')
41 call assert_fails('set termwinsize=abc', 'E474:')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020042
43 set termwinsize=
44endfunc
45
46func Test_terminal_termwinsize_option_zero()
47 set termwinsize=0x0
48 let buf = Run_shell_in_terminal({})
49 let win = bufwinid(buf)
50 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
51 call StopShellInTerminal(buf)
52 call TermWait(buf)
53 exe buf . 'bwipe'
54
55 set termwinsize=7x0
56 let buf = Run_shell_in_terminal({})
57 let win = bufwinid(buf)
58 call assert_equal([7, winwidth(win)], term_getsize(buf))
59 call StopShellInTerminal(buf)
60 call TermWait(buf)
61 exe buf . 'bwipe'
62
63 set termwinsize=0x33
64 let buf = Run_shell_in_terminal({})
65 let win = bufwinid(buf)
66 call assert_equal([winheight(win), 33], term_getsize(buf))
67 call StopShellInTerminal(buf)
68 call TermWait(buf)
69 exe buf . 'bwipe'
70
71 set termwinsize=
72endfunc
73
74func Test_terminal_termwinsize_minimum()
75 set termwinsize=10*50
76 vsplit
77 let buf = Run_shell_in_terminal({})
78 let win = bufwinid(buf)
79 call assert_inrange(10, 1000, winheight(win))
80 call assert_inrange(50, 1000, winwidth(win))
81 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
82
83 resize 15
84 vertical resize 60
85 redraw
86 call assert_equal([15, 60], term_getsize(buf))
87 call assert_equal(15, winheight(win))
88 call assert_equal(60, winwidth(win))
89
90 resize 7
91 vertical resize 30
92 redraw
93 call assert_equal([10, 50], term_getsize(buf))
94 call assert_equal(7, winheight(win))
95 call assert_equal(30, winwidth(win))
96
97 call StopShellInTerminal(buf)
98 call TermWait(buf)
99 exe buf . 'bwipe'
100
101 set termwinsize=0*0
102 let buf = Run_shell_in_terminal({})
103 let win = bufwinid(buf)
104 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
105 call StopShellInTerminal(buf)
106 call TermWait(buf)
107 exe buf . 'bwipe'
108
109 set termwinsize=
110endfunc
111
Bram Moolenaarb936b792020-09-04 18:34:09 +0200112func Test_terminal_termwinsize_overruled()
113 let cmd = GetDummyCmd()
114 set termwinsize=5x43
115 let buf = term_start(cmd, #{term_rows: 7, term_cols: 50})
116 call TermWait(buf)
117 call assert_equal([7, 50], term_getsize(buf))
118 exe "bwipe! " .. buf
119
120 let buf = term_start(cmd, #{term_cols: 50})
121 call TermWait(buf)
122 call assert_equal([5, 50], term_getsize(buf))
123 exe "bwipe! " .. buf
124
125 let buf = term_start(cmd, #{term_rows: 7})
126 call TermWait(buf)
127 call assert_equal([7, 43], term_getsize(buf))
128 exe "bwipe! " .. buf
129
130 set termwinsize=
131endfunc
132
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200133func Test_terminal_termwinkey()
134 " make three tabpages, terminal in the middle
135 0tabnew
136 tabnext
137 tabnew
138 tabprev
139 call assert_equal(1, winnr('$'))
140 call assert_equal(2, tabpagenr())
141 let thiswin = win_getid()
142
143 let buf = Run_shell_in_terminal({})
144 let termwin = bufwinid(buf)
145 set termwinkey=<C-L>
146 call feedkeys("\<C-L>w", 'tx')
147 call assert_equal(thiswin, win_getid())
148 call feedkeys("\<C-W>w", 'tx')
149 call assert_equal(termwin, win_getid())
150
151 if has('langmap')
152 set langmap=xjyk
153 call feedkeys("\<C-L>x", 'tx')
154 call assert_equal(thiswin, win_getid())
155 call feedkeys("\<C-W>y", 'tx')
156 call assert_equal(termwin, win_getid())
157 set langmap=
158 endif
159
160 call feedkeys("\<C-L>gt", "xt")
161 call assert_equal(3, tabpagenr())
162 tabprev
163 call assert_equal(2, tabpagenr())
164 call assert_equal(termwin, win_getid())
165
166 call feedkeys("\<C-L>gT", "xt")
167 call assert_equal(1, tabpagenr())
168 tabnext
169 call assert_equal(2, tabpagenr())
170 call assert_equal(termwin, win_getid())
171
172 let job = term_getjob(buf)
173 call feedkeys("\<C-L>\<C-C>", 'tx')
174 call WaitForAssert({-> assert_equal("dead", job_status(job))})
175
176 set termwinkey&
177 tabnext
178 tabclose
179 tabprev
180 tabclose
181endfunc
182
183func Test_terminal_out_err()
184 CheckUnix
185
186 call writefile([
187 \ '#!/bin/sh',
188 \ 'echo "this is standard error" >&2',
189 \ 'echo "this is standard out" >&1',
190 \ ], 'Xechoerrout.sh')
191 call setfperm('Xechoerrout.sh', 'rwxrwx---')
192
193 let outfile = 'Xtermstdout'
194 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
195
196 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
197 call assert_equal(['this is standard out'], readfile(outfile))
198 call assert_equal('this is standard error', term_getline(buf, 1))
199
200 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
201 exe buf . 'bwipe'
202 call delete('Xechoerrout.sh')
203 call delete(outfile)
204endfunc
205
206func Test_termwinscroll()
207 CheckUnix
Bram Moolenaarf65927f2020-07-11 14:04:28 +0200208 " TODO: Somehow this test sometimes hangs in the GUI
209 CheckNotGui
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200210
211 " Let the terminal output more than 'termwinscroll' lines, some at the start
212 " will be dropped.
213 exe 'set termwinscroll=' . &lines
214 let buf = term_start('/bin/sh')
215 for i in range(1, &lines)
216 call feedkeys("echo " . i . "\<CR>", 'xt')
217 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
218 endfor
219 " Go to Terminal-Normal mode to update the buffer.
220 call feedkeys("\<C-W>N", 'xt')
221 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
222
223 " Every "echo nr" must only appear once
224 let lines = getline(1, line('$'))
225 for i in range(&lines - len(lines) / 2 + 2, &lines)
226 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
227 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
228 endfor
229
230 exe buf . 'bwipe!'
231endfunc
232
233" Resizing the terminal window caused an ml_get error.
234" TODO: This does not reproduce the original problem.
235func Test_terminal_resize()
236 set statusline=x
237 terminal
238 call assert_equal(2, winnr('$'))
239
240 " Fill the terminal with text.
241 if has('win32')
242 call feedkeys("dir\<CR>", 'xt')
243 else
244 call feedkeys("ls\<CR>", 'xt')
245 endif
246 " Go to Terminal-Normal mode for a moment.
247 call feedkeys("\<C-W>N", 'xt')
248 " Open a new window
249 call feedkeys("i\<C-W>n", 'xt')
250 call assert_equal(3, winnr('$'))
251 redraw
252
253 close
254 call assert_equal(2, winnr('$'))
255 call feedkeys("exit\<CR>", 'xt')
256 set statusline&
257endfunc
258
259" must be nearly the last, we can't go back from GUI to terminal
260func Test_zz1_terminal_in_gui()
261 CheckCanRunGui
262
263 " Ignore the "failed to create input context" error.
264 call test_ignore_error('E285:')
265
266 gui -f
267
268 call assert_equal(1, winnr('$'))
269 let buf = Run_shell_in_terminal({'term_finish': 'close'})
270 call StopShellInTerminal(buf)
271 call TermWait(buf)
272
273 " closing window wipes out the terminal buffer a with finished job
274 call WaitForAssert({-> assert_equal(1, winnr('$'))})
275 call assert_equal("", bufname(buf))
276
277 unlet g:job
278endfunc
279
Bram Moolenaarc85156b2020-07-12 14:09:23 +0200280" TODO: reenable when this no longer hangs on Travis
281"func Test_zz2_terminal_guioptions_bang()
282" CheckGui
283" set guioptions+=!
284"
285" let filename = 'Xtestscript'
286" if has('win32')
287" let filename .= '.bat'
288" let prefix = ''
289" let contents = ['@echo off', 'exit %1']
290" else
291" let filename .= '.sh'
292" let prefix = './'
293" let contents = ['#!/bin/sh', 'exit $1']
294" endif
295" call writefile(contents, filename)
296" call setfperm(filename, 'rwxrwx---')
297"
298" " Check if v:shell_error is equal to the exit status.
299" let exitval = 0
300" execute printf(':!%s%s %d', prefix, filename, exitval)
301" call assert_equal(exitval, v:shell_error)
302"
303" let exitval = 9
304" execute printf(':!%s%s %d', prefix, filename, exitval)
305" call assert_equal(exitval, v:shell_error)
306"
307" set guioptions&
308" call delete(filename)
309"endfunc
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200310
311func Test_terminal_hidden()
312 CheckUnix
313
314 term ++hidden cat
315 let bnr = bufnr('$')
316 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
317 exe 'sbuf ' . bnr
318 call assert_equal('terminal', &buftype)
319 call term_sendkeys(bnr, "asdf\<CR>")
320 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
321 call term_sendkeys(bnr, "\<C-D>")
322 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
323 bwipe!
324endfunc
325
326func Test_terminal_switch_mode()
327 term
328 let bnr = bufnr('$')
329 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaarc85156b2020-07-12 14:09:23 +0200330 " In the GUI the first switch sometimes doesn't work. Switch twice to avoid
331 " flakyness.
332 call feedkeys("\<C-W>N", 'xt')
333 call feedkeys("A", 'xt')
334 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200335 call feedkeys("\<C-W>N", 'xt')
336 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
337 call feedkeys("A", 'xt')
338 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
339 call feedkeys("\<C-\>\<C-N>", 'xt')
340 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
341 call feedkeys("I", 'xt')
342 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
343 call feedkeys("\<C-W>Nv", 'xt')
344 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
345 call feedkeys("I", 'xt')
346 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
347 call feedkeys("\<C-W>Nv", 'xt')
348 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
349 call feedkeys("A", 'xt')
350 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
351 bwipe!
352endfunc
353
354func Test_terminal_normal_mode()
355 CheckRunVimInTerminal
356
357 " Run Vim in a terminal and open a terminal window to run Vim in.
358 let lines =<< trim END
359 call setline(1, range(11111, 11122))
360 3
361 END
362 call writefile(lines, 'XtermNormal')
363 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
364 call TermWait(buf)
365
366 call term_sendkeys(buf, "\<C-W>N")
367 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
368 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
369
370 call term_sendkeys(buf, ":set culopt=number\r")
371 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
372
373 call term_sendkeys(buf, ":set culopt=line\r")
374 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
375
376 call assert_fails('call term_sendkeys(buf, [])', 'E730:')
377 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
378 call StopVimInTerminal(buf)
379 call delete('XtermNormal')
380endfunc
381
382func Test_terminal_hidden_and_close()
383 CheckUnix
384
385 call assert_equal(1, winnr('$'))
386 term ++hidden ++close ls
387 let bnr = bufnr('$')
388 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
389 call WaitForAssert({-> assert_false(bufexists(bnr))})
390 call assert_equal(1, winnr('$'))
391endfunc
392
393func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200394 if has('conpty')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200395 throw 'Skipped: fail on ConPTY'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200396 endif
397 let contents = [
398 \ [ 'One', '', 'X' ],
399 \ [ 'Two', '', '' ],
400 \ [ 'Three' ] + repeat([''], 30)
401 \ ]
402
403 for c in contents
404 call writefile(c, 'Xfile')
405 if has('win32')
406 term cmd /c type Xfile
407 else
408 term cat Xfile
409 endif
410 let bnr = bufnr('$')
411 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
412 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
413 sleep 100m
414 call assert_equal(c, getline(1, line('$')))
415 quit
416 endfor
417
418 call delete('Xfile')
419endfunc
420
Bram Moolenaarb936b792020-09-04 18:34:09 +0200421func GetDummyCmd()
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200422 if has('win32')
Bram Moolenaarb936b792020-09-04 18:34:09 +0200423 return 'cmd /c ""'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200424 else
425 CheckExecutable false
Bram Moolenaarb936b792020-09-04 18:34:09 +0200426 return 'false'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200427 endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200428endfunc
429
430func Test_terminal_no_job()
431 let cmd = GetDummyCmd()
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200432 let term = term_start(cmd, {'term_finish': 'close'})
433 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
434endfunc
435
436func Test_term_getcursor()
437 CheckUnix
438
439 let buf = Run_shell_in_terminal({})
440
441 " Wait for the shell to display a prompt.
442 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
443
444 " Hide the cursor.
445 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
446 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
447
448 " Show the cursor.
449 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
450 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
451
452 " Change color of cursor.
453 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
454 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
455 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
456 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
457 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
458
459 " Make cursor a blinking block.
460 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
461 call WaitForAssert({-> assert_equal([1, 1],
462 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
463
464 " Make cursor a steady block.
465 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
466 call WaitForAssert({-> assert_equal([0, 1],
467 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
468
469 " Make cursor a blinking underline.
470 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
471 call WaitForAssert({-> assert_equal([1, 2],
472 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
473
474 " Make cursor a steady underline.
475 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
476 call WaitForAssert({-> assert_equal([0, 2],
477 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
478
479 " Make cursor a blinking vertical bar.
480 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
481 call WaitForAssert({-> assert_equal([1, 3],
482 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
483
484 " Make cursor a steady vertical bar.
485 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
486 call WaitForAssert({-> assert_equal([0, 3],
487 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
488
489 call StopShellInTerminal(buf)
490endfunc
491
492" Test for term_gettitle()
493func Test_term_gettitle()
494 " term_gettitle() returns an empty string for a non-terminal buffer
495 " and for a non-existing buffer.
496 call assert_equal('', bufnr('%')->term_gettitle())
497 call assert_equal('', term_gettitle(bufnr('$') + 1))
498
499 if !has('title') || empty(&t_ts)
500 throw "Skipped: can't get/set title"
501 endif
502
503 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', '-c', 'set title'])
504 if has('autoservername')
505 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
506 call term_sendkeys(term, ":e Xfoo\r")
507 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
508 else
509 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
510 call term_sendkeys(term, ":e Xfoo\r")
511 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
512 endif
513
514 call term_sendkeys(term, ":set titlestring=foo\r")
515 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
516
517 exe term . 'bwipe!'
518endfunc
519
520func Test_term_gettty()
521 let buf = Run_shell_in_terminal({})
522 let gettty = term_gettty(buf)
523
524 if has('unix') && executable('tty')
525 " Find tty using the tty shell command.
526 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
527 call term_sendkeys(buf, "tty\r")
528 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
529 let tty = term_getline(buf, 2)
530 call assert_equal(tty, gettty)
531 endif
532
533 let gettty0 = term_gettty(buf, 0)
534 let gettty1 = term_gettty(buf, 1)
535
536 call assert_equal(gettty, gettty0)
537 call assert_equal(job_info(g:job).tty_out, gettty0)
538 call assert_equal(job_info(g:job).tty_in, gettty1)
539
540 if has('unix')
541 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
542 " are identical according to :help term_gettty()
543 call assert_equal(gettty0, gettty1)
544 call assert_match('^/dev/', gettty)
545 else
546 " ConPTY works on anonymous pipe.
547 if !has('conpty')
548 call assert_match('^\\\\.\\pipe\\', gettty0)
549 call assert_match('^\\\\.\\pipe\\', gettty1)
550 endif
551 endif
552
553 call assert_fails('call term_gettty(buf, 2)', 'E475:')
554 call assert_fails('call term_gettty(buf, -1)', 'E475:')
555
556 call assert_equal('', term_gettty(buf + 1))
557
558 call StopShellInTerminal(buf)
559 call TermWait(buf)
560 exe buf . 'bwipe'
561endfunc
562
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200563
564" vim: shiftwidth=2 sts=2 expandtab