robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8b51e50cdc
master
1" Functions shared by several tests. 2 3" Only load this script once. 4if exists ( '*PythonProg' ) 5finish 6endif 7 8source view_util.vim 9 10" {Nvim} 11" Filepath captured from output may be truncated, like this: 12" /home/va...estdir/X-test-tmpdir/nvimxbXN4i/10 13" Get last 2 segments, then combine with $TMPDIR. 14func ! Fix_truncated_tmpfile ( fname ) 15" sanity check 16if $ TMPDIR ==# '' 17throw '$TMPDIR is empty' 18endif 19let tmpdir_tail = fnamemodify ( substitute ( $ TMPDIR , '[\/]\+$' , '' , 'g' ), ':t' ) 20if tmpdir_tail ==# '' 21throw 'empty tmpdir_tail' 22endif 23if a: fname !~# tmpdir_tail 24throw printf ( '$TMPDIR (%s) not in fname: %s' , tmpdir_tail , a: fname ) 25endif 26let last2segments = matchstr ( a: fname , '[\/][^\/]\+[\/][^\/]\+$' ) 27return $ TMPDIR . last2segments 28endfunc 29 30" Get the name of the Python executable. 31" Also keeps it in s:python. 32func PythonProg () 33" This test requires the Python command to run the test server. 34" This most likely only works on Unix and Windows. 35if has ( 'unix' ) 36" We also need the job feature or the pkill command to make sure the server 37" can be stopped. 38if ! ( has ( 'job' ) || executable ( 'pkill' )) 39return '' 40endif 41if executable ( 'python3' ) 42let s: python = 'python3' 43elseif executable ( 'python' ) 44let s: python = 'python' 45else 46return '' 47end 48elseif has ( 'win32' ) 49" Use Python Launcher for Windows (py.exe) if available. 50" NOTE: if you get a "Python was not found" error, disable the Python 51" shortcuts in "Windows menu / Settings / Manage App Execution Aliases". 52if executable ( 'py.exe' ) 53let s: python = 'py.exe' 54elseif executable ( 'python.exe' ) 55let s: python = 'python.exe' 56else 57return '' 58endif 59else 60return '' 61endif 62return s: python 63endfunc 64 65" Run "cmd". Returns the job if using a job. 66func RunCommand ( cmd ) 67" Running an external command can occasionally be slow or fail. 68let g: test_is_flaky = 1 69 70let job = 0 71if has ( 'job' ) 72let job = job_start ( a: cmd , { "stoponexit" : "hup" }) 73call job_setoptions ( job , { "stoponexit" : "kill" }) 74elseif has ( 'win32' ) 75exe 'silent !start cmd /D /c start "test_channel" ' . a: cmd 76else 77exe 'silent !' . a: cmd . '&' 78endif 79return job 80endfunc 81 82" Read the port number from the Xportnr file. 83func GetPort () 84let l = [] 85" with 200 it sometimes failed, with 400 is rarily failed 86for i in range ( 600 ) 87try 88let l = readfile ( "Xportnr" ) 89catch 90endtry 91if len ( l ) >= 1 92break 93endif 94sleep 10m 95endfor 96call delete ( "Xportnr" ) 97 98if len ( l ) == 0 99" Can't make the connection, give up. 100return 0 101endif 102return l [ 0 ] 103endfunc 104 105" Run a Python server for "cmd" and call "testfunc". 106" Always kills the server before returning. 107func RunServer ( cmd , testfunc , args ) 108" The Python program writes the port number in Xportnr. 109call delete ( "Xportnr" ) 110 111if len ( a: args ) == 1 112let arg = ' ' . a: args [ 0 ] 113else 114let arg = '' 115endif 116let pycmd = s: python . " " . a: cmd . arg 117 118try 119let g: currentJob = RunCommand ( pycmd ) 120 121" Wait for some time for the port number to be there. 122let port = GetPort () 123if port == 0 124call assert_report ( strftime ( "%H:%M:%S" ) .. " Can't start " .. a: cmd ) 125return 126endif 127 128call call ( function ( a: testfunc ), [ port ]) 129catch /E901.*Address family for hostname not supported/ 130throw 'Skipped: Invalid network setup ("' .. v: exception .. '" in ' .. v: throwpoint .. ')' 131catch 132call assert_report ( 'Caught exception: "' . v: exception . '" in ' . v: throwpoint ) 133finally 134call s: kill_server ( a: cmd ) 135endtry 136endfunc 137 138func s: kill_server ( cmd ) 139if has ( 'job' ) 140if exists ( 'g:currentJob' ) 141call job_stop ( g: currentJob ) 142unlet g: currentJob 143endif 144elseif has ( 'win32' ) 145let cmd = substitute ( a: cmd , ".py" , '' , '' ) 146call system ( 'taskkill /IM ' . s: python . ' /T /F /FI "WINDOWTITLE eq ' . cmd . '"' ) 147else 148call system ( "pkill -f " . a: cmd ) 149endif 150endfunc 151 152" Wait for up to five seconds for "expr" to become true. "expr" can be a 153" stringified expression to evaluate, or a funcref without arguments. 154" Using a lambda works best. Example: 155" call WaitFor({-> status == "ok"}) 156" 157" A second argument can be used to specify a different timeout in msec. 158" 159" When successful the time slept is returned. 160" When running into the timeout an exception is thrown, thus the function does 161" not return. 162func WaitFor ( expr , ...) 163let timeout = get ( a: 000 , 0 , 5000 ) 164let slept = s: WaitForCommon ( a: expr , v: null , timeout ) 165if slept < 0 166throw 'WaitFor() timed out after ' . timeout . ' msec' 167endif 168return slept 169endfunc 170 171" Wait for up to five seconds for "assert" to return zero. "assert" must be a 172" (lambda) function containing one assert function. Example: 173" call WaitForAssert({-> assert_equal("dead", job_status(job)}) 174" 175" A second argument can be used to specify a different timeout in msec. 176" 177" Return zero for success, one for failure (like the assert function). 178func WaitForAssert ( assert , ...) 179let timeout = get ( a: 000 , 0 , 5000 ) 180if s: WaitForCommon ( v: null , a: assert , timeout ) < 0 181return 1 182endif 183return 0 184endfunc 185 186" Common implementation of WaitFor() and WaitForAssert(). 187" Either "expr" or "assert" is not v:null 188" Return the waiting time for success, -1 for failure. 189func s: WaitForCommon ( expr , assert , timeout ) 190" using reltime() is more accurate, but not always available 191let slept = 0 192if exists ( '*reltimefloat' ) 193let start = reltime () 194endif 195 196while 1 197if type ( a: expr ) == v: t_func 198let success = a: expr () 199elseif type ( a: assert ) == v: t_func 200let success = a: assert () == 0 201else 202let success = eval ( a: expr ) 203endif 204if success 205return slept 206endif 207 208if slept >= a: timeout 209break 210endif 211if type ( a: assert ) == v: t_func 212" Remove the error added by the assert function. 213call remove ( v: errors , -1 ) 214endif 215 216sleep 1m 217if exists ( '*reltimefloat' ) 218let slept = float2nr ( reltimefloat ( reltime ( start )) * 1000 ) 219else 220let slept += 1 221endif 222endwhile 223 224return -1 " timed out 225endfunc 226 227 228" Wait for up to a given milliseconds. 229" With the +timers feature this waits for key-input by getchar(), Resume() 230" feeds key-input and resumes process. Return time waited in milliseconds. 231" Without +timers it uses simply :sleep. 232func Standby ( msec ) 233if has ( 'timers' ) && exists ( '*reltimefloat' ) 234let start = reltime () 235let g: _standby_timer = timer_start ( a: msec , function ( 's:feedkeys' )) 236call getchar () 237return float2nr ( reltimefloat ( reltime ( start )) * 1000 ) 238else 239execute 'sleep ' a: msec . 'm' 240return a: msec 241endif 242endfunc 243 244func Resume () 245if exists ( 'g:_standby_timer' ) 246call timer_stop ( g: _standby_timer ) 247call s: feedkeys ( 0 ) 248unlet g: _standby_timer 249endif 250endfunc 251 252func s: feedkeys ( timer ) 253call feedkeys ( 'x' , 'nt' ) 254endfunc 255 256" Get $VIMPROG to run the Vim executable. 257" The Makefile writes it as the first line in the "vimcmd" file. 258" Falls back to the Vim executable in the src directory. 259" Nvim: uses $NVIM_TEST_ARG0. 260func GetVimProg () 261if ! empty ( $ NVIM_TEST_ARG0 ) 262if has ( 'win32' ) 263return substitute ( $ NVIM_TEST_ARG0 , '/' , '\\' , 'g' ) 264else 265return $ NVIM_TEST_ARG0 266endif 267endif 268" echo 'Cannot read the "vimcmd" file, falling back to ../vim.' 269 270" Probably the script was sourced instead of running "make". 271" We assume Vim was just build in the src directory then. 272return v: progpath 273endfunc 274 275let g: valgrind_cnt = 1 276 277" Get the command to run Vim, with -u NONE and --headless arguments. 278" If there is an argument use it instead of "NONE". 279func GetVimCommand (...) 280if a: 0 == 0 281let name = 'NONE' 282else 283let name = a: 1 284endif 285let cmd = GetVimProg () 286let cmd = substitute ( cmd , '-u \f\+' , '-u ' . name , '' ) 287if cmd !~ '-u ' . name 288let cmd = cmd . ' -u ' . name 289endif 290let cmd .= ' --headless -i NONE' 291let cmd = substitute ( cmd , 'VIMRUNTIME=\S\+' , '' , '' ) 292 293" If using valgrind, make sure every run uses a different log file. 294if cmd =~ 'valgrind.*--log-file=' 295let cmd = substitute ( cmd , '--log-file=\(\S*\)' , '--log-file=\1.' . g: valgrind_cnt , '' ) 296let g: valgrind_cnt += 1 297endif 298 299return cmd 300endfunc 301 302" Return one when it looks like the tests are run with valgrind, which means 303" that everything is much slower. 304func RunningWithValgrind () 305return GetVimCommand () =~ '\<valgrind\>' 306endfunc 307 308" Get the command to run Vim, with --clean instead of "-u NONE". 309func GetVimCommandClean () 310let cmd = GetVimCommand () 311let cmd = substitute ( cmd , '-u NONE' , '--clean' , '' ) 312let cmd = substitute ( cmd , '--headless' , '' , '' ) 313 314" Force using utf-8, Vim may pick up something else from the environment. 315" let cmd ..= ' --cmd "set enc=utf8" ' 316 317" Optionally run Vim under valgrind 318" let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd 319 320return cmd 321endfunc 322 323" Get the command to run Vim, with --clean, and force to run in terminal so it 324" won't start a new GUI. 325func GetVimCommandCleanTerm () 326" Add -v to have gvim run in the terminal (if possible) 327return GetVimCommandClean () .. ' -v ' 328endfunc 329 330" Run Vim, using the "vimcmd" file and "-u NORC". 331" "before" is a list of Vim commands to be executed before loading plugins. 332" "after" is a list of Vim commands to be executed after loading plugins. 333" Plugins are not loaded, unless 'loadplugins' is set in "before". 334" Return 1 if Vim could be executed. 335func RunVim ( before , after , arguments ) 336return RunVimPiped ( a: before , a: after , a: arguments , '' ) 337endfunc 338 339func RunVimPiped ( before , after , arguments , pipecmd ) 340let cmd = GetVimCommand () 341let args = '' 342if len ( a: before ) > 0 343call writefile ( a: before , 'Xbefore.vim' ) 344let args .= ' --cmd "so Xbefore.vim"' 345endif 346if len ( a: after ) > 0 347call writefile ( a: after , 'Xafter.vim' ) 348let args .= ' -S Xafter.vim' 349endif 350 351" Optionally run Vim under valgrind 352" let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd 353 354let $ NVIM_LOG_FILE = exists ( $ NVIM_LOG_FILE ) ? $ NVIM_LOG_FILE : 'Xnvim.log' 355" Nvim does not support -Z flag, remove it. 356exe "silent !" .. a: pipecmd .. ' ' .. cmd .. args .. ' ' .. a: arguments ->substitute ( '-Z' , '' , 'g' ) 357 358if len ( a: before ) > 0 359call delete ( 'Xbefore.vim' ) 360endif 361if len ( a: after ) > 0 362call delete ( 'Xafter.vim' ) 363endif 364return 1 365endfunc 366 367func IsRoot () 368if ! has ( 'unix' ) 369return v: false 370elseif $ USER == 'root' || system ( 'id -un' ) =~ '\<root\>' 371return v: true 372endif 373return v: false 374endfunc 375 376" Get all messages but drop the maintainer entry. 377func GetMessages () 378redir => result 379redraw |messages 380redir END 381let msg_list = split ( result , "\n" ) 382" if msg_list->len() > 0 && msg_list[0] =~ 'Messages maintainer:' 383" return msg_list[1:] 384" endif 385return msg_list 386endfunc 387 388" Run the list of commands in 'cmds' and look for 'errstr' in exception. 389" Note that assert_fails() cannot be used in some places and this function 390" can be used. 391func AssertException ( cmds , errstr ) 392let save_exception = '' 393try 394for cmd in a: cmds 395exe cmd 396endfor 397catch 398let save_exception = v: exception 399endtry 400call assert_match ( a: errstr , save_exception ) 401endfunc 402 403" vim: shiftwidth=2 sts=2 expandtab