1" 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 27" 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 42" 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" "keep_t_u7" - when 1 do not make t_u7 empty (resetting t_u7 avoids clearing 48" parts of line 2 and 3 on the display) 49" "rows" - height of the terminal window (max. 20) 50" "cols" - width of the terminal window (max. 78) 51" "statusoff" - number of lines the status is offset from default 52func RunVimInTerminal(arguments, options) 53 " If Vim doesn't exit a swap file remains, causing other tests to fail. 54 " Remove it here. 55 call delete(".swp") 56 57 if exists('$COLORFGBG') 58 " Clear $COLORFGBG to avoid 'background' being set to "dark", which will 59 " only be corrected if the response to t_RB is received, which may be too 60 " late. 61 let $COLORFGBG = '' 62 endif 63 64 " Make a horizontal and vertical split, so that we can get exactly the right 65 " size terminal window. Works only when the current window is full width. 66 call assert_equal(&columns, winwidth(0)) 67 split 68 vsplit 69 70 " Always do this with 256 colors and a light background. 71 set t_Co=256 background=light 72 hi Normal ctermfg=NONE ctermbg=NONE 73 74 " Make the window 20 lines high and 75 columns, unless told otherwise. 75 let rows = get(a:options, 'rows', 20) 76 let cols = get(a:options, 'cols', 75) 77 let statusoff = get(a:options, 'statusoff', 1) 78 79 if get(a:options, 'keep_t_u7', 0) 80 let reset_u7 = '' 81 else 82 let reset_u7 = ' --cmd "set t_u7=" ' 83 endif 84 85 let cmd = GetVimCommandCleanTerm() .. reset_u7 .. a:arguments 86 87 let options = { 88 \ 'curwin': 1, 89 \ 'term_rows': rows, 90 \ 'term_cols': cols, 91 \ } 92 " Accept other options whose name starts with 'term_'. 93 call extend(options, filter(copy(a:options), 'v:key =~# "^term_"')) 94 95 let buf = term_start(cmd, options) 96 97 if &termwinsize == '' 98 " in the GUI we may end up with a different size, try to set it. 99 if term_getsize(buf) != [rows, cols] 100 call term_setsize(buf, rows, cols) 101 endif 102 call assert_equal([rows, cols], term_getsize(buf)) 103 else 104 let rows = term_getsize(buf)[0] 105 let cols = term_getsize(buf)[1] 106 endif 107 108 call TermWait(buf) 109 110 " Wait for "All" or "Top" of the ruler to be shown in the last line or in 111 " the status line of the last window. This can be quite slow (e.g. when 112 " using valgrind). 113 " If it fails then show the terminal contents for debugging. 114 try 115 call WaitFor({-> len(term_getline(buf, rows)) >= cols - 1 || len(term_getline(buf, rows - statusoff)) >= cols - 1}) 116 catch /timed out after/ 117 let lines = map(range(1, rows), {key, val -> term_getline(buf, val)}) 118 call assert_report('RunVimInTerminal() failed, screen contents: ' . join(lines, "<NL>")) 119 endtry 120 121 " Starting a terminal to run Vim is always considered flaky. 122 let g:test_is_flaky = 1 123 124 return buf 125endfunc 126 127" Stop a Vim running in terminal buffer "buf". 128func StopVimInTerminal(buf) 129 " Using a terminal to run Vim is always considered flaky. 130 let g:test_is_flaky = 1 131 132 call assert_equal("running", term_getstatus(a:buf)) 133 134 " CTRL-O : works both in Normal mode and Insert mode to start a command line. 135 " In Command-line it's inserted, the CTRL-U removes it again. 136 call term_sendkeys(a:buf, "\<C-O>:\<C-U>qa!\<cr>") 137 138 " Wait for all the pending updates to terminal to complete 139 call TermWait(a:buf) 140 141 call WaitForAssert({-> assert_equal("finished", term_getstatus(a:buf))}) 142 only! 143endfunc 144 145" vim: shiftwidth=2 sts=2 expandtab 146