blob: ddd3f3725e70d0f55cd6e25fe9245929e223b47b [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
Christian Brabandteb380b92025-07-07 20:53:55 +02008source util/view_util.vim
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02009
Bram Moolenaarc255b782022-11-26 19:16:48 +000010" When 'term' is changed some status requests may be sent. The responses may
11" interfere with what is being tested. A short sleep is used to process any of
12" those responses first.
13func WaitForResponses()
14 sleep 50m
15endfunc
16
Bram Moolenaar321efdd2016-07-15 17:09:11 +020017" Get the name of the Python executable.
18" Also keeps it in s:python.
19func PythonProg()
20 " This test requires the Python command to run the test server.
21 " This most likely only works on Unix and Windows.
22 if has('unix')
23 " We also need the job feature or the pkill command to make sure the server
24 " can be stopped.
LemonBoycc766a82022-04-04 15:46:58 +010025 if !(has('job') || executable('pkill'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020026 return ''
27 endif
Yee Cheng Chincef8ab22024-11-04 20:26:47 +010028 if executable('python3')
LemonBoycc766a82022-04-04 15:46:58 +010029 let s:python = 'python3'
Yee Cheng Chincef8ab22024-11-04 20:26:47 +010030 elseif executable('python')
31 let s:python = 'python'
LemonBoycc766a82022-04-04 15:46:58 +010032 else
33 return ''
34 end
Bram Moolenaar321efdd2016-07-15 17:09:11 +020035 elseif has('win32')
36 " Use Python Launcher for Windows (py.exe) if available.
Bram Moolenaar1bb0da22021-05-02 19:15:05 +020037 " NOTE: if you get a "Python was not found" error, disable the Python
38 " shortcuts in "Windows menu / Settings / Manage App Execution Aliases".
Bram Moolenaar321efdd2016-07-15 17:09:11 +020039 if executable('py.exe')
40 let s:python = 'py.exe'
41 elseif executable('python.exe')
42 let s:python = 'python.exe'
43 else
44 return ''
45 endif
46 else
47 return ''
48 endif
49 return s:python
50endfunc
51
52" Run "cmd". Returns the job if using a job.
53func RunCommand(cmd)
Bram Moolenaar4a070cc2020-05-07 18:16:35 +020054 " Running an external command can occasionally be slow or fail.
55 let g:test_is_flaky = 1
56
Bram Moolenaar321efdd2016-07-15 17:09:11 +020057 let job = 0
58 if has('job')
59 let job = job_start(a:cmd, {"stoponexit": "hup"})
60 call job_setoptions(job, {"stoponexit": "kill"})
61 elseif has('win32')
Milly4f5681d2024-10-20 11:06:00 +020062 exe 'silent !start cmd /D /c start "test_channel" ' . a:cmd
Bram Moolenaar321efdd2016-07-15 17:09:11 +020063 else
64 exe 'silent !' . a:cmd . '&'
65 endif
66 return job
67endfunc
68
69" Read the port number from the Xportnr file.
70func GetPort()
71 let l = []
Bram Moolenaara906e8e2022-09-01 18:42:32 +010072 " with 200 it sometimes failed, with 400 is rarily failed
73 for i in range(600)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020074 try
75 let l = readfile("Xportnr")
76 catch
77 endtry
78 if len(l) >= 1
79 break
80 endif
81 sleep 10m
82 endfor
83 call delete("Xportnr")
84
85 if len(l) == 0
86 " Can't make the connection, give up.
87 return 0
88 endif
89 return l[0]
90endfunc
91
92" Run a Python server for "cmd" and call "testfunc".
93" Always kills the server before returning.
94func RunServer(cmd, testfunc, args)
95 " The Python program writes the port number in Xportnr.
96 call delete("Xportnr")
97
98 if len(a:args) == 1
99 let arg = ' ' . a:args[0]
100 else
101 let arg = ''
102 endif
103 let pycmd = s:python . " " . a:cmd . arg
104
105 try
106 let g:currentJob = RunCommand(pycmd)
107
Bram Moolenaar06d32a02022-09-03 13:58:47 +0100108 " Wait for some time for the port number to be there.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200109 let port = GetPort()
110 if port == 0
Bram Moolenaar5fbbec12022-09-03 22:08:11 +0100111 call assert_report(strftime("%H:%M:%S") .. " Can't start " .. a:cmd)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200112 return
113 endif
114
115 call call(function(a:testfunc), [port])
James McCoy43cb8e12023-11-02 20:05:38 +0100116 catch /E901.*Address family for hostname not supported/
117 throw 'Skipped: Invalid network setup ("' .. v:exception .. '" in ' .. v:throwpoint .. ')'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200118 catch
Bram Moolenaar06d32a02022-09-03 13:58:47 +0100119 call assert_report('Caught exception: "' . v:exception . '" in ' . v:throwpoint)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200120 finally
121 call s:kill_server(a:cmd)
122 endtry
123endfunc
124
125func s:kill_server(cmd)
126 if has('job')
127 if exists('g:currentJob')
128 call job_stop(g:currentJob)
129 unlet g:currentJob
130 endif
131 elseif has('win32')
132 let cmd = substitute(a:cmd, ".py", '', '')
133 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq ' . cmd . '"')
134 else
135 call system("pkill -f " . a:cmd)
136 endif
137endfunc
138
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100139" Callback function to be invoked by a child terminal job. The parent could
140" then wait for the notification using WaitForChildNotification()
141let g:child_notification = 0
142func Tapi_notify_parent(bufnum, arglist)
143 let g:child_notification = 1
144endfunc
145
146" Generates a command that we can pass to a terminal job that it uses to
147" notify us. Argument 'escape' will specify whether to escape the double
148" quote.
149func TermNotifyParentCmd(escape)
150 call assert_false(has("win32"), 'Windows does not support terminal API right now. Use another method to synchronize timing.')
151 let cmd = '\033]51;["call", "Tapi_notify_parent", []]\007'
152 if a:escape
153 return escape(cmd, '"')
154 endif
155 return cmd
156endfunc
157
158" Wait for a child process to notify us. This allows us to sequence events in
159" conjunction with the child. Currently the only supported notification method
160" is for a terminal job to call Tapi_notify_parent() using terminal API.
161func WaitForChildNotification(...)
162 let timeout = get(a:000, 0, 5000)
163 call WaitFor({-> g:child_notification == 1}, timeout)
164 let g:child_notification = 0
165endfunc
166
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200167" Wait for up to five seconds for "expr" to become true. "expr" can be a
Bram Moolenaar13deab82017-11-04 18:48:43 +0100168" stringified expression to evaluate, or a funcref without arguments.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200169" Using a lambda works best. Example:
170" call WaitFor({-> status == "ok"})
171"
Bram Moolenaar5d7ead32018-02-27 17:17:42 +0100172" A second argument can be used to specify a different timeout in msec.
Bram Moolenaar13deab82017-11-04 18:48:43 +0100173"
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200174" When successful the time slept is returned.
175" When running into the timeout an exception is thrown, thus the function does
176" not return.
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +0200177func WaitFor(expr, ...)
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200178 let timeout = get(a:000, 0, 5000)
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200179 let slept = s:WaitForCommon(a:expr, v:null, timeout)
180 if slept < 0
181 throw 'WaitFor() timed out after ' . timeout . ' msec'
182 endif
183 return slept
184endfunc
185
186" Wait for up to five seconds for "assert" to return zero. "assert" must be a
187" (lambda) function containing one assert function. Example:
188" call WaitForAssert({-> assert_equal("dead", job_status(job)})
189"
190" A second argument can be used to specify a different timeout in msec.
191"
192" Return zero for success, one for failure (like the assert function).
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100193func g:WaitForAssert(assert, ...)
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200194 let timeout = get(a:000, 0, 5000)
195 if s:WaitForCommon(v:null, a:assert, timeout) < 0
196 return 1
197 endif
198 return 0
199endfunc
200
201" Common implementation of WaitFor() and WaitForAssert().
202" Either "expr" or "assert" is not v:null
203" Return the waiting time for success, -1 for failure.
204func s:WaitForCommon(expr, assert, timeout)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200205 " using reltime() is more accurate, but not always available
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200206 let slept = 0
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100207 if exists('*reltimefloat')
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200208 let start = reltime()
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200209 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200210
211 while 1
212 if type(a:expr) == v:t_func
213 let success = a:expr()
214 elseif type(a:assert) == v:t_func
215 let success = a:assert() == 0
216 else
217 let success = eval(a:expr)
218 endif
219 if success
Bram Moolenaarc20e0d52017-11-02 18:19:19 +0100220 return slept
221 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200222
223 if slept >= a:timeout
224 break
225 endif
226 if type(a:assert) == v:t_func
227 " Remove the error added by the assert function.
228 call remove(v:errors, -1)
229 endif
230
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100231 sleep 1m
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100232 if exists('*reltimefloat')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200233 let slept = float2nr(reltimefloat(reltime(start)) * 1000)
234 else
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100235 let slept += 1
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200236 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200237 endwhile
238
239 return -1 " timed out
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200240endfunc
Bram Moolenaar66459b72016-08-06 19:01:55 +0200241
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200242
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200243" Wait for up to a given milliseconds.
244" With the +timers feature this waits for key-input by getchar(), Resume()
245" feeds key-input and resumes process. Return time waited in milliseconds.
246" Without +timers it uses simply :sleep.
247func Standby(msec)
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100248 if has('timers') && exists('*reltimefloat')
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200249 let start = reltime()
250 let g:_standby_timer = timer_start(a:msec, function('s:feedkeys'))
251 call getchar()
252 return float2nr(reltimefloat(reltime(start)) * 1000)
253 else
254 execute 'sleep ' a:msec . 'm'
255 return a:msec
256 endif
257endfunc
258
259func Resume()
260 if exists('g:_standby_timer')
261 call timer_stop(g:_standby_timer)
262 call s:feedkeys(0)
263 unlet g:_standby_timer
264 endif
265endfunc
266
267func s:feedkeys(timer)
268 call feedkeys('x', 'nt')
269endfunc
270
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000271" Get the name of the Vim executable that we expect has been build in the src
272" directory.
273func s:GetJustBuildVimExe()
274 if has("win32")
275 if !filereadable('..\vim.exe') && filereadable('..\vimd.exe')
276 " looks like the debug executable was intentionally build, so use it
277 return '..\vimd.exe'
278 endif
279 return '..\vim.exe'
280 endif
281 return '../vim'
282endfunc
283
Bram Moolenaar22286892020-11-05 20:50:51 +0100284" Get $VIMPROG to run the Vim executable.
Bram Moolenaar63182052017-10-07 20:03:23 +0200285" The Makefile writes it as the first line in the "vimcmd" file.
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000286" Falls back to the Vim executable in the src directory.
Bram Moolenaar63182052017-10-07 20:03:23 +0200287func GetVimProg()
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000288 if filereadable('vimcmd')
289 return readfile('vimcmd')[0]
Bram Moolenaar63182052017-10-07 20:03:23 +0200290 endif
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000291 echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
292
293 " Probably the script was sourced instead of running "make".
294 " We assume Vim was just build in the src directory then.
295 return s:GetJustBuildVimExe()
Bram Moolenaar63182052017-10-07 20:03:23 +0200296endfunc
297
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200298let g:valgrind_cnt = 1
299
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100300" Get the command to run Vim, with -u NONE and --not-a-term arguments.
Bram Moolenaar9d954202017-09-04 20:34:19 +0200301" If there is an argument use it instead of "NONE".
Bram Moolenaar9d954202017-09-04 20:34:19 +0200302func GetVimCommand(...)
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000303 if filereadable('vimcmd')
Bram Moolenaarda650582018-02-20 15:51:40 +0100304 let lines = readfile('vimcmd')
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000305 else
306 echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
307 let lines = [s:GetJustBuildVimExe()]
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100308 endif
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000309
Bram Moolenaar9d954202017-09-04 20:34:19 +0200310 if a:0 == 0
311 let name = 'NONE'
312 else
313 let name = a:1
314 endif
Bram Moolenaar63182052017-10-07 20:03:23 +0200315 " For Unix Makefile writes the command to use in the second line of the
316 " "vimcmd" file, including environment options.
317 " Other Makefiles just write the executable in the first line, so fall back
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200318 " to that if there is no second line or it is empty.
319 if len(lines) > 1 && lines[1] != ''
320 let cmd = lines[1]
321 else
322 let cmd = lines[0]
323 endif
324
Bram Moolenaar9d954202017-09-04 20:34:19 +0200325 let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '')
326 if cmd !~ '-u '. name
327 let cmd = cmd . ' -u ' . name
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100328 endif
329 let cmd .= ' --not-a-term'
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100330 let cmd .= ' --gui-dialog-file guidialogfile'
Christian Brabandt627c9502024-02-10 13:02:17 +0100331 " remove any environment variables
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +0100332 let cmd = substitute(cmd, '[A-Z_]\+=\S\+ *', '', 'g')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200333
334 " If using valgrind, make sure every run uses a different log file.
335 if cmd =~ 'valgrind.*--log-file='
Bram Moolenaar657a8262020-07-15 18:29:18 +0200336 let cmd = substitute(cmd, '--log-file=\(\S*\)', '--log-file=\1.' . g:valgrind_cnt, '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200337 let g:valgrind_cnt += 1
338 endif
339
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100340 return cmd
341endfunc
342
Bram Moolenaare366ed42022-06-19 20:13:56 +0100343" Return one when it looks like the tests are run with valgrind, which means
344" that everything is much slower.
345func RunningWithValgrind()
346 return GetVimCommand() =~ '\<valgrind\>'
347endfunc
348
Aliaksei Budaveif1d83c42024-11-02 15:51:14 +0100349func RunningAsan()
350 return exists("$ASAN_OPTIONS")
351endfunc
352
353func ValgrindOrAsan()
354 return RunningWithValgrind() || RunningAsan()
355endfun
356
Bram Moolenaarf1699962019-08-31 17:48:19 +0200357" Get the command to run Vim, with --clean instead of "-u NONE".
Bram Moolenaarda650582018-02-20 15:51:40 +0100358func GetVimCommandClean()
359 let cmd = GetVimCommand()
360 let cmd = substitute(cmd, '-u NONE', '--clean', '')
361 let cmd = substitute(cmd, '--not-a-term', '', '')
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200362
Bram Moolenaar0fdddee2019-09-01 15:26:23 +0200363 " Force using utf-8, Vim may pick up something else from the environment.
364 let cmd ..= ' --cmd "set enc=utf8" '
365
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200366 " Optionally run Vim under valgrind
367 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
368
Bram Moolenaarda650582018-02-20 15:51:40 +0100369 return cmd
370endfunc
371
Bram Moolenaar0d702022019-07-04 14:20:41 +0200372" Get the command to run Vim, with --clean, and force to run in terminal so it
373" won't start a new GUI.
374func GetVimCommandCleanTerm()
375 " Add -v to have gvim run in the terminal (if possible)
376 return GetVimCommandClean() .. ' -v '
377endfunc
378
Bram Moolenaar66459b72016-08-06 19:01:55 +0200379" Run Vim, using the "vimcmd" file and "-u NORC".
Bram Moolenaar3a938382016-08-07 16:36:40 +0200380" "before" is a list of Vim commands to be executed before loading plugins.
381" "after" is a list of Vim commands to be executed after loading plugins.
Bram Moolenaar66459b72016-08-06 19:01:55 +0200382" Plugins are not loaded, unless 'loadplugins' is set in "before".
383" Return 1 if Vim could be executed.
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200384func RunVim(before, after, arguments)
Bram Moolenaar7a9a5f42016-08-08 22:34:14 +0200385 return RunVimPiped(a:before, a:after, a:arguments, '')
Bram Moolenaar3a938382016-08-07 16:36:40 +0200386endfunc
387
388func RunVimPiped(before, after, arguments, pipecmd)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100389 let cmd = GetVimCommand()
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200390 let args = ''
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200391 if len(a:before) > 0
392 call writefile(a:before, 'Xbefore.vim')
393 let args .= ' --cmd "so Xbefore.vim"'
394 endif
395 if len(a:after) > 0
396 call writefile(a:after, 'Xafter.vim')
397 let args .= ' -S Xafter.vim'
398 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200399
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +0100400 " Optionally run Vim under valgrind
401 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
402
Christian Brabandt52eb0b82024-02-10 13:50:54 +0100403 exe "silent !" .. a:pipecmd .. ' ' .. cmd .. args .. ' ' .. a:arguments
Bram Moolenaar66459b72016-08-06 19:01:55 +0200404
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200405 if len(a:before) > 0
406 call delete('Xbefore.vim')
407 endif
408 if len(a:after) > 0
409 call delete('Xafter.vim')
410 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200411 return 1
412endfunc
Bram Moolenaar07282f02019-10-10 16:46:17 +0200413
414func IsRoot()
415 if !has('unix')
416 return v:false
417 elseif $USER == 'root' || system('id -un') =~ '\<root\>'
418 return v:true
419 endif
420 return v:false
421endfunc
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200422
Bram Moolenaar88e6cc22020-04-30 19:19:29 +0200423" Get all messages but drop the maintainer entry.
424func GetMessages()
425 redir => result
426 redraw | messages
427 redir END
428 let msg_list = split(result, "\n")
429 if msg_list->len() > 0 && msg_list[0] =~ 'Messages maintainer:'
430 return msg_list[1:]
431 endif
432 return msg_list
433endfunc
434
Bram Moolenaarab589462020-07-06 21:03:06 +0200435" Run the list of commands in 'cmds' and look for 'errstr' in exception.
436" Note that assert_fails() cannot be used in some places and this function
437" can be used.
438func AssertException(cmds, errstr)
439 let save_exception = ''
440 try
441 for cmd in a:cmds
442 exe cmd
443 endfor
444 catch
445 let save_exception = v:exception
446 endtry
447 call assert_match(a:errstr, save_exception)
448endfunc
449
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200450" vim: shiftwidth=2 sts=2 expandtab