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. |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 4 | if exists('*PythonProg') |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 5 | finish |
| 6 | endif |
| 7 | |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 8 | source view_util.vim |
| 9 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 10 | " Get the name of the Python executable. |
| 11 | " Also keeps it in s:python. |
| 12 | func PythonProg() |
| 13 | " This test requires the Python command to run the test server. |
| 14 | " This most likely only works on Unix and Windows. |
| 15 | if has('unix') |
| 16 | " We also need the job feature or the pkill command to make sure the server |
| 17 | " can be stopped. |
| 18 | if !(executable('python') && (has('job') || executable('pkill'))) |
| 19 | return '' |
| 20 | endif |
| 21 | let s:python = 'python' |
| 22 | elseif has('win32') |
| 23 | " Use Python Launcher for Windows (py.exe) if available. |
| 24 | if executable('py.exe') |
| 25 | let s:python = 'py.exe' |
| 26 | elseif executable('python.exe') |
| 27 | let s:python = 'python.exe' |
| 28 | else |
| 29 | return '' |
| 30 | endif |
| 31 | else |
| 32 | return '' |
| 33 | endif |
| 34 | return s:python |
| 35 | endfunc |
| 36 | |
| 37 | " Run "cmd". Returns the job if using a job. |
| 38 | func RunCommand(cmd) |
| 39 | let job = 0 |
| 40 | if has('job') |
| 41 | let job = job_start(a:cmd, {"stoponexit": "hup"}) |
| 42 | call job_setoptions(job, {"stoponexit": "kill"}) |
| 43 | elseif has('win32') |
| 44 | exe 'silent !start cmd /c start "test_channel" ' . a:cmd |
| 45 | else |
| 46 | exe 'silent !' . a:cmd . '&' |
| 47 | endif |
| 48 | return job |
| 49 | endfunc |
| 50 | |
| 51 | " Read the port number from the Xportnr file. |
| 52 | func GetPort() |
| 53 | let l = [] |
Bram Moolenaar | 453ce7c | 2018-10-12 22:15:12 +0200 | [diff] [blame] | 54 | " with 200 it sometimes failed |
| 55 | for i in range(400) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 56 | try |
| 57 | let l = readfile("Xportnr") |
| 58 | catch |
| 59 | endtry |
| 60 | if len(l) >= 1 |
| 61 | break |
| 62 | endif |
| 63 | sleep 10m |
| 64 | endfor |
| 65 | call delete("Xportnr") |
| 66 | |
| 67 | if len(l) == 0 |
| 68 | " Can't make the connection, give up. |
| 69 | return 0 |
| 70 | endif |
| 71 | return l[0] |
| 72 | endfunc |
| 73 | |
| 74 | " Run a Python server for "cmd" and call "testfunc". |
| 75 | " Always kills the server before returning. |
| 76 | func RunServer(cmd, testfunc, args) |
| 77 | " The Python program writes the port number in Xportnr. |
| 78 | call delete("Xportnr") |
| 79 | |
| 80 | if len(a:args) == 1 |
| 81 | let arg = ' ' . a:args[0] |
| 82 | else |
| 83 | let arg = '' |
| 84 | endif |
| 85 | let pycmd = s:python . " " . a:cmd . arg |
| 86 | |
| 87 | try |
| 88 | let g:currentJob = RunCommand(pycmd) |
| 89 | |
| 90 | " Wait for up to 2 seconds for the port number to be there. |
| 91 | let port = GetPort() |
| 92 | if port == 0 |
| 93 | call assert_false(1, "Can't start " . a:cmd) |
| 94 | return |
| 95 | endif |
| 96 | |
| 97 | call call(function(a:testfunc), [port]) |
| 98 | catch |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 99 | call assert_false(1, 'Caught exception: "' . v:exception . '" in ' . v:throwpoint) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 100 | finally |
| 101 | call s:kill_server(a:cmd) |
| 102 | endtry |
| 103 | endfunc |
| 104 | |
| 105 | func s:kill_server(cmd) |
| 106 | if has('job') |
| 107 | if exists('g:currentJob') |
| 108 | call job_stop(g:currentJob) |
| 109 | unlet g:currentJob |
| 110 | endif |
| 111 | elseif has('win32') |
| 112 | let cmd = substitute(a:cmd, ".py", '', '') |
| 113 | call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq ' . cmd . '"') |
| 114 | else |
| 115 | call system("pkill -f " . a:cmd) |
| 116 | endif |
| 117 | endfunc |
| 118 | |
Bram Moolenaar | 769e9d2 | 2018-04-11 20:53:49 +0200 | [diff] [blame] | 119 | " Wait for up to five seconds for "expr" to become true. "expr" can be a |
Bram Moolenaar | 13deab8 | 2017-11-04 18:48:43 +0100 | [diff] [blame] | 120 | " stringified expression to evaluate, or a funcref without arguments. |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 121 | " Using a lambda works best. Example: |
| 122 | " call WaitFor({-> status == "ok"}) |
| 123 | " |
Bram Moolenaar | 5d7ead3 | 2018-02-27 17:17:42 +0100 | [diff] [blame] | 124 | " 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] | 125 | " |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 126 | " When successful the time slept is returned. |
| 127 | " When running into the timeout an exception is thrown, thus the function does |
| 128 | " not return. |
Bram Moolenaar | cdb7e1b | 2017-07-19 19:55:58 +0200 | [diff] [blame] | 129 | func WaitFor(expr, ...) |
Bram Moolenaar | 769e9d2 | 2018-04-11 20:53:49 +0200 | [diff] [blame] | 130 | let timeout = get(a:000, 0, 5000) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 131 | let slept = s:WaitForCommon(a:expr, v:null, timeout) |
| 132 | if slept < 0 |
| 133 | throw 'WaitFor() timed out after ' . timeout . ' msec' |
| 134 | endif |
| 135 | return slept |
| 136 | endfunc |
| 137 | |
| 138 | " Wait for up to five seconds for "assert" to return zero. "assert" must be a |
| 139 | " (lambda) function containing one assert function. Example: |
| 140 | " call WaitForAssert({-> assert_equal("dead", job_status(job)}) |
| 141 | " |
| 142 | " A second argument can be used to specify a different timeout in msec. |
| 143 | " |
| 144 | " Return zero for success, one for failure (like the assert function). |
| 145 | func WaitForAssert(assert, ...) |
| 146 | let timeout = get(a:000, 0, 5000) |
| 147 | if s:WaitForCommon(v:null, a:assert, timeout) < 0 |
| 148 | return 1 |
| 149 | endif |
| 150 | return 0 |
| 151 | endfunc |
| 152 | |
| 153 | " Common implementation of WaitFor() and WaitForAssert(). |
| 154 | " Either "expr" or "assert" is not v:null |
| 155 | " Return the waiting time for success, -1 for failure. |
| 156 | func s:WaitForCommon(expr, assert, timeout) |
Bram Moolenaar | f267f8b | 2016-08-22 21:40:29 +0200 | [diff] [blame] | 157 | " using reltime() is more accurate, but not always available |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 158 | let slept = 0 |
Bram Moolenaar | f267f8b | 2016-08-22 21:40:29 +0200 | [diff] [blame] | 159 | if has('reltime') |
| 160 | let start = reltime() |
Bram Moolenaar | f267f8b | 2016-08-22 21:40:29 +0200 | [diff] [blame] | 161 | endif |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 162 | |
| 163 | while 1 |
| 164 | if type(a:expr) == v:t_func |
| 165 | let success = a:expr() |
| 166 | elseif type(a:assert) == v:t_func |
| 167 | let success = a:assert() == 0 |
| 168 | else |
| 169 | let success = eval(a:expr) |
| 170 | endif |
| 171 | if success |
Bram Moolenaar | c20e0d5 | 2017-11-02 18:19:19 +0100 | [diff] [blame] | 172 | return slept |
| 173 | endif |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 174 | |
| 175 | if slept >= a:timeout |
| 176 | break |
| 177 | endif |
| 178 | if type(a:assert) == v:t_func |
| 179 | " Remove the error added by the assert function. |
| 180 | call remove(v:errors, -1) |
| 181 | endif |
| 182 | |
| 183 | sleep 10m |
| 184 | if has('reltime') |
| 185 | let slept = float2nr(reltimefloat(reltime(start)) * 1000) |
| 186 | else |
Bram Moolenaar | f267f8b | 2016-08-22 21:40:29 +0200 | [diff] [blame] | 187 | let slept += 10 |
| 188 | endif |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 189 | endwhile |
| 190 | |
| 191 | return -1 " timed out |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 192 | endfunc |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 193 | |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 194 | |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 195 | " Wait for up to a given milliseconds. |
| 196 | " With the +timers feature this waits for key-input by getchar(), Resume() |
| 197 | " feeds key-input and resumes process. Return time waited in milliseconds. |
| 198 | " Without +timers it uses simply :sleep. |
| 199 | func Standby(msec) |
| 200 | if has('timers') |
| 201 | let start = reltime() |
| 202 | let g:_standby_timer = timer_start(a:msec, function('s:feedkeys')) |
| 203 | call getchar() |
| 204 | return float2nr(reltimefloat(reltime(start)) * 1000) |
| 205 | else |
| 206 | execute 'sleep ' a:msec . 'm' |
| 207 | return a:msec |
| 208 | endif |
| 209 | endfunc |
| 210 | |
| 211 | func Resume() |
| 212 | if exists('g:_standby_timer') |
| 213 | call timer_stop(g:_standby_timer) |
| 214 | call s:feedkeys(0) |
| 215 | unlet g:_standby_timer |
| 216 | endif |
| 217 | endfunc |
| 218 | |
| 219 | func s:feedkeys(timer) |
| 220 | call feedkeys('x', 'nt') |
| 221 | endfunc |
| 222 | |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 223 | " Get $VIMPROG to run Vim executable. |
| 224 | " The Makefile writes it as the first line in the "vimcmd" file. |
| 225 | func GetVimProg() |
| 226 | if !filereadable('vimcmd') |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 227 | " Assume the script was sourced instead of running "make". |
| 228 | return '../vim' |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 229 | endif |
| 230 | return readfile('vimcmd')[0] |
| 231 | endfunc |
| 232 | |
Bram Moolenaar | 248be5c | 2018-05-05 15:47:19 +0200 | [diff] [blame] | 233 | let g:valgrind_cnt = 1 |
| 234 | |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 235 | " 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] | 236 | " If there is an argument use it instead of "NONE". |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 237 | func GetVimCommand(...) |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 238 | if !filereadable('vimcmd') |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 239 | echo 'Cannot read the "vimcmd" file, falling back to ../vim.' |
| 240 | let lines = ['../vim'] |
| 241 | else |
| 242 | let lines = readfile('vimcmd') |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 243 | endif |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 244 | if a:0 == 0 |
| 245 | let name = 'NONE' |
| 246 | else |
| 247 | let name = a:1 |
| 248 | endif |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 249 | " For Unix Makefile writes the command to use in the second line of the |
| 250 | " "vimcmd" file, including environment options. |
| 251 | " Other Makefiles just write the executable in the first line, so fall back |
Bram Moolenaar | 248be5c | 2018-05-05 15:47:19 +0200 | [diff] [blame] | 252 | " to that if there is no second line or it is empty. |
| 253 | if len(lines) > 1 && lines[1] != '' |
| 254 | let cmd = lines[1] |
| 255 | else |
| 256 | let cmd = lines[0] |
| 257 | endif |
| 258 | |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 259 | let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '') |
| 260 | if cmd !~ '-u '. name |
| 261 | let cmd = cmd . ' -u ' . name |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 262 | endif |
| 263 | let cmd .= ' --not-a-term' |
| 264 | let cmd = substitute(cmd, 'VIMRUNTIME=.*VIMRUNTIME;', '', '') |
Bram Moolenaar | 248be5c | 2018-05-05 15:47:19 +0200 | [diff] [blame] | 265 | |
| 266 | " If using valgrind, make sure every run uses a different log file. |
| 267 | if cmd =~ 'valgrind.*--log-file=' |
| 268 | let cmd = substitute(cmd, '--log-file=\(^\s*\)', '--log-file=\1.' . g:valgrind_cnt, '') |
| 269 | let g:valgrind_cnt += 1 |
| 270 | endif |
| 271 | |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 272 | return cmd |
| 273 | endfunc |
| 274 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 275 | " Get the command to run Vim, with --clean. |
| 276 | func GetVimCommandClean() |
| 277 | let cmd = GetVimCommand() |
| 278 | let cmd = substitute(cmd, '-u NONE', '--clean', '') |
| 279 | let cmd = substitute(cmd, '--not-a-term', '', '') |
Bram Moolenaar | 453ce7c | 2018-10-12 22:15:12 +0200 | [diff] [blame] | 280 | |
| 281 | " Optionally run Vim under valgrind |
| 282 | " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd |
| 283 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 284 | return cmd |
| 285 | endfunc |
| 286 | |
Bram Moolenaar | 0d70202 | 2019-07-04 14:20:41 +0200 | [diff] [blame] | 287 | " Get the command to run Vim, with --clean, and force to run in terminal so it |
| 288 | " won't start a new GUI. |
| 289 | func GetVimCommandCleanTerm() |
| 290 | " Add -v to have gvim run in the terminal (if possible) |
| 291 | return GetVimCommandClean() .. ' -v ' |
| 292 | endfunc |
| 293 | |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 294 | " Run Vim, using the "vimcmd" file and "-u NORC". |
Bram Moolenaar | 3a93838 | 2016-08-07 16:36:40 +0200 | [diff] [blame] | 295 | " "before" is a list of Vim commands to be executed before loading plugins. |
| 296 | " "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] | 297 | " Plugins are not loaded, unless 'loadplugins' is set in "before". |
| 298 | " Return 1 if Vim could be executed. |
Bram Moolenaar | 472a0a8 | 2016-08-06 22:31:42 +0200 | [diff] [blame] | 299 | func RunVim(before, after, arguments) |
Bram Moolenaar | 7a9a5f4 | 2016-08-08 22:34:14 +0200 | [diff] [blame] | 300 | return RunVimPiped(a:before, a:after, a:arguments, '') |
Bram Moolenaar | 3a93838 | 2016-08-07 16:36:40 +0200 | [diff] [blame] | 301 | endfunc |
| 302 | |
| 303 | func RunVimPiped(before, after, arguments, pipecmd) |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 304 | let cmd = GetVimCommand() |
Bram Moolenaar | ba98bef | 2016-08-07 15:51:39 +0200 | [diff] [blame] | 305 | let args = '' |
Bram Moolenaar | 472a0a8 | 2016-08-06 22:31:42 +0200 | [diff] [blame] | 306 | if len(a:before) > 0 |
| 307 | call writefile(a:before, 'Xbefore.vim') |
| 308 | let args .= ' --cmd "so Xbefore.vim"' |
| 309 | endif |
| 310 | if len(a:after) > 0 |
| 311 | call writefile(a:after, 'Xafter.vim') |
| 312 | let args .= ' -S Xafter.vim' |
| 313 | endif |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 314 | |
Bram Moolenaar | 3a93838 | 2016-08-07 16:36:40 +0200 | [diff] [blame] | 315 | exe "silent !" . a:pipecmd . cmd . args . ' ' . a:arguments |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 316 | |
Bram Moolenaar | 472a0a8 | 2016-08-06 22:31:42 +0200 | [diff] [blame] | 317 | if len(a:before) > 0 |
| 318 | call delete('Xbefore.vim') |
| 319 | endif |
| 320 | if len(a:after) > 0 |
| 321 | call delete('Xafter.vim') |
| 322 | endif |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 323 | return 1 |
| 324 | endfunc |
Bram Moolenaar | 9f0139a | 2017-08-13 20:26:20 +0200 | [diff] [blame] | 325 | |
| 326 | func CanRunGui() |
| 327 | return has('gui') && ($DISPLAY != "" || has('gui_running')) |
| 328 | endfunc |
Bram Moolenaar | 5f73ef8 | 2018-02-27 21:09:30 +0100 | [diff] [blame] | 329 | |
Bram Moolenaar | a903472 | 2018-03-13 15:43:46 +0100 | [diff] [blame] | 330 | func WorkingClipboard() |
| 331 | if !has('clipboard') |
| 332 | return 0 |
| 333 | endif |
| 334 | if has('x11') |
| 335 | return $DISPLAY != "" |
| 336 | endif |
| 337 | return 1 |
| 338 | endfunc |