blob: c1b38d2d24c34732982c9d2296c3abf364b4e579 [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
259func Test_zz2_terminal_guioptions_bang()
260 CheckGui
261 set guioptions+=!
262
263 let filename = 'Xtestscript'
264 if has('win32')
265 let filename .= '.bat'
266 let prefix = ''
267 let contents = ['@echo off', 'exit %1']
268 else
269 let filename .= '.sh'
270 let prefix = './'
271 let contents = ['#!/bin/sh', 'exit $1']
272 endif
273 call writefile(contents, filename)
274 call setfperm(filename, 'rwxrwx---')
275
276 " Check if v:shell_error is equal to the exit status.
277 let exitval = 0
278 execute printf(':!%s%s %d', prefix, filename, exitval)
279 call assert_equal(exitval, v:shell_error)
280
281 let exitval = 9
282 execute printf(':!%s%s %d', prefix, filename, exitval)
283 call assert_equal(exitval, v:shell_error)
284
285 set guioptions&
286 call delete(filename)
287endfunc
288
289func Test_terminal_hidden()
290 CheckUnix
291
292 term ++hidden cat
293 let bnr = bufnr('$')
294 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
295 exe 'sbuf ' . bnr
296 call assert_equal('terminal', &buftype)
297 call term_sendkeys(bnr, "asdf\<CR>")
298 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
299 call term_sendkeys(bnr, "\<C-D>")
300 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
301 bwipe!
302endfunc
303
304func Test_terminal_switch_mode()
305 term
306 let bnr = bufnr('$')
307 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
308 call feedkeys("\<C-W>N", 'xt')
309 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
310 call feedkeys("A", 'xt')
311 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
312 call feedkeys("\<C-\>\<C-N>", 'xt')
313 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
314 call feedkeys("I", 'xt')
315 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
316 call feedkeys("\<C-W>Nv", 'xt')
317 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
318 call feedkeys("I", 'xt')
319 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
320 call feedkeys("\<C-W>Nv", 'xt')
321 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
322 call feedkeys("A", 'xt')
323 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
324 bwipe!
325endfunc
326
327func Test_terminal_normal_mode()
328 CheckRunVimInTerminal
329
330 " Run Vim in a terminal and open a terminal window to run Vim in.
331 let lines =<< trim END
332 call setline(1, range(11111, 11122))
333 3
334 END
335 call writefile(lines, 'XtermNormal')
336 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
337 call TermWait(buf)
338
339 call term_sendkeys(buf, "\<C-W>N")
340 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
341 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
342
343 call term_sendkeys(buf, ":set culopt=number\r")
344 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
345
346 call term_sendkeys(buf, ":set culopt=line\r")
347 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
348
349 call assert_fails('call term_sendkeys(buf, [])', 'E730:')
350 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
351 call StopVimInTerminal(buf)
352 call delete('XtermNormal')
353endfunc
354
355func Test_terminal_hidden_and_close()
356 CheckUnix
357
358 call assert_equal(1, winnr('$'))
359 term ++hidden ++close ls
360 let bnr = bufnr('$')
361 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
362 call WaitForAssert({-> assert_false(bufexists(bnr))})
363 call assert_equal(1, winnr('$'))
364endfunc
365
366func Test_terminal_does_not_truncate_last_newlines()
367 " This test does not pass through ConPTY.
368 if has('conpty')
369 return
370 endif
371 let contents = [
372 \ [ 'One', '', 'X' ],
373 \ [ 'Two', '', '' ],
374 \ [ 'Three' ] + repeat([''], 30)
375 \ ]
376
377 for c in contents
378 call writefile(c, 'Xfile')
379 if has('win32')
380 term cmd /c type Xfile
381 else
382 term cat Xfile
383 endif
384 let bnr = bufnr('$')
385 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
386 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
387 sleep 100m
388 call assert_equal(c, getline(1, line('$')))
389 quit
390 endfor
391
392 call delete('Xfile')
393endfunc
394
395func Test_terminal_no_job()
396 if has('win32')
397 let cmd = 'cmd /c ""'
398 else
399 CheckExecutable false
400 let cmd = 'false'
401 endif
402 let term = term_start(cmd, {'term_finish': 'close'})
403 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
404endfunc
405
406func Test_term_getcursor()
407 CheckUnix
408
409 let buf = Run_shell_in_terminal({})
410
411 " Wait for the shell to display a prompt.
412 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
413
414 " Hide the cursor.
415 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
416 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
417
418 " Show the cursor.
419 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
420 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
421
422 " Change color of cursor.
423 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
424 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
425 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
426 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
427 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
428
429 " Make cursor a blinking block.
430 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
431 call WaitForAssert({-> assert_equal([1, 1],
432 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
433
434 " Make cursor a steady block.
435 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
436 call WaitForAssert({-> assert_equal([0, 1],
437 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
438
439 " Make cursor a blinking underline.
440 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
441 call WaitForAssert({-> assert_equal([1, 2],
442 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
443
444 " Make cursor a steady underline.
445 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
446 call WaitForAssert({-> assert_equal([0, 2],
447 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
448
449 " Make cursor a blinking vertical bar.
450 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
451 call WaitForAssert({-> assert_equal([1, 3],
452 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
453
454 " Make cursor a steady vertical bar.
455 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
456 call WaitForAssert({-> assert_equal([0, 3],
457 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
458
459 call StopShellInTerminal(buf)
460endfunc
461
462" Test for term_gettitle()
463func Test_term_gettitle()
464 " term_gettitle() returns an empty string for a non-terminal buffer
465 " and for a non-existing buffer.
466 call assert_equal('', bufnr('%')->term_gettitle())
467 call assert_equal('', term_gettitle(bufnr('$') + 1))
468
469 if !has('title') || empty(&t_ts)
470 throw "Skipped: can't get/set title"
471 endif
472
473 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', '-c', 'set title'])
474 if has('autoservername')
475 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
476 call term_sendkeys(term, ":e Xfoo\r")
477 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
478 else
479 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
480 call term_sendkeys(term, ":e Xfoo\r")
481 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
482 endif
483
484 call term_sendkeys(term, ":set titlestring=foo\r")
485 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
486
487 exe term . 'bwipe!'
488endfunc
489
490func Test_term_gettty()
491 let buf = Run_shell_in_terminal({})
492 let gettty = term_gettty(buf)
493
494 if has('unix') && executable('tty')
495 " Find tty using the tty shell command.
496 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
497 call term_sendkeys(buf, "tty\r")
498 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
499 let tty = term_getline(buf, 2)
500 call assert_equal(tty, gettty)
501 endif
502
503 let gettty0 = term_gettty(buf, 0)
504 let gettty1 = term_gettty(buf, 1)
505
506 call assert_equal(gettty, gettty0)
507 call assert_equal(job_info(g:job).tty_out, gettty0)
508 call assert_equal(job_info(g:job).tty_in, gettty1)
509
510 if has('unix')
511 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
512 " are identical according to :help term_gettty()
513 call assert_equal(gettty0, gettty1)
514 call assert_match('^/dev/', gettty)
515 else
516 " ConPTY works on anonymous pipe.
517 if !has('conpty')
518 call assert_match('^\\\\.\\pipe\\', gettty0)
519 call assert_match('^\\\\.\\pipe\\', gettty1)
520 endif
521 endif
522
523 call assert_fails('call term_gettty(buf, 2)', 'E475:')
524 call assert_fails('call term_gettty(buf, -1)', 'E475:')
525
526 call assert_equal('', term_gettty(buf + 1))
527
528 call StopShellInTerminal(buf)
529 call TermWait(buf)
530 exe buf . 'bwipe'
531endfunc
532
533func Test_terminal_getwinpos()
534 CheckRunVimInTerminal
535
536 " split, go to the bottom-right window
537 split
538 wincmd j
539 set splitright
540
541 call writefile([
542 \ 'echo getwinpos()',
543 \ ], 'XTest_getwinpos')
544 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
545 call TermWait(buf)
546
547 " Find the output of getwinpos() in the bottom line.
548 let rows = term_getsize(buf)[0]
549 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
550 let line = term_getline(buf, rows)
551 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
552 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
553
554 " Position must be bigger than the getwinpos() result of Vim itself.
555 " The calculation in the console assumes a 10 x 7 character cell.
556 " In the GUI it can be more, let's assume a 20 x 14 cell.
557 " And then add 100 / 200 tolerance.
558 let [xroot, yroot] = getwinpos()
559 let winpos = 50->getwinpos()
560 call assert_equal(xroot, winpos[0])
561 call assert_equal(yroot, winpos[1])
562 let [winrow, wincol] = win_screenpos('.')
563 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
564 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
565 call assert_inrange(xroot + 2, xroot + xoff, xpos)
566 call assert_inrange(yroot + 2, yroot + yoff, ypos)
567
568 call TermWait(buf)
569 call term_sendkeys(buf, ":q\<CR>")
570 call StopVimInTerminal(buf)
571 call delete('XTest_getwinpos')
572 exe buf . 'bwipe!'
573 set splitright&
574 only!
575endfunc
576
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200577
578" vim: shiftwidth=2 sts=2 expandtab