blob: 1b41c6ff3e1f5c280bb3c865560082c62f02cd7a [file] [log] [blame]
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001" Functions shared by several tests.
2
Bram Moolenaara5e66212017-09-29 22:42:33 +02003" Only load this script once.
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02004if exists('*PythonProg')
Bram Moolenaara5e66212017-09-29 22:42:33 +02005 finish
6endif
7
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02008source view_util.vim
9
Bram Moolenaar321efdd2016-07-15 17:09:11 +020010" Get the name of the Python executable.
11" Also keeps it in s:python.
12func 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.
LemonBoycc766a82022-04-04 15:46:58 +010018 if !(has('job') || executable('pkill'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020019 return ''
20 endif
LemonBoycc766a82022-04-04 15:46:58 +010021 if executable('python')
22 let s:python = 'python'
23 elseif executable('python3')
24 let s:python = 'python3'
25 else
26 return ''
27 end
Bram Moolenaar321efdd2016-07-15 17:09:11 +020028 elseif has('win32')
29 " Use Python Launcher for Windows (py.exe) if available.
Bram Moolenaar1bb0da22021-05-02 19:15:05 +020030 " NOTE: if you get a "Python was not found" error, disable the Python
31 " shortcuts in "Windows menu / Settings / Manage App Execution Aliases".
Bram Moolenaar321efdd2016-07-15 17:09:11 +020032 if executable('py.exe')
33 let s:python = 'py.exe'
34 elseif executable('python.exe')
35 let s:python = 'python.exe'
36 else
37 return ''
38 endif
39 else
40 return ''
41 endif
42 return s:python
43endfunc
44
45" Run "cmd". Returns the job if using a job.
46func RunCommand(cmd)
Bram Moolenaar4a070cc2020-05-07 18:16:35 +020047 " Running an external command can occasionally be slow or fail.
48 let g:test_is_flaky = 1
49
Bram Moolenaar321efdd2016-07-15 17:09:11 +020050 let job = 0
51 if has('job')
52 let job = job_start(a:cmd, {"stoponexit": "hup"})
53 call job_setoptions(job, {"stoponexit": "kill"})
54 elseif has('win32')
55 exe 'silent !start cmd /c start "test_channel" ' . a:cmd
56 else
57 exe 'silent !' . a:cmd . '&'
58 endif
59 return job
60endfunc
61
62" Read the port number from the Xportnr file.
63func GetPort()
64 let l = []
Bram Moolenaara906e8e2022-09-01 18:42:32 +010065 " with 200 it sometimes failed, with 400 is rarily failed
66 for i in range(600)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020067 try
68 let l = readfile("Xportnr")
69 catch
70 endtry
71 if len(l) >= 1
72 break
73 endif
74 sleep 10m
75 endfor
76 call delete("Xportnr")
77
78 if len(l) == 0
79 " Can't make the connection, give up.
80 return 0
81 endif
82 return l[0]
83endfunc
84
85" Run a Python server for "cmd" and call "testfunc".
86" Always kills the server before returning.
87func RunServer(cmd, testfunc, args)
88 " The Python program writes the port number in Xportnr.
89 call delete("Xportnr")
90
91 if len(a:args) == 1
92 let arg = ' ' . a:args[0]
93 else
94 let arg = ''
95 endif
96 let pycmd = s:python . " " . a:cmd . arg
97
98 try
99 let g:currentJob = RunCommand(pycmd)
100
Bram Moolenaar06d32a02022-09-03 13:58:47 +0100101 " Wait for some time for the port number to be there.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200102 let port = GetPort()
103 if port == 0
Bram Moolenaar5fbbec12022-09-03 22:08:11 +0100104 call assert_report(strftime("%H:%M:%S") .. " Can't start " .. a:cmd)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200105 return
106 endif
107
108 call call(function(a:testfunc), [port])
109 catch
Bram Moolenaar06d32a02022-09-03 13:58:47 +0100110 call assert_report('Caught exception: "' . v:exception . '" in ' . v:throwpoint)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200111 finally
112 call s:kill_server(a:cmd)
113 endtry
114endfunc
115
116func s:kill_server(cmd)
117 if has('job')
118 if exists('g:currentJob')
119 call job_stop(g:currentJob)
120 unlet g:currentJob
121 endif
122 elseif has('win32')
123 let cmd = substitute(a:cmd, ".py", '', '')
124 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq ' . cmd . '"')
125 else
126 call system("pkill -f " . a:cmd)
127 endif
128endfunc
129
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200130" Wait for up to five seconds for "expr" to become true. "expr" can be a
Bram Moolenaar13deab82017-11-04 18:48:43 +0100131" stringified expression to evaluate, or a funcref without arguments.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200132" Using a lambda works best. Example:
133" call WaitFor({-> status == "ok"})
134"
Bram Moolenaar5d7ead32018-02-27 17:17:42 +0100135" A second argument can be used to specify a different timeout in msec.
Bram Moolenaar13deab82017-11-04 18:48:43 +0100136"
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200137" When successful the time slept is returned.
138" When running into the timeout an exception is thrown, thus the function does
139" not return.
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +0200140func WaitFor(expr, ...)
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200141 let timeout = get(a:000, 0, 5000)
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200142 let slept = s:WaitForCommon(a:expr, v:null, timeout)
143 if slept < 0
144 throw 'WaitFor() timed out after ' . timeout . ' msec'
145 endif
146 return slept
147endfunc
148
149" Wait for up to five seconds for "assert" to return zero. "assert" must be a
150" (lambda) function containing one assert function. Example:
151" call WaitForAssert({-> assert_equal("dead", job_status(job)})
152"
153" A second argument can be used to specify a different timeout in msec.
154"
155" Return zero for success, one for failure (like the assert function).
156func WaitForAssert(assert, ...)
157 let timeout = get(a:000, 0, 5000)
158 if s:WaitForCommon(v:null, a:assert, timeout) < 0
159 return 1
160 endif
161 return 0
162endfunc
163
164" Common implementation of WaitFor() and WaitForAssert().
165" Either "expr" or "assert" is not v:null
166" Return the waiting time for success, -1 for failure.
167func s:WaitForCommon(expr, assert, timeout)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200168 " using reltime() is more accurate, but not always available
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200169 let slept = 0
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100170 if exists('*reltimefloat')
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200171 let start = reltime()
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200172 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200173
174 while 1
175 if type(a:expr) == v:t_func
176 let success = a:expr()
177 elseif type(a:assert) == v:t_func
178 let success = a:assert() == 0
179 else
180 let success = eval(a:expr)
181 endif
182 if success
Bram Moolenaarc20e0d52017-11-02 18:19:19 +0100183 return slept
184 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200185
186 if slept >= a:timeout
187 break
188 endif
189 if type(a:assert) == v:t_func
190 " Remove the error added by the assert function.
191 call remove(v:errors, -1)
192 endif
193
194 sleep 10m
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100195 if exists('*reltimefloat')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200196 let slept = float2nr(reltimefloat(reltime(start)) * 1000)
197 else
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200198 let slept += 10
199 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200200 endwhile
201
202 return -1 " timed out
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200203endfunc
Bram Moolenaar66459b72016-08-06 19:01:55 +0200204
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200205
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200206" Wait for up to a given milliseconds.
207" With the +timers feature this waits for key-input by getchar(), Resume()
208" feeds key-input and resumes process. Return time waited in milliseconds.
209" Without +timers it uses simply :sleep.
210func Standby(msec)
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100211 if has('timers') && exists('*reltimefloat')
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200212 let start = reltime()
213 let g:_standby_timer = timer_start(a:msec, function('s:feedkeys'))
214 call getchar()
215 return float2nr(reltimefloat(reltime(start)) * 1000)
216 else
217 execute 'sleep ' a:msec . 'm'
218 return a:msec
219 endif
220endfunc
221
222func Resume()
223 if exists('g:_standby_timer')
224 call timer_stop(g:_standby_timer)
225 call s:feedkeys(0)
226 unlet g:_standby_timer
227 endif
228endfunc
229
230func s:feedkeys(timer)
231 call feedkeys('x', 'nt')
232endfunc
233
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000234" Get the name of the Vim executable that we expect has been build in the src
235" directory.
236func s:GetJustBuildVimExe()
237 if has("win32")
238 if !filereadable('..\vim.exe') && filereadable('..\vimd.exe')
239 " looks like the debug executable was intentionally build, so use it
240 return '..\vimd.exe'
241 endif
242 return '..\vim.exe'
243 endif
244 return '../vim'
245endfunc
246
Bram Moolenaar22286892020-11-05 20:50:51 +0100247" Get $VIMPROG to run the Vim executable.
Bram Moolenaar63182052017-10-07 20:03:23 +0200248" The Makefile writes it as the first line in the "vimcmd" file.
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000249" Falls back to the Vim executable in the src directory.
Bram Moolenaar63182052017-10-07 20:03:23 +0200250func GetVimProg()
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000251 if filereadable('vimcmd')
252 return readfile('vimcmd')[0]
Bram Moolenaar63182052017-10-07 20:03:23 +0200253 endif
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000254 echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
255
256 " Probably the script was sourced instead of running "make".
257 " We assume Vim was just build in the src directory then.
258 return s:GetJustBuildVimExe()
Bram Moolenaar63182052017-10-07 20:03:23 +0200259endfunc
260
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200261let g:valgrind_cnt = 1
262
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100263" Get the command to run Vim, with -u NONE and --not-a-term arguments.
Bram Moolenaar9d954202017-09-04 20:34:19 +0200264" If there is an argument use it instead of "NONE".
Bram Moolenaar9d954202017-09-04 20:34:19 +0200265func GetVimCommand(...)
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000266 if filereadable('vimcmd')
Bram Moolenaarda650582018-02-20 15:51:40 +0100267 let lines = readfile('vimcmd')
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000268 else
269 echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
270 let lines = [s:GetJustBuildVimExe()]
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100271 endif
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000272
Bram Moolenaar9d954202017-09-04 20:34:19 +0200273 if a:0 == 0
274 let name = 'NONE'
275 else
276 let name = a:1
277 endif
Bram Moolenaar63182052017-10-07 20:03:23 +0200278 " For Unix Makefile writes the command to use in the second line of the
279 " "vimcmd" file, including environment options.
280 " Other Makefiles just write the executable in the first line, so fall back
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200281 " to that if there is no second line or it is empty.
282 if len(lines) > 1 && lines[1] != ''
283 let cmd = lines[1]
284 else
285 let cmd = lines[0]
286 endif
287
Bram Moolenaar9d954202017-09-04 20:34:19 +0200288 let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '')
289 if cmd !~ '-u '. name
290 let cmd = cmd . ' -u ' . name
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100291 endif
292 let cmd .= ' --not-a-term'
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100293 let cmd .= ' --gui-dialog-file guidialogfile'
Bram Moolenaar41a82602019-07-14 21:54:26 +0200294 let cmd = substitute(cmd, 'VIMRUNTIME=\S\+', '', '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200295
296 " If using valgrind, make sure every run uses a different log file.
297 if cmd =~ 'valgrind.*--log-file='
Bram Moolenaar657a8262020-07-15 18:29:18 +0200298 let cmd = substitute(cmd, '--log-file=\(\S*\)', '--log-file=\1.' . g:valgrind_cnt, '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200299 let g:valgrind_cnt += 1
300 endif
301
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100302 return cmd
303endfunc
304
Bram Moolenaare366ed42022-06-19 20:13:56 +0100305" Return one when it looks like the tests are run with valgrind, which means
306" that everything is much slower.
307func RunningWithValgrind()
308 return GetVimCommand() =~ '\<valgrind\>'
309endfunc
310
Bram Moolenaarf1699962019-08-31 17:48:19 +0200311" Get the command to run Vim, with --clean instead of "-u NONE".
Bram Moolenaarda650582018-02-20 15:51:40 +0100312func GetVimCommandClean()
313 let cmd = GetVimCommand()
314 let cmd = substitute(cmd, '-u NONE', '--clean', '')
315 let cmd = substitute(cmd, '--not-a-term', '', '')
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200316
Bram Moolenaar0fdddee2019-09-01 15:26:23 +0200317 " Force using utf-8, Vim may pick up something else from the environment.
318 let cmd ..= ' --cmd "set enc=utf8" '
319
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200320 " Optionally run Vim under valgrind
321 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
322
Bram Moolenaarda650582018-02-20 15:51:40 +0100323 return cmd
324endfunc
325
Bram Moolenaar0d702022019-07-04 14:20:41 +0200326" Get the command to run Vim, with --clean, and force to run in terminal so it
327" won't start a new GUI.
328func GetVimCommandCleanTerm()
329 " Add -v to have gvim run in the terminal (if possible)
330 return GetVimCommandClean() .. ' -v '
331endfunc
332
Bram Moolenaar66459b72016-08-06 19:01:55 +0200333" Run Vim, using the "vimcmd" file and "-u NORC".
Bram Moolenaar3a938382016-08-07 16:36:40 +0200334" "before" is a list of Vim commands to be executed before loading plugins.
335" "after" is a list of Vim commands to be executed after loading plugins.
Bram Moolenaar66459b72016-08-06 19:01:55 +0200336" Plugins are not loaded, unless 'loadplugins' is set in "before".
337" Return 1 if Vim could be executed.
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200338func RunVim(before, after, arguments)
Bram Moolenaar7a9a5f42016-08-08 22:34:14 +0200339 return RunVimPiped(a:before, a:after, a:arguments, '')
Bram Moolenaar3a938382016-08-07 16:36:40 +0200340endfunc
341
342func RunVimPiped(before, after, arguments, pipecmd)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100343 let cmd = GetVimCommand()
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200344 let args = ''
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200345 if len(a:before) > 0
346 call writefile(a:before, 'Xbefore.vim')
347 let args .= ' --cmd "so Xbefore.vim"'
348 endif
349 if len(a:after) > 0
350 call writefile(a:after, 'Xafter.vim')
351 let args .= ' -S Xafter.vim'
352 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200353
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +0100354 " Optionally run Vim under valgrind
355 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
356
Bram Moolenaar3a938382016-08-07 16:36:40 +0200357 exe "silent !" . a:pipecmd . cmd . args . ' ' . a:arguments
Bram Moolenaar66459b72016-08-06 19:01:55 +0200358
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200359 if len(a:before) > 0
360 call delete('Xbefore.vim')
361 endif
362 if len(a:after) > 0
363 call delete('Xafter.vim')
364 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200365 return 1
366endfunc
Bram Moolenaar07282f02019-10-10 16:46:17 +0200367
368func IsRoot()
369 if !has('unix')
370 return v:false
371 elseif $USER == 'root' || system('id -un') =~ '\<root\>'
372 return v:true
373 endif
374 return v:false
375endfunc
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200376
Bram Moolenaar88e6cc22020-04-30 19:19:29 +0200377" Get all messages but drop the maintainer entry.
378func GetMessages()
379 redir => result
380 redraw | messages
381 redir END
382 let msg_list = split(result, "\n")
383 if msg_list->len() > 0 && msg_list[0] =~ 'Messages maintainer:'
384 return msg_list[1:]
385 endif
386 return msg_list
387endfunc
388
Bram Moolenaarab589462020-07-06 21:03:06 +0200389" Run the list of commands in 'cmds' and look for 'errstr' in exception.
390" Note that assert_fails() cannot be used in some places and this function
391" can be used.
392func AssertException(cmds, errstr)
393 let save_exception = ''
394 try
395 for cmd in a:cmds
396 exe cmd
397 endfor
398 catch
399 let save_exception = v:exception
400 endtry
401 call assert_match(a:errstr, save_exception)
402endfunc
403
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200404" vim: shiftwidth=2 sts=2 expandtab