blob: 8ae6fa0213fbb8b0f8e93fe928ad26d271ba8636 [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 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
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200139" Wait for up to five seconds for "expr" to become true. "expr" can be a
Bram Moolenaar13deab82017-11-04 18:48:43 +0100140" stringified expression to evaluate, or a funcref without arguments.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200141" Using a lambda works best. Example:
142" call WaitFor({-> status == "ok"})
143"
Bram Moolenaar5d7ead32018-02-27 17:17:42 +0100144" A second argument can be used to specify a different timeout in msec.
Bram Moolenaar13deab82017-11-04 18:48:43 +0100145"
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200146" When successful the time slept is returned.
147" When running into the timeout an exception is thrown, thus the function does
148" not return.
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +0200149func WaitFor(expr, ...)
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200150 let timeout = get(a:000, 0, 5000)
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200151 let slept = s:WaitForCommon(a:expr, v:null, timeout)
152 if slept < 0
153 throw 'WaitFor() timed out after ' . timeout . ' msec'
154 endif
155 return slept
156endfunc
157
158" Wait for up to five seconds for "assert" to return zero. "assert" must be a
159" (lambda) function containing one assert function. Example:
160" call WaitForAssert({-> assert_equal("dead", job_status(job)})
161"
162" A second argument can be used to specify a different timeout in msec.
163"
164" Return zero for success, one for failure (like the assert function).
165func WaitForAssert(assert, ...)
166 let timeout = get(a:000, 0, 5000)
167 if s:WaitForCommon(v:null, a:assert, timeout) < 0
168 return 1
169 endif
170 return 0
171endfunc
172
173" Common implementation of WaitFor() and WaitForAssert().
174" Either "expr" or "assert" is not v:null
175" Return the waiting time for success, -1 for failure.
176func s:WaitForCommon(expr, assert, timeout)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200177 " using reltime() is more accurate, but not always available
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200178 let slept = 0
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100179 if exists('*reltimefloat')
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200180 let start = reltime()
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200181 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200182
183 while 1
184 if type(a:expr) == v:t_func
185 let success = a:expr()
186 elseif type(a:assert) == v:t_func
187 let success = a:assert() == 0
188 else
189 let success = eval(a:expr)
190 endif
191 if success
Bram Moolenaarc20e0d52017-11-02 18:19:19 +0100192 return slept
193 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200194
195 if slept >= a:timeout
196 break
197 endif
198 if type(a:assert) == v:t_func
199 " Remove the error added by the assert function.
200 call remove(v:errors, -1)
201 endif
202
203 sleep 10m
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100204 if exists('*reltimefloat')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200205 let slept = float2nr(reltimefloat(reltime(start)) * 1000)
206 else
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200207 let slept += 10
208 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200209 endwhile
210
211 return -1 " timed out
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200212endfunc
Bram Moolenaar66459b72016-08-06 19:01:55 +0200213
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200214
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200215" Wait for up to a given milliseconds.
216" With the +timers feature this waits for key-input by getchar(), Resume()
217" feeds key-input and resumes process. Return time waited in milliseconds.
218" Without +timers it uses simply :sleep.
219func Standby(msec)
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100220 if has('timers') && exists('*reltimefloat')
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200221 let start = reltime()
222 let g:_standby_timer = timer_start(a:msec, function('s:feedkeys'))
223 call getchar()
224 return float2nr(reltimefloat(reltime(start)) * 1000)
225 else
226 execute 'sleep ' a:msec . 'm'
227 return a:msec
228 endif
229endfunc
230
231func Resume()
232 if exists('g:_standby_timer')
233 call timer_stop(g:_standby_timer)
234 call s:feedkeys(0)
235 unlet g:_standby_timer
236 endif
237endfunc
238
239func s:feedkeys(timer)
240 call feedkeys('x', 'nt')
241endfunc
242
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000243" Get the name of the Vim executable that we expect has been build in the src
244" directory.
245func s:GetJustBuildVimExe()
246 if has("win32")
247 if !filereadable('..\vim.exe') && filereadable('..\vimd.exe')
248 " looks like the debug executable was intentionally build, so use it
249 return '..\vimd.exe'
250 endif
251 return '..\vim.exe'
252 endif
253 return '../vim'
254endfunc
255
Bram Moolenaar22286892020-11-05 20:50:51 +0100256" Get $VIMPROG to run the Vim executable.
Bram Moolenaar63182052017-10-07 20:03:23 +0200257" The Makefile writes it as the first line in the "vimcmd" file.
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000258" Falls back to the Vim executable in the src directory.
Bram Moolenaar63182052017-10-07 20:03:23 +0200259func GetVimProg()
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000260 if filereadable('vimcmd')
261 return readfile('vimcmd')[0]
Bram Moolenaar63182052017-10-07 20:03:23 +0200262 endif
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000263 echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
264
265 " Probably the script was sourced instead of running "make".
266 " We assume Vim was just build in the src directory then.
267 return s:GetJustBuildVimExe()
Bram Moolenaar63182052017-10-07 20:03:23 +0200268endfunc
269
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200270let g:valgrind_cnt = 1
271
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100272" Get the command to run Vim, with -u NONE and --not-a-term arguments.
Bram Moolenaar9d954202017-09-04 20:34:19 +0200273" If there is an argument use it instead of "NONE".
Bram Moolenaar9d954202017-09-04 20:34:19 +0200274func GetVimCommand(...)
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000275 if filereadable('vimcmd')
Bram Moolenaarda650582018-02-20 15:51:40 +0100276 let lines = readfile('vimcmd')
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000277 else
278 echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
279 let lines = [s:GetJustBuildVimExe()]
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100280 endif
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000281
Bram Moolenaar9d954202017-09-04 20:34:19 +0200282 if a:0 == 0
283 let name = 'NONE'
284 else
285 let name = a:1
286 endif
Bram Moolenaar63182052017-10-07 20:03:23 +0200287 " For Unix Makefile writes the command to use in the second line of the
288 " "vimcmd" file, including environment options.
289 " Other Makefiles just write the executable in the first line, so fall back
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200290 " to that if there is no second line or it is empty.
291 if len(lines) > 1 && lines[1] != ''
292 let cmd = lines[1]
293 else
294 let cmd = lines[0]
295 endif
296
Bram Moolenaar9d954202017-09-04 20:34:19 +0200297 let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '')
298 if cmd !~ '-u '. name
299 let cmd = cmd . ' -u ' . name
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100300 endif
301 let cmd .= ' --not-a-term'
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100302 let cmd .= ' --gui-dialog-file guidialogfile'
Christian Brabandt627c9502024-02-10 13:02:17 +0100303 " remove any environment variables
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +0100304 let cmd = substitute(cmd, '[A-Z_]\+=\S\+ *', '', 'g')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200305
306 " If using valgrind, make sure every run uses a different log file.
307 if cmd =~ 'valgrind.*--log-file='
Bram Moolenaar657a8262020-07-15 18:29:18 +0200308 let cmd = substitute(cmd, '--log-file=\(\S*\)', '--log-file=\1.' . g:valgrind_cnt, '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200309 let g:valgrind_cnt += 1
310 endif
311
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100312 return cmd
313endfunc
314
Bram Moolenaare366ed42022-06-19 20:13:56 +0100315" Return one when it looks like the tests are run with valgrind, which means
316" that everything is much slower.
317func RunningWithValgrind()
318 return GetVimCommand() =~ '\<valgrind\>'
319endfunc
320
Aliaksei Budaveif1d83c42024-11-02 15:51:14 +0100321func RunningAsan()
322 return exists("$ASAN_OPTIONS")
323endfunc
324
325func ValgrindOrAsan()
326 return RunningWithValgrind() || RunningAsan()
327endfun
328
Bram Moolenaarf1699962019-08-31 17:48:19 +0200329" Get the command to run Vim, with --clean instead of "-u NONE".
Bram Moolenaarda650582018-02-20 15:51:40 +0100330func GetVimCommandClean()
331 let cmd = GetVimCommand()
332 let cmd = substitute(cmd, '-u NONE', '--clean', '')
333 let cmd = substitute(cmd, '--not-a-term', '', '')
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200334
Bram Moolenaar0fdddee2019-09-01 15:26:23 +0200335 " Force using utf-8, Vim may pick up something else from the environment.
336 let cmd ..= ' --cmd "set enc=utf8" '
337
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200338 " Optionally run Vim under valgrind
339 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
340
Bram Moolenaarda650582018-02-20 15:51:40 +0100341 return cmd
342endfunc
343
Bram Moolenaar0d702022019-07-04 14:20:41 +0200344" Get the command to run Vim, with --clean, and force to run in terminal so it
345" won't start a new GUI.
346func GetVimCommandCleanTerm()
347 " Add -v to have gvim run in the terminal (if possible)
348 return GetVimCommandClean() .. ' -v '
349endfunc
350
Bram Moolenaar66459b72016-08-06 19:01:55 +0200351" Run Vim, using the "vimcmd" file and "-u NORC".
Bram Moolenaar3a938382016-08-07 16:36:40 +0200352" "before" is a list of Vim commands to be executed before loading plugins.
353" "after" is a list of Vim commands to be executed after loading plugins.
Bram Moolenaar66459b72016-08-06 19:01:55 +0200354" Plugins are not loaded, unless 'loadplugins' is set in "before".
355" Return 1 if Vim could be executed.
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200356func RunVim(before, after, arguments)
Bram Moolenaar7a9a5f42016-08-08 22:34:14 +0200357 return RunVimPiped(a:before, a:after, a:arguments, '')
Bram Moolenaar3a938382016-08-07 16:36:40 +0200358endfunc
359
360func RunVimPiped(before, after, arguments, pipecmd)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100361 let cmd = GetVimCommand()
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200362 let args = ''
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200363 if len(a:before) > 0
364 call writefile(a:before, 'Xbefore.vim')
365 let args .= ' --cmd "so Xbefore.vim"'
366 endif
367 if len(a:after) > 0
368 call writefile(a:after, 'Xafter.vim')
369 let args .= ' -S Xafter.vim'
370 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200371
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +0100372 " Optionally run Vim under valgrind
373 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
374
Christian Brabandt52eb0b82024-02-10 13:50:54 +0100375 exe "silent !" .. a:pipecmd .. ' ' .. cmd .. args .. ' ' .. a:arguments
Bram Moolenaar66459b72016-08-06 19:01:55 +0200376
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200377 if len(a:before) > 0
378 call delete('Xbefore.vim')
379 endif
380 if len(a:after) > 0
381 call delete('Xafter.vim')
382 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200383 return 1
384endfunc
Bram Moolenaar07282f02019-10-10 16:46:17 +0200385
386func IsRoot()
387 if !has('unix')
388 return v:false
389 elseif $USER == 'root' || system('id -un') =~ '\<root\>'
390 return v:true
391 endif
392 return v:false
393endfunc
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200394
Bram Moolenaar88e6cc22020-04-30 19:19:29 +0200395" Get all messages but drop the maintainer entry.
396func GetMessages()
397 redir => result
398 redraw | messages
399 redir END
400 let msg_list = split(result, "\n")
401 if msg_list->len() > 0 && msg_list[0] =~ 'Messages maintainer:'
402 return msg_list[1:]
403 endif
404 return msg_list
405endfunc
406
Bram Moolenaarab589462020-07-06 21:03:06 +0200407" Run the list of commands in 'cmds' and look for 'errstr' in exception.
408" Note that assert_fails() cannot be used in some places and this function
409" can be used.
410func AssertException(cmds, errstr)
411 let save_exception = ''
412 try
413 for cmd in a:cmds
414 exe cmd
415 endfor
416 catch
417 let save_exception = v:exception
418 endtry
419 call assert_match(a:errstr, save_exception)
420endfunc
421
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200422" vim: shiftwidth=2 sts=2 expandtab