blob: 01d741c08e29424f4e4412d0a5ba927e31e930a7 [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.
18 if !(executable('python') && (has('job') || executable('pkill')))
19 return ''
20 endif
21 let s:python = 'python'
22 elseif has('win32')
23 " Use Python Launcher for Windows (py.exe) if available.
Bram Moolenaar1bb0da22021-05-02 19:15:05 +020024 " NOTE: if you get a "Python was not found" error, disable the Python
25 " shortcuts in "Windows menu / Settings / Manage App Execution Aliases".
Bram Moolenaar321efdd2016-07-15 17:09:11 +020026 if executable('py.exe')
27 let s:python = 'py.exe'
28 elseif executable('python.exe')
29 let s:python = 'python.exe'
30 else
31 return ''
32 endif
33 else
34 return ''
35 endif
36 return s:python
37endfunc
38
39" Run "cmd". Returns the job if using a job.
40func RunCommand(cmd)
Bram Moolenaar4a070cc2020-05-07 18:16:35 +020041 " Running an external command can occasionally be slow or fail.
42 let g:test_is_flaky = 1
43
Bram Moolenaar321efdd2016-07-15 17:09:11 +020044 let job = 0
45 if has('job')
46 let job = job_start(a:cmd, {"stoponexit": "hup"})
47 call job_setoptions(job, {"stoponexit": "kill"})
48 elseif has('win32')
49 exe 'silent !start cmd /c start "test_channel" ' . a:cmd
50 else
51 exe 'silent !' . a:cmd . '&'
52 endif
53 return job
54endfunc
55
56" Read the port number from the Xportnr file.
57func GetPort()
58 let l = []
Bram Moolenaar453ce7c2018-10-12 22:15:12 +020059 " with 200 it sometimes failed
60 for i in range(400)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020061 try
62 let l = readfile("Xportnr")
63 catch
64 endtry
65 if len(l) >= 1
66 break
67 endif
68 sleep 10m
69 endfor
70 call delete("Xportnr")
71
72 if len(l) == 0
73 " Can't make the connection, give up.
74 return 0
75 endif
76 return l[0]
77endfunc
78
79" Run a Python server for "cmd" and call "testfunc".
80" Always kills the server before returning.
81func RunServer(cmd, testfunc, args)
82 " The Python program writes the port number in Xportnr.
83 call delete("Xportnr")
84
85 if len(a:args) == 1
86 let arg = ' ' . a:args[0]
87 else
88 let arg = ''
89 endif
90 let pycmd = s:python . " " . a:cmd . arg
91
92 try
93 let g:currentJob = RunCommand(pycmd)
94
95 " Wait for up to 2 seconds for the port number to be there.
96 let port = GetPort()
97 if port == 0
98 call assert_false(1, "Can't start " . a:cmd)
99 return
100 endif
101
102 call call(function(a:testfunc), [port])
103 catch
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100104 call assert_false(1, 'Caught exception: "' . v:exception . '" in ' . v:throwpoint)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200105 finally
106 call s:kill_server(a:cmd)
107 endtry
108endfunc
109
110func s:kill_server(cmd)
111 if has('job')
112 if exists('g:currentJob')
113 call job_stop(g:currentJob)
114 unlet g:currentJob
115 endif
116 elseif has('win32')
117 let cmd = substitute(a:cmd, ".py", '', '')
118 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq ' . cmd . '"')
119 else
120 call system("pkill -f " . a:cmd)
121 endif
122endfunc
123
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200124" Wait for up to five seconds for "expr" to become true. "expr" can be a
Bram Moolenaar13deab82017-11-04 18:48:43 +0100125" stringified expression to evaluate, or a funcref without arguments.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200126" Using a lambda works best. Example:
127" call WaitFor({-> status == "ok"})
128"
Bram Moolenaar5d7ead32018-02-27 17:17:42 +0100129" A second argument can be used to specify a different timeout in msec.
Bram Moolenaar13deab82017-11-04 18:48:43 +0100130"
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200131" When successful the time slept is returned.
132" When running into the timeout an exception is thrown, thus the function does
133" not return.
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +0200134func WaitFor(expr, ...)
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200135 let timeout = get(a:000, 0, 5000)
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200136 let slept = s:WaitForCommon(a:expr, v:null, timeout)
137 if slept < 0
138 throw 'WaitFor() timed out after ' . timeout . ' msec'
139 endif
140 return slept
141endfunc
142
143" Wait for up to five seconds for "assert" to return zero. "assert" must be a
144" (lambda) function containing one assert function. Example:
145" call WaitForAssert({-> assert_equal("dead", job_status(job)})
146"
147" A second argument can be used to specify a different timeout in msec.
148"
149" Return zero for success, one for failure (like the assert function).
150func WaitForAssert(assert, ...)
151 let timeout = get(a:000, 0, 5000)
152 if s:WaitForCommon(v:null, a:assert, timeout) < 0
153 return 1
154 endif
155 return 0
156endfunc
157
158" Common implementation of WaitFor() and WaitForAssert().
159" Either "expr" or "assert" is not v:null
160" Return the waiting time for success, -1 for failure.
161func s:WaitForCommon(expr, assert, timeout)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200162 " using reltime() is more accurate, but not always available
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200163 let slept = 0
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100164 if exists('*reltimefloat')
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200165 let start = reltime()
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200166 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200167
168 while 1
169 if type(a:expr) == v:t_func
170 let success = a:expr()
171 elseif type(a:assert) == v:t_func
172 let success = a:assert() == 0
173 else
174 let success = eval(a:expr)
175 endif
176 if success
Bram Moolenaarc20e0d52017-11-02 18:19:19 +0100177 return slept
178 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200179
180 if slept >= a:timeout
181 break
182 endif
183 if type(a:assert) == v:t_func
184 " Remove the error added by the assert function.
185 call remove(v:errors, -1)
186 endif
187
188 sleep 10m
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100189 if exists('*reltimefloat')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200190 let slept = float2nr(reltimefloat(reltime(start)) * 1000)
191 else
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200192 let slept += 10
193 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200194 endwhile
195
196 return -1 " timed out
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200197endfunc
Bram Moolenaar66459b72016-08-06 19:01:55 +0200198
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200199
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200200" Wait for up to a given milliseconds.
201" With the +timers feature this waits for key-input by getchar(), Resume()
202" feeds key-input and resumes process. Return time waited in milliseconds.
203" Without +timers it uses simply :sleep.
204func Standby(msec)
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100205 if has('timers') && exists('*reltimefloat')
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200206 let start = reltime()
207 let g:_standby_timer = timer_start(a:msec, function('s:feedkeys'))
208 call getchar()
209 return float2nr(reltimefloat(reltime(start)) * 1000)
210 else
211 execute 'sleep ' a:msec . 'm'
212 return a:msec
213 endif
214endfunc
215
216func Resume()
217 if exists('g:_standby_timer')
218 call timer_stop(g:_standby_timer)
219 call s:feedkeys(0)
220 unlet g:_standby_timer
221 endif
222endfunc
223
224func s:feedkeys(timer)
225 call feedkeys('x', 'nt')
226endfunc
227
Bram Moolenaar22286892020-11-05 20:50:51 +0100228" Get $VIMPROG to run the Vim executable.
Bram Moolenaar63182052017-10-07 20:03:23 +0200229" The Makefile writes it as the first line in the "vimcmd" file.
230func GetVimProg()
231 if !filereadable('vimcmd')
Bram Moolenaarda650582018-02-20 15:51:40 +0100232 " Assume the script was sourced instead of running "make".
233 return '../vim'
Bram Moolenaar63182052017-10-07 20:03:23 +0200234 endif
235 return readfile('vimcmd')[0]
236endfunc
237
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200238let g:valgrind_cnt = 1
239
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100240" Get the command to run Vim, with -u NONE and --not-a-term arguments.
Bram Moolenaar9d954202017-09-04 20:34:19 +0200241" If there is an argument use it instead of "NONE".
Bram Moolenaar9d954202017-09-04 20:34:19 +0200242func GetVimCommand(...)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100243 if !filereadable('vimcmd')
Bram Moolenaarda650582018-02-20 15:51:40 +0100244 echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
245 let lines = ['../vim']
246 else
247 let lines = readfile('vimcmd')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100248 endif
Bram Moolenaar9d954202017-09-04 20:34:19 +0200249 if a:0 == 0
250 let name = 'NONE'
251 else
252 let name = a:1
253 endif
Bram Moolenaar63182052017-10-07 20:03:23 +0200254 " For Unix Makefile writes the command to use in the second line of the
255 " "vimcmd" file, including environment options.
256 " Other Makefiles just write the executable in the first line, so fall back
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200257 " to that if there is no second line or it is empty.
258 if len(lines) > 1 && lines[1] != ''
259 let cmd = lines[1]
260 else
261 let cmd = lines[0]
262 endif
263
Bram Moolenaar9d954202017-09-04 20:34:19 +0200264 let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '')
265 if cmd !~ '-u '. name
266 let cmd = cmd . ' -u ' . name
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100267 endif
268 let cmd .= ' --not-a-term'
Bram Moolenaar41a82602019-07-14 21:54:26 +0200269 let cmd = substitute(cmd, 'VIMRUNTIME=\S\+', '', '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200270
271 " If using valgrind, make sure every run uses a different log file.
272 if cmd =~ 'valgrind.*--log-file='
Bram Moolenaar657a8262020-07-15 18:29:18 +0200273 let cmd = substitute(cmd, '--log-file=\(\S*\)', '--log-file=\1.' . g:valgrind_cnt, '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200274 let g:valgrind_cnt += 1
275 endif
276
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100277 return cmd
278endfunc
279
Bram Moolenaarf1699962019-08-31 17:48:19 +0200280" Get the command to run Vim, with --clean instead of "-u NONE".
Bram Moolenaarda650582018-02-20 15:51:40 +0100281func GetVimCommandClean()
282 let cmd = GetVimCommand()
283 let cmd = substitute(cmd, '-u NONE', '--clean', '')
284 let cmd = substitute(cmd, '--not-a-term', '', '')
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200285
Bram Moolenaar0fdddee2019-09-01 15:26:23 +0200286 " Force using utf-8, Vim may pick up something else from the environment.
287 let cmd ..= ' --cmd "set enc=utf8" '
288
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200289 " Optionally run Vim under valgrind
290 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
291
Bram Moolenaarda650582018-02-20 15:51:40 +0100292 return cmd
293endfunc
294
Bram Moolenaar0d702022019-07-04 14:20:41 +0200295" Get the command to run Vim, with --clean, and force to run in terminal so it
296" won't start a new GUI.
297func GetVimCommandCleanTerm()
298 " Add -v to have gvim run in the terminal (if possible)
299 return GetVimCommandClean() .. ' -v '
300endfunc
301
Bram Moolenaar66459b72016-08-06 19:01:55 +0200302" Run Vim, using the "vimcmd" file and "-u NORC".
Bram Moolenaar3a938382016-08-07 16:36:40 +0200303" "before" is a list of Vim commands to be executed before loading plugins.
304" "after" is a list of Vim commands to be executed after loading plugins.
Bram Moolenaar66459b72016-08-06 19:01:55 +0200305" Plugins are not loaded, unless 'loadplugins' is set in "before".
306" Return 1 if Vim could be executed.
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200307func RunVim(before, after, arguments)
Bram Moolenaar7a9a5f42016-08-08 22:34:14 +0200308 return RunVimPiped(a:before, a:after, a:arguments, '')
Bram Moolenaar3a938382016-08-07 16:36:40 +0200309endfunc
310
311func RunVimPiped(before, after, arguments, pipecmd)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100312 let cmd = GetVimCommand()
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200313 let args = ''
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200314 if len(a:before) > 0
315 call writefile(a:before, 'Xbefore.vim')
316 let args .= ' --cmd "so Xbefore.vim"'
317 endif
318 if len(a:after) > 0
319 call writefile(a:after, 'Xafter.vim')
320 let args .= ' -S Xafter.vim'
321 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200322
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +0100323 " Optionally run Vim under valgrind
324 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
325
Bram Moolenaar3a938382016-08-07 16:36:40 +0200326 exe "silent !" . a:pipecmd . cmd . args . ' ' . a:arguments
Bram Moolenaar66459b72016-08-06 19:01:55 +0200327
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200328 if len(a:before) > 0
329 call delete('Xbefore.vim')
330 endif
331 if len(a:after) > 0
332 call delete('Xafter.vim')
333 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200334 return 1
335endfunc
Bram Moolenaar07282f02019-10-10 16:46:17 +0200336
337func IsRoot()
338 if !has('unix')
339 return v:false
340 elseif $USER == 'root' || system('id -un') =~ '\<root\>'
341 return v:true
342 endif
343 return v:false
344endfunc
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200345
Bram Moolenaar88e6cc22020-04-30 19:19:29 +0200346" Get all messages but drop the maintainer entry.
347func GetMessages()
348 redir => result
349 redraw | messages
350 redir END
351 let msg_list = split(result, "\n")
352 if msg_list->len() > 0 && msg_list[0] =~ 'Messages maintainer:'
353 return msg_list[1:]
354 endif
355 return msg_list
356endfunc
357
Bram Moolenaarab589462020-07-06 21:03:06 +0200358" Run the list of commands in 'cmds' and look for 'errstr' in exception.
359" Note that assert_fails() cannot be used in some places and this function
360" can be used.
361func AssertException(cmds, errstr)
362 let save_exception = ''
363 try
364 for cmd in a:cmds
365 exe cmd
366 endfor
367 catch
368 let save_exception = v:exception
369 endtry
370 call assert_match(a:errstr, save_exception)
371endfunc
372
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200373" vim: shiftwidth=2 sts=2 expandtab