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