blob: 8e3dfc9c3a1d623c29b340b4600af6f22ac9ec93 [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 Moolenaar453ce7c2018-10-12 22:15:12 +020065 " with 200 it sometimes failed
66 for i in range(400)
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.'
251 let lines = ['../vim']
252 else
253 let lines = readfile('vimcmd')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100254 endif
Bram Moolenaar9d954202017-09-04 20:34:19 +0200255 if a:0 == 0
256 let name = 'NONE'
257 else
258 let name = a:1
259 endif
Bram Moolenaar63182052017-10-07 20:03:23 +0200260 " For Unix Makefile writes the command to use in the second line of the
261 " "vimcmd" file, including environment options.
262 " Other Makefiles just write the executable in the first line, so fall back
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200263 " to that if there is no second line or it is empty.
264 if len(lines) > 1 && lines[1] != ''
265 let cmd = lines[1]
266 else
267 let cmd = lines[0]
268 endif
269
Bram Moolenaar9d954202017-09-04 20:34:19 +0200270 let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '')
271 if cmd !~ '-u '. name
272 let cmd = cmd . ' -u ' . name
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100273 endif
274 let cmd .= ' --not-a-term'
Bram Moolenaar41a82602019-07-14 21:54:26 +0200275 let cmd = substitute(cmd, 'VIMRUNTIME=\S\+', '', '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200276
277 " If using valgrind, make sure every run uses a different log file.
278 if cmd =~ 'valgrind.*--log-file='
Bram Moolenaar657a8262020-07-15 18:29:18 +0200279 let cmd = substitute(cmd, '--log-file=\(\S*\)', '--log-file=\1.' . g:valgrind_cnt, '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200280 let g:valgrind_cnt += 1
281 endif
282
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100283 return cmd
284endfunc
285
Bram Moolenaarf1699962019-08-31 17:48:19 +0200286" Get the command to run Vim, with --clean instead of "-u NONE".
Bram Moolenaarda650582018-02-20 15:51:40 +0100287func GetVimCommandClean()
288 let cmd = GetVimCommand()
289 let cmd = substitute(cmd, '-u NONE', '--clean', '')
290 let cmd = substitute(cmd, '--not-a-term', '', '')
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200291
Bram Moolenaar0fdddee2019-09-01 15:26:23 +0200292 " Force using utf-8, Vim may pick up something else from the environment.
293 let cmd ..= ' --cmd "set enc=utf8" '
294
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200295 " Optionally run Vim under valgrind
296 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
297
Bram Moolenaarda650582018-02-20 15:51:40 +0100298 return cmd
299endfunc
300
Bram Moolenaar0d702022019-07-04 14:20:41 +0200301" Get the command to run Vim, with --clean, and force to run in terminal so it
302" won't start a new GUI.
303func GetVimCommandCleanTerm()
304 " Add -v to have gvim run in the terminal (if possible)
305 return GetVimCommandClean() .. ' -v '
306endfunc
307
Bram Moolenaar66459b72016-08-06 19:01:55 +0200308" Run Vim, using the "vimcmd" file and "-u NORC".
Bram Moolenaar3a938382016-08-07 16:36:40 +0200309" "before" is a list of Vim commands to be executed before loading plugins.
310" "after" is a list of Vim commands to be executed after loading plugins.
Bram Moolenaar66459b72016-08-06 19:01:55 +0200311" Plugins are not loaded, unless 'loadplugins' is set in "before".
312" Return 1 if Vim could be executed.
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200313func RunVim(before, after, arguments)
Bram Moolenaar7a9a5f42016-08-08 22:34:14 +0200314 return RunVimPiped(a:before, a:after, a:arguments, '')
Bram Moolenaar3a938382016-08-07 16:36:40 +0200315endfunc
316
317func RunVimPiped(before, after, arguments, pipecmd)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100318 let cmd = GetVimCommand()
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200319 let args = ''
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200320 if len(a:before) > 0
321 call writefile(a:before, 'Xbefore.vim')
322 let args .= ' --cmd "so Xbefore.vim"'
323 endif
324 if len(a:after) > 0
325 call writefile(a:after, 'Xafter.vim')
326 let args .= ' -S Xafter.vim'
327 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200328
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +0100329 " Optionally run Vim under valgrind
330 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
331
Bram Moolenaar3a938382016-08-07 16:36:40 +0200332 exe "silent !" . a:pipecmd . cmd . args . ' ' . a:arguments
Bram Moolenaar66459b72016-08-06 19:01:55 +0200333
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200334 if len(a:before) > 0
335 call delete('Xbefore.vim')
336 endif
337 if len(a:after) > 0
338 call delete('Xafter.vim')
339 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200340 return 1
341endfunc
Bram Moolenaar07282f02019-10-10 16:46:17 +0200342
343func IsRoot()
344 if !has('unix')
345 return v:false
346 elseif $USER == 'root' || system('id -un') =~ '\<root\>'
347 return v:true
348 endif
349 return v:false
350endfunc
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200351
Bram Moolenaar88e6cc22020-04-30 19:19:29 +0200352" Get all messages but drop the maintainer entry.
353func GetMessages()
354 redir => result
355 redraw | messages
356 redir END
357 let msg_list = split(result, "\n")
358 if msg_list->len() > 0 && msg_list[0] =~ 'Messages maintainer:'
359 return msg_list[1:]
360 endif
361 return msg_list
362endfunc
363
Bram Moolenaarab589462020-07-06 21:03:06 +0200364" Run the list of commands in 'cmds' and look for 'errstr' in exception.
365" Note that assert_fails() cannot be used in some places and this function
366" can be used.
367func AssertException(cmds, errstr)
368 let save_exception = ''
369 try
370 for cmd in a:cmds
371 exe cmd
372 endfor
373 catch
374 let save_exception = v:exception
375 endtry
376 call assert_match(a:errstr, save_exception)
377endfunc
378
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200379" vim: shiftwidth=2 sts=2 expandtab