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