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