blob: 0f0ffe878a7bc1c6788be959bfab72801214461e [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
101 " Wait for up to 2 seconds for the port number to be there.
102 let port = GetPort()
103 if port == 0
104 call assert_false(1, "Can't start " . a:cmd)
105 return
106 endif
107
108 call call(function(a:testfunc), [port])
109 catch
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100110 call assert_false(1, '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
Bram Moolenaar22286892020-11-05 20:50:51 +0100234" Get $VIMPROG to run the Vim executable.
Bram Moolenaar63182052017-10-07 20:03:23 +0200235" The Makefile writes it as the first line in the "vimcmd" file.
236func GetVimProg()
237 if !filereadable('vimcmd')
Bram Moolenaarda650582018-02-20 15:51:40 +0100238 " Assume the script was sourced instead of running "make".
239 return '../vim'
Bram Moolenaar63182052017-10-07 20:03:23 +0200240 endif
241 return readfile('vimcmd')[0]
242endfunc
243
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200244let g:valgrind_cnt = 1
245
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100246" Get the command to run Vim, with -u NONE and --not-a-term arguments.
Bram Moolenaar9d954202017-09-04 20:34:19 +0200247" If there is an argument use it instead of "NONE".
Bram Moolenaar9d954202017-09-04 20:34:19 +0200248func GetVimCommand(...)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100249 if !filereadable('vimcmd')
Bram Moolenaarda650582018-02-20 15:51:40 +0100250 echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
Christian Brabandtdfbdadc2022-05-05 20:46:47 +0100251 if !has("win32")
252 let lines = ['../vim']
253 else
254 let lines = ['..\vim.exe']
255 endif
Bram Moolenaarda650582018-02-20 15:51:40 +0100256 else
257 let lines = readfile('vimcmd')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100258 endif
Bram Moolenaar9d954202017-09-04 20:34:19 +0200259 if a:0 == 0
260 let name = 'NONE'
261 else
262 let name = a:1
263 endif
Bram Moolenaar63182052017-10-07 20:03:23 +0200264 " For Unix Makefile writes the command to use in the second line of the
265 " "vimcmd" file, including environment options.
266 " Other Makefiles just write the executable in the first line, so fall back
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200267 " to that if there is no second line or it is empty.
268 if len(lines) > 1 && lines[1] != ''
269 let cmd = lines[1]
270 else
271 let cmd = lines[0]
272 endif
273
Bram Moolenaar9d954202017-09-04 20:34:19 +0200274 let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '')
275 if cmd !~ '-u '. name
276 let cmd = cmd . ' -u ' . name
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100277 endif
278 let cmd .= ' --not-a-term'
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100279 let cmd .= ' --gui-dialog-file guidialogfile'
Bram Moolenaar41a82602019-07-14 21:54:26 +0200280 let cmd = substitute(cmd, 'VIMRUNTIME=\S\+', '', '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200281
282 " If using valgrind, make sure every run uses a different log file.
283 if cmd =~ 'valgrind.*--log-file='
Bram Moolenaar657a8262020-07-15 18:29:18 +0200284 let cmd = substitute(cmd, '--log-file=\(\S*\)', '--log-file=\1.' . g:valgrind_cnt, '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200285 let g:valgrind_cnt += 1
286 endif
287
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100288 return cmd
289endfunc
290
Bram Moolenaare366ed42022-06-19 20:13:56 +0100291" Return one when it looks like the tests are run with valgrind, which means
292" that everything is much slower.
293func RunningWithValgrind()
294 return GetVimCommand() =~ '\<valgrind\>'
295endfunc
296
Bram Moolenaarf1699962019-08-31 17:48:19 +0200297" Get the command to run Vim, with --clean instead of "-u NONE".
Bram Moolenaarda650582018-02-20 15:51:40 +0100298func GetVimCommandClean()
299 let cmd = GetVimCommand()
300 let cmd = substitute(cmd, '-u NONE', '--clean', '')
301 let cmd = substitute(cmd, '--not-a-term', '', '')
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200302
Bram Moolenaar0fdddee2019-09-01 15:26:23 +0200303 " Force using utf-8, Vim may pick up something else from the environment.
304 let cmd ..= ' --cmd "set enc=utf8" '
305
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200306 " Optionally run Vim under valgrind
307 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
308
Bram Moolenaarda650582018-02-20 15:51:40 +0100309 return cmd
310endfunc
311
Bram Moolenaar0d702022019-07-04 14:20:41 +0200312" Get the command to run Vim, with --clean, and force to run in terminal so it
313" won't start a new GUI.
314func GetVimCommandCleanTerm()
315 " Add -v to have gvim run in the terminal (if possible)
316 return GetVimCommandClean() .. ' -v '
317endfunc
318
Bram Moolenaar66459b72016-08-06 19:01:55 +0200319" Run Vim, using the "vimcmd" file and "-u NORC".
Bram Moolenaar3a938382016-08-07 16:36:40 +0200320" "before" is a list of Vim commands to be executed before loading plugins.
321" "after" is a list of Vim commands to be executed after loading plugins.
Bram Moolenaar66459b72016-08-06 19:01:55 +0200322" Plugins are not loaded, unless 'loadplugins' is set in "before".
323" Return 1 if Vim could be executed.
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200324func RunVim(before, after, arguments)
Bram Moolenaar7a9a5f42016-08-08 22:34:14 +0200325 return RunVimPiped(a:before, a:after, a:arguments, '')
Bram Moolenaar3a938382016-08-07 16:36:40 +0200326endfunc
327
328func RunVimPiped(before, after, arguments, pipecmd)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100329 let cmd = GetVimCommand()
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200330 let args = ''
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200331 if len(a:before) > 0
332 call writefile(a:before, 'Xbefore.vim')
333 let args .= ' --cmd "so Xbefore.vim"'
334 endif
335 if len(a:after) > 0
336 call writefile(a:after, 'Xafter.vim')
337 let args .= ' -S Xafter.vim'
338 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200339
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +0100340 " Optionally run Vim under valgrind
341 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
342
Bram Moolenaar3a938382016-08-07 16:36:40 +0200343 exe "silent !" . a:pipecmd . cmd . args . ' ' . a:arguments
Bram Moolenaar66459b72016-08-06 19:01:55 +0200344
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200345 if len(a:before) > 0
346 call delete('Xbefore.vim')
347 endif
348 if len(a:after) > 0
349 call delete('Xafter.vim')
350 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200351 return 1
352endfunc
Bram Moolenaar07282f02019-10-10 16:46:17 +0200353
354func IsRoot()
355 if !has('unix')
356 return v:false
357 elseif $USER == 'root' || system('id -un') =~ '\<root\>'
358 return v:true
359 endif
360 return v:false
361endfunc
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200362
Bram Moolenaar88e6cc22020-04-30 19:19:29 +0200363" Get all messages but drop the maintainer entry.
364func GetMessages()
365 redir => result
366 redraw | messages
367 redir END
368 let msg_list = split(result, "\n")
369 if msg_list->len() > 0 && msg_list[0] =~ 'Messages maintainer:'
370 return msg_list[1:]
371 endif
372 return msg_list
373endfunc
374
Bram Moolenaarab589462020-07-06 21:03:06 +0200375" Run the list of commands in 'cmds' and look for 'errstr' in exception.
376" Note that assert_fails() cannot be used in some places and this function
377" can be used.
378func AssertException(cmds, errstr)
379 let save_exception = ''
380 try
381 for cmd in a:cmds
382 exe cmd
383 endfor
384 catch
385 let save_exception = v:exception
386 endtry
387 call assert_match(a:errstr, save_exception)
388endfunc
389
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200390" vim: shiftwidth=2 sts=2 expandtab