Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 1 | " Functions shared by several tests. |
| 2 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 3 | " Only load this script once. |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 4 | if exists('*PythonProg') |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 5 | finish |
| 6 | endif |
| 7 | |
Christian Brabandt | eb380b9 | 2025-07-07 20:53:55 +0200 | [diff] [blame] | 8 | source util/view_util.vim |
Bram Moolenaar | 7a39dd7 | 2019-06-23 00:50:15 +0200 | [diff] [blame] | 9 | |
Bram Moolenaar | c255b78 | 2022-11-26 19:16:48 +0000 | [diff] [blame] | 10 | " When 'term' is changed some status requests may be sent. The responses may |
| 11 | " interfere with what is being tested. A short sleep is used to process any of |
| 12 | " those responses first. |
| 13 | func WaitForResponses() |
| 14 | sleep 50m |
| 15 | endfunc |
| 16 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 17 | " Get the name of the Python executable. |
| 18 | " Also keeps it in s:python. |
| 19 | func PythonProg() |
| 20 | " This test requires the Python command to run the test server. |
| 21 | " This most likely only works on Unix and Windows. |
| 22 | if has('unix') |
| 23 | " We also need the job feature or the pkill command to make sure the server |
| 24 | " can be stopped. |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 25 | if !(has('job') || executable('pkill')) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 26 | return '' |
| 27 | endif |
Yee Cheng Chin | cef8ab2 | 2024-11-04 20:26:47 +0100 | [diff] [blame] | 28 | if executable('python3') |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 29 | let s:python = 'python3' |
Yee Cheng Chin | cef8ab2 | 2024-11-04 20:26:47 +0100 | [diff] [blame] | 30 | elseif executable('python') |
| 31 | let s:python = 'python' |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 32 | else |
| 33 | return '' |
| 34 | end |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 35 | elseif has('win32') |
| 36 | " Use Python Launcher for Windows (py.exe) if available. |
Bram Moolenaar | 1bb0da2 | 2021-05-02 19:15:05 +0200 | [diff] [blame] | 37 | " NOTE: if you get a "Python was not found" error, disable the Python |
| 38 | " shortcuts in "Windows menu / Settings / Manage App Execution Aliases". |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 39 | if executable('py.exe') |
| 40 | let s:python = 'py.exe' |
| 41 | elseif executable('python.exe') |
| 42 | let s:python = 'python.exe' |
| 43 | else |
| 44 | return '' |
| 45 | endif |
| 46 | else |
| 47 | return '' |
| 48 | endif |
| 49 | return s:python |
| 50 | endfunc |
| 51 | |
| 52 | " Run "cmd". Returns the job if using a job. |
| 53 | func RunCommand(cmd) |
Bram Moolenaar | 4a070cc | 2020-05-07 18:16:35 +0200 | [diff] [blame] | 54 | " Running an external command can occasionally be slow or fail. |
| 55 | let g:test_is_flaky = 1 |
| 56 | |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 57 | let job = 0 |
| 58 | if has('job') |
| 59 | let job = job_start(a:cmd, {"stoponexit": "hup"}) |
| 60 | call job_setoptions(job, {"stoponexit": "kill"}) |
| 61 | elseif has('win32') |
Milly | 4f5681d | 2024-10-20 11:06:00 +0200 | [diff] [blame] | 62 | exe 'silent !start cmd /D /c start "test_channel" ' . a:cmd |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 63 | else |
| 64 | exe 'silent !' . a:cmd . '&' |
| 65 | endif |
| 66 | return job |
| 67 | endfunc |
| 68 | |
| 69 | " Read the port number from the Xportnr file. |
| 70 | func GetPort() |
| 71 | let l = [] |
Bram Moolenaar | a906e8e | 2022-09-01 18:42:32 +0100 | [diff] [blame] | 72 | " with 200 it sometimes failed, with 400 is rarily failed |
| 73 | for i in range(600) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 74 | try |
| 75 | let l = readfile("Xportnr") |
| 76 | catch |
| 77 | endtry |
| 78 | if len(l) >= 1 |
| 79 | break |
| 80 | endif |
| 81 | sleep 10m |
| 82 | endfor |
| 83 | call delete("Xportnr") |
| 84 | |
| 85 | if len(l) == 0 |
| 86 | " Can't make the connection, give up. |
| 87 | return 0 |
| 88 | endif |
| 89 | return l[0] |
| 90 | endfunc |
| 91 | |
| 92 | " Run a Python server for "cmd" and call "testfunc". |
| 93 | " Always kills the server before returning. |
| 94 | func RunServer(cmd, testfunc, args) |
| 95 | " The Python program writes the port number in Xportnr. |
| 96 | call delete("Xportnr") |
| 97 | |
| 98 | if len(a:args) == 1 |
| 99 | let arg = ' ' . a:args[0] |
| 100 | else |
| 101 | let arg = '' |
| 102 | endif |
| 103 | let pycmd = s:python . " " . a:cmd . arg |
| 104 | |
| 105 | try |
| 106 | let g:currentJob = RunCommand(pycmd) |
| 107 | |
Bram Moolenaar | 06d32a0 | 2022-09-03 13:58:47 +0100 | [diff] [blame] | 108 | " Wait for some time for the port number to be there. |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 109 | let port = GetPort() |
| 110 | if port == 0 |
Bram Moolenaar | 5fbbec1 | 2022-09-03 22:08:11 +0100 | [diff] [blame] | 111 | call assert_report(strftime("%H:%M:%S") .. " Can't start " .. a:cmd) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 112 | return |
| 113 | endif |
| 114 | |
| 115 | call call(function(a:testfunc), [port]) |
James McCoy | 43cb8e1 | 2023-11-02 20:05:38 +0100 | [diff] [blame] | 116 | catch /E901.*Address family for hostname not supported/ |
| 117 | throw 'Skipped: Invalid network setup ("' .. v:exception .. '" in ' .. v:throwpoint .. ')' |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 118 | catch |
Bram Moolenaar | 06d32a0 | 2022-09-03 13:58:47 +0100 | [diff] [blame] | 119 | call assert_report('Caught exception: "' . v:exception . '" in ' . v:throwpoint) |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 120 | finally |
| 121 | call s:kill_server(a:cmd) |
| 122 | endtry |
| 123 | endfunc |
| 124 | |
| 125 | func s:kill_server(cmd) |
| 126 | if has('job') |
| 127 | if exists('g:currentJob') |
| 128 | call job_stop(g:currentJob) |
| 129 | unlet g:currentJob |
| 130 | endif |
| 131 | elseif has('win32') |
| 132 | let cmd = substitute(a:cmd, ".py", '', '') |
| 133 | call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq ' . cmd . '"') |
| 134 | else |
| 135 | call system("pkill -f " . a:cmd) |
| 136 | endif |
| 137 | endfunc |
| 138 | |
Yee Cheng Chin | e70587d | 2025-02-13 20:55:45 +0100 | [diff] [blame] | 139 | " Callback function to be invoked by a child terminal job. The parent could |
| 140 | " then wait for the notification using WaitForChildNotification() |
| 141 | let g:child_notification = 0 |
| 142 | func Tapi_notify_parent(bufnum, arglist) |
| 143 | let g:child_notification = 1 |
| 144 | endfunc |
| 145 | |
| 146 | " Generates a command that we can pass to a terminal job that it uses to |
| 147 | " notify us. Argument 'escape' will specify whether to escape the double |
| 148 | " quote. |
| 149 | func TermNotifyParentCmd(escape) |
| 150 | call assert_false(has("win32"), 'Windows does not support terminal API right now. Use another method to synchronize timing.') |
| 151 | let cmd = '\033]51;["call", "Tapi_notify_parent", []]\007' |
| 152 | if a:escape |
| 153 | return escape(cmd, '"') |
| 154 | endif |
| 155 | return cmd |
| 156 | endfunc |
| 157 | |
| 158 | " Wait for a child process to notify us. This allows us to sequence events in |
| 159 | " conjunction with the child. Currently the only supported notification method |
| 160 | " is for a terminal job to call Tapi_notify_parent() using terminal API. |
| 161 | func WaitForChildNotification(...) |
| 162 | let timeout = get(a:000, 0, 5000) |
| 163 | call WaitFor({-> g:child_notification == 1}, timeout) |
| 164 | let g:child_notification = 0 |
| 165 | endfunc |
| 166 | |
Bram Moolenaar | 769e9d2 | 2018-04-11 20:53:49 +0200 | [diff] [blame] | 167 | " Wait for up to five seconds for "expr" to become true. "expr" can be a |
Bram Moolenaar | 13deab8 | 2017-11-04 18:48:43 +0100 | [diff] [blame] | 168 | " stringified expression to evaluate, or a funcref without arguments. |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 169 | " Using a lambda works best. Example: |
| 170 | " call WaitFor({-> status == "ok"}) |
| 171 | " |
Bram Moolenaar | 5d7ead3 | 2018-02-27 17:17:42 +0100 | [diff] [blame] | 172 | " A second argument can be used to specify a different timeout in msec. |
Bram Moolenaar | 13deab8 | 2017-11-04 18:48:43 +0100 | [diff] [blame] | 173 | " |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 174 | " When successful the time slept is returned. |
| 175 | " When running into the timeout an exception is thrown, thus the function does |
| 176 | " not return. |
Bram Moolenaar | cdb7e1b | 2017-07-19 19:55:58 +0200 | [diff] [blame] | 177 | func WaitFor(expr, ...) |
Bram Moolenaar | 769e9d2 | 2018-04-11 20:53:49 +0200 | [diff] [blame] | 178 | let timeout = get(a:000, 0, 5000) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 179 | let slept = s:WaitForCommon(a:expr, v:null, timeout) |
| 180 | if slept < 0 |
| 181 | throw 'WaitFor() timed out after ' . timeout . ' msec' |
| 182 | endif |
| 183 | return slept |
| 184 | endfunc |
| 185 | |
| 186 | " Wait for up to five seconds for "assert" to return zero. "assert" must be a |
| 187 | " (lambda) function containing one assert function. Example: |
| 188 | " call WaitForAssert({-> assert_equal("dead", job_status(job)}) |
| 189 | " |
| 190 | " A second argument can be used to specify a different timeout in msec. |
| 191 | " |
| 192 | " Return zero for success, one for failure (like the assert function). |
Yee Cheng Chin | e70587d | 2025-02-13 20:55:45 +0100 | [diff] [blame] | 193 | func g:WaitForAssert(assert, ...) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 194 | let timeout = get(a:000, 0, 5000) |
| 195 | if s:WaitForCommon(v:null, a:assert, timeout) < 0 |
| 196 | return 1 |
| 197 | endif |
| 198 | return 0 |
| 199 | endfunc |
| 200 | |
| 201 | " Common implementation of WaitFor() and WaitForAssert(). |
| 202 | " Either "expr" or "assert" is not v:null |
| 203 | " Return the waiting time for success, -1 for failure. |
| 204 | func s:WaitForCommon(expr, assert, timeout) |
Bram Moolenaar | f267f8b | 2016-08-22 21:40:29 +0200 | [diff] [blame] | 205 | " using reltime() is more accurate, but not always available |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 206 | let slept = 0 |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 207 | if exists('*reltimefloat') |
Bram Moolenaar | f267f8b | 2016-08-22 21:40:29 +0200 | [diff] [blame] | 208 | let start = reltime() |
Bram Moolenaar | f267f8b | 2016-08-22 21:40:29 +0200 | [diff] [blame] | 209 | endif |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 210 | |
| 211 | while 1 |
| 212 | if type(a:expr) == v:t_func |
| 213 | let success = a:expr() |
| 214 | elseif type(a:assert) == v:t_func |
| 215 | let success = a:assert() == 0 |
| 216 | else |
| 217 | let success = eval(a:expr) |
| 218 | endif |
| 219 | if success |
Bram Moolenaar | c20e0d5 | 2017-11-02 18:19:19 +0100 | [diff] [blame] | 220 | return slept |
| 221 | endif |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 222 | |
| 223 | if slept >= a:timeout |
| 224 | break |
| 225 | endif |
| 226 | if type(a:assert) == v:t_func |
| 227 | " Remove the error added by the assert function. |
| 228 | call remove(v:errors, -1) |
| 229 | endif |
| 230 | |
Yee Cheng Chin | e70587d | 2025-02-13 20:55:45 +0100 | [diff] [blame] | 231 | sleep 1m |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 232 | if exists('*reltimefloat') |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 233 | let slept = float2nr(reltimefloat(reltime(start)) * 1000) |
| 234 | else |
Yee Cheng Chin | e70587d | 2025-02-13 20:55:45 +0100 | [diff] [blame] | 235 | let slept += 1 |
Bram Moolenaar | f267f8b | 2016-08-22 21:40:29 +0200 | [diff] [blame] | 236 | endif |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 237 | endwhile |
| 238 | |
| 239 | return -1 " timed out |
Bram Moolenaar | 321efdd | 2016-07-15 17:09:11 +0200 | [diff] [blame] | 240 | endfunc |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 241 | |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 242 | |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 243 | " Wait for up to a given milliseconds. |
| 244 | " With the +timers feature this waits for key-input by getchar(), Resume() |
| 245 | " feeds key-input and resumes process. Return time waited in milliseconds. |
| 246 | " Without +timers it uses simply :sleep. |
| 247 | func Standby(msec) |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 248 | if has('timers') && exists('*reltimefloat') |
Bram Moolenaar | 01688ad | 2016-10-27 20:00:07 +0200 | [diff] [blame] | 249 | let start = reltime() |
| 250 | let g:_standby_timer = timer_start(a:msec, function('s:feedkeys')) |
| 251 | call getchar() |
| 252 | return float2nr(reltimefloat(reltime(start)) * 1000) |
| 253 | else |
| 254 | execute 'sleep ' a:msec . 'm' |
| 255 | return a:msec |
| 256 | endif |
| 257 | endfunc |
| 258 | |
| 259 | func Resume() |
| 260 | if exists('g:_standby_timer') |
| 261 | call timer_stop(g:_standby_timer) |
| 262 | call s:feedkeys(0) |
| 263 | unlet g:_standby_timer |
| 264 | endif |
| 265 | endfunc |
| 266 | |
| 267 | func s:feedkeys(timer) |
| 268 | call feedkeys('x', 'nt') |
| 269 | endfunc |
| 270 | |
Christopher Plewright | d55bfca | 2022-11-10 18:21:30 +0000 | [diff] [blame] | 271 | " Get the name of the Vim executable that we expect has been build in the src |
| 272 | " directory. |
| 273 | func s:GetJustBuildVimExe() |
| 274 | if has("win32") |
| 275 | if !filereadable('..\vim.exe') && filereadable('..\vimd.exe') |
| 276 | " looks like the debug executable was intentionally build, so use it |
| 277 | return '..\vimd.exe' |
| 278 | endif |
| 279 | return '..\vim.exe' |
| 280 | endif |
| 281 | return '../vim' |
| 282 | endfunc |
| 283 | |
Bram Moolenaar | 2228689 | 2020-11-05 20:50:51 +0100 | [diff] [blame] | 284 | " Get $VIMPROG to run the Vim executable. |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 285 | " The Makefile writes it as the first line in the "vimcmd" file. |
Christopher Plewright | d55bfca | 2022-11-10 18:21:30 +0000 | [diff] [blame] | 286 | " Falls back to the Vim executable in the src directory. |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 287 | func GetVimProg() |
Christopher Plewright | d55bfca | 2022-11-10 18:21:30 +0000 | [diff] [blame] | 288 | if filereadable('vimcmd') |
| 289 | return readfile('vimcmd')[0] |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 290 | endif |
Christopher Plewright | d55bfca | 2022-11-10 18:21:30 +0000 | [diff] [blame] | 291 | echo 'Cannot read the "vimcmd" file, falling back to ../vim.' |
| 292 | |
| 293 | " Probably the script was sourced instead of running "make". |
| 294 | " We assume Vim was just build in the src directory then. |
| 295 | return s:GetJustBuildVimExe() |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 296 | endfunc |
| 297 | |
Bram Moolenaar | 248be5c | 2018-05-05 15:47:19 +0200 | [diff] [blame] | 298 | let g:valgrind_cnt = 1 |
| 299 | |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 300 | " Get the command to run Vim, with -u NONE and --not-a-term arguments. |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 301 | " If there is an argument use it instead of "NONE". |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 302 | func GetVimCommand(...) |
Christopher Plewright | d55bfca | 2022-11-10 18:21:30 +0000 | [diff] [blame] | 303 | if filereadable('vimcmd') |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 304 | let lines = readfile('vimcmd') |
Christopher Plewright | d55bfca | 2022-11-10 18:21:30 +0000 | [diff] [blame] | 305 | else |
| 306 | echo 'Cannot read the "vimcmd" file, falling back to ../vim.' |
| 307 | let lines = [s:GetJustBuildVimExe()] |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 308 | endif |
Christopher Plewright | d55bfca | 2022-11-10 18:21:30 +0000 | [diff] [blame] | 309 | |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 310 | if a:0 == 0 |
| 311 | let name = 'NONE' |
| 312 | else |
| 313 | let name = a:1 |
| 314 | endif |
Bram Moolenaar | 6318205 | 2017-10-07 20:03:23 +0200 | [diff] [blame] | 315 | " For Unix Makefile writes the command to use in the second line of the |
| 316 | " "vimcmd" file, including environment options. |
| 317 | " Other Makefiles just write the executable in the first line, so fall back |
Bram Moolenaar | 248be5c | 2018-05-05 15:47:19 +0200 | [diff] [blame] | 318 | " to that if there is no second line or it is empty. |
| 319 | if len(lines) > 1 && lines[1] != '' |
| 320 | let cmd = lines[1] |
| 321 | else |
| 322 | let cmd = lines[0] |
| 323 | endif |
| 324 | |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 325 | let cmd = substitute(cmd, '-u \f\+', '-u ' . name, '') |
| 326 | if cmd !~ '-u '. name |
| 327 | let cmd = cmd . ' -u ' . name |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 328 | endif |
| 329 | let cmd .= ' --not-a-term' |
Bram Moolenaar | 2d12c25 | 2022-06-13 21:42:45 +0100 | [diff] [blame] | 330 | let cmd .= ' --gui-dialog-file guidialogfile' |
Christian Brabandt | 627c950 | 2024-02-10 13:02:17 +0100 | [diff] [blame] | 331 | " remove any environment variables |
Yegappan Lakshmanan | 35b867b | 2024-03-09 15:44:19 +0100 | [diff] [blame] | 332 | let cmd = substitute(cmd, '[A-Z_]\+=\S\+ *', '', 'g') |
Bram Moolenaar | 248be5c | 2018-05-05 15:47:19 +0200 | [diff] [blame] | 333 | |
| 334 | " If using valgrind, make sure every run uses a different log file. |
| 335 | if cmd =~ 'valgrind.*--log-file=' |
Bram Moolenaar | 657a826 | 2020-07-15 18:29:18 +0200 | [diff] [blame] | 336 | let cmd = substitute(cmd, '--log-file=\(\S*\)', '--log-file=\1.' . g:valgrind_cnt, '') |
Bram Moolenaar | 248be5c | 2018-05-05 15:47:19 +0200 | [diff] [blame] | 337 | let g:valgrind_cnt += 1 |
| 338 | endif |
| 339 | |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 340 | return cmd |
| 341 | endfunc |
| 342 | |
Bram Moolenaar | e366ed4 | 2022-06-19 20:13:56 +0100 | [diff] [blame] | 343 | " Return one when it looks like the tests are run with valgrind, which means |
| 344 | " that everything is much slower. |
| 345 | func RunningWithValgrind() |
| 346 | return GetVimCommand() =~ '\<valgrind\>' |
| 347 | endfunc |
| 348 | |
Aliaksei Budavei | f1d83c4 | 2024-11-02 15:51:14 +0100 | [diff] [blame] | 349 | func RunningAsan() |
| 350 | return exists("$ASAN_OPTIONS") |
| 351 | endfunc |
| 352 | |
| 353 | func ValgrindOrAsan() |
| 354 | return RunningWithValgrind() || RunningAsan() |
| 355 | endfun |
| 356 | |
Bram Moolenaar | f169996 | 2019-08-31 17:48:19 +0200 | [diff] [blame] | 357 | " Get the command to run Vim, with --clean instead of "-u NONE". |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 358 | func GetVimCommandClean() |
| 359 | let cmd = GetVimCommand() |
| 360 | let cmd = substitute(cmd, '-u NONE', '--clean', '') |
| 361 | let cmd = substitute(cmd, '--not-a-term', '', '') |
Bram Moolenaar | 453ce7c | 2018-10-12 22:15:12 +0200 | [diff] [blame] | 362 | |
Bram Moolenaar | 0fdddee | 2019-09-01 15:26:23 +0200 | [diff] [blame] | 363 | " Force using utf-8, Vim may pick up something else from the environment. |
| 364 | let cmd ..= ' --cmd "set enc=utf8" ' |
| 365 | |
Bram Moolenaar | 453ce7c | 2018-10-12 22:15:12 +0200 | [diff] [blame] | 366 | " Optionally run Vim under valgrind |
| 367 | " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd |
| 368 | |
Bram Moolenaar | da65058 | 2018-02-20 15:51:40 +0100 | [diff] [blame] | 369 | return cmd |
| 370 | endfunc |
| 371 | |
Bram Moolenaar | 0d70202 | 2019-07-04 14:20:41 +0200 | [diff] [blame] | 372 | " Get the command to run Vim, with --clean, and force to run in terminal so it |
| 373 | " won't start a new GUI. |
| 374 | func GetVimCommandCleanTerm() |
| 375 | " Add -v to have gvim run in the terminal (if possible) |
| 376 | return GetVimCommandClean() .. ' -v ' |
| 377 | endfunc |
| 378 | |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 379 | " Run Vim, using the "vimcmd" file and "-u NORC". |
Bram Moolenaar | 3a93838 | 2016-08-07 16:36:40 +0200 | [diff] [blame] | 380 | " "before" is a list of Vim commands to be executed before loading plugins. |
| 381 | " "after" is a list of Vim commands to be executed after loading plugins. |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 382 | " Plugins are not loaded, unless 'loadplugins' is set in "before". |
| 383 | " Return 1 if Vim could be executed. |
Bram Moolenaar | 472a0a8 | 2016-08-06 22:31:42 +0200 | [diff] [blame] | 384 | func RunVim(before, after, arguments) |
Bram Moolenaar | 7a9a5f4 | 2016-08-08 22:34:14 +0200 | [diff] [blame] | 385 | return RunVimPiped(a:before, a:after, a:arguments, '') |
Bram Moolenaar | 3a93838 | 2016-08-07 16:36:40 +0200 | [diff] [blame] | 386 | endfunc |
| 387 | |
| 388 | func RunVimPiped(before, after, arguments, pipecmd) |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 389 | let cmd = GetVimCommand() |
Bram Moolenaar | ba98bef | 2016-08-07 15:51:39 +0200 | [diff] [blame] | 390 | let args = '' |
Bram Moolenaar | 472a0a8 | 2016-08-06 22:31:42 +0200 | [diff] [blame] | 391 | if len(a:before) > 0 |
| 392 | call writefile(a:before, 'Xbefore.vim') |
| 393 | let args .= ' --cmd "so Xbefore.vim"' |
| 394 | endif |
| 395 | if len(a:after) > 0 |
| 396 | call writefile(a:after, 'Xafter.vim') |
| 397 | let args .= ' -S Xafter.vim' |
| 398 | endif |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 399 | |
Bram Moolenaar | f48ee3c | 2019-12-06 22:18:20 +0100 | [diff] [blame] | 400 | " Optionally run Vim under valgrind |
| 401 | " let cmd = 'valgrind --tool=memcheck --leak-check=yes --num-callers=25 --log-file=valgrind ' . cmd |
| 402 | |
Christian Brabandt | 52eb0b8 | 2024-02-10 13:50:54 +0100 | [diff] [blame] | 403 | exe "silent !" .. a:pipecmd .. ' ' .. cmd .. args .. ' ' .. a:arguments |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 404 | |
Bram Moolenaar | 472a0a8 | 2016-08-06 22:31:42 +0200 | [diff] [blame] | 405 | if len(a:before) > 0 |
| 406 | call delete('Xbefore.vim') |
| 407 | endif |
| 408 | if len(a:after) > 0 |
| 409 | call delete('Xafter.vim') |
| 410 | endif |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 411 | return 1 |
| 412 | endfunc |
Bram Moolenaar | 07282f0 | 2019-10-10 16:46:17 +0200 | [diff] [blame] | 413 | |
| 414 | func IsRoot() |
| 415 | if !has('unix') |
| 416 | return v:false |
| 417 | elseif $USER == 'root' || system('id -un') =~ '\<root\>' |
| 418 | return v:true |
| 419 | endif |
| 420 | return v:false |
| 421 | endfunc |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 422 | |
Bram Moolenaar | 88e6cc2 | 2020-04-30 19:19:29 +0200 | [diff] [blame] | 423 | " Get all messages but drop the maintainer entry. |
| 424 | func GetMessages() |
| 425 | redir => result |
| 426 | redraw | messages |
| 427 | redir END |
| 428 | let msg_list = split(result, "\n") |
| 429 | if msg_list->len() > 0 && msg_list[0] =~ 'Messages maintainer:' |
| 430 | return msg_list[1:] |
| 431 | endif |
| 432 | return msg_list |
| 433 | endfunc |
| 434 | |
Bram Moolenaar | ab58946 | 2020-07-06 21:03:06 +0200 | [diff] [blame] | 435 | " Run the list of commands in 'cmds' and look for 'errstr' in exception. |
| 436 | " Note that assert_fails() cannot be used in some places and this function |
| 437 | " can be used. |
| 438 | func AssertException(cmds, errstr) |
| 439 | let save_exception = '' |
| 440 | try |
| 441 | for cmd in a:cmds |
| 442 | exe cmd |
| 443 | endfor |
| 444 | catch |
| 445 | let save_exception = v:exception |
| 446 | endtry |
| 447 | call assert_match(a:errstr, save_exception) |
| 448 | endfunc |
| 449 | |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 450 | " vim: shiftwidth=2 sts=2 expandtab |