Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1 | " Functions shared by several tests. |
| 2 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 3 | " Only load this script once. |
| 4 | if exists('*WaitFor') |
| 5 | finish |
| 6 | endif |
| 7 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 8 | " Get the name of the Python executable. |
| 9 | " Also keeps it in s:python. |
| 10 | func PythonProg() |
| 11 | " This test requires the Python command to run the test server. |
| 12 | " This most likely only works on Unix and Windows. |
| 13 | if has('unix') |
| 14 | " We also need the job feature or the pkill command to make sure the server |
| 15 | " can be stopped. |
| 16 | if !(executable('python') && (has('job') || executable('pkill'))) |
| 17 | return '' |
| 18 | endif |
| 19 | let s:python = 'python' |
| 20 | elseif has('win32') |
| 21 | " Use Python Launcher for Windows (py.exe) if available. |
| 22 | if executable('py.exe') |
| 23 | let s:python = 'py.exe' |
| 24 | elseif executable('python.exe') |
| 25 | let s:python = 'python.exe' |
| 26 | else |
| 27 | return '' |
| 28 | endif |
| 29 | else |
| 30 | return '' |
| 31 | endif |
| 32 | return s:python |
| 33 | endfunc |
| 34 | |
| 35 | " Run "cmd". Returns the job if using a job. |
| 36 | func RunCommand(cmd) |
| 37 | let job = 0 |
| 38 | if has('job') |
| 39 | let job = job_start(a:cmd, {"stoponexit": "hup"}) |
| 40 | call job_setoptions(job, {"stoponexit": "kill"}) |
| 41 | elseif has('win32') |
| 42 | exe 'silent !start cmd /c start "test_channel" ' . a:cmd |
| 43 | else |
| 44 | exe 'silent !' . a:cmd . '&' |
| 45 | endif |
| 46 | return job |
| 47 | endfunc |
| 48 | |
| 49 | " Read the port number from the Xportnr file. |
| 50 | func GetPort() |
| 51 | let l = [] |
| 52 | for i in range(200) |
| 53 | try |
| 54 | let l = readfile("Xportnr") |
| 55 | catch |
| 56 | endtry |
| 57 | if len(l) >= 1 |
| 58 | break |
| 59 | endif |
| 60 | sleep 10m |
| 61 | endfor |
| 62 | call delete("Xportnr") |
| 63 | |
| 64 | if len(l) == 0 |
| 65 | " Can't make the connection, give up. |
| 66 | return 0 |
| 67 | endif |
| 68 | return l[0] |
| 69 | endfunc |
| 70 | |
| 71 | " Run a Python server for "cmd" and call "testfunc". |
| 72 | " Always kills the server before returning. |
| 73 | func RunServer(cmd, testfunc, args) |
| 74 | " The Python program writes the port number in Xportnr. |
| 75 | call delete("Xportnr") |
| 76 | |
| 77 | if len(a:args) == 1 |
| 78 | let arg = ' ' . a:args[0] |
| 79 | else |
| 80 | let arg = '' |
| 81 | endif |
| 82 | let pycmd = s:python . " " . a:cmd . arg |
| 83 | |
| 84 | try |
| 85 | let g:currentJob = RunCommand(pycmd) |
| 86 | |
| 87 | " Wait for up to 2 seconds for the port number to be there. |
| 88 | let port = GetPort() |
| 89 | if port == 0 |
| 90 | call assert_false(1, "Can't start " . a:cmd) |
| 91 | return |
| 92 | endif |
| 93 | |
| 94 | call call(function(a:testfunc), [port]) |
| 95 | catch |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 96 | call assert_false(1, 'Caught exception: "' . v:exception . '" in ' . v:throwpoint) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 97 | finally |
| 98 | call s:kill_server(a:cmd) |
| 99 | endtry |
| 100 | endfunc |
| 101 | |
| 102 | func s:kill_server(cmd) |
| 103 | if has('job') |
| 104 | if exists('g:currentJob') |
| 105 | call job_stop(g:currentJob) |
| 106 | unlet g:currentJob |
| 107 | endif |
| 108 | elseif has('win32') |
| 109 | let cmd = substitute(a:cmd, ".py", '', '') |
| 110 | call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq ' . cmd . '"') |
| 111 | else |
| 112 | call system("pkill -f " . a:cmd) |
| 113 | endif |
| 114 | endfunc |
| 115 | |
Bram Moolenaar | 13deab8 | 2017-11-04 18:48:43 +0100 | [diff] [blame] | 116 | " Wait for up to a second for "expr" to become true. "expr" can be a |
| 117 | " stringified expression to evaluate, or a funcref without arguments. |
Bram Moolenaar | 5d7ead3 | 2018-02-27 17:17:42 +0100 | [diff] [blame] | 118 | " A second argument can be used to specify a different timeout in msec. |
Bram Moolenaar | 13deab8 | 2017-11-04 18:48:43 +0100 | [diff] [blame] | 119 | " |
Bram Moolenaar | f267f8b | 2016-08-22 21:40:29 +0200 | [diff] [blame] | 120 | " Return time slept in milliseconds. With the +reltime feature this can be |
| 121 | " more than the actual waiting time. Without +reltime it can also be less. |
Bram Moolenaar | cdb7e1b | 2017-07-19 19:55:58 +0200 | [diff] [blame] | 122 | func WaitFor(expr, ...) |
| 123 | let timeout = get(a:000, 0, 1000) |
Bram Moolenaar | f267f8b | 2016-08-22 21:40:29 +0200 | [diff] [blame] | 124 | " using reltime() is more accurate, but not always available |
| 125 | if has('reltime') |
| 126 | let start = reltime() |
| 127 | else |
| 128 | let slept = 0 |
| 129 | endif |
Bram Moolenaar | 13deab8 | 2017-11-04 18:48:43 +0100 | [diff] [blame] | 130 | if type(a:expr) == v:t_func |
| 131 | let Test = a:expr |
| 132 | else |
| 133 | let Test = {-> eval(a:expr) } |
| 134 | endif |
Bram Moolenaar | cdb7e1b | 2017-07-19 19:55:58 +0200 | [diff] [blame] | 135 | for i in range(timeout / 10) |
Bram Moolenaar | 13deab8 | 2017-11-04 18:48:43 +0100 | [diff] [blame] | 136 | if Test() |
Bram Moolenaar | c20e0d5 | 2017-11-02 18:19:19 +0100 | [diff] [blame] | 137 | if has('reltime') |
| 138 | return float2nr(reltimefloat(reltime(start)) * 1000) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 139 | endif |
Bram Moolenaar | c20e0d5 | 2017-11-02 18:19:19 +0100 | [diff] [blame] | 140 | return slept |
| 141 | endif |
Bram Moolenaar | f267f8b | 2016-08-22 21:40:29 +0200 | [diff] [blame] | 142 | if !has('reltime') |
| 143 | let slept += 10 |
| 144 | endif |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 145 | sleep 10m |
| 146 | endfor |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 147 | throw 'WaitFor() timed out after ' . timeout . ' msec' |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 148 | endfunc |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 149 | |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 150 | " Wait for up to a given milliseconds. |
| 151 | " With the +timers feature this waits for key-input by getchar(), Resume() |
| 152 | " feeds key-input and resumes process. Return time waited in milliseconds. |
| 153 | " Without +timers it uses simply :sleep. |
| 154 | func Standby(msec) |
| 155 | if has('timers') |
| 156 | let start = reltime() |
| 157 | let g:_standby_timer = timer_start(a:msec, function('s:feedkeys')) |
| 158 | call getchar() |
| 159 | return float2nr(reltimefloat(reltime(start)) * 1000) |
| 160 | else |
| 161 | execute 'sleep ' a:msec . 'm' |
| 162 | return a:msec |
| 163 | endif |
| 164 | endfunc |
| 165 | |
| 166 | func Resume() |
| 167 | if exists('g:_standby_timer') |
| 168 | call timer_stop(g:_standby_timer) |
| 169 | call s:feedkeys(0) |
| 170 | unlet g:_standby_timer |
| 171 | endif |
| 172 | endfunc |
| 173 | |
| 174 | func s:feedkeys(timer) |
| 175 | call feedkeys('x', 'nt') |
| 176 | endfunc |
| 177 | |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 178 | " Get $VIMPROG to run Vim executable. |
| 179 | " The Makefile writes it as the first line in the "vimcmd" file. |
| 180 | func GetVimProg() |
| 181 | if !filereadable('vimcmd') |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 182 | " Assume the script was sourced instead of running "make". |
| 183 | return '../vim' |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 184 | endif |
| 185 | return readfile('vimcmd')[0] |
| 186 | endfunc |
| 187 | |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 188 | " Get the command to run Vim, with -u NONE and --not-a-term arguments. |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 189 | " If there is an argument use it instead of "NONE". |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 190 | func GetVimCommand(...) |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 191 | if !filereadable('vimcmd') |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 192 | echo 'Cannot read the "vimcmd" file, falling back to ../vim.' |
| 193 | let lines = ['../vim'] |
| 194 | else |
| 195 | let lines = readfile('vimcmd') |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 196 | endif |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 197 | if a:0 == 0 |
| 198 | let name = 'NONE' |
| 199 | else |
| 200 | let name = a:1 |
| 201 | endif |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 202 | " For Unix Makefile writes the command to use in the second line of the |
| 203 | " "vimcmd" file, including environment options. |
| 204 | " Other Makefiles just write the executable in the first line, so fall back |
| 205 | " to that if there is no second line. |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 206 | let cmd = get(lines, 1, lines[0]) |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 207 | let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '') |
| 208 | if cmd !~ '-u '. name |
| 209 | let cmd = cmd . ' -u ' . name |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 210 | endif |
| 211 | let cmd .= ' --not-a-term' |
| 212 | let cmd = substitute(cmd, 'VIMRUNTIME=.*VIMRUNTIME;', '', '') |
| 213 | return cmd |
| 214 | endfunc |
| 215 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 216 | " Get the command to run Vim, with --clean. |
| 217 | func GetVimCommandClean() |
| 218 | let cmd = GetVimCommand() |
| 219 | let cmd = substitute(cmd, '-u NONE', '--clean', '') |
| 220 | let cmd = substitute(cmd, '--not-a-term', '', '') |
| 221 | return cmd |
| 222 | endfunc |
| 223 | |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 224 | " Run Vim, using the "vimcmd" file and "-u NORC". |
Bram Moolenaar | 3a93838 | 2016-08-07 16:36:40 +0200 | [diff] [blame] | 225 | " "before" is a list of Vim commands to be executed before loading plugins. |
| 226 | " "after" is a list of Vim commands to be executed after loading plugins. |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 227 | " Plugins are not loaded, unless 'loadplugins' is set in "before". |
| 228 | " Return 1 if Vim could be executed. |
Bram Moolenaar | 472a0a8 | 2016-08-06 22:31:42 +0200 | [diff] [blame] | 229 | func RunVim(before, after, arguments) |
Bram Moolenaar | 7a9a5f4 | 2016-08-08 22:34:14 +0200 | [diff] [blame] | 230 | return RunVimPiped(a:before, a:after, a:arguments, '') |
Bram Moolenaar | 3a93838 | 2016-08-07 16:36:40 +0200 | [diff] [blame] | 231 | endfunc |
| 232 | |
| 233 | func RunVimPiped(before, after, arguments, pipecmd) |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 234 | let cmd = GetVimCommand() |
| 235 | if cmd == '' |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 236 | return 0 |
| 237 | endif |
Bram Moolenaar | ba98bef | 2016-08-07 15:51:39 +0200 | [diff] [blame] | 238 | let args = '' |
Bram Moolenaar | 472a0a8 | 2016-08-06 22:31:42 +0200 | [diff] [blame] | 239 | if len(a:before) > 0 |
| 240 | call writefile(a:before, 'Xbefore.vim') |
| 241 | let args .= ' --cmd "so Xbefore.vim"' |
| 242 | endif |
| 243 | if len(a:after) > 0 |
| 244 | call writefile(a:after, 'Xafter.vim') |
| 245 | let args .= ' -S Xafter.vim' |
| 246 | endif |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 247 | |
Bram Moolenaar | 3a93838 | 2016-08-07 16:36:40 +0200 | [diff] [blame] | 248 | exe "silent !" . a:pipecmd . cmd . args . ' ' . a:arguments |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 249 | |
Bram Moolenaar | 472a0a8 | 2016-08-06 22:31:42 +0200 | [diff] [blame] | 250 | if len(a:before) > 0 |
| 251 | call delete('Xbefore.vim') |
| 252 | endif |
| 253 | if len(a:after) > 0 |
| 254 | call delete('Xafter.vim') |
| 255 | endif |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 256 | return 1 |
| 257 | endfunc |
Bram Moolenaar | 9f0139a | 2017-08-13 20:26:20 +0200 | [diff] [blame] | 258 | |
| 259 | func CanRunGui() |
| 260 | return has('gui') && ($DISPLAY != "" || has('gui_running')) |
| 261 | endfunc |
Bram Moolenaar | 5f73ef8 | 2018-02-27 21:09:30 +0100 | [diff] [blame] | 262 | |
| 263 | " Get line "lnum" as displayed on the screen. |
| 264 | " Trailing white space is trimmed. |
| 265 | func! Screenline(lnum) |
| 266 | let chars = [] |
| 267 | for c in range(1, winwidth(0)) |
| 268 | call add(chars, nr2char(screenchar(a:lnum, c))) |
| 269 | endfor |
| 270 | let line = join(chars, '') |
| 271 | return matchstr(line, '^.\{-}\ze\s*$') |
| 272 | endfunc |
Bram Moolenaar | 4d8bac8 | 2018-03-09 21:33:34 +0100 | [diff] [blame] | 273 | |
| 274 | " Stops the shell running in terminal "buf". |
| 275 | func Stop_shell_in_terminal(buf) |
| 276 | call term_sendkeys(a:buf, "exit\r") |
| 277 | let job = term_getjob(a:buf) |
| 278 | call WaitFor({-> job_status(job) == "dead"}) |
| 279 | endfunc |