2016-07-15 17:09:11 +02:00
|
|
|
" Functions shared by several tests.
|
|
|
|
|
|
2017-09-29 22:42:33 +02:00
|
|
|
" Only load this script once.
|
|
|
|
|
if exists('*WaitFor')
|
|
|
|
|
finish
|
|
|
|
|
endif
|
|
|
|
|
|
2016-07-15 17:09:11 +02:00
|
|
|
" Get the name of the Python executable.
|
|
|
|
|
" Also keeps it in s:python.
|
|
|
|
|
func PythonProg()
|
|
|
|
|
" This test requires the Python command to run the test server.
|
|
|
|
|
" This most likely only works on Unix and Windows.
|
|
|
|
|
if has('unix')
|
|
|
|
|
" We also need the job feature or the pkill command to make sure the server
|
|
|
|
|
" can be stopped.
|
|
|
|
|
if !(executable('python') && (has('job') || executable('pkill')))
|
|
|
|
|
return ''
|
|
|
|
|
endif
|
|
|
|
|
let s:python = 'python'
|
|
|
|
|
elseif has('win32')
|
|
|
|
|
" Use Python Launcher for Windows (py.exe) if available.
|
|
|
|
|
if executable('py.exe')
|
|
|
|
|
let s:python = 'py.exe'
|
|
|
|
|
elseif executable('python.exe')
|
|
|
|
|
let s:python = 'python.exe'
|
|
|
|
|
else
|
|
|
|
|
return ''
|
|
|
|
|
endif
|
|
|
|
|
else
|
|
|
|
|
return ''
|
|
|
|
|
endif
|
|
|
|
|
return s:python
|
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
|
|
" Run "cmd". Returns the job if using a job.
|
|
|
|
|
func RunCommand(cmd)
|
|
|
|
|
let job = 0
|
|
|
|
|
if has('job')
|
|
|
|
|
let job = job_start(a:cmd, {"stoponexit": "hup"})
|
|
|
|
|
call job_setoptions(job, {"stoponexit": "kill"})
|
|
|
|
|
elseif has('win32')
|
|
|
|
|
exe 'silent !start cmd /c start "test_channel" ' . a:cmd
|
|
|
|
|
else
|
|
|
|
|
exe 'silent !' . a:cmd . '&'
|
|
|
|
|
endif
|
|
|
|
|
return job
|
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
|
|
" Read the port number from the Xportnr file.
|
|
|
|
|
func GetPort()
|
|
|
|
|
let l = []
|
|
|
|
|
for i in range(200)
|
|
|
|
|
try
|
|
|
|
|
let l = readfile("Xportnr")
|
|
|
|
|
catch
|
|
|
|
|
endtry
|
|
|
|
|
if len(l) >= 1
|
|
|
|
|
break
|
|
|
|
|
endif
|
|
|
|
|
sleep 10m
|
|
|
|
|
endfor
|
|
|
|
|
call delete("Xportnr")
|
|
|
|
|
|
|
|
|
|
if len(l) == 0
|
|
|
|
|
" Can't make the connection, give up.
|
|
|
|
|
return 0
|
|
|
|
|
endif
|
|
|
|
|
return l[0]
|
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
|
|
" Run a Python server for "cmd" and call "testfunc".
|
|
|
|
|
" Always kills the server before returning.
|
|
|
|
|
func RunServer(cmd, testfunc, args)
|
|
|
|
|
" The Python program writes the port number in Xportnr.
|
|
|
|
|
call delete("Xportnr")
|
|
|
|
|
|
|
|
|
|
if len(a:args) == 1
|
|
|
|
|
let arg = ' ' . a:args[0]
|
|
|
|
|
else
|
|
|
|
|
let arg = ''
|
|
|
|
|
endif
|
|
|
|
|
let pycmd = s:python . " " . a:cmd . arg
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
let g:currentJob = RunCommand(pycmd)
|
|
|
|
|
|
|
|
|
|
" Wait for up to 2 seconds for the port number to be there.
|
|
|
|
|
let port = GetPort()
|
|
|
|
|
if port == 0
|
|
|
|
|
call assert_false(1, "Can't start " . a:cmd)
|
|
|
|
|
return
|
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
call call(function(a:testfunc), [port])
|
|
|
|
|
catch
|
2016-11-29 21:54:44 +01:00
|
|
|
call assert_false(1, 'Caught exception: "' . v:exception . '" in ' . v:throwpoint)
|
2016-07-15 17:09:11 +02:00
|
|
|
finally
|
|
|
|
|
call s:kill_server(a:cmd)
|
|
|
|
|
endtry
|
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
|
|
func s:kill_server(cmd)
|
|
|
|
|
if has('job')
|
|
|
|
|
if exists('g:currentJob')
|
|
|
|
|
call job_stop(g:currentJob)
|
|
|
|
|
unlet g:currentJob
|
|
|
|
|
endif
|
|
|
|
|
elseif has('win32')
|
|
|
|
|
let cmd = substitute(a:cmd, ".py", '', '')
|
|
|
|
|
call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq ' . cmd . '"')
|
|
|
|
|
else
|
|
|
|
|
call system("pkill -f " . a:cmd)
|
|
|
|
|
endif
|
|
|
|
|
endfunc
|
|
|
|
|
|
2018-04-11 20:53:49 +02:00
|
|
|
" Wait for up to five seconds for "expr" to become true. "expr" can be a
|
2017-11-04 18:48:43 +01:00
|
|
|
" stringified expression to evaluate, or a funcref without arguments.
|
2018-02-27 17:17:42 +01:00
|
|
|
" A second argument can be used to specify a different timeout in msec.
|
2017-11-04 18:48:43 +01:00
|
|
|
"
|
2016-08-22 21:40:29 +02:00
|
|
|
" Return time slept in milliseconds. With the +reltime feature this can be
|
|
|
|
|
" more than the actual waiting time. Without +reltime it can also be less.
|
2017-07-19 19:55:58 +02:00
|
|
|
func WaitFor(expr, ...)
|
2018-04-11 20:53:49 +02:00
|
|
|
let timeout = get(a:000, 0, 5000)
|
2016-08-22 21:40:29 +02:00
|
|
|
" using reltime() is more accurate, but not always available
|
|
|
|
|
if has('reltime')
|
|
|
|
|
let start = reltime()
|
|
|
|
|
else
|
|
|
|
|
let slept = 0
|
|
|
|
|
endif
|
2017-11-04 18:48:43 +01:00
|
|
|
if type(a:expr) == v:t_func
|
|
|
|
|
let Test = a:expr
|
|
|
|
|
else
|
|
|
|
|
let Test = {-> eval(a:expr) }
|
|
|
|
|
endif
|
2017-07-19 19:55:58 +02:00
|
|
|
for i in range(timeout / 10)
|
2017-11-04 18:48:43 +01:00
|
|
|
if Test()
|
2017-11-02 18:19:19 +01:00
|
|
|
if has('reltime')
|
|
|
|
|
return float2nr(reltimefloat(reltime(start)) * 1000)
|
2016-07-15 17:09:11 +02:00
|
|
|
endif
|
2017-11-02 18:19:19 +01:00
|
|
|
return slept
|
|
|
|
|
endif
|
2016-08-22 21:40:29 +02:00
|
|
|
if !has('reltime')
|
|
|
|
|
let slept += 10
|
|
|
|
|
endif
|
2016-07-15 17:09:11 +02:00
|
|
|
sleep 10m
|
|
|
|
|
endfor
|
2017-11-02 16:58:00 +01:00
|
|
|
throw 'WaitFor() timed out after ' . timeout . ' msec'
|
2016-07-15 17:09:11 +02:00
|
|
|
endfunc
|
2016-08-06 19:01:55 +02:00
|
|
|
|
2016-10-27 20:00:07 +02:00
|
|
|
" Wait for up to a given milliseconds.
|
|
|
|
|
" With the +timers feature this waits for key-input by getchar(), Resume()
|
|
|
|
|
" feeds key-input and resumes process. Return time waited in milliseconds.
|
|
|
|
|
" Without +timers it uses simply :sleep.
|
|
|
|
|
func Standby(msec)
|
|
|
|
|
if has('timers')
|
|
|
|
|
let start = reltime()
|
|
|
|
|
let g:_standby_timer = timer_start(a:msec, function('s:feedkeys'))
|
|
|
|
|
call getchar()
|
|
|
|
|
return float2nr(reltimefloat(reltime(start)) * 1000)
|
|
|
|
|
else
|
|
|
|
|
execute 'sleep ' a:msec . 'm'
|
|
|
|
|
return a:msec
|
|
|
|
|
endif
|
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
|
|
func Resume()
|
|
|
|
|
if exists('g:_standby_timer')
|
|
|
|
|
call timer_stop(g:_standby_timer)
|
|
|
|
|
call s:feedkeys(0)
|
|
|
|
|
unlet g:_standby_timer
|
|
|
|
|
endif
|
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
|
|
func s:feedkeys(timer)
|
|
|
|
|
call feedkeys('x', 'nt')
|
|
|
|
|
endfunc
|
|
|
|
|
|
2017-10-07 20:03:23 +02:00
|
|
|
" Get $VIMPROG to run Vim executable.
|
|
|
|
|
" The Makefile writes it as the first line in the "vimcmd" file.
|
|
|
|
|
func GetVimProg()
|
|
|
|
|
if !filereadable('vimcmd')
|
2018-02-20 15:51:40 +01:00
|
|
|
" Assume the script was sourced instead of running "make".
|
|
|
|
|
return '../vim'
|
2017-10-07 20:03:23 +02:00
|
|
|
endif
|
|
|
|
|
return readfile('vimcmd')[0]
|
|
|
|
|
endfunc
|
|
|
|
|
|
2017-03-18 16:18:37 +01:00
|
|
|
" Get the command to run Vim, with -u NONE and --not-a-term arguments.
|
2017-09-04 20:34:19 +02:00
|
|
|
" If there is an argument use it instead of "NONE".
|
|
|
|
|
func GetVimCommand(...)
|
2017-03-18 16:18:37 +01:00
|
|
|
if !filereadable('vimcmd')
|
2018-02-20 15:51:40 +01:00
|
|
|
echo 'Cannot read the "vimcmd" file, falling back to ../vim.'
|
|
|
|
|
let lines = ['../vim']
|
|
|
|
|
else
|
|
|
|
|
let lines = readfile('vimcmd')
|
2017-03-18 16:18:37 +01:00
|
|
|
endif
|
2017-09-04 20:34:19 +02:00
|
|
|
if a:0 == 0
|
|
|
|
|
let name = 'NONE'
|
|
|
|
|
else
|
|
|
|
|
let name = a:1
|
|
|
|
|
endif
|
2017-10-07 20:03:23 +02:00
|
|
|
" For Unix Makefile writes the command to use in the second line of the
|
|
|
|
|
" "vimcmd" file, including environment options.
|
|
|
|
|
" Other Makefiles just write the executable in the first line, so fall back
|
|
|
|
|
" to that if there is no second line.
|
|
|
|
|
let cmd = get(lines, 1, lines[0])
|
2017-09-04 20:34:19 +02:00
|
|
|
let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '')
|
|
|
|
|
if cmd !~ '-u '. name
|
|
|
|
|
let cmd = cmd . ' -u ' . name
|
2017-03-18 16:18:37 +01:00
|
|
|
endif
|
|
|
|
|
let cmd .= ' --not-a-term'
|
|
|
|
|
let cmd = substitute(cmd, 'VIMRUNTIME=.*VIMRUNTIME;', '', '')
|
|
|
|
|
return cmd
|
|
|
|
|
endfunc
|
|
|
|
|
|
2018-02-20 15:51:40 +01:00
|
|
|
" Get the command to run Vim, with --clean.
|
|
|
|
|
func GetVimCommandClean()
|
|
|
|
|
let cmd = GetVimCommand()
|
|
|
|
|
let cmd = substitute(cmd, '-u NONE', '--clean', '')
|
|
|
|
|
let cmd = substitute(cmd, '--not-a-term', '', '')
|
|
|
|
|
return cmd
|
|
|
|
|
endfunc
|
|
|
|
|
|
2016-08-06 19:01:55 +02:00
|
|
|
" Run Vim, using the "vimcmd" file and "-u NORC".
|
2016-08-07 16:36:40 +02:00
|
|
|
" "before" is a list of Vim commands to be executed before loading plugins.
|
|
|
|
|
" "after" is a list of Vim commands to be executed after loading plugins.
|
2016-08-06 19:01:55 +02:00
|
|
|
" Plugins are not loaded, unless 'loadplugins' is set in "before".
|
|
|
|
|
" Return 1 if Vim could be executed.
|
2016-08-06 22:31:42 +02:00
|
|
|
func RunVim(before, after, arguments)
|
2016-08-08 22:34:14 +02:00
|
|
|
return RunVimPiped(a:before, a:after, a:arguments, '')
|
2016-08-07 16:36:40 +02:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
|
|
func RunVimPiped(before, after, arguments, pipecmd)
|
2017-03-18 16:18:37 +01:00
|
|
|
let cmd = GetVimCommand()
|
|
|
|
|
if cmd == ''
|
2016-08-06 19:01:55 +02:00
|
|
|
return 0
|
|
|
|
|
endif
|
2016-08-07 15:51:39 +02:00
|
|
|
let args = ''
|
2016-08-06 22:31:42 +02:00
|
|
|
if len(a:before) > 0
|
|
|
|
|
call writefile(a:before, 'Xbefore.vim')
|
|
|
|
|
let args .= ' --cmd "so Xbefore.vim"'
|
|
|
|
|
endif
|
|
|
|
|
if len(a:after) > 0
|
|
|
|
|
call writefile(a:after, 'Xafter.vim')
|
|
|
|
|
let args .= ' -S Xafter.vim'
|
|
|
|
|
endif
|
2016-08-06 19:01:55 +02:00
|
|
|
|
2016-08-07 16:36:40 +02:00
|
|
|
exe "silent !" . a:pipecmd . cmd . args . ' ' . a:arguments
|
2016-08-06 19:01:55 +02:00
|
|
|
|
2016-08-06 22:31:42 +02:00
|
|
|
if len(a:before) > 0
|
|
|
|
|
call delete('Xbefore.vim')
|
|
|
|
|
endif
|
|
|
|
|
if len(a:after) > 0
|
|
|
|
|
call delete('Xafter.vim')
|
|
|
|
|
endif
|
2016-08-06 19:01:55 +02:00
|
|
|
return 1
|
|
|
|
|
endfunc
|
2017-08-13 20:26:20 +02:00
|
|
|
|
|
|
|
|
func CanRunGui()
|
|
|
|
|
return has('gui') && ($DISPLAY != "" || has('gui_running'))
|
|
|
|
|
endfunc
|
2018-02-27 21:09:30 +01:00
|
|
|
|
2018-03-13 15:43:46 +01:00
|
|
|
func WorkingClipboard()
|
|
|
|
|
if !has('clipboard')
|
|
|
|
|
return 0
|
|
|
|
|
endif
|
|
|
|
|
if has('x11')
|
|
|
|
|
return $DISPLAY != ""
|
|
|
|
|
endif
|
|
|
|
|
return 1
|
|
|
|
|
endfunc
|
|
|
|
|
|
2018-02-27 21:09:30 +01:00
|
|
|
" Get line "lnum" as displayed on the screen.
|
|
|
|
|
" Trailing white space is trimmed.
|
|
|
|
|
func! Screenline(lnum)
|
|
|
|
|
let chars = []
|
|
|
|
|
for c in range(1, winwidth(0))
|
|
|
|
|
call add(chars, nr2char(screenchar(a:lnum, c)))
|
|
|
|
|
endfor
|
|
|
|
|
let line = join(chars, '')
|
|
|
|
|
return matchstr(line, '^.\{-}\ze\s*$')
|
|
|
|
|
endfunc
|
2018-03-09 21:33:34 +01:00
|
|
|
|
|
|
|
|
" Stops the shell running in terminal "buf".
|
|
|
|
|
func Stop_shell_in_terminal(buf)
|
|
|
|
|
call term_sendkeys(a:buf, "exit\r")
|
|
|
|
|
let job = term_getjob(a:buf)
|
|
|
|
|
call WaitFor({-> job_status(job) == "dead"})
|
|
|
|
|
endfunc
|