blob: 93ace42d827b945a96392ec0835a41bf4349c1ca [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
Bram Moolenaarc85156b2020-07-12 14:09:23 +0200259" TODO: reenable when this no longer hangs on Travis
260"func Test_zz2_terminal_guioptions_bang()
261" CheckGui
262" set guioptions+=!
263"
264" let filename = 'Xtestscript'
265" if has('win32')
266" let filename .= '.bat'
267" let prefix = ''
268" let contents = ['@echo off', 'exit %1']
269" else
270" let filename .= '.sh'
271" let prefix = './'
272" let contents = ['#!/bin/sh', 'exit $1']
273" endif
274" call writefile(contents, filename)
275" call setfperm(filename, 'rwxrwx---')
276"
277" " Check if v:shell_error is equal to the exit status.
278" let exitval = 0
279" execute printf(':!%s%s %d', prefix, filename, exitval)
280" call assert_equal(exitval, v:shell_error)
281"
282" let exitval = 9
283" execute printf(':!%s%s %d', prefix, filename, exitval)
284" call assert_equal(exitval, v:shell_error)
285"
286" set guioptions&
287" call delete(filename)
288"endfunc
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200289
290func Test_terminal_hidden()
291 CheckUnix
292
293 term ++hidden cat
294 let bnr = bufnr('$')
295 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
296 exe 'sbuf ' . bnr
297 call assert_equal('terminal', &buftype)
298 call term_sendkeys(bnr, "asdf\<CR>")
299 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
300 call term_sendkeys(bnr, "\<C-D>")
301 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
302 bwipe!
303endfunc
304
305func Test_terminal_switch_mode()
306 term
307 let bnr = bufnr('$')
308 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaarc85156b2020-07-12 14:09:23 +0200309 " In the GUI the first switch sometimes doesn't work. Switch twice to avoid
310 " flakyness.
311 call feedkeys("\<C-W>N", 'xt')
312 call feedkeys("A", 'xt')
313 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200314 call feedkeys("\<C-W>N", 'xt')
315 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
316 call feedkeys("A", 'xt')
317 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
318 call feedkeys("\<C-\>\<C-N>", 'xt')
319 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
320 call feedkeys("I", 'xt')
321 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
322 call feedkeys("\<C-W>Nv", 'xt')
323 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
324 call feedkeys("I", 'xt')
325 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
326 call feedkeys("\<C-W>Nv", 'xt')
327 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
328 call feedkeys("A", 'xt')
329 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
330 bwipe!
331endfunc
332
333func Test_terminal_normal_mode()
334 CheckRunVimInTerminal
335
336 " Run Vim in a terminal and open a terminal window to run Vim in.
337 let lines =<< trim END
338 call setline(1, range(11111, 11122))
339 3
340 END
341 call writefile(lines, 'XtermNormal')
342 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
343 call TermWait(buf)
344
345 call term_sendkeys(buf, "\<C-W>N")
346 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
347 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
348
349 call term_sendkeys(buf, ":set culopt=number\r")
350 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
351
352 call term_sendkeys(buf, ":set culopt=line\r")
353 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
354
355 call assert_fails('call term_sendkeys(buf, [])', 'E730:')
356 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
357 call StopVimInTerminal(buf)
358 call delete('XtermNormal')
359endfunc
360
361func Test_terminal_hidden_and_close()
362 CheckUnix
363
364 call assert_equal(1, winnr('$'))
365 term ++hidden ++close ls
366 let bnr = bufnr('$')
367 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
368 call WaitForAssert({-> assert_false(bufexists(bnr))})
369 call assert_equal(1, winnr('$'))
370endfunc
371
372func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200373 if has('conpty')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200374 throw 'Skipped: fail on ConPTY'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200375 endif
376 let contents = [
377 \ [ 'One', '', 'X' ],
378 \ [ 'Two', '', '' ],
379 \ [ 'Three' ] + repeat([''], 30)
380 \ ]
381
382 for c in contents
383 call writefile(c, 'Xfile')
384 if has('win32')
385 term cmd /c type Xfile
386 else
387 term cat Xfile
388 endif
389 let bnr = bufnr('$')
390 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
391 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
392 sleep 100m
393 call assert_equal(c, getline(1, line('$')))
394 quit
395 endfor
396
397 call delete('Xfile')
398endfunc
399
400func Test_terminal_no_job()
401 if has('win32')
402 let cmd = 'cmd /c ""'
403 else
404 CheckExecutable false
405 let cmd = 'false'
406 endif
407 let term = term_start(cmd, {'term_finish': 'close'})
408 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
409endfunc
410
411func Test_term_getcursor()
412 CheckUnix
413
414 let buf = Run_shell_in_terminal({})
415
416 " Wait for the shell to display a prompt.
417 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
418
419 " Hide the cursor.
420 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
421 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
422
423 " Show the cursor.
424 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
425 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
426
427 " Change color of cursor.
428 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
429 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
430 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
431 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
432 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
433
434 " Make cursor a blinking block.
435 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
436 call WaitForAssert({-> assert_equal([1, 1],
437 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
438
439 " Make cursor a steady block.
440 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
441 call WaitForAssert({-> assert_equal([0, 1],
442 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
443
444 " Make cursor a blinking underline.
445 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
446 call WaitForAssert({-> assert_equal([1, 2],
447 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
448
449 " Make cursor a steady underline.
450 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
451 call WaitForAssert({-> assert_equal([0, 2],
452 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
453
454 " Make cursor a blinking vertical bar.
455 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
456 call WaitForAssert({-> assert_equal([1, 3],
457 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
458
459 " Make cursor a steady vertical bar.
460 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
461 call WaitForAssert({-> assert_equal([0, 3],
462 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
463
464 call StopShellInTerminal(buf)
465endfunc
466
467" Test for term_gettitle()
468func Test_term_gettitle()
469 " term_gettitle() returns an empty string for a non-terminal buffer
470 " and for a non-existing buffer.
471 call assert_equal('', bufnr('%')->term_gettitle())
472 call assert_equal('', term_gettitle(bufnr('$') + 1))
473
474 if !has('title') || empty(&t_ts)
475 throw "Skipped: can't get/set title"
476 endif
477
478 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', '-c', 'set title'])
479 if has('autoservername')
480 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
481 call term_sendkeys(term, ":e Xfoo\r")
482 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
483 else
484 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
485 call term_sendkeys(term, ":e Xfoo\r")
486 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
487 endif
488
489 call term_sendkeys(term, ":set titlestring=foo\r")
490 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
491
492 exe term . 'bwipe!'
493endfunc
494
495func Test_term_gettty()
496 let buf = Run_shell_in_terminal({})
497 let gettty = term_gettty(buf)
498
499 if has('unix') && executable('tty')
500 " Find tty using the tty shell command.
501 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
502 call term_sendkeys(buf, "tty\r")
503 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
504 let tty = term_getline(buf, 2)
505 call assert_equal(tty, gettty)
506 endif
507
508 let gettty0 = term_gettty(buf, 0)
509 let gettty1 = term_gettty(buf, 1)
510
511 call assert_equal(gettty, gettty0)
512 call assert_equal(job_info(g:job).tty_out, gettty0)
513 call assert_equal(job_info(g:job).tty_in, gettty1)
514
515 if has('unix')
516 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
517 " are identical according to :help term_gettty()
518 call assert_equal(gettty0, gettty1)
519 call assert_match('^/dev/', gettty)
520 else
521 " ConPTY works on anonymous pipe.
522 if !has('conpty')
523 call assert_match('^\\\\.\\pipe\\', gettty0)
524 call assert_match('^\\\\.\\pipe\\', gettty1)
525 endif
526 endif
527
528 call assert_fails('call term_gettty(buf, 2)', 'E475:')
529 call assert_fails('call term_gettty(buf, -1)', 'E475:')
530
531 call assert_equal('', term_gettty(buf + 1))
532
533 call StopShellInTerminal(buf)
534 call TermWait(buf)
535 exe buf . 'bwipe'
536endfunc
537
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200538
539" vim: shiftwidth=2 sts=2 expandtab