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