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