Christian Brabandt | eb380b9 | 2025-07-07 20:53:55 +0200 | [diff] [blame] | 1 | source util/shared.vim |
| 2 | source util/term_util.vim |
Bram Moolenaar | 07282f0 | 2019-10-10 16:46:17 +0200 | [diff] [blame] | 3 | |
Christian Brabandt | b0905e2 | 2025-07-07 20:39:29 +0200 | [diff] [blame] | 4 | " uses line-continuation |
| 5 | let s:cpo_save = &cpo |
| 6 | set cpo&vim |
| 7 | |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 8 | command -nargs=1 MissingFeature throw 'Skipped: ' .. <args> .. ' feature missing' |
| 9 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 10 | " Command to check for the presence of a feature. |
| 11 | command -nargs=1 CheckFeature call CheckFeature(<f-args>) |
| 12 | func CheckFeature(name) |
Bram Moolenaar | 7929651 | 2020-03-22 16:17:14 +0100 | [diff] [blame] | 13 | if !has(a:name, 1) |
| 14 | throw 'Checking for non-existent feature ' .. a:name |
| 15 | endif |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 16 | if !has(a:name) |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 17 | MissingFeature a:name |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 18 | endif |
| 19 | endfunc |
| 20 | |
Bram Moolenaar | fb773a3 | 2021-07-03 21:37:59 +0200 | [diff] [blame] | 21 | " Command to check for the absence of a feature. |
| 22 | command -nargs=1 CheckNotFeature call CheckNotFeature(<f-args>) |
| 23 | func CheckNotFeature(name) |
| 24 | if !has(a:name, 1) |
| 25 | throw 'Checking for non-existent feature ' .. a:name |
| 26 | endif |
| 27 | if has(a:name) |
| 28 | throw 'Skipped: ' .. a:name .. ' feature present' |
| 29 | endif |
| 30 | endfunc |
| 31 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 32 | " Command to check for the presence of a working option. |
| 33 | command -nargs=1 CheckOption call CheckOption(<f-args>) |
| 34 | func CheckOption(name) |
Bram Moolenaar | c5a8fdc | 2020-03-22 20:13:39 +0100 | [diff] [blame] | 35 | if !exists('&' .. a:name) |
| 36 | throw 'Checking for non-existent option ' .. a:name |
| 37 | endif |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 38 | if !exists('+' .. a:name) |
| 39 | throw 'Skipped: ' .. a:name .. ' option not supported' |
| 40 | endif |
| 41 | endfunc |
| 42 | |
Bram Moolenaar | 15c4760 | 2020-03-26 22:16:48 +0100 | [diff] [blame] | 43 | " Command to check for the presence of a built-in function. |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 44 | command -nargs=1 CheckFunction call CheckFunction(<f-args>) |
| 45 | func CheckFunction(name) |
Bram Moolenaar | 15c4760 | 2020-03-26 22:16:48 +0100 | [diff] [blame] | 46 | if !exists('?' .. a:name) |
| 47 | throw 'Checking for non-existent function ' .. a:name |
| 48 | endif |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 49 | if !exists('*' .. a:name) |
| 50 | throw 'Skipped: ' .. a:name .. ' function missing' |
| 51 | endif |
| 52 | endfunc |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 53 | |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 54 | " Command to check for the presence of an Ex command |
| 55 | command -nargs=1 CheckCommand call CheckCommand(<f-args>) |
| 56 | func CheckCommand(name) |
| 57 | if !exists(':' .. a:name) |
| 58 | throw 'Skipped: ' .. a:name .. ' command not supported' |
| 59 | endif |
| 60 | endfunc |
| 61 | |
| 62 | " Command to check for the presence of a shell command |
| 63 | command -nargs=1 CheckExecutable call CheckExecutable(<f-args>) |
| 64 | func CheckExecutable(name) |
| 65 | if !executable(a:name) |
| 66 | throw 'Skipped: ' .. a:name .. ' program not executable' |
| 67 | endif |
| 68 | endfunc |
| 69 | |
Bram Moolenaar | a161cb5 | 2020-04-30 19:09:35 +0200 | [diff] [blame] | 70 | " Command to check for the presence of python. Argument should have been |
| 71 | " obtained with PythonProg() |
| 72 | func CheckPython(name) |
| 73 | if a:name == '' |
| 74 | throw 'Skipped: python command not available' |
| 75 | endif |
| 76 | endfunc |
| 77 | |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 78 | " Command to check for running on MS-Windows |
| 79 | command CheckMSWindows call CheckMSWindows() |
| 80 | func CheckMSWindows() |
| 81 | if !has('win32') |
| 82 | throw 'Skipped: only works on MS-Windows' |
| 83 | endif |
| 84 | endfunc |
| 85 | |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 86 | " Command to check for NOT running on MS-Windows |
| 87 | command CheckNotMSWindows call CheckNotMSWindows() |
| 88 | func CheckNotMSWindows() |
| 89 | if has('win32') |
| 90 | throw 'Skipped: does not work on MS-Windows' |
| 91 | endif |
| 92 | endfunc |
| 93 | |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 94 | " Command to check for running on Unix |
| 95 | command CheckUnix call CheckUnix() |
| 96 | func CheckUnix() |
| 97 | if !has('unix') |
| 98 | throw 'Skipped: only works on Unix' |
| 99 | endif |
| 100 | endfunc |
Bram Moolenaar | 3c8ee62 | 2019-08-03 22:55:50 +0200 | [diff] [blame] | 101 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 102 | " Command to check for running on Linux |
Bram Moolenaar | 6635ae1 | 2021-03-10 21:46:39 +0100 | [diff] [blame] | 103 | command CheckLinux call CheckLinux() |
| 104 | func CheckLinux() |
| 105 | if !has('linux') |
| 106 | throw 'Skipped: only works on Linux' |
| 107 | endif |
| 108 | endfunc |
| 109 | |
Bram Moolenaar | 8b9abfd | 2021-03-29 20:49:05 +0200 | [diff] [blame] | 110 | " Command to check for not running on a BSD system. |
| 111 | command CheckNotBSD call CheckNotBSD() |
| 112 | func CheckNotBSD() |
| 113 | if has('bsd') |
| 114 | throw 'Skipped: does not work on BSD' |
| 115 | endif |
| 116 | endfunc |
| 117 | |
Bram Moolenaar | f802767 | 2022-09-27 15:55:43 +0100 | [diff] [blame] | 118 | " Command to check for not running on a MacOS |
| 119 | command CheckNotMac call CheckNotMac() |
| 120 | func CheckNotMac() |
| 121 | if has('mac') |
| 122 | throw 'Skipped: does not work on MacOS' |
| 123 | endif |
| 124 | endfunc |
| 125 | |
Bram Moolenaar | 0056ca7 | 2022-09-23 21:26:39 +0100 | [diff] [blame] | 126 | " Command to check for not running on a MacOS M1 system. |
| 127 | command CheckNotMacM1 call CheckNotMacM1() |
| 128 | func CheckNotMacM1() |
| 129 | if has('mac') && system('uname -a') =~ '\<arm64\>' |
| 130 | throw 'Skipped: does not work on MacOS M1' |
| 131 | endif |
| 132 | endfunc |
| 133 | |
Drew Vogel | ea67ba7 | 2025-05-07 22:05:17 +0200 | [diff] [blame] | 134 | func SetupWindowSizeToForVisualDumps() |
| 135 | " The dumps used as reference in these tests were created with a terminal |
| 136 | " width of 75 columns. The vim window that uses the remainder of the GUI |
| 137 | " window width must be at least 3 columns. In theory this means we need the |
| 138 | " GUI shell to provide 78+ columns. However the GTK3 resize logic is flaky, |
| 139 | " sometimes resulting in X11 Configure events that are narrower than |
| 140 | " expected by a number of pixels equal to 2 column widths. Therefore |
| 141 | " setting 80 columns ensures that the GUI shell can still provide 78+ |
| 142 | " columns. This is very likely papering over a GTK3 resize bug but one that |
| 143 | " has existed for a very long time. Establishing this workaround is meant to |
| 144 | " get the GTK3 code working under CI so that we can focus on removing this |
| 145 | " over the long term. |
| 146 | if &columns != 80 |
| 147 | set columns=80 |
| 148 | endif |
| 149 | " Without resetting lines, some GTK3 resize events can carry over between |
| 150 | " tests, which invalidate assumptions in the scrollbar offset calculations. |
| 151 | if &lines != 25 |
| 152 | set lines=25 |
| 153 | endif |
| 154 | endfunc |
| 155 | |
Bram Moolenaar | 3c8ee62 | 2019-08-03 22:55:50 +0200 | [diff] [blame] | 156 | " Command to check that making screendumps is supported. |
Christian Brabandt | eb380b9 | 2025-07-07 20:53:55 +0200 | [diff] [blame] | 157 | " Caller must source util/screendump.vim |
Bram Moolenaar | 3c8ee62 | 2019-08-03 22:55:50 +0200 | [diff] [blame] | 158 | command CheckScreendump call CheckScreendump() |
| 159 | func CheckScreendump() |
Drew Vogel | ea67ba7 | 2025-05-07 22:05:17 +0200 | [diff] [blame] | 160 | let g:check_screendump_called = v:true |
Bram Moolenaar | 3c8ee62 | 2019-08-03 22:55:50 +0200 | [diff] [blame] | 161 | if !CanRunVimInTerminal() |
| 162 | throw 'Skipped: cannot make screendumps' |
| 163 | endif |
Drew Vogel | ea67ba7 | 2025-05-07 22:05:17 +0200 | [diff] [blame] | 164 | if has('gui_running') |
| 165 | call SetupWindowSizeToForVisualDumps() |
| 166 | endif |
Bram Moolenaar | 3c8ee62 | 2019-08-03 22:55:50 +0200 | [diff] [blame] | 167 | endfunc |
| 168 | |
| 169 | " Command to check that we can Run Vim in a terminal window |
| 170 | command CheckRunVimInTerminal call CheckRunVimInTerminal() |
| 171 | func CheckRunVimInTerminal() |
| 172 | if !CanRunVimInTerminal() |
| 173 | throw 'Skipped: cannot run Vim in a terminal window' |
| 174 | endif |
| 175 | endfunc |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 176 | |
| 177 | " Command to check that we can run the GUI |
| 178 | command CheckCanRunGui call CheckCanRunGui() |
| 179 | func CheckCanRunGui() |
| 180 | if !has('gui') || ($DISPLAY == "" && !has('gui_running')) |
Bram Moolenaar | 2514315 | 2019-08-09 14:13:57 +0200 | [diff] [blame] | 181 | throw 'Skipped: cannot start the GUI' |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 182 | endif |
| 183 | endfunc |
| 184 | |
Bram Moolenaar | d6fa7bd | 2021-07-05 14:10:04 +0200 | [diff] [blame] | 185 | " Command to Check for an environment variable |
| 186 | command -nargs=1 CheckEnv call CheckEnv(<f-args>) |
| 187 | func CheckEnv(name) |
Bram Moolenaar | f6d8779 | 2021-07-05 17:49:02 +0200 | [diff] [blame] | 188 | if empty(eval('$' .. a:name)) |
Bram Moolenaar | d6fa7bd | 2021-07-05 14:10:04 +0200 | [diff] [blame] | 189 | throw 'Skipped: Environment variable ' .. a:name .. ' is not set' |
| 190 | endif |
| 191 | endfunc |
| 192 | |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 193 | " Command to Check for pure X11 (no Wayland) |
| 194 | command -nargs=0 CheckX11 call CheckX11() |
| 195 | func CheckX11() |
| 196 | if !empty($WAYLAND_DISPLAY) || empty($DISPLAY) |
| 197 | throw 'Skipped: not pure X11 environment' |
| 198 | endif |
| 199 | endfunc |
| 200 | |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 201 | " Command to check that we are using the GUI |
| 202 | command CheckGui call CheckGui() |
| 203 | func CheckGui() |
| 204 | if !has('gui_running') |
| 205 | throw 'Skipped: only works in the GUI' |
| 206 | endif |
| 207 | endfunc |
| 208 | |
| 209 | " Command to check that not currently using the GUI |
| 210 | command CheckNotGui call CheckNotGui() |
| 211 | func CheckNotGui() |
| 212 | if has('gui_running') |
| 213 | throw 'Skipped: only works in the terminal' |
| 214 | endif |
| 215 | endfunc |
Bram Moolenaar | 07282f0 | 2019-10-10 16:46:17 +0200 | [diff] [blame] | 216 | |
| 217 | " Command to check that test is not running as root |
| 218 | command CheckNotRoot call CheckNotRoot() |
| 219 | func CheckNotRoot() |
| 220 | if IsRoot() |
| 221 | throw 'Skipped: cannot run test as root' |
| 222 | endif |
| 223 | endfunc |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 224 | |
| 225 | " Command to check that the current language is English |
| 226 | command CheckEnglish call CheckEnglish() |
| 227 | func CheckEnglish() |
| 228 | if v:lang != "C" && v:lang !~ '^[Ee]n' |
| 229 | throw 'Skipped: only works in English language environment' |
| 230 | endif |
| 231 | endfunc |
| 232 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 233 | " Command to check that loopback device has IPv6 address |
| 234 | command CheckIPv6 call CheckIPv6() |
| 235 | func CheckIPv6() |
| 236 | if !has('ipv6') |
| 237 | throw 'Skipped: cannot use IPv6 networking' |
| 238 | endif |
| 239 | if !exists('s:ipv6_loopback') |
| 240 | let s:ipv6_loopback = s:CheckIPv6Loopback() |
| 241 | endif |
| 242 | if !s:ipv6_loopback |
| 243 | throw 'Skipped: no IPv6 address for loopback device' |
| 244 | endif |
| 245 | endfunc |
| 246 | |
| 247 | func s:CheckIPv6Loopback() |
| 248 | if has('win32') |
| 249 | return system('netsh interface ipv6 show interface') =~? '\<Loopback\>' |
| 250 | elseif filereadable('/proc/net/if_inet6') |
| 251 | return (match(readfile('/proc/net/if_inet6'), '\slo$') >= 0) |
| 252 | elseif executable('ifconfig') |
| 253 | for dev in ['lo0', 'lo', 'loop'] |
| 254 | " NOTE: On SunOS, need specify address family 'inet6' to get IPv6 info. |
| 255 | if system('ifconfig ' .. dev .. ' inet6 2>/dev/null') =~? '\<inet6\>' |
| 256 | \ || system('ifconfig ' .. dev .. ' 2>/dev/null') =~? '\<inet6\>' |
| 257 | return v:true |
| 258 | endif |
| 259 | endfor |
| 260 | else |
| 261 | " TODO: How to check it in other platforms? |
| 262 | endif |
| 263 | return v:false |
| 264 | endfunc |
| 265 | |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 266 | " Command to check for not running under ASAN |
| 267 | command CheckNotAsan call CheckNotAsan() |
| 268 | func CheckNotAsan() |
| 269 | if execute('version') =~# '-fsanitize=[a-z,]*\<address\>' |
| 270 | throw 'Skipped: does not work with ASAN' |
| 271 | endif |
| 272 | endfunc |
| 273 | |
Bram Moolenaar | cf801d4 | 2022-06-21 18:34:42 +0100 | [diff] [blame] | 274 | " Command to check for not running under valgrind |
| 275 | command CheckNotValgrind call CheckNotValgrind() |
| 276 | func CheckNotValgrind() |
| 277 | if RunningWithValgrind() |
| 278 | throw 'Skipped: does not work well with valgrind' |
| 279 | endif |
| 280 | endfunc |
| 281 | |
Bram Moolenaar | 40bd5a1 | 2021-10-16 21:58:27 +0100 | [diff] [blame] | 282 | " Command to check for X11 based GUI |
| 283 | command CheckX11BasedGui call CheckX11BasedGui() |
| 284 | func CheckX11BasedGui() |
| 285 | if !g:x11_based_gui |
| 286 | throw 'Skipped: requires X11 based GUI' |
| 287 | endif |
| 288 | endfunc |
| 289 | |
Ernie Rael | 559f230 | 2022-07-26 14:44:36 +0100 | [diff] [blame] | 290 | " Command to check that there are two clipboards |
| 291 | command CheckTwoClipboards call CheckTwoClipboards() |
| 292 | func CheckTwoClipboards() |
| 293 | " avoid changing the clipboard here, only X11 supports both |
| 294 | if !has('X11') |
| 295 | throw 'Skipped: requires two clipboards' |
| 296 | endif |
| 297 | endfunc |
| 298 | |
Bram Moolenaar | f8c52e8 | 2021-03-17 12:27:23 +0100 | [diff] [blame] | 299 | " Command to check for satisfying any of the conditions. |
| 300 | " e.g. CheckAnyOf Feature:bsd Feature:sun Linux |
| 301 | command -nargs=+ CheckAnyOf call CheckAnyOf(<f-args>) |
| 302 | func CheckAnyOf(...) |
| 303 | let excp = [] |
| 304 | for arg in a:000 |
| 305 | try |
| 306 | exe 'Check' .. substitute(arg, ':', ' ', '') |
| 307 | return |
| 308 | catch /^Skipped:/ |
| 309 | let excp += [substitute(v:exception, '^Skipped:\s*', '', '')] |
| 310 | endtry |
| 311 | endfor |
| 312 | throw 'Skipped: ' .. join(excp, '; ') |
| 313 | endfunc |
| 314 | |
| 315 | " Command to check for satisfying all of the conditions. |
| 316 | " e.g. CheckAllOf Unix Gui Option:ballooneval |
| 317 | command -nargs=+ CheckAllOf call CheckAllOf(<f-args>) |
| 318 | func CheckAllOf(...) |
| 319 | for arg in a:000 |
| 320 | exe 'Check' .. substitute(arg, ':', ' ', '') |
| 321 | endfor |
| 322 | endfunc |
| 323 | |
Christian Brabandt | 6f14ef4 | 2025-02-09 17:05:21 +0100 | [diff] [blame] | 324 | " Check if running under Github Actions |
| 325 | command CheckGithubActions call CheckGithubActions() |
| 326 | func CheckGithubActions() |
| 327 | if expand('$GITHUB_ACTIONS') ==# 'true' |
| 328 | throw "Skipped: FIXME: this test doesn't work on Github Actions CI" |
| 329 | endif |
| 330 | endfunc |
Christian Brabandt | b0905e2 | 2025-07-07 20:39:29 +0200 | [diff] [blame] | 331 | |
| 332 | let &cpo = s:cpo_save |
| 333 | unlet s:cpo_save |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 334 | " vim: shiftwidth=2 sts=2 expandtab |