blob: deeb86f945763678a0a367db46f9519e5d1c0c8f [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
39 call assert_fails('set termwinsize=40', 'E474')
40 call assert_fails('set termwinsize=10+40', 'E474')
41 call assert_fails('set termwinsize=abc', 'E474')
42
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
112func Test_terminal_termwinkey()
113 " make three tabpages, terminal in the middle
114 0tabnew
115 tabnext
116 tabnew
117 tabprev
118 call assert_equal(1, winnr('$'))
119 call assert_equal(2, tabpagenr())
120 let thiswin = win_getid()
121
122 let buf = Run_shell_in_terminal({})
123 let termwin = bufwinid(buf)
124 set termwinkey=<C-L>
125 call feedkeys("\<C-L>w", 'tx')
126 call assert_equal(thiswin, win_getid())
127 call feedkeys("\<C-W>w", 'tx')
128 call assert_equal(termwin, win_getid())
129
130 if has('langmap')
131 set langmap=xjyk
132 call feedkeys("\<C-L>x", 'tx')
133 call assert_equal(thiswin, win_getid())
134 call feedkeys("\<C-W>y", 'tx')
135 call assert_equal(termwin, win_getid())
136 set langmap=
137 endif
138
139 call feedkeys("\<C-L>gt", "xt")
140 call assert_equal(3, tabpagenr())
141 tabprev
142 call assert_equal(2, tabpagenr())
143 call assert_equal(termwin, win_getid())
144
145 call feedkeys("\<C-L>gT", "xt")
146 call assert_equal(1, tabpagenr())
147 tabnext
148 call assert_equal(2, tabpagenr())
149 call assert_equal(termwin, win_getid())
150
151 let job = term_getjob(buf)
152 call feedkeys("\<C-L>\<C-C>", 'tx')
153 call WaitForAssert({-> assert_equal("dead", job_status(job))})
154
155 set termwinkey&
156 tabnext
157 tabclose
158 tabprev
159 tabclose
160endfunc
161
162func Test_terminal_out_err()
163 CheckUnix
164
165 call writefile([
166 \ '#!/bin/sh',
167 \ 'echo "this is standard error" >&2',
168 \ 'echo "this is standard out" >&1',
169 \ ], 'Xechoerrout.sh')
170 call setfperm('Xechoerrout.sh', 'rwxrwx---')
171
172 let outfile = 'Xtermstdout'
173 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
174
175 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
176 call assert_equal(['this is standard out'], readfile(outfile))
177 call assert_equal('this is standard error', term_getline(buf, 1))
178
179 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
180 exe buf . 'bwipe'
181 call delete('Xechoerrout.sh')
182 call delete(outfile)
183endfunc
184
185func Test_termwinscroll()
186 CheckUnix
Bram Moolenaarf65927f2020-07-11 14:04:28 +0200187 " TODO: Somehow this test sometimes hangs in the GUI
188 CheckNotGui
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200189
190 " Let the terminal output more than 'termwinscroll' lines, some at the start
191 " will be dropped.
192 exe 'set termwinscroll=' . &lines
193 let buf = term_start('/bin/sh')
194 for i in range(1, &lines)
195 call feedkeys("echo " . i . "\<CR>", 'xt')
196 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
197 endfor
198 " Go to Terminal-Normal mode to update the buffer.
199 call feedkeys("\<C-W>N", 'xt')
200 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
201
202 " Every "echo nr" must only appear once
203 let lines = getline(1, line('$'))
204 for i in range(&lines - len(lines) / 2 + 2, &lines)
205 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
206 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
207 endfor
208
209 exe buf . 'bwipe!'
210endfunc
211
212" Resizing the terminal window caused an ml_get error.
213" TODO: This does not reproduce the original problem.
214func Test_terminal_resize()
215 set statusline=x
216 terminal
217 call assert_equal(2, winnr('$'))
218
219 " Fill the terminal with text.
220 if has('win32')
221 call feedkeys("dir\<CR>", 'xt')
222 else
223 call feedkeys("ls\<CR>", 'xt')
224 endif
225 " Go to Terminal-Normal mode for a moment.
226 call feedkeys("\<C-W>N", 'xt')
227 " Open a new window
228 call feedkeys("i\<C-W>n", 'xt')
229 call assert_equal(3, winnr('$'))
230 redraw
231
232 close
233 call assert_equal(2, winnr('$'))
234 call feedkeys("exit\<CR>", 'xt')
235 set statusline&
236endfunc
237
238" must be nearly the last, we can't go back from GUI to terminal
239func Test_zz1_terminal_in_gui()
240 CheckCanRunGui
241
242 " Ignore the "failed to create input context" error.
243 call test_ignore_error('E285:')
244
245 gui -f
246
247 call assert_equal(1, winnr('$'))
248 let buf = Run_shell_in_terminal({'term_finish': 'close'})
249 call StopShellInTerminal(buf)
250 call TermWait(buf)
251
252 " closing window wipes out the terminal buffer a with finished job
253 call WaitForAssert({-> assert_equal(1, winnr('$'))})
254 call assert_equal("", bufname(buf))
255
256 unlet g:job
257endfunc
258
Bram Moolenaarc85156b2020-07-12 14:09:23 +0200259" TODO: reenable when this no longer hangs on Travis
260"func Test_zz2_terminal_guioptions_bang()
261" CheckGui
262" set guioptions+=!
263"
264" let filename = 'Xtestscript'
265" if has('win32')
266" let filename .= '.bat'
267" let prefix = ''
268" let contents = ['@echo off', 'exit %1']
269" else
270" let filename .= '.sh'
271" let prefix = './'
272" let contents = ['#!/bin/sh', 'exit $1']
273" endif
274" call writefile(contents, filename)
275" call setfperm(filename, 'rwxrwx---')
276"
277" " Check if v:shell_error is equal to the exit status.
278" let exitval = 0
279" execute printf(':!%s%s %d', prefix, filename, exitval)
280" call assert_equal(exitval, v:shell_error)
281"
282" let exitval = 9
283" execute printf(':!%s%s %d', prefix, filename, exitval)
284" call assert_equal(exitval, v:shell_error)
285"
286" set guioptions&
287" call delete(filename)
288"endfunc
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200289
290func Test_terminal_hidden()
291 CheckUnix
292
293 term ++hidden cat
294 let bnr = bufnr('$')
295 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
296 exe 'sbuf ' . bnr
297 call assert_equal('terminal', &buftype)
298 call term_sendkeys(bnr, "asdf\<CR>")
299 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
300 call term_sendkeys(bnr, "\<C-D>")
301 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
302 bwipe!
303endfunc
304
305func Test_terminal_switch_mode()
306 term
307 let bnr = bufnr('$')
308 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaarc85156b2020-07-12 14:09:23 +0200309 " In the GUI the first switch sometimes doesn't work. Switch twice to avoid
310 " flakyness.
311 call feedkeys("\<C-W>N", 'xt')
312 call feedkeys("A", 'xt')
313 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200314 call feedkeys("\<C-W>N", 'xt')
315 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
316 call feedkeys("A", 'xt')
317 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
318 call feedkeys("\<C-\>\<C-N>", 'xt')
319 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
320 call feedkeys("I", 'xt')
321 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
322 call feedkeys("\<C-W>Nv", 'xt')
323 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
324 call feedkeys("I", 'xt')
325 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
326 call feedkeys("\<C-W>Nv", 'xt')
327 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
328 call feedkeys("A", 'xt')
329 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
330 bwipe!
331endfunc
332
333func Test_terminal_normal_mode()
334 CheckRunVimInTerminal
335
336 " Run Vim in a terminal and open a terminal window to run Vim in.
337 let lines =<< trim END
338 call setline(1, range(11111, 11122))
339 3
340 END
341 call writefile(lines, 'XtermNormal')
342 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
343 call TermWait(buf)
344
345 call term_sendkeys(buf, "\<C-W>N")
346 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
347 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
348
349 call term_sendkeys(buf, ":set culopt=number\r")
350 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
351
352 call term_sendkeys(buf, ":set culopt=line\r")
353 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
354
355 call assert_fails('call term_sendkeys(buf, [])', 'E730:')
356 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
357 call StopVimInTerminal(buf)
358 call delete('XtermNormal')
359endfunc
360
361func Test_terminal_hidden_and_close()
362 CheckUnix
363
364 call assert_equal(1, winnr('$'))
365 term ++hidden ++close ls
366 let bnr = bufnr('$')
367 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
368 call WaitForAssert({-> assert_false(bufexists(bnr))})
369 call assert_equal(1, winnr('$'))
370endfunc
371
372func Test_terminal_does_not_truncate_last_newlines()
373 " This test does not pass through ConPTY.
374 if has('conpty')
375 return
376 endif
377 let contents = [
378 \ [ 'One', '', 'X' ],
379 \ [ 'Two', '', '' ],
380 \ [ 'Three' ] + repeat([''], 30)
381 \ ]
382
383 for c in contents
384 call writefile(c, 'Xfile')
385 if has('win32')
386 term cmd /c type Xfile
387 else
388 term cat Xfile
389 endif
390 let bnr = bufnr('$')
391 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
392 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
393 sleep 100m
394 call assert_equal(c, getline(1, line('$')))
395 quit
396 endfor
397
398 call delete('Xfile')
399endfunc
400
401func Test_terminal_no_job()
402 if has('win32')
403 let cmd = 'cmd /c ""'
404 else
405 CheckExecutable false
406 let cmd = 'false'
407 endif
408 let term = term_start(cmd, {'term_finish': 'close'})
409 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
410endfunc
411
412func Test_term_getcursor()
413 CheckUnix
414
415 let buf = Run_shell_in_terminal({})
416
417 " Wait for the shell to display a prompt.
418 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
419
420 " Hide the cursor.
421 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
422 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
423
424 " Show the cursor.
425 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
426 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
427
428 " Change color of cursor.
429 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
430 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
431 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
432 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
433 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
434
435 " Make cursor a blinking block.
436 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
437 call WaitForAssert({-> assert_equal([1, 1],
438 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
439
440 " Make cursor a steady block.
441 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
442 call WaitForAssert({-> assert_equal([0, 1],
443 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
444
445 " Make cursor a blinking underline.
446 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
447 call WaitForAssert({-> assert_equal([1, 2],
448 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
449
450 " Make cursor a steady underline.
451 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
452 call WaitForAssert({-> assert_equal([0, 2],
453 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
454
455 " Make cursor a blinking vertical bar.
456 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
457 call WaitForAssert({-> assert_equal([1, 3],
458 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
459
460 " Make cursor a steady vertical bar.
461 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
462 call WaitForAssert({-> assert_equal([0, 3],
463 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
464
465 call StopShellInTerminal(buf)
466endfunc
467
468" Test for term_gettitle()
469func Test_term_gettitle()
470 " term_gettitle() returns an empty string for a non-terminal buffer
471 " and for a non-existing buffer.
472 call assert_equal('', bufnr('%')->term_gettitle())
473 call assert_equal('', term_gettitle(bufnr('$') + 1))
474
475 if !has('title') || empty(&t_ts)
476 throw "Skipped: can't get/set title"
477 endif
478
479 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', '-c', 'set title'])
480 if has('autoservername')
481 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
482 call term_sendkeys(term, ":e Xfoo\r")
483 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
484 else
485 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
486 call term_sendkeys(term, ":e Xfoo\r")
487 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
488 endif
489
490 call term_sendkeys(term, ":set titlestring=foo\r")
491 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
492
493 exe term . 'bwipe!'
494endfunc
495
496func Test_term_gettty()
497 let buf = Run_shell_in_terminal({})
498 let gettty = term_gettty(buf)
499
500 if has('unix') && executable('tty')
501 " Find tty using the tty shell command.
502 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
503 call term_sendkeys(buf, "tty\r")
504 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
505 let tty = term_getline(buf, 2)
506 call assert_equal(tty, gettty)
507 endif
508
509 let gettty0 = term_gettty(buf, 0)
510 let gettty1 = term_gettty(buf, 1)
511
512 call assert_equal(gettty, gettty0)
513 call assert_equal(job_info(g:job).tty_out, gettty0)
514 call assert_equal(job_info(g:job).tty_in, gettty1)
515
516 if has('unix')
517 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
518 " are identical according to :help term_gettty()
519 call assert_equal(gettty0, gettty1)
520 call assert_match('^/dev/', gettty)
521 else
522 " ConPTY works on anonymous pipe.
523 if !has('conpty')
524 call assert_match('^\\\\.\\pipe\\', gettty0)
525 call assert_match('^\\\\.\\pipe\\', gettty1)
526 endif
527 endif
528
529 call assert_fails('call term_gettty(buf, 2)', 'E475:')
530 call assert_fails('call term_gettty(buf, -1)', 'E475:')
531
532 call assert_equal('', term_gettty(buf + 1))
533
534 call StopShellInTerminal(buf)
535 call TermWait(buf)
536 exe buf . 'bwipe'
537endfunc
538
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200539
540" vim: shiftwidth=2 sts=2 expandtab