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 |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 22 | call writefile(text, 'Xwinsize', 'D') |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 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) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 37 | |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 38 | call assert_fails('set termwinsize=40', 'E474:') |
| 39 | call assert_fails('set termwinsize=10+40', 'E474:') |
| 40 | call assert_fails('set termwinsize=abc', 'E474:') |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 41 | |
| 42 | set termwinsize= |
| 43 | endfunc |
| 44 | |
| 45 | func Test_terminal_termwinsize_option_zero() |
| 46 | set termwinsize=0x0 |
| 47 | let buf = Run_shell_in_terminal({}) |
| 48 | let win = bufwinid(buf) |
| 49 | call assert_equal([winheight(win), winwidth(win)], term_getsize(buf)) |
| 50 | call StopShellInTerminal(buf) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 51 | exe buf . 'bwipe' |
| 52 | |
| 53 | set termwinsize=7x0 |
| 54 | let buf = Run_shell_in_terminal({}) |
| 55 | let win = bufwinid(buf) |
| 56 | call assert_equal([7, winwidth(win)], term_getsize(buf)) |
| 57 | call StopShellInTerminal(buf) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 58 | exe buf . 'bwipe' |
| 59 | |
| 60 | set termwinsize=0x33 |
| 61 | let buf = Run_shell_in_terminal({}) |
| 62 | let win = bufwinid(buf) |
| 63 | call assert_equal([winheight(win), 33], term_getsize(buf)) |
| 64 | call StopShellInTerminal(buf) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 65 | exe buf . 'bwipe' |
| 66 | |
| 67 | set termwinsize= |
| 68 | endfunc |
| 69 | |
| 70 | func Test_terminal_termwinsize_minimum() |
| 71 | set termwinsize=10*50 |
| 72 | vsplit |
| 73 | let buf = Run_shell_in_terminal({}) |
| 74 | let win = bufwinid(buf) |
| 75 | call assert_inrange(10, 1000, winheight(win)) |
| 76 | call assert_inrange(50, 1000, winwidth(win)) |
| 77 | call assert_equal([winheight(win), winwidth(win)], term_getsize(buf)) |
| 78 | |
| 79 | resize 15 |
| 80 | vertical resize 60 |
| 81 | redraw |
| 82 | call assert_equal([15, 60], term_getsize(buf)) |
| 83 | call assert_equal(15, winheight(win)) |
| 84 | call assert_equal(60, winwidth(win)) |
| 85 | |
| 86 | resize 7 |
| 87 | vertical resize 30 |
| 88 | redraw |
| 89 | call assert_equal([10, 50], term_getsize(buf)) |
| 90 | call assert_equal(7, winheight(win)) |
| 91 | call assert_equal(30, winwidth(win)) |
| 92 | |
| 93 | call StopShellInTerminal(buf) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 94 | exe buf . 'bwipe' |
| 95 | |
| 96 | set termwinsize=0*0 |
| 97 | let buf = Run_shell_in_terminal({}) |
| 98 | let win = bufwinid(buf) |
| 99 | call assert_equal([winheight(win), winwidth(win)], term_getsize(buf)) |
| 100 | call StopShellInTerminal(buf) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 101 | exe buf . 'bwipe' |
| 102 | |
| 103 | set termwinsize= |
| 104 | endfunc |
| 105 | |
Bram Moolenaar | b936b79 | 2020-09-04 18:34:09 +0200 | [diff] [blame] | 106 | func Test_terminal_termwinsize_overruled() |
| 107 | let cmd = GetDummyCmd() |
| 108 | set termwinsize=5x43 |
| 109 | let buf = term_start(cmd, #{term_rows: 7, term_cols: 50}) |
| 110 | call TermWait(buf) |
| 111 | call assert_equal([7, 50], term_getsize(buf)) |
| 112 | exe "bwipe! " .. buf |
| 113 | |
| 114 | let buf = term_start(cmd, #{term_cols: 50}) |
| 115 | call TermWait(buf) |
| 116 | call assert_equal([5, 50], term_getsize(buf)) |
| 117 | exe "bwipe! " .. buf |
| 118 | |
| 119 | let buf = term_start(cmd, #{term_rows: 7}) |
| 120 | call TermWait(buf) |
| 121 | call assert_equal([7, 43], term_getsize(buf)) |
| 122 | exe "bwipe! " .. buf |
| 123 | |
| 124 | set termwinsize= |
| 125 | endfunc |
| 126 | |
Bram Moolenaar | 2ce1458 | 2020-09-05 16:08:49 +0200 | [diff] [blame] | 127 | " hidden terminal must not change current window size |
| 128 | func Test_terminal_hidden_winsize() |
| 129 | let cmd = GetDummyCmd() |
| 130 | let rows = winheight(0) |
| 131 | let buf = term_start(cmd, #{hidden: 1, term_rows: 10}) |
Yegappan Lakshmanan | e446a01 | 2023-01-19 17:49:58 +0000 | [diff] [blame] | 132 | call TermWait(buf) |
Bram Moolenaar | 2ce1458 | 2020-09-05 16:08:49 +0200 | [diff] [blame] | 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', |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 195 | \ ], 'Xechoerrout.sh', 'D') |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 196 | call setfperm('Xechoerrout.sh', 'rwxrwx---') |
| 197 | |
| 198 | let outfile = 'Xtermstdout' |
| 199 | let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile}) |
Yegappan Lakshmanan | e446a01 | 2023-01-19 17:49:58 +0000 | [diff] [blame] | 200 | call TermWait(buf) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 201 | |
| 202 | call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))}) |
| 203 | call assert_equal(['this is standard out'], readfile(outfile)) |
| 204 | call assert_equal('this is standard error', term_getline(buf, 1)) |
| 205 | |
| 206 | call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))}) |
| 207 | exe buf . 'bwipe' |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 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') |
Yegappan Lakshmanan | e446a01 | 2023-01-19 17:49:58 +0000 | [diff] [blame] | 221 | call TermWait(buf) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 222 | for i in range(1, &lines) |
| 223 | call feedkeys("echo " . i . "\<CR>", 'xt') |
| 224 | call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))}) |
| 225 | endfor |
| 226 | " Go to Terminal-Normal mode to update the buffer. |
| 227 | call feedkeys("\<C-W>N", 'xt') |
| 228 | call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$')) |
| 229 | |
| 230 | " Every "echo nr" must only appear once |
| 231 | let lines = getline(1, line('$')) |
| 232 | for i in range(&lines - len(lines) / 2 + 2, &lines) |
| 233 | let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'}) |
| 234 | call assert_equal(1, len(filtered), 'for "echo ' . i . '"') |
| 235 | endfor |
| 236 | |
| 237 | exe buf . 'bwipe!' |
| 238 | endfunc |
| 239 | |
| 240 | " Resizing the terminal window caused an ml_get error. |
| 241 | " TODO: This does not reproduce the original problem. |
| 242 | func Test_terminal_resize() |
| 243 | set statusline=x |
| 244 | terminal |
| 245 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | c54f347 | 2021-03-23 19:22:12 +0100 | [diff] [blame] | 246 | let buf = bufnr() |
| 247 | |
| 248 | " Wait for the shell to display a prompt |
| 249 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 250 | |
| 251 | " Fill the terminal with text. |
| 252 | if has('win32') |
| 253 | call feedkeys("dir\<CR>", 'xt') |
| 254 | else |
| 255 | call feedkeys("ls\<CR>", 'xt') |
| 256 | endif |
Bram Moolenaar | c54f347 | 2021-03-23 19:22:12 +0100 | [diff] [blame] | 257 | " Wait for some output |
| 258 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))}) |
| 259 | |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 260 | " Go to Terminal-Normal mode for a moment. |
| 261 | call feedkeys("\<C-W>N", 'xt') |
| 262 | " Open a new window |
| 263 | call feedkeys("i\<C-W>n", 'xt') |
| 264 | call assert_equal(3, winnr('$')) |
| 265 | redraw |
| 266 | |
| 267 | close |
| 268 | call assert_equal(2, winnr('$')) |
| 269 | call feedkeys("exit\<CR>", 'xt') |
Bram Moolenaar | c54f347 | 2021-03-23 19:22:12 +0100 | [diff] [blame] | 270 | call TermWait(buf) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 271 | set statusline& |
| 272 | endfunc |
| 273 | |
| 274 | " must be nearly the last, we can't go back from GUI to terminal |
| 275 | func Test_zz1_terminal_in_gui() |
| 276 | CheckCanRunGui |
| 277 | |
| 278 | " Ignore the "failed to create input context" error. |
| 279 | call test_ignore_error('E285:') |
| 280 | |
| 281 | gui -f |
| 282 | |
| 283 | call assert_equal(1, winnr('$')) |
| 284 | let buf = Run_shell_in_terminal({'term_finish': 'close'}) |
| 285 | call StopShellInTerminal(buf) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 286 | |
| 287 | " closing window wipes out the terminal buffer a with finished job |
| 288 | call WaitForAssert({-> assert_equal(1, winnr('$'))}) |
| 289 | call assert_equal("", bufname(buf)) |
| 290 | |
| 291 | unlet g:job |
| 292 | endfunc |
| 293 | |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [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, 'D') |
| 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 | endfunc |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 322 | |
| 323 | func Test_terminal_hidden() |
| 324 | CheckUnix |
| 325 | |
| 326 | term ++hidden cat |
| 327 | let bnr = bufnr('$') |
| 328 | call assert_equal('terminal', getbufvar(bnr, '&buftype')) |
| 329 | exe 'sbuf ' . bnr |
| 330 | call assert_equal('terminal', &buftype) |
| 331 | call term_sendkeys(bnr, "asdf\<CR>") |
| 332 | call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))}) |
| 333 | call term_sendkeys(bnr, "\<C-D>") |
| 334 | call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())}) |
| 335 | bwipe! |
| 336 | endfunc |
| 337 | |
| 338 | func Test_terminal_switch_mode() |
| 339 | term |
| 340 | let bnr = bufnr('$') |
| 341 | call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))}) |
Bram Moolenaar | c85156b | 2020-07-12 14:09:23 +0200 | [diff] [blame] | 342 | " 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] | 343 | " flakiness. |
Bram Moolenaar | c85156b | 2020-07-12 14:09:23 +0200 | [diff] [blame] | 344 | call feedkeys("\<C-W>N", 'xt') |
| 345 | call feedkeys("A", 'xt') |
| 346 | call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))}) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 347 | call feedkeys("\<C-W>N", '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 | call feedkeys("\<C-\>\<C-N>", 'xt') |
| 352 | call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))}) |
| 353 | call feedkeys("I", 'xt') |
| 354 | call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))}) |
| 355 | call feedkeys("\<C-W>Nv", 'xt') |
| 356 | call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))}) |
| 357 | call feedkeys("I", 'xt') |
| 358 | call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))}) |
| 359 | call feedkeys("\<C-W>Nv", 'xt') |
| 360 | call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))}) |
| 361 | call feedkeys("A", 'xt') |
| 362 | call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))}) |
| 363 | bwipe! |
| 364 | endfunc |
| 365 | |
| 366 | func Test_terminal_normal_mode() |
| 367 | CheckRunVimInTerminal |
| 368 | |
| 369 | " Run Vim in a terminal and open a terminal window to run Vim in. |
| 370 | let lines =<< trim END |
| 371 | call setline(1, range(11111, 11122)) |
| 372 | 3 |
| 373 | END |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 374 | call writefile(lines, 'XtermNormal', 'D') |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 375 | let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8}) |
| 376 | call TermWait(buf) |
| 377 | |
| 378 | call term_sendkeys(buf, "\<C-W>N") |
| 379 | call term_sendkeys(buf, ":set number cursorline culopt=both\r") |
| 380 | call VerifyScreenDump(buf, 'Test_terminal_normal_1', {}) |
| 381 | |
| 382 | call term_sendkeys(buf, ":set culopt=number\r") |
| 383 | call VerifyScreenDump(buf, 'Test_terminal_normal_2', {}) |
| 384 | |
| 385 | call term_sendkeys(buf, ":set culopt=line\r") |
| 386 | call VerifyScreenDump(buf, 'Test_terminal_normal_3', {}) |
| 387 | |
| 388 | call assert_fails('call term_sendkeys(buf, [])', 'E730:') |
| 389 | call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>") |
| 390 | call StopVimInTerminal(buf) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 391 | endfunc |
| 392 | |
| 393 | func Test_terminal_hidden_and_close() |
| 394 | CheckUnix |
| 395 | |
| 396 | call assert_equal(1, winnr('$')) |
| 397 | term ++hidden ++close ls |
| 398 | let bnr = bufnr('$') |
| 399 | call assert_equal('terminal', getbufvar(bnr, '&buftype')) |
| 400 | call WaitForAssert({-> assert_false(bufexists(bnr))}) |
| 401 | call assert_equal(1, winnr('$')) |
| 402 | endfunc |
| 403 | |
| 404 | func Test_terminal_does_not_truncate_last_newlines() |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 405 | if has('conpty') |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 406 | throw 'Skipped: fail on ConPTY' |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 407 | endif |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 408 | let g:test_is_flaky = 1 |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 409 | let contents = [ |
| 410 | \ [ 'One', '', 'X' ], |
| 411 | \ [ 'Two', '', '' ], |
| 412 | \ [ 'Three' ] + repeat([''], 30) |
| 413 | \ ] |
| 414 | |
| 415 | for c in contents |
Bram Moolenaar | c4860bd | 2022-10-15 20:52:26 +0100 | [diff] [blame] | 416 | call writefile(c, 'Xdntfile', 'D') |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 417 | if has('win32') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 418 | term cmd /c type Xdntfile |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 419 | else |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 420 | term cat Xdntfile |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 421 | endif |
| 422 | let bnr = bufnr('$') |
| 423 | call assert_equal('terminal', getbufvar(bnr, '&buftype')) |
| 424 | call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))}) |
| 425 | sleep 100m |
| 426 | call assert_equal(c, getline(1, line('$'))) |
| 427 | quit |
| 428 | endfor |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 429 | endfunc |
| 430 | |
Bram Moolenaar | b936b79 | 2020-09-04 18:34:09 +0200 | [diff] [blame] | 431 | func GetDummyCmd() |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 432 | if has('win32') |
Bram Moolenaar | b936b79 | 2020-09-04 18:34:09 +0200 | [diff] [blame] | 433 | return 'cmd /c ""' |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 434 | else |
| 435 | CheckExecutable false |
Bram Moolenaar | b936b79 | 2020-09-04 18:34:09 +0200 | [diff] [blame] | 436 | return 'false' |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 437 | endif |
Bram Moolenaar | b936b79 | 2020-09-04 18:34:09 +0200 | [diff] [blame] | 438 | endfunc |
| 439 | |
| 440 | func Test_terminal_no_job() |
| 441 | let cmd = GetDummyCmd() |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 442 | let term = term_start(cmd, {'term_finish': 'close'}) |
| 443 | call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) }) |
| 444 | endfunc |
| 445 | |
| 446 | func 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) |
| 500 | endfunc |
| 501 | |
| 502 | " Test for term_gettitle() |
| 503 | func 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']) |
Yegappan Lakshmanan | e446a01 | 2023-01-19 17:49:58 +0000 | [diff] [blame] | 514 | call TermWait(term) |
Bram Moolenaar | 3bb79dc | 2021-12-12 18:50:19 +0000 | [diff] [blame] | 515 | " When Vim is running as a server then the title ends in VIM{number}, thus |
| 516 | " optionally match a number after "VIM". |
| 517 | call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d*$', term_gettitle(term)) }) |
| 518 | call term_sendkeys(term, ":e Xfoo\r") |
| 519 | call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d*$', term_gettitle(term)) }) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 520 | |
| 521 | call term_sendkeys(term, ":set titlestring=foo\r") |
| 522 | call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) }) |
| 523 | |
| 524 | exe term . 'bwipe!' |
| 525 | endfunc |
| 526 | |
| 527 | func Test_term_gettty() |
| 528 | let buf = Run_shell_in_terminal({}) |
| 529 | let gettty = term_gettty(buf) |
| 530 | |
| 531 | if has('unix') && executable('tty') |
| 532 | " Find tty using the tty shell command. |
| 533 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))}) |
| 534 | call term_sendkeys(buf, "tty\r") |
| 535 | call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))}) |
| 536 | let tty = term_getline(buf, 2) |
| 537 | call assert_equal(tty, gettty) |
| 538 | endif |
| 539 | |
| 540 | let gettty0 = term_gettty(buf, 0) |
| 541 | let gettty1 = term_gettty(buf, 1) |
| 542 | |
| 543 | call assert_equal(gettty, gettty0) |
| 544 | call assert_equal(job_info(g:job).tty_out, gettty0) |
| 545 | call assert_equal(job_info(g:job).tty_in, gettty1) |
| 546 | |
| 547 | if has('unix') |
| 548 | " For unix, term_gettty(..., 0) and term_gettty(..., 1) |
| 549 | " are identical according to :help term_gettty() |
| 550 | call assert_equal(gettty0, gettty1) |
| 551 | call assert_match('^/dev/', gettty) |
| 552 | else |
| 553 | " ConPTY works on anonymous pipe. |
| 554 | if !has('conpty') |
| 555 | call assert_match('^\\\\.\\pipe\\', gettty0) |
| 556 | call assert_match('^\\\\.\\pipe\\', gettty1) |
| 557 | endif |
| 558 | endif |
| 559 | |
Bram Moolenaar | bade44e | 2020-09-26 22:39:24 +0200 | [diff] [blame] | 560 | call assert_fails('call term_gettty(buf, 2)', 'E475:') |
| 561 | call assert_fails('call term_gettty(buf, -1)', 'E475:') |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 562 | |
| 563 | call assert_equal('', term_gettty(buf + 1)) |
| 564 | |
| 565 | call StopShellInTerminal(buf) |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 566 | exe buf . 'bwipe' |
| 567 | endfunc |
| 568 | |
Bram Moolenaar | 1112c0f | 2020-07-01 21:53:50 +0200 | [diff] [blame] | 569 | |
| 570 | " vim: shiftwidth=2 sts=2 expandtab |