blob: 715a4ac76004cde89e5485a97934417c60dbb266 [file] [log] [blame]
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001" Functions about terminal shared by several tests
2
3" Only load this script once.
4if exists('*CanRunVimInTerminal')
5 finish
6endif
7
8" For most tests we need to be able to run terminal Vim with 256 colors. On
9" MS-Windows the console only has 16 colors and the GUI can't run in a
10" terminal.
11func CanRunVimInTerminal()
12 return has('terminal') && !has('win32')
13endfunc
14
15" Skip the rest if there is no terminal feature at all.
16if !has('terminal')
17 finish
18endif
19
20" Stops the shell running in terminal "buf".
21func StopShellInTerminal(buf)
22 call term_sendkeys(a:buf, "exit\r")
23 let job = term_getjob(a:buf)
24 call WaitFor({-> job_status(job) == "dead"})
25endfunc
26
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020027" Wrapper around term_wait() to allow more time for re-runs of flaky tests
28" The second argument is the minimum time to wait in msec, 10 if omitted.
29func TermWait(buf, ...)
30 let wait_time = a:0 ? a:1 : 10
31 if g:run_nr == 2
32 let wait_time *= 4
33 elseif g:run_nr > 2
34 let wait_time *= 10
35 endif
36 call term_wait(a:buf, wait_time)
37
38 " In case it wasn't set yet.
39 let g:test_is_flaky = 1
40endfunc
41
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020042" Run Vim with "arguments" in a new terminal window.
43" By default uses a size of 20 lines and 75 columns.
44" Returns the buffer number of the terminal.
45"
46" Options is a dictionary, these items are recognized:
47" "rows" - height of the terminal window (max. 20)
48" "cols" - width of the terminal window (max. 78)
49" "statusoff" - number of lines the status is offset from default
50func RunVimInTerminal(arguments, options)
51 " If Vim doesn't exit a swap file remains, causing other tests to fail.
52 " Remove it here.
53 call delete(".swp")
54
55 if exists('$COLORFGBG')
56 " Clear $COLORFGBG to avoid 'background' being set to "dark", which will
57 " only be corrected if the response to t_RB is received, which may be too
58 " late.
59 let $COLORFGBG = ''
60 endif
61
62 " Make a horizontal and vertical split, so that we can get exactly the right
63 " size terminal window. Works only when the current window is full width.
64 call assert_equal(&columns, winwidth(0))
65 split
66 vsplit
67
68 " Always do this with 256 colors and a light background.
69 set t_Co=256 background=light
70 hi Normal ctermfg=NONE ctermbg=NONE
71
72 " Make the window 20 lines high and 75 columns, unless told otherwise.
73 let rows = get(a:options, 'rows', 20)
74 let cols = get(a:options, 'cols', 75)
75 let statusoff = get(a:options, 'statusoff', 1)
76
Bram Moolenaar0d702022019-07-04 14:20:41 +020077 let cmd = GetVimCommandCleanTerm() .. a:arguments
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020078
Bram Moolenaard2842ea2019-09-26 23:08:54 +020079 let options = {
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020080 \ 'curwin': 1,
81 \ 'term_rows': rows,
82 \ 'term_cols': cols,
Bram Moolenaard2842ea2019-09-26 23:08:54 +020083 \ }
84 " Accept other options whose name starts with 'term_'.
85 call extend(options, filter(copy(a:options), 'v:key =~# "^term_"'))
86
87 let buf = term_start(cmd, options)
88
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020089 if &termwinsize == ''
90 " in the GUI we may end up with a different size, try to set it.
91 if term_getsize(buf) != [rows, cols]
92 call term_setsize(buf, rows, cols)
93 endif
94 call assert_equal([rows, cols], term_getsize(buf))
95 else
96 let rows = term_getsize(buf)[0]
97 let cols = term_getsize(buf)[1]
98 endif
99
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200100 call TermWait(buf)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200101
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200102 " Wait for "All" or "Top" of the ruler to be shown in the last line or in
103 " the status line of the last window. This can be quite slow (e.g. when
104 " using valgrind).
105 " If it fails then show the terminal contents for debugging.
106 try
107 call WaitFor({-> len(term_getline(buf, rows)) >= cols - 1 || len(term_getline(buf, rows - statusoff)) >= cols - 1})
108 catch /timed out after/
109 let lines = map(range(1, rows), {key, val -> term_getline(buf, val)})
110 call assert_report('RunVimInTerminal() failed, screen contents: ' . join(lines, "<NL>"))
111 endtry
112
Bram Moolenaar3cdcb092020-03-18 19:18:10 +0100113 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +0100114 let g:test_is_flaky = 1
Bram Moolenaar3cdcb092020-03-18 19:18:10 +0100115
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200116 return buf
117endfunc
118
119" Stop a Vim running in terminal buffer "buf".
120func StopVimInTerminal(buf)
Bram Moolenaar3cdcb092020-03-18 19:18:10 +0100121 " Using a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +0100122 let g:test_is_flaky = 1
Bram Moolenaar3cdcb092020-03-18 19:18:10 +0100123
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200124 call assert_equal("running", term_getstatus(a:buf))
125
126 " CTRL-O : works both in Normal mode and Insert mode to start a command line.
127 " In Command-line it's inserted, the CTRL-U removes it again.
128 call term_sendkeys(a:buf, "\<C-O>:\<C-U>qa!\<cr>")
129
130 call WaitForAssert({-> assert_equal("finished", term_getstatus(a:buf))})
131 only!
132endfunc
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200133
134" vim: shiftwidth=2 sts=2 expandtab