blob: d373a6e7407b4b0cdda9bd5e20e686d7746749c1 [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
LemonBoycc766a82022-04-04 15:46:58 +010028 if executable('python')
29 let s:python = 'python'
30 elseif executable('python3')
31 let s:python = 'python3'
32 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')
62 exe 'silent !start cmd /c start "test_channel" ' . a:cmd
63 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])
116 catch
Bram Moolenaar06d32a02022-09-03 13:58:47 +0100117 call assert_report('Caught exception: "' . v:exception . '" in ' . v:throwpoint)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200118 finally
119 call s:kill_server(a:cmd)
120 endtry
121endfunc
122
123func s:kill_server(cmd)
124 if has('job')
125 if exists('g:currentJob')
126 call job_stop(g:currentJob)
127 unlet g:currentJob
128 endif
129 elseif has('win32')
130 let cmd = substitute(a:cmd, ".py", '', '')
131 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq ' . cmd . '"')
132 else
133 call system("pkill -f " . a:cmd)
134 endif
135endfunc
136
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200137" Wait for up to five seconds for "expr" to become true. "expr" can be a
Bram Moolenaar13deab82017-11-04 18:48:43 +0100138" stringified expression to evaluate, or a funcref without arguments.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200139" Using a lambda works best. Example:
140" call WaitFor({-> status == "ok"})
141"
Bram Moolenaar5d7ead32018-02-27 17:17:42 +0100142" A second argument can be used to specify a different timeout in msec.
Bram Moolenaar13deab82017-11-04 18:48:43 +0100143"
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200144" When successful the time slept is returned.
145" When running into the timeout an exception is thrown, thus the function does
146" not return.
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +0200147func WaitFor(expr, ...)
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200148 let timeout = get(a:000, 0, 5000)
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200149 let slept = s:WaitForCommon(a:expr, v:null, timeout)
150 if slept < 0
151 throw 'WaitFor() timed out after ' . timeout . ' msec'
152 endif
153 return slept
154endfunc
155
156" Wait for up to five seconds for "assert" to return zero. "assert" must be a
157" (lambda) function containing one assert function. Example:
158" call WaitForAssert({-> assert_equal("dead", job_status(job)})
159"
160" A second argument can be used to specify a different timeout in msec.
161"
162" Return zero for success, one for failure (like the assert function).
163func WaitForAssert(assert, ...)
164 let timeout = get(a:000, 0, 5000)
165 if s:WaitForCommon(v:null, a:assert, timeout) < 0
166 return 1
167 endif
168 return 0
169endfunc
170
171" Common implementation of WaitFor() and WaitForAssert().
172" Either "expr" or "assert" is not v:null
173" Return the waiting time for success, -1 for failure.
174func s:WaitForCommon(expr, assert, timeout)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200175 " using reltime() is more accurate, but not always available
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200176 let slept = 0
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100177 if exists('*reltimefloat')
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200178 let start = reltime()
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200179 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200180
181 while 1
182 if type(a:expr) == v:t_func
183 let success = a:expr()
184 elseif type(a:assert) == v:t_func
185 let success = a:assert() == 0
186 else
187 let success = eval(a:expr)
188 endif
189 if success
Bram Moolenaarc20e0d52017-11-02 18:19:19 +0100190 return slept
191 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200192
193 if slept >= a:timeout
194 break
195 endif
196 if type(a:assert) == v:t_func
197 " Remove the error added by the assert function.
198 call remove(v:errors, -1)
199 endif
200
201 sleep 10m
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100202 if exists('*reltimefloat')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200203 let slept = float2nr(reltimefloat(reltime(start)) * 1000)
204 else
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200205 let slept += 10
206 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200207 endwhile
208
209 return -1 " timed out
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200210endfunc
Bram Moolenaar66459b72016-08-06 19:01:55 +0200211
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200212
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200213" Wait for up to a given milliseconds.
214" With the +timers feature this waits for key-input by getchar(), Resume()
215" feeds key-input and resumes process. Return time waited in milliseconds.
216" Without +timers it uses simply :sleep.
217func Standby(msec)
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100218 if has('timers') && exists('*reltimefloat')
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200219 let start = reltime()
220 let g:_standby_timer = timer_start(a:msec, function('s:feedkeys'))
221 call getchar()
222 return float2nr(reltimefloat(reltime(start)) * 1000)
223 else
224 execute 'sleep ' a:msec . 'm'
225 return a:msec
226 endif
227endfunc
228
229func Resume()
230 if exists('g:_standby_timer')
231 call timer_stop(g:_standby_timer)
232 call s:feedkeys(0)
233 unlet g:_standby_timer
234 endif
235endfunc
236
237func s:feedkeys(timer)
238 call feedkeys('x', 'nt')
239endfunc
240
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000241" Get the name of the Vim executable that we expect has been build in the src
242" directory.
243func s:GetJustBuildVimExe()
244 if has("win32")
245 if !filereadable('..\vim.exe') && filereadable('..\vimd.exe')
246 " looks like the debug executable was intentionally build, so use it
247 return '..\vimd.exe'
248 endif
249 return '..\vim.exe'
250 endif
251 return '../vim'
252endfunc
253
Bram Moolenaar22286892020-11-05 20:50:51 +0100254" Get $VIMPROG to run the Vim executable.
Bram Moolenaar63182052017-10-07 20:03:23 +0200255" The Makefile writes it as the first line in the "vimcmd" file.
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000256" Falls back to the Vim executable in the src directory.
Bram Moolenaar63182052017-10-07 20:03:23 +0200257func GetVimProg()
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000258 if filereadable('vimcmd')
259 return readfile('vimcmd')[0]
Bram Moolenaar63182052017-10-07 20:03:23 +0200260 endif
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000261 echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
262
263 " Probably the script was sourced instead of running "make".
264 " We assume Vim was just build in the src directory then.
265 return s:GetJustBuildVimExe()
Bram Moolenaar63182052017-10-07 20:03:23 +0200266endfunc
267
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200268let g:valgrind_cnt = 1
269
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100270" Get the command to run Vim, with -u NONE and --not-a-term arguments.
Bram Moolenaar9d954202017-09-04 20:34:19 +0200271" If there is an argument use it instead of "NONE".
Bram Moolenaar9d954202017-09-04 20:34:19 +0200272func GetVimCommand(...)
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000273 if filereadable('vimcmd')
Bram Moolenaarda650582018-02-20 15:51:40 +0100274 let lines = readfile('vimcmd')
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000275 else
276 echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
277 let lines = [s:GetJustBuildVimExe()]
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100278 endif
Christopher Plewrightd55bfca2022-11-10 18:21:30 +0000279
Bram Moolenaar9d954202017-09-04 20:34:19 +0200280 if a:0 == 0
281 let name = 'NONE'
282 else
283 let name = a:1
284 endif
Bram Moolenaar63182052017-10-07 20:03:23 +0200285 " For Unix Makefile writes the command to use in the second line of the
286 " "vimcmd" file, including environment options.
287 " Other Makefiles just write the executable in the first line, so fall back
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200288 " to that if there is no second line or it is empty.
289 if len(lines) > 1 && lines[1] != ''
290 let cmd = lines[1]
291 else
292 let cmd = lines[0]
293 endif
294
Bram Moolenaar9d954202017-09-04 20:34:19 +0200295 let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '')
296 if cmd !~ '-u '. name
297 let cmd = cmd . ' -u ' . name
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100298 endif
299 let cmd .= ' --not-a-term'
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100300 let cmd .= ' --gui-dialog-file guidialogfile'
Bram Moolenaar41a82602019-07-14 21:54:26 +0200301 let cmd = substitute(cmd, 'VIMRUNTIME=\S\+', '', '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200302
303 " If using valgrind, make sure every run uses a different log file.
304 if cmd =~ 'valgrind.*--log-file='
Bram Moolenaar657a8262020-07-15 18:29:18 +0200305 let cmd = substitute(cmd, '--log-file=\(\S*\)', '--log-file=\1.' . g:valgrind_cnt, '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200306 let g:valgrind_cnt += 1
307 endif
308
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100309 return cmd
310endfunc
311
Bram Moolenaare366ed42022-06-19 20:13:56 +0100312" Return one when it looks like the tests are run with valgrind, which means
313" that everything is much slower.
314func RunningWithValgrind()
315 return GetVimCommand() =~ '\<valgrind\>'
316endfunc
317
Bram Moolenaarf1699962019-08-31 17:48:19 +0200318" Get the command to run Vim, with --clean instead of "-u NONE".
Bram Moolenaarda650582018-02-20 15:51:40 +0100319func GetVimCommandClean()
320 let cmd = GetVimCommand()
321 let cmd = substitute(cmd, '-u NONE', '--clean', '')
322 let cmd = substitute(cmd, '--not-a-term', '', '')
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200323
Bram Moolenaar0fdddee2019-09-01 15:26:23 +0200324 " Force using utf-8, Vim may pick up something else from the environment.
325 let cmd ..= ' --cmd "set enc=utf8" '
326
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200327 " Optionally run Vim under valgrind
328 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
329
Bram Moolenaarda650582018-02-20 15:51:40 +0100330 return cmd
331endfunc
332
Bram Moolenaar0d702022019-07-04 14:20:41 +0200333" Get the command to run Vim, with --clean, and force to run in terminal so it
334" won't start a new GUI.
335func GetVimCommandCleanTerm()
336 " Add -v to have gvim run in the terminal (if possible)
337 return GetVimCommandClean() .. ' -v '
338endfunc
339
Bram Moolenaar66459b72016-08-06 19:01:55 +0200340" Run Vim, using the "vimcmd" file and "-u NORC".
Bram Moolenaar3a938382016-08-07 16:36:40 +0200341" "before" is a list of Vim commands to be executed before loading plugins.
342" "after" is a list of Vim commands to be executed after loading plugins.
Bram Moolenaar66459b72016-08-06 19:01:55 +0200343" Plugins are not loaded, unless 'loadplugins' is set in "before".
344" Return 1 if Vim could be executed.
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200345func RunVim(before, after, arguments)
Bram Moolenaar7a9a5f42016-08-08 22:34:14 +0200346 return RunVimPiped(a:before, a:after, a:arguments, '')
Bram Moolenaar3a938382016-08-07 16:36:40 +0200347endfunc
348
349func RunVimPiped(before, after, arguments, pipecmd)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100350 let cmd = GetVimCommand()
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200351 let args = ''
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200352 if len(a:before) > 0
353 call writefile(a:before, 'Xbefore.vim')
354 let args .= ' --cmd "so Xbefore.vim"'
355 endif
356 if len(a:after) > 0
357 call writefile(a:after, 'Xafter.vim')
358 let args .= ' -S Xafter.vim'
359 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200360
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +0100361 " Optionally run Vim under valgrind
362 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
363
Bram Moolenaar3a938382016-08-07 16:36:40 +0200364 exe "silent !" . a:pipecmd . cmd . args . ' ' . a:arguments
Bram Moolenaar66459b72016-08-06 19:01:55 +0200365
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200366 if len(a:before) > 0
367 call delete('Xbefore.vim')
368 endif
369 if len(a:after) > 0
370 call delete('Xafter.vim')
371 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200372 return 1
373endfunc
Bram Moolenaar07282f02019-10-10 16:46:17 +0200374
375func IsRoot()
376 if !has('unix')
377 return v:false
378 elseif $USER == 'root' || system('id -un') =~ '\<root\>'
379 return v:true
380 endif
381 return v:false
382endfunc
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200383
Bram Moolenaar88e6cc22020-04-30 19:19:29 +0200384" Get all messages but drop the maintainer entry.
385func GetMessages()
386 redir => result
387 redraw | messages
388 redir END
389 let msg_list = split(result, "\n")
390 if msg_list->len() > 0 && msg_list[0] =~ 'Messages maintainer:'
391 return msg_list[1:]
392 endif
393 return msg_list
394endfunc
395
Bram Moolenaarab589462020-07-06 21:03:06 +0200396" Run the list of commands in 'cmds' and look for 'errstr' in exception.
397" Note that assert_fails() cannot be used in some places and this function
398" can be used.
399func AssertException(cmds, errstr)
400 let save_exception = ''
401 try
402 for cmd in a:cmds
403 exe cmd
404 endfor
405 catch
406 let save_exception = v:exception
407 endtry
408 call assert_match(a:errstr, save_exception)
409endfunc
410
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200411" vim: shiftwidth=2 sts=2 expandtab