blob: 4546be7f7404db687927ef2e686a0b94e12a0f70 [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.
4if exists('*WaitFor')
5 finish
6endif
7
Bram Moolenaar321efdd2016-07-15 17:09:11 +02008" Get the name of the Python executable.
9" Also keeps it in s:python.
10func PythonProg()
11 " This test requires the Python command to run the test server.
12 " This most likely only works on Unix and Windows.
13 if has('unix')
14 " We also need the job feature or the pkill command to make sure the server
15 " can be stopped.
16 if !(executable('python') && (has('job') || executable('pkill')))
17 return ''
18 endif
19 let s:python = 'python'
20 elseif has('win32')
21 " Use Python Launcher for Windows (py.exe) if available.
22 if executable('py.exe')
23 let s:python = 'py.exe'
24 elseif executable('python.exe')
25 let s:python = 'python.exe'
26 else
27 return ''
28 endif
29 else
30 return ''
31 endif
32 return s:python
33endfunc
34
35" Run "cmd". Returns the job if using a job.
36func RunCommand(cmd)
37 let job = 0
38 if has('job')
39 let job = job_start(a:cmd, {"stoponexit": "hup"})
40 call job_setoptions(job, {"stoponexit": "kill"})
41 elseif has('win32')
42 exe 'silent !start cmd /c start "test_channel" ' . a:cmd
43 else
44 exe 'silent !' . a:cmd . '&'
45 endif
46 return job
47endfunc
48
49" Read the port number from the Xportnr file.
50func GetPort()
51 let l = []
Bram Moolenaar453ce7c2018-10-12 22:15:12 +020052 " with 200 it sometimes failed
53 for i in range(400)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020054 try
55 let l = readfile("Xportnr")
56 catch
57 endtry
58 if len(l) >= 1
59 break
60 endif
61 sleep 10m
62 endfor
63 call delete("Xportnr")
64
65 if len(l) == 0
66 " Can't make the connection, give up.
67 return 0
68 endif
69 return l[0]
70endfunc
71
72" Run a Python server for "cmd" and call "testfunc".
73" Always kills the server before returning.
74func RunServer(cmd, testfunc, args)
75 " The Python program writes the port number in Xportnr.
76 call delete("Xportnr")
77
78 if len(a:args) == 1
79 let arg = ' ' . a:args[0]
80 else
81 let arg = ''
82 endif
83 let pycmd = s:python . " " . a:cmd . arg
84
85 try
86 let g:currentJob = RunCommand(pycmd)
87
88 " Wait for up to 2 seconds for the port number to be there.
89 let port = GetPort()
90 if port == 0
91 call assert_false(1, "Can't start " . a:cmd)
92 return
93 endif
94
95 call call(function(a:testfunc), [port])
96 catch
Bram Moolenaar4b785f62016-11-29 21:54:44 +010097 call assert_false(1, 'Caught exception: "' . v:exception . '" in ' . v:throwpoint)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020098 finally
99 call s:kill_server(a:cmd)
100 endtry
101endfunc
102
103func s:kill_server(cmd)
104 if has('job')
105 if exists('g:currentJob')
106 call job_stop(g:currentJob)
107 unlet g:currentJob
108 endif
109 elseif has('win32')
110 let cmd = substitute(a:cmd, ".py", '', '')
111 call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq ' . cmd . '"')
112 else
113 call system("pkill -f " . a:cmd)
114 endif
115endfunc
116
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200117" Wait for up to five seconds for "expr" to become true. "expr" can be a
Bram Moolenaar13deab82017-11-04 18:48:43 +0100118" stringified expression to evaluate, or a funcref without arguments.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200119" Using a lambda works best. Example:
120" call WaitFor({-> status == "ok"})
121"
Bram Moolenaar5d7ead32018-02-27 17:17:42 +0100122" A second argument can be used to specify a different timeout in msec.
Bram Moolenaar13deab82017-11-04 18:48:43 +0100123"
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200124" When successful the time slept is returned.
125" When running into the timeout an exception is thrown, thus the function does
126" not return.
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +0200127func WaitFor(expr, ...)
Bram Moolenaar769e9d22018-04-11 20:53:49 +0200128 let timeout = get(a:000, 0, 5000)
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200129 let slept = s:WaitForCommon(a:expr, v:null, timeout)
130 if slept < 0
131 throw 'WaitFor() timed out after ' . timeout . ' msec'
132 endif
133 return slept
134endfunc
135
136" Wait for up to five seconds for "assert" to return zero. "assert" must be a
137" (lambda) function containing one assert function. Example:
138" call WaitForAssert({-> assert_equal("dead", job_status(job)})
139"
140" A second argument can be used to specify a different timeout in msec.
141"
142" Return zero for success, one for failure (like the assert function).
143func WaitForAssert(assert, ...)
144 let timeout = get(a:000, 0, 5000)
145 if s:WaitForCommon(v:null, a:assert, timeout) < 0
146 return 1
147 endif
148 return 0
149endfunc
150
151" Common implementation of WaitFor() and WaitForAssert().
152" Either "expr" or "assert" is not v:null
153" Return the waiting time for success, -1 for failure.
154func s:WaitForCommon(expr, assert, timeout)
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200155 " using reltime() is more accurate, but not always available
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200156 let slept = 0
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200157 if has('reltime')
158 let start = reltime()
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200159 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200160
161 while 1
162 if type(a:expr) == v:t_func
163 let success = a:expr()
164 elseif type(a:assert) == v:t_func
165 let success = a:assert() == 0
166 else
167 let success = eval(a:expr)
168 endif
169 if success
Bram Moolenaarc20e0d52017-11-02 18:19:19 +0100170 return slept
171 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200172
173 if slept >= a:timeout
174 break
175 endif
176 if type(a:assert) == v:t_func
177 " Remove the error added by the assert function.
178 call remove(v:errors, -1)
179 endif
180
181 sleep 10m
182 if has('reltime')
183 let slept = float2nr(reltimefloat(reltime(start)) * 1000)
184 else
Bram Moolenaarf267f8b2016-08-22 21:40:29 +0200185 let slept += 10
186 endif
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200187 endwhile
188
189 return -1 " timed out
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200190endfunc
Bram Moolenaar66459b72016-08-06 19:01:55 +0200191
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200192
Bram Moolenaar01688ad2016-10-27 20:00:07 +0200193" Wait for up to a given milliseconds.
194" With the +timers feature this waits for key-input by getchar(), Resume()
195" feeds key-input and resumes process. Return time waited in milliseconds.
196" Without +timers it uses simply :sleep.
197func Standby(msec)
198 if has('timers')
199 let start = reltime()
200 let g:_standby_timer = timer_start(a:msec, function('s:feedkeys'))
201 call getchar()
202 return float2nr(reltimefloat(reltime(start)) * 1000)
203 else
204 execute 'sleep ' a:msec . 'm'
205 return a:msec
206 endif
207endfunc
208
209func Resume()
210 if exists('g:_standby_timer')
211 call timer_stop(g:_standby_timer)
212 call s:feedkeys(0)
213 unlet g:_standby_timer
214 endif
215endfunc
216
217func s:feedkeys(timer)
218 call feedkeys('x', 'nt')
219endfunc
220
Bram Moolenaar63182052017-10-07 20:03:23 +0200221" Get $VIMPROG to run Vim executable.
222" The Makefile writes it as the first line in the "vimcmd" file.
223func GetVimProg()
224 if !filereadable('vimcmd')
Bram Moolenaarda650582018-02-20 15:51:40 +0100225 " Assume the script was sourced instead of running "make".
226 return '../vim'
Bram Moolenaar63182052017-10-07 20:03:23 +0200227 endif
228 return readfile('vimcmd')[0]
229endfunc
230
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200231let g:valgrind_cnt = 1
232
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100233" Get the command to run Vim, with -u NONE and --not-a-term arguments.
Bram Moolenaar9d954202017-09-04 20:34:19 +0200234" If there is an argument use it instead of "NONE".
Bram Moolenaar9d954202017-09-04 20:34:19 +0200235func GetVimCommand(...)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100236 if !filereadable('vimcmd')
Bram Moolenaarda650582018-02-20 15:51:40 +0100237 echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
238 let lines = ['../vim']
239 else
240 let lines = readfile('vimcmd')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100241 endif
Bram Moolenaar9d954202017-09-04 20:34:19 +0200242 if a:0 == 0
243 let name = 'NONE'
244 else
245 let name = a:1
246 endif
Bram Moolenaar63182052017-10-07 20:03:23 +0200247 " For Unix Makefile writes the command to use in the second line of the
248 " "vimcmd" file, including environment options.
249 " Other Makefiles just write the executable in the first line, so fall back
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200250 " to that if there is no second line or it is empty.
251 if len(lines) > 1 && lines[1] != ''
252 let cmd = lines[1]
253 else
254 let cmd = lines[0]
255 endif
256
Bram Moolenaar9d954202017-09-04 20:34:19 +0200257 let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '')
258 if cmd !~ '-u '. name
259 let cmd = cmd . ' -u ' . name
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100260 endif
261 let cmd .= ' --not-a-term'
262 let cmd = substitute(cmd, 'VIMRUNTIME=.*VIMRUNTIME;', '', '')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200263
264 " If using valgrind, make sure every run uses a different log file.
265 if cmd =~ 'valgrind.*--log-file='
266 let cmd = substitute(cmd, '--log-file=\(^\s*\)', '--log-file=\1.' . g:valgrind_cnt, '')
267 let g:valgrind_cnt += 1
268 endif
269
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100270 return cmd
271endfunc
272
Bram Moolenaarda650582018-02-20 15:51:40 +0100273" Get the command to run Vim, with --clean.
274func GetVimCommandClean()
275 let cmd = GetVimCommand()
276 let cmd = substitute(cmd, '-u NONE', '--clean', '')
277 let cmd = substitute(cmd, '--not-a-term', '', '')
Bram Moolenaar453ce7c2018-10-12 22:15:12 +0200278
279 " Optionally run Vim under valgrind
280 " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd
281
Bram Moolenaarda650582018-02-20 15:51:40 +0100282 return cmd
283endfunc
284
Bram Moolenaar66459b72016-08-06 19:01:55 +0200285" Run Vim, using the "vimcmd" file and "-u NORC".
Bram Moolenaar3a938382016-08-07 16:36:40 +0200286" "before" is a list of Vim commands to be executed before loading plugins.
287" "after" is a list of Vim commands to be executed after loading plugins.
Bram Moolenaar66459b72016-08-06 19:01:55 +0200288" Plugins are not loaded, unless 'loadplugins' is set in "before".
289" Return 1 if Vim could be executed.
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200290func RunVim(before, after, arguments)
Bram Moolenaar7a9a5f42016-08-08 22:34:14 +0200291 return RunVimPiped(a:before, a:after, a:arguments, '')
Bram Moolenaar3a938382016-08-07 16:36:40 +0200292endfunc
293
294func RunVimPiped(before, after, arguments, pipecmd)
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100295 let cmd = GetVimCommand()
Bram Moolenaarba98bef2016-08-07 15:51:39 +0200296 let args = ''
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200297 if len(a:before) > 0
298 call writefile(a:before, 'Xbefore.vim')
299 let args .= ' --cmd "so Xbefore.vim"'
300 endif
301 if len(a:after) > 0
302 call writefile(a:after, 'Xafter.vim')
303 let args .= ' -S Xafter.vim'
304 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200305
Bram Moolenaar3a938382016-08-07 16:36:40 +0200306 exe "silent !" . a:pipecmd . cmd . args . ' ' . a:arguments
Bram Moolenaar66459b72016-08-06 19:01:55 +0200307
Bram Moolenaar472a0a82016-08-06 22:31:42 +0200308 if len(a:before) > 0
309 call delete('Xbefore.vim')
310 endif
311 if len(a:after) > 0
312 call delete('Xafter.vim')
313 endif
Bram Moolenaar66459b72016-08-06 19:01:55 +0200314 return 1
315endfunc
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200316
317func CanRunGui()
318 return has('gui') && ($DISPLAY != "" || has('gui_running'))
319endfunc
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100320
Bram Moolenaara9034722018-03-13 15:43:46 +0100321func WorkingClipboard()
322 if !has('clipboard')
323 return 0
324 endif
325 if has('x11')
326 return $DISPLAY != ""
327 endif
328 return 1
329endfunc
330
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100331" Get line "lnum" as displayed on the screen.
332" Trailing white space is trimmed.
333func! Screenline(lnum)
334 let chars = []
335 for c in range(1, winwidth(0))
336 call add(chars, nr2char(screenchar(a:lnum, c)))
337 endfor
338 let line = join(chars, '')
339 return matchstr(line, '^.\{-}\ze\s*$')
340endfunc
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100341
342" Stops the shell running in terminal "buf".
343func Stop_shell_in_terminal(buf)
344 call term_sendkeys(a:buf, "exit\r")
345 let job = term_getjob(a:buf)
346 call WaitFor({-> job_status(job) == "dead"})
347endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +0100348
349" Gets the text of a terminal line, using term_scrape()
350func Get_terminal_text(bufnr, row)
351 let list = term_scrape(a:bufnr, a:row)
352 let text = ''
353 for item in list
354 let text .= item.chars
355 endfor
356 return text
357endfunc