Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 1 | " This script is sourced while editing the .vim file with the tests. |
| 2 | " When the script is successful the .res file will be created. |
| 3 | " Errors are appended to the test.log file. |
| 4 | " |
Bram Moolenaar | befb366 | 2016-02-20 14:41:40 +0100 | [diff] [blame] | 5 | " To execute only specific test functions, add a second argument. It will be |
Bram Moolenaar | e219f73 | 2019-11-30 15:34:08 +0100 | [diff] [blame] | 6 | " matched against the names of the Test_ function. E.g.: |
Bram Moolenaar | befb366 | 2016-02-20 14:41:40 +0100 | [diff] [blame] | 7 | " ../vim -u NONE -S runtest.vim test_channel.vim open_delay |
| 8 | " The output can be found in the "messages" file. |
| 9 | " |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 10 | " If the environment variable $TEST_FILTER is set then only test functions |
| 11 | " matching this pattern are executed. E.g. for sh/bash: |
| 12 | " export TEST_FILTER=Test_channel |
| 13 | " For csh: |
| 14 | " setenv TEST_FILTER Test_channel |
| 15 | " |
Bram Moolenaar | dae453f | 2021-08-07 17:20:16 +0200 | [diff] [blame] | 16 | " If the environment variable $TEST_SKIP_PAT is set then test functions |
| 17 | " matching this pattern will be skipped. It's the opposite of $TEST_FILTER. |
| 18 | " |
Bram Moolenaar | 6ca6ca4 | 2020-07-27 19:47:07 +0200 | [diff] [blame] | 19 | " While working on a test you can make $TEST_NO_RETRY non-empty to not retry: |
| 20 | " export TEST_NO_RETRY=yes |
| 21 | " |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 22 | " To ignore failure for tests that are known to fail in a certain environment, |
| 23 | " set $TEST_MAY_FAIL to a comma separated list of function names. E.g. for |
| 24 | " sh/bash: |
| 25 | " export TEST_MAY_FAIL=Test_channel_one,Test_channel_other |
| 26 | " The failure report will then not be included in the test.log file and |
| 27 | " "make test" will not fail. |
| 28 | " |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 29 | " The test script may contain anything, only functions that start with |
| 30 | " "Test_" are special. These will be invoked and should contain assert |
| 31 | " functions. See test_assert.vim for an example. |
| 32 | " |
| 33 | " It is possible to source other files that contain "Test_" functions. This |
| 34 | " can speed up testing, since Vim does not need to restart. But be careful |
| 35 | " that the tests do not interfere with each other. |
| 36 | " |
| 37 | " If an error cannot be detected properly with an assert function add the |
| 38 | " error to the v:errors list: |
| 39 | " call add(v:errors, 'test foo failed: Cannot find xyz') |
| 40 | " |
| 41 | " If preparation for each Test_ function is needed, define a SetUp function. |
| 42 | " It will be called before each Test_ function. |
| 43 | " |
| 44 | " If cleanup after each Test_ function is needed, define a TearDown function. |
| 45 | " It will be called after each Test_ function. |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 46 | " |
| 47 | " When debugging a test it can be useful to add messages to v:errors: |
Bram Moolenaar | 8ad16da | 2019-01-06 15:29:57 +0100 | [diff] [blame] | 48 | " call add(v:errors, "this happened") |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 49 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 50 | |
| 51 | " Without the +eval feature we can't run these tests, bail out. |
Bram Moolenaar | b96a32e | 2020-08-13 18:59:55 +0200 | [diff] [blame] | 52 | silent! while 0 |
| 53 | qa! |
| 54 | silent! endwhile |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 55 | |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 56 | " In the GUI we can always change the screen size. |
| 57 | if has('gui_running') |
matveyt | ad3b6a3 | 2024-12-08 10:26:51 +0100 | [diff] [blame] | 58 | if has('gui_gtk') |
Aliaksei Budavei | c4eb1cb | 2025-06-08 15:52:42 +0200 | [diff] [blame] | 59 | " Use e.g. SetUp() and TearDown() to change "&guifont" when needed; |
| 60 | " otherwise, keep the following value to match current screendumps. |
matveyt | ad3b6a3 | 2024-12-08 10:26:51 +0100 | [diff] [blame] | 61 | set guifont=Monospace\ 10 |
| 62 | endif |
Aliaksei Budavei | c4eb1cb | 2025-06-08 15:52:42 +0200 | [diff] [blame] | 63 | |
| 64 | func s:SetDefaultOptionsForGUIBuilds() |
| 65 | set columns=80 lines=25 |
| 66 | endfunc |
| 67 | else |
| 68 | func s:SetDefaultOptionsForGUIBuilds() |
| 69 | endfunc |
Bram Moolenaar | 18aa13d | 2020-07-11 13:09:36 +0200 | [diff] [blame] | 70 | endif |
| 71 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 72 | " Check that the screen size is at least 24 x 80 characters. |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 73 | if &lines < 24 || &columns < 80 |
Bram Moolenaar | 0b5dc64 | 2019-08-11 22:56:15 +0200 | [diff] [blame] | 74 | let error = 'Screen size too small! Tests require at least 24 lines with 80 characters, got ' .. &lines .. ' lines with ' .. &columns .. ' characters' |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 75 | echoerr error |
| 76 | split test.log |
| 77 | $put =error |
Bram Moolenaar | 45aa07d | 2019-06-15 18:20:38 +0200 | [diff] [blame] | 78 | write |
| 79 | split messages |
Bram Moolenaar | 0b5dc64 | 2019-08-11 22:56:15 +0200 | [diff] [blame] | 80 | call append(line('$'), '') |
| 81 | call append(line('$'), 'From ' . expand('%') . ':') |
Bram Moolenaar | 45aa07d | 2019-06-15 18:20:38 +0200 | [diff] [blame] | 82 | call append(line('$'), error) |
| 83 | write |
| 84 | qa! |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 85 | endif |
| 86 | |
Bram Moolenaar | 75ee544 | 2019-06-06 18:05:25 +0200 | [diff] [blame] | 87 | if has('reltime') |
Bram Moolenaar | b9093d5 | 2022-09-23 19:42:31 +0100 | [diff] [blame] | 88 | let s:run_start_time = reltime() |
| 89 | |
| 90 | if !filereadable('starttime') |
| 91 | " first test, store the overall test starting time |
| 92 | let s:test_start_time = localtime() |
| 93 | call writefile([string(s:test_start_time)], 'starttime') |
| 94 | else |
| 95 | " second or later test, read the overall test starting time |
| 96 | let s:test_start_time = readfile('starttime')[0]->str2nr() |
| 97 | endif |
Bram Moolenaar | 75ee544 | 2019-06-06 18:05:25 +0200 | [diff] [blame] | 98 | endif |
| 99 | |
Bakudankun | 29f3a45 | 2021-12-11 12:28:08 +0000 | [diff] [blame] | 100 | " Always use forward slashes. |
| 101 | set shellslash |
| 102 | |
Bram Moolenaar | 89b1042 | 2016-07-12 22:51:22 +0200 | [diff] [blame] | 103 | " Common with all tests on all systems. |
Christian Brabandt | eb380b9 | 2025-07-07 20:53:55 +0200 | [diff] [blame] | 104 | source util/setup.vim |
Bram Moolenaar | 89b1042 | 2016-07-12 22:51:22 +0200 | [diff] [blame] | 105 | |
Bram Moolenaar | 7c2beb4 | 2023-07-08 00:25:56 +0100 | [diff] [blame] | 106 | " Needed for RunningWithValgrind(). |
Christian Brabandt | eb380b9 | 2025-07-07 20:53:55 +0200 | [diff] [blame] | 107 | source util/shared.vim |
Bram Moolenaar | 7c2beb4 | 2023-07-08 00:25:56 +0100 | [diff] [blame] | 108 | |
Christian Brabandt | b0905e2 | 2025-07-07 20:39:29 +0200 | [diff] [blame] | 109 | " Needed for the various Check commands |
Christian Brabandt | eb380b9 | 2025-07-07 20:53:55 +0200 | [diff] [blame] | 110 | source util/check.vim |
Christian Brabandt | b0905e2 | 2025-07-07 20:39:29 +0200 | [diff] [blame] | 111 | |
Bram Moolenaar | c066246 | 2015-12-30 15:49:05 +0100 | [diff] [blame] | 112 | " For consistency run all tests with 'nocompatible' set. |
| 113 | " This also enables use of line continuation. |
| 114 | set nocp viminfo+=nviminfo |
| 115 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 116 | " Use utf-8 by default, instead of whatever the system default happens to be. |
Bram Moolenaar | ed79d1e | 2019-02-22 14:38:58 +0100 | [diff] [blame] | 117 | " Individual tests can overrule this at the top of the file and use |
| 118 | " g:orig_encoding if needed. |
| 119 | let g:orig_encoding = &encoding |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 120 | set encoding=utf-8 |
Bram Moolenaar | ac105ed | 2016-07-21 20:33:32 +0200 | [diff] [blame] | 121 | |
Bram Moolenaar | d8f27b3 | 2018-10-07 15:42:07 +0200 | [diff] [blame] | 122 | " REDIR_TEST_TO_NULL has a very permissive SwapExists autocommand which is for |
| 123 | " the test_name.vim file itself. Replace it here with a more restrictive one, |
| 124 | " so we still catch mistakes. |
Christian Brabandt | 4b2c804 | 2021-11-03 22:31:44 +0000 | [diff] [blame] | 125 | if has("win32") |
| 126 | " replace any '/' directory separators by '\\' |
| 127 | let s:test_script_fname = substitute(expand('%'), '/', '\\', 'g') |
| 128 | else |
| 129 | let s:test_script_fname = expand('%') |
| 130 | endif |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 131 | |
Bram Moolenaar | d8f27b3 | 2018-10-07 15:42:07 +0200 | [diff] [blame] | 132 | au! SwapExists * call HandleSwapExists() |
| 133 | func HandleSwapExists() |
Bram Moolenaar | 8ce4b7e | 2020-08-07 18:12:18 +0200 | [diff] [blame] | 134 | if exists('g:ignoreSwapExists') |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 135 | if type(g:ignoreSwapExists) == v:t_string |
| 136 | let v:swapchoice = g:ignoreSwapExists |
| 137 | endif |
Bram Moolenaar | 8ce4b7e | 2020-08-07 18:12:18 +0200 | [diff] [blame] | 138 | return |
| 139 | endif |
Bram Moolenaar | b073da8 | 2019-07-13 14:47:26 +0200 | [diff] [blame] | 140 | " Ignore finding a swap file for the test script (the user might be |
Bram Moolenaar | d8f27b3 | 2018-10-07 15:42:07 +0200 | [diff] [blame] | 141 | " editing it and do ":make test_name") and the output file. |
Bram Moolenaar | b073da8 | 2019-07-13 14:47:26 +0200 | [diff] [blame] | 142 | " Report finding another swap file and chose 'q' to avoid getting stuck. |
Bram Moolenaar | d8f27b3 | 2018-10-07 15:42:07 +0200 | [diff] [blame] | 143 | if expand('<afile>') == 'messages' || expand('<afile>') =~ s:test_script_fname |
| 144 | let v:swapchoice = 'e' |
Bram Moolenaar | b073da8 | 2019-07-13 14:47:26 +0200 | [diff] [blame] | 145 | else |
| 146 | call assert_report('Unexpected swap file: ' .. v:swapname) |
| 147 | let v:swapchoice = 'q' |
Bram Moolenaar | d8f27b3 | 2018-10-07 15:42:07 +0200 | [diff] [blame] | 148 | endif |
| 149 | endfunc |
| 150 | |
Bram Moolenaar | 7a07354 | 2017-02-01 23:17:36 +0100 | [diff] [blame] | 151 | " Avoid stopping at the "hit enter" prompt |
| 152 | set nomore |
| 153 | |
Bram Moolenaar | c066246 | 2015-12-30 15:49:05 +0100 | [diff] [blame] | 154 | " Output all messages in English. |
| 155 | lang mess C |
| 156 | |
Bram Moolenaar | 10e1d01 | 2020-07-18 22:03:11 +0200 | [diff] [blame] | 157 | " suppress menu translation |
| 158 | if has('gui_running') && exists('did_install_default_menus') |
| 159 | source $VIMRUNTIME/delmenu.vim |
| 160 | set langmenu=none |
| 161 | source $VIMRUNTIME/menu.vim |
| 162 | endif |
| 163 | |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 164 | let s:srcdir = expand('%:p:h:h') |
| 165 | |
Bram Moolenaar | 2690b5a | 2020-07-22 18:14:58 +0200 | [diff] [blame] | 166 | if has('win32') |
| 167 | " avoid prompt that is long or contains a line break |
| 168 | let $PROMPT = '$P$G' |
Bram Moolenaar | 45df2a0 | 2020-07-29 15:03:01 +0200 | [diff] [blame] | 169 | " On MS-Windows t_md and t_me are Vim specific escape sequences. |
| 170 | let s:t_bold = "\x1b[1m" |
| 171 | let s:t_normal = "\x1b[m" |
| 172 | else |
| 173 | let s:t_bold = &t_md |
| 174 | let s:t_normal = &t_me |
Bram Moolenaar | 2690b5a | 2020-07-22 18:14:58 +0200 | [diff] [blame] | 175 | endif |
| 176 | |
Bram Moolenaar | 4b2ce12 | 2020-11-21 21:41:41 +0100 | [diff] [blame] | 177 | if has('mac') |
Dominique Pelle | 923dce2 | 2021-11-21 11:36:04 +0000 | [diff] [blame] | 178 | " In macOS, when starting a shell in a terminal, a bash deprecation warning |
Bram Moolenaar | 4b2ce12 | 2020-11-21 21:41:41 +0100 | [diff] [blame] | 179 | " message is displayed. This breaks the terminal test. Disable the warning |
| 180 | " message. |
| 181 | let $BASH_SILENCE_DEPRECATION_WARNING = 1 |
| 182 | endif |
| 183 | |
Bram Moolenaar | c216a7a | 2022-12-05 13:50:55 +0000 | [diff] [blame] | 184 | |
Bram Moolenaar | 8e8df25 | 2016-05-25 21:23:21 +0200 | [diff] [blame] | 185 | " Prepare for calling test_garbagecollect_now(). |
zeertzjq | 7a734b7 | 2025-07-05 17:02:04 +0200 | [diff] [blame] | 186 | " Also avoids some delays in Insert mode completion. |
Bram Moolenaar | ebf7dfa | 2016-04-14 12:46:51 +0200 | [diff] [blame] | 187 | let v:testing = 1 |
| 188 | |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 189 | " By default, copy each buffer line into allocated memory, so that valgrind can |
| 190 | " detect accessing memory before and after it. |
| 191 | call test_override('alloc_lines', 1) |
| 192 | |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 193 | " Support function: get the alloc ID by name. |
| 194 | function GetAllocId(name) |
| 195 | exe 'split ' . s:srcdir . '/alloc.h' |
Bram Moolenaar | 065ee9a | 2016-01-15 20:53:38 +0100 | [diff] [blame] | 196 | let top = search('typedef enum') |
| 197 | if top == 0 |
| 198 | call add(v:errors, 'typedef not found in alloc.h') |
| 199 | endif |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 200 | let lnum = search('aid_' . a:name . ',') |
| 201 | if lnum == 0 |
| 202 | call add(v:errors, 'Alloc ID ' . a:name . ' not defined') |
| 203 | endif |
| 204 | close |
Bram Moolenaar | 065ee9a | 2016-01-15 20:53:38 +0100 | [diff] [blame] | 205 | return lnum - top - 1 |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 206 | endfunc |
| 207 | |
Bram Moolenaar | c216a7a | 2022-12-05 13:50:55 +0000 | [diff] [blame] | 208 | " Get the list of swap files in the current directory. |
| 209 | func s:GetSwapFileList() |
| 210 | let save_dir = &directory |
| 211 | let &directory = '.' |
| 212 | let files = swapfilelist() |
| 213 | let &directory = save_dir |
| 214 | |
| 215 | " remove a match with runtest.vim |
| 216 | let idx = indexof(files, 'v:val =~ "runtest.vim."') |
| 217 | if idx >= 0 |
| 218 | call remove(files, idx) |
| 219 | endif |
| 220 | |
| 221 | return files |
| 222 | endfunc |
| 223 | |
Bram Moolenaar | 6572a90 | 2022-12-06 14:21:09 +0000 | [diff] [blame] | 224 | " A previous (failed) test run may have left swap files behind. Delete them |
| 225 | " before running tests again, they might interfere. |
| 226 | for name in s:GetSwapFileList() |
| 227 | call delete(name) |
| 228 | endfor |
zeertzjq | a36acb7 | 2023-10-21 11:50:26 +0200 | [diff] [blame] | 229 | unlet! name |
Bram Moolenaar | 6572a90 | 2022-12-06 14:21:09 +0000 | [diff] [blame] | 230 | |
| 231 | |
Bram Moolenaar | 3bcd0dd | 2022-09-23 20:25:55 +0100 | [diff] [blame] | 232 | " Invoked when a test takes too much time. |
| 233 | func TestTimeout(id) |
| 234 | split test.log |
| 235 | call append(line('$'), '') |
Bram Moolenaar | 7c2beb4 | 2023-07-08 00:25:56 +0100 | [diff] [blame] | 236 | |
| 237 | let text = 'Test timed out: ' .. g:testfunc |
| 238 | if g:timeout_start > 0 |
| 239 | let text ..= strftime(' after %s seconds', localtime() - g:timeout_start) |
| 240 | endif |
| 241 | call append(line('$'), text) |
Bram Moolenaar | 3bcd0dd | 2022-09-23 20:25:55 +0100 | [diff] [blame] | 242 | write |
Bram Moolenaar | 7c2beb4 | 2023-07-08 00:25:56 +0100 | [diff] [blame] | 243 | call add(v:errors, text) |
Bram Moolenaar | 3bcd0dd | 2022-09-23 20:25:55 +0100 | [diff] [blame] | 244 | |
| 245 | cquit! 42 |
| 246 | endfunc |
Bram Moolenaar | 7c2beb4 | 2023-07-08 00:25:56 +0100 | [diff] [blame] | 247 | let g:timeout_start = 0 |
Bram Moolenaar | 3bcd0dd | 2022-09-23 20:25:55 +0100 | [diff] [blame] | 248 | |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 249 | func RunTheTest(test) |
Bram Moolenaar | daaa3d9 | 2022-09-22 15:13:00 +0100 | [diff] [blame] | 250 | let prefix = '' |
Bram Moolenaar | 75ee544 | 2019-06-06 18:05:25 +0200 | [diff] [blame] | 251 | if has('reltime') |
Bram Moolenaar | b9093d5 | 2022-09-23 19:42:31 +0100 | [diff] [blame] | 252 | let prefix = strftime('%M:%S', localtime() - s:test_start_time) .. ' ' |
Bram Moolenaar | daaa3d9 | 2022-09-22 15:13:00 +0100 | [diff] [blame] | 253 | let g:func_start = reltime() |
Bram Moolenaar | 75ee544 | 2019-06-06 18:05:25 +0200 | [diff] [blame] | 254 | endif |
Bram Moolenaar | daaa3d9 | 2022-09-22 15:13:00 +0100 | [diff] [blame] | 255 | echoconsole prefix .. 'Executing ' .. a:test |
Bram Moolenaar | e5f2a07 | 2017-02-01 22:31:49 +0100 | [diff] [blame] | 256 | |
Bram Moolenaar | 3bcd0dd | 2022-09-23 20:25:55 +0100 | [diff] [blame] | 257 | if has('timers') |
Christian Brabandt | ec7a4e4 | 2025-02-09 17:16:36 +0100 | [diff] [blame] | 258 | " No test should take longer than 45 seconds. If it takes longer we |
Bram Moolenaar | 3bcd0dd | 2022-09-23 20:25:55 +0100 | [diff] [blame] | 259 | " assume we are stuck and need to break out. |
Bram Moolenaar | 7c2beb4 | 2023-07-08 00:25:56 +0100 | [diff] [blame] | 260 | let test_timeout_timer = |
Christian Brabandt | ec7a4e4 | 2025-02-09 17:16:36 +0100 | [diff] [blame] | 261 | \ timer_start(RunningWithValgrind() ? 90000 : 45000, 'TestTimeout') |
Bram Moolenaar | 7c2beb4 | 2023-07-08 00:25:56 +0100 | [diff] [blame] | 262 | let g:timeout_start = localtime() |
Bram Moolenaar | 3bcd0dd | 2022-09-23 20:25:55 +0100 | [diff] [blame] | 263 | endif |
| 264 | |
Bram Moolenaar | e5f2a07 | 2017-02-01 22:31:49 +0100 | [diff] [blame] | 265 | " Avoid stopping at the "hit enter" prompt |
| 266 | set nomore |
| 267 | |
| 268 | " Avoid a three second wait when a message is about to be overwritten by the |
| 269 | " mode message. |
| 270 | set noshowmode |
| 271 | |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 272 | " Clear any overrides, except "alloc_lines". |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 273 | call test_override('ALL', 0) |
| 274 | |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 275 | " Some tests wipe out buffers. To be consistent, always wipe out all |
| 276 | " buffers. |
| 277 | %bwipe! |
| 278 | |
Yee Cheng Chin | e70587d | 2025-02-13 20:55:45 +0100 | [diff] [blame] | 279 | " Clear all children notifications in case there are stale ones left |
| 280 | let g:child_notification = 0 |
| 281 | |
Bram Moolenaar | 209d387 | 2017-11-16 21:52:51 +0100 | [diff] [blame] | 282 | " The test may change the current directory. Save and restore the |
| 283 | " directory after executing the test. |
| 284 | let save_cwd = getcwd() |
| 285 | |
Aliaksei Budavei | c4eb1cb | 2025-06-08 15:52:42 +0200 | [diff] [blame] | 286 | " Permit "SetUp()" implementations to override default settings. |
| 287 | call s:SetDefaultOptionsForGUIBuilds() |
| 288 | |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 289 | if exists("*SetUp") |
Bram Moolenaar | cc28e2d | 2016-11-17 17:56:13 +0100 | [diff] [blame] | 290 | try |
| 291 | call SetUp() |
| 292 | catch |
| 293 | call add(v:errors, 'Caught exception in SetUp() before ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) |
| 294 | endtry |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 295 | endif |
| 296 | |
zeertzjq | a0e1f06 | 2023-10-18 11:50:37 +0200 | [diff] [blame] | 297 | let skipped = v:false |
| 298 | |
Bram Moolenaar | 8bea171 | 2022-06-15 20:49:35 +0100 | [diff] [blame] | 299 | au VimLeavePre * call EarlyExit(g:testfunc) |
Bram Moolenaar | f204e05 | 2017-10-26 17:14:01 +0200 | [diff] [blame] | 300 | if a:test =~ 'Test_nocatch_' |
| 301 | " Function handles errors itself. This avoids skipping commands after the |
| 302 | " error. |
Bram Moolenaar | 776b954 | 2021-03-10 22:27:48 +0100 | [diff] [blame] | 303 | let g:skipped_reason = '' |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 304 | exe 'call ' . a:test |
Bram Moolenaar | 776b954 | 2021-03-10 22:27:48 +0100 | [diff] [blame] | 305 | if g:skipped_reason != '' |
| 306 | call add(s:messages, ' Skipped') |
| 307 | call add(s:skipped, 'SKIPPED ' . a:test . ': ' . g:skipped_reason) |
zeertzjq | a0e1f06 | 2023-10-18 11:50:37 +0200 | [diff] [blame] | 308 | let skipped = v:true |
Bram Moolenaar | 776b954 | 2021-03-10 22:27:48 +0100 | [diff] [blame] | 309 | endif |
Bram Moolenaar | f204e05 | 2017-10-26 17:14:01 +0200 | [diff] [blame] | 310 | else |
| 311 | try |
| 312 | exe 'call ' . a:test |
| 313 | catch /^\cskipped/ |
| 314 | call add(s:messages, ' Skipped') |
| 315 | call add(s:skipped, 'SKIPPED ' . a:test . ': ' . substitute(v:exception, '^\S*\s\+', '', '')) |
zeertzjq | a0e1f06 | 2023-10-18 11:50:37 +0200 | [diff] [blame] | 316 | let skipped = v:true |
Bram Moolenaar | f204e05 | 2017-10-26 17:14:01 +0200 | [diff] [blame] | 317 | catch |
| 318 | call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) |
| 319 | endtry |
| 320 | endif |
Bram Moolenaar | 8bea171 | 2022-06-15 20:49:35 +0100 | [diff] [blame] | 321 | au! VimLeavePre |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 322 | |
Bram Moolenaar | a22c56a | 2022-09-20 15:10:31 +0100 | [diff] [blame] | 323 | if a:test =~ '_terminal_' |
| 324 | " Terminal tests sometimes hang, give extra information |
| 325 | echoconsole 'After executing ' .. a:test |
| 326 | endif |
| 327 | |
Bram Moolenaar | 8ad16da | 2019-01-06 15:29:57 +0100 | [diff] [blame] | 328 | " In case 'insertmode' was set and something went wrong, make sure it is |
| 329 | " reset to avoid trouble with anything else. |
| 330 | set noinsertmode |
| 331 | |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 332 | if exists("*TearDown") |
Bram Moolenaar | cc28e2d | 2016-11-17 17:56:13 +0100 | [diff] [blame] | 333 | try |
| 334 | call TearDown() |
| 335 | catch |
| 336 | call add(v:errors, 'Caught exception in TearDown() after ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) |
| 337 | endtry |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 338 | endif |
Bram Moolenaar | 7cba71d | 2016-08-02 23:04:49 +0200 | [diff] [blame] | 339 | |
Bram Moolenaar | 3bcd0dd | 2022-09-23 20:25:55 +0100 | [diff] [blame] | 340 | if has('timers') |
| 341 | call timer_stop(test_timeout_timer) |
Bram Moolenaar | 7c2beb4 | 2023-07-08 00:25:56 +0100 | [diff] [blame] | 342 | let g:timeout_start = 0 |
Bram Moolenaar | 3bcd0dd | 2022-09-23 20:25:55 +0100 | [diff] [blame] | 343 | endif |
| 344 | |
Bram Moolenaar | 0b5dc64 | 2019-08-11 22:56:15 +0200 | [diff] [blame] | 345 | " Clear any autocommands and put back the catch-all for SwapExists. |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 346 | au! |
Bram Moolenaar | d8f27b3 | 2018-10-07 15:42:07 +0200 | [diff] [blame] | 347 | au SwapExists * call HandleSwapExists() |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 348 | |
Bram Moolenaar | ef6b979 | 2020-05-13 16:34:15 +0200 | [diff] [blame] | 349 | " Check for and close any stray popup windows. |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 350 | if has('popupwin') |
Bram Moolenaar | f05a1e5 | 2022-08-02 11:48:53 +0100 | [diff] [blame] | 351 | call assert_equal([], popup_list(), 'Popup is still present') |
Bram Moolenaar | 03a9f84 | 2020-05-13 13:40:16 +0200 | [diff] [blame] | 352 | call popup_clear(1) |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 353 | endif |
| 354 | |
Bram Moolenaar | 2d12c25 | 2022-06-13 21:42:45 +0100 | [diff] [blame] | 355 | if filereadable('guidialogfile') |
Bram Moolenaar | 217ea51 | 2022-06-14 17:13:59 +0100 | [diff] [blame] | 356 | call add(v:errors, "Unexpected dialog: " .. readfile('guidialogfile')->join('<NL>')) |
Bram Moolenaar | 2d12c25 | 2022-06-13 21:42:45 +0100 | [diff] [blame] | 357 | call delete('guidialogfile') |
| 358 | endif |
| 359 | |
Bram Moolenaar | ce11de8 | 2017-10-26 22:00:00 +0200 | [diff] [blame] | 360 | " Close any extra tab pages and windows and make the current one not modified. |
| 361 | while tabpagenr('$') > 1 |
Bram Moolenaar | bdf931c | 2020-10-01 22:37:40 +0200 | [diff] [blame] | 362 | let winid = win_getid() |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 363 | quit! |
Bram Moolenaar | bdf931c | 2020-10-01 22:37:40 +0200 | [diff] [blame] | 364 | if winid == win_getid() |
| 365 | echoerr 'Could not quit window' |
| 366 | break |
| 367 | endif |
Bram Moolenaar | ce11de8 | 2017-10-26 22:00:00 +0200 | [diff] [blame] | 368 | endwhile |
| 369 | |
Bram Moolenaar | 358308d | 2016-08-24 21:21:26 +0200 | [diff] [blame] | 370 | while 1 |
| 371 | let wincount = winnr('$') |
| 372 | if wincount == 1 |
| 373 | break |
| 374 | endif |
Bram Moolenaar | 7cba71d | 2016-08-02 23:04:49 +0200 | [diff] [blame] | 375 | bwipe! |
Bram Moolenaar | 358308d | 2016-08-24 21:21:26 +0200 | [diff] [blame] | 376 | if wincount == winnr('$') |
| 377 | " Did not manage to close a window. |
| 378 | only! |
| 379 | break |
| 380 | endif |
Bram Moolenaar | 7cba71d | 2016-08-02 23:04:49 +0200 | [diff] [blame] | 381 | endwhile |
Bram Moolenaar | 209d387 | 2017-11-16 21:52:51 +0100 | [diff] [blame] | 382 | |
| 383 | exe 'cd ' . save_cwd |
Bram Moolenaar | 640d4f0 | 2019-06-10 17:43:46 +0200 | [diff] [blame] | 384 | |
Bram Moolenaar | a22c56a | 2022-09-20 15:10:31 +0100 | [diff] [blame] | 385 | if a:test =~ '_terminal_' |
| 386 | " Terminal tests sometimes hang, give extra information |
| 387 | echoconsole 'Finished ' . a:test |
| 388 | endif |
| 389 | |
Bram Moolenaar | 640d4f0 | 2019-06-10 17:43:46 +0200 | [diff] [blame] | 390 | let message = 'Executed ' . a:test |
| 391 | if has('reltime') |
Bram Moolenaar | 8d94379 | 2020-06-21 20:39:37 +0200 | [diff] [blame] | 392 | let message ..= repeat(' ', 50 - len(message)) |
Bram Moolenaar | daaa3d9 | 2022-09-22 15:13:00 +0100 | [diff] [blame] | 393 | let time = reltime(g:func_start) |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 394 | if reltimefloat(time) > 0.1 |
Bram Moolenaar | 45df2a0 | 2020-07-29 15:03:01 +0200 | [diff] [blame] | 395 | let message = s:t_bold .. message |
Bram Moolenaar | 8d94379 | 2020-06-21 20:39:37 +0200 | [diff] [blame] | 396 | endif |
| 397 | let message ..= ' in ' .. reltimestr(time) .. ' seconds' |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 398 | if reltimefloat(time) > 0.1 |
Bram Moolenaar | 45df2a0 | 2020-07-29 15:03:01 +0200 | [diff] [blame] | 399 | let message ..= s:t_normal |
Bram Moolenaar | 8d94379 | 2020-06-21 20:39:37 +0200 | [diff] [blame] | 400 | endif |
Bram Moolenaar | 640d4f0 | 2019-06-10 17:43:46 +0200 | [diff] [blame] | 401 | endif |
| 402 | call add(s:messages, message) |
| 403 | let s:done += 1 |
Bram Moolenaar | c216a7a | 2022-12-05 13:50:55 +0000 | [diff] [blame] | 404 | |
Bram Moolenaar | e5eae82 | 2022-12-08 16:30:16 +0000 | [diff] [blame] | 405 | " close any split windows |
| 406 | while winnr('$') > 1 |
Bram Moolenaar | 7c2beb4 | 2023-07-08 00:25:56 +0100 | [diff] [blame] | 407 | noswapfile bwipe! |
Bram Moolenaar | e5eae82 | 2022-12-08 16:30:16 +0000 | [diff] [blame] | 408 | endwhile |
| 409 | |
Bram Moolenaar | 23526d2 | 2022-12-05 15:50:41 +0000 | [diff] [blame] | 410 | " May be editing some buffer, wipe it out. Then we may end up in another |
| 411 | " buffer, continue until we end up in an empty no-name buffer without a swap |
| 412 | " file. |
| 413 | while bufname() != '' || execute('swapname') !~ 'No swap file' |
Bram Moolenaar | fa2533c | 2022-12-05 20:58:04 +0000 | [diff] [blame] | 414 | let bn = bufnr() |
| 415 | |
| 416 | noswapfile bwipe! |
| 417 | |
| 418 | if bn == bufnr() |
| 419 | " avoid getting stuck in the same buffer |
| 420 | break |
| 421 | endif |
Bram Moolenaar | 23526d2 | 2022-12-05 15:50:41 +0000 | [diff] [blame] | 422 | endwhile |
| 423 | |
zeertzjq | a0e1f06 | 2023-10-18 11:50:37 +0200 | [diff] [blame] | 424 | if !skipped |
| 425 | " Check if the test has left any swap files behind. Delete them before |
| 426 | " running tests again, they might interfere. |
| 427 | let swapfiles = s:GetSwapFileList() |
| 428 | if len(swapfiles) > 0 |
| 429 | call add(s:messages, "Found swap files: " .. string(swapfiles)) |
| 430 | for name in swapfiles |
| 431 | call delete(name) |
| 432 | endfor |
| 433 | endif |
Bram Moolenaar | c216a7a | 2022-12-05 13:50:55 +0000 | [diff] [blame] | 434 | endif |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 435 | endfunc |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 436 | |
Christian Brabandt | 84bc00e | 2023-07-13 11:45:54 +0200 | [diff] [blame] | 437 | function Delete_Xtest_Files() |
| 438 | for file in glob('X*', v:false, v:true) |
| 439 | if file ==? 'XfakeHOME' |
| 440 | " Clean up files created by setup.vim |
| 441 | call delete('XfakeHOME', 'rf') |
| 442 | continue |
| 443 | endif |
| 444 | " call add(v:errors, file .. " exists when it shouldn't, trying to delete it!") |
| 445 | call delete(file) |
| 446 | if !empty(glob(file, v:false, v:true)) |
| 447 | " call add(v:errors, file .. " still exists after trying to delete it!") |
| 448 | if has('unix') |
| 449 | call system('rm -rf ' .. file) |
| 450 | endif |
| 451 | endif |
| 452 | endfor |
| 453 | endfunc |
| 454 | |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 455 | func AfterTheTest(func_name) |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 456 | if len(v:errors) > 0 |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 457 | if match(s:may_fail_list, '^' .. a:func_name) >= 0 |
| 458 | let s:fail_expected += 1 |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 459 | call add(s:errors_expected, 'Found errors in ' . g:testfunc . ':') |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 460 | call extend(s:errors_expected, v:errors) |
| 461 | else |
| 462 | let s:fail += 1 |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 463 | call add(s:errors, 'Found errors in ' . g:testfunc . ':') |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 464 | call extend(s:errors, v:errors) |
| 465 | endif |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 466 | let v:errors = [] |
| 467 | endif |
| 468 | endfunc |
| 469 | |
Bram Moolenaar | 8903676 | 2018-06-12 14:58:39 +0200 | [diff] [blame] | 470 | func EarlyExit(test) |
| 471 | " It's OK for the test we use to test the quit detection. |
| 472 | if a:test != 'Test_zz_quit_detected()' |
Bram Moolenaar | 1c67f3a | 2021-12-30 13:32:09 +0000 | [diff] [blame] | 473 | call add(v:errors, v:errmsg) |
Bram Moolenaar | 8903676 | 2018-06-12 14:58:39 +0200 | [diff] [blame] | 474 | call add(v:errors, 'Test caused Vim to exit: ' . a:test) |
| 475 | endif |
| 476 | |
| 477 | call FinishTesting() |
| 478 | endfunc |
| 479 | |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 480 | " This function can be called by a test if it wants to abort testing. |
| 481 | func FinishTesting() |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 482 | call AfterTheTest('') |
Christian Brabandt | 84bc00e | 2023-07-13 11:45:54 +0200 | [diff] [blame] | 483 | call Delete_Xtest_Files() |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 484 | |
| 485 | " Don't write viminfo on exit. |
| 486 | set viminfo= |
| 487 | |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 488 | if s:fail == 0 && s:fail_expected == 0 |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 489 | " Success, create the .res file so that make knows it's done. |
| 490 | exe 'split ' . fnamemodify(g:testname, ':r') . '.res' |
| 491 | write |
| 492 | endif |
| 493 | |
| 494 | if len(s:errors) > 0 |
| 495 | " Append errors to test.log |
| 496 | split test.log |
| 497 | call append(line('$'), '') |
| 498 | call append(line('$'), 'From ' . g:testname . ':') |
| 499 | call append(line('$'), s:errors) |
| 500 | write |
| 501 | endif |
| 502 | |
Bram Moolenaar | 29f9ed2 | 2018-04-10 19:20:31 +0200 | [diff] [blame] | 503 | if s:done == 0 |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 504 | if s:filtered > 0 |
Bram Moolenaar | dae453f | 2021-08-07 17:20:16 +0200 | [diff] [blame] | 505 | if $TEST_FILTER != '' |
| 506 | let message = "NO tests match $TEST_FILTER: '" .. $TEST_FILTER .. "'" |
| 507 | else |
| 508 | let message = "ALL tests match $TEST_SKIP_PAT: '" .. $TEST_SKIP_PAT .. "'" |
| 509 | endif |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 510 | else |
| 511 | let message = 'NO tests executed' |
| 512 | endif |
Bram Moolenaar | 29f9ed2 | 2018-04-10 19:20:31 +0200 | [diff] [blame] | 513 | else |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 514 | if s:filtered > 0 |
Bram Moolenaar | dae453f | 2021-08-07 17:20:16 +0200 | [diff] [blame] | 515 | call add(s:messages, "Filtered " .. s:filtered .. " tests with $TEST_FILTER and $TEST_SKIP_PAT") |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 516 | endif |
Bram Moolenaar | 29f9ed2 | 2018-04-10 19:20:31 +0200 | [diff] [blame] | 517 | let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test') |
| 518 | endif |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 519 | if s:done > 0 && has('reltime') |
Bram Moolenaar | 45df2a0 | 2020-07-29 15:03:01 +0200 | [diff] [blame] | 520 | let message = s:t_bold .. message .. repeat(' ', 40 - len(message)) |
Bram Moolenaar | b9093d5 | 2022-09-23 19:42:31 +0100 | [diff] [blame] | 521 | let message ..= ' in ' .. reltimestr(reltime(s:run_start_time)) .. ' seconds' |
Bram Moolenaar | 45df2a0 | 2020-07-29 15:03:01 +0200 | [diff] [blame] | 522 | let message ..= s:t_normal |
Bram Moolenaar | 75ee544 | 2019-06-06 18:05:25 +0200 | [diff] [blame] | 523 | endif |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 524 | echo message |
| 525 | call add(s:messages, message) |
| 526 | if s:fail > 0 |
| 527 | let message = s:fail . ' FAILED:' |
| 528 | echo message |
| 529 | call add(s:messages, message) |
| 530 | call extend(s:messages, s:errors) |
| 531 | endif |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 532 | if s:fail_expected > 0 |
| 533 | let message = s:fail_expected . ' FAILED (matching $TEST_MAY_FAIL):' |
| 534 | echo message |
| 535 | call add(s:messages, message) |
| 536 | call extend(s:messages, s:errors_expected) |
| 537 | endif |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 538 | |
| 539 | " Add SKIPPED messages |
| 540 | call extend(s:messages, s:skipped) |
| 541 | |
Christian Brabandt | 075ab5a | 2024-10-03 16:38:52 +0200 | [diff] [blame] | 542 | " Append messages to the file "messages", but remove ANSI Escape sequences |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 543 | split messages |
| 544 | call append(line('$'), '') |
| 545 | call append(line('$'), 'From ' . g:testname . ':') |
Christian Brabandt | bcb06c9 | 2025-07-07 20:03:03 +0200 | [diff] [blame] | 546 | call append(line('$'), s:messages->map({_, val -> substitute(val, '\%x1b[[|]\(\d\?\|\d\+\)[hm]', '', 'g')})) |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 547 | write |
| 548 | |
| 549 | qall! |
| 550 | endfunc |
| 551 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 552 | " Source the test script. First grab the file name, in case the script |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 553 | " navigates away. g:testname can be used by the tests. |
| 554 | let g:testname = expand('%') |
| 555 | let s:done = 0 |
| 556 | let s:fail = 0 |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 557 | let s:fail_expected = 0 |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 558 | let s:errors = [] |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 559 | let s:errors_expected = [] |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 560 | let s:messages = [] |
Bram Moolenaar | dac1947 | 2016-09-03 22:35:40 +0200 | [diff] [blame] | 561 | let s:skipped = [] |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 562 | if expand('%') =~ 'test_vimscript.vim' |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 563 | " this test has intentional errors, don't use try/catch. |
Bram Moolenaar | 4686b32 | 2015-12-28 14:44:10 +0100 | [diff] [blame] | 564 | source % |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 565 | else |
| 566 | try |
| 567 | source % |
Bram Moolenaar | 9c0cec6 | 2019-06-06 13:38:15 +0200 | [diff] [blame] | 568 | catch /^\cskipped/ |
| 569 | call add(s:messages, ' Skipped') |
| 570 | call add(s:skipped, 'SKIPPED ' . expand('%') . ': ' . substitute(v:exception, '^\S*\s\+', '', '')) |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 571 | catch |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 572 | let s:fail += 1 |
| 573 | call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint) |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 574 | endtry |
| 575 | endif |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 576 | |
Bram Moolenaar | 7e0be3e | 2022-03-20 13:40:41 +0000 | [diff] [blame] | 577 | " Delete the .res file, it may change behavior for completion |
| 578 | call delete(fnamemodify(g:testname, ':r') .. '.res') |
| 579 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 580 | " Locate Test_ functions and execute them. |
| 581 | redir @q |
Bram Moolenaar | 93bf558 | 2016-02-18 22:25:47 +0100 | [diff] [blame] | 582 | silent function /^Test_ |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 583 | redir END |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 584 | let s:tests = split(substitute(@q, '\(function\|def\) \(\k*()\)', '\2', 'g')) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 585 | |
Bram Moolenaar | befb366 | 2016-02-20 14:41:40 +0100 | [diff] [blame] | 586 | " If there is an extra argument filter the function names against it. |
| 587 | if argc() > 1 |
| 588 | let s:tests = filter(s:tests, 'v:val =~ argv(1)') |
| 589 | endif |
| 590 | |
Bram Moolenaar | a7f6c3c | 2019-09-27 15:34:16 +0200 | [diff] [blame] | 591 | " If the environment variable $TEST_FILTER is set then filter the function |
| 592 | " names against it. |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 593 | let s:filtered = 0 |
Bram Moolenaar | a7f6c3c | 2019-09-27 15:34:16 +0200 | [diff] [blame] | 594 | if $TEST_FILTER != '' |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 595 | let s:filtered = len(s:tests) |
Bram Moolenaar | a7f6c3c | 2019-09-27 15:34:16 +0200 | [diff] [blame] | 596 | let s:tests = filter(s:tests, 'v:val =~ $TEST_FILTER') |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 597 | let s:filtered -= len(s:tests) |
Bram Moolenaar | a7f6c3c | 2019-09-27 15:34:16 +0200 | [diff] [blame] | 598 | endif |
| 599 | |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 600 | let s:may_fail_list = [] |
| 601 | if $TEST_MAY_FAIL != '' |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 602 | " Split the list at commas and add () to make it match g:testfunc. |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 603 | let s:may_fail_list = split($TEST_MAY_FAIL, ',')->map({i, v -> v .. '()'}) |
| 604 | endif |
| 605 | |
Bram Moolenaar | cfc0a35 | 2016-01-09 20:23:00 +0100 | [diff] [blame] | 606 | " Execute the tests in alphabetical order. |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 607 | for g:testfunc in sort(s:tests) |
Bram Moolenaar | dae453f | 2021-08-07 17:20:16 +0200 | [diff] [blame] | 608 | if $TEST_SKIP_PAT != '' && g:testfunc =~ $TEST_SKIP_PAT |
| 609 | call add(s:messages, g:testfunc .. ' matches $TEST_SKIP_PAT') |
| 610 | let s:filtered += 1 |
| 611 | continue |
| 612 | endif |
| 613 | |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 614 | " Silence, please! |
| 615 | set belloff=all |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 616 | let prev_error = '' |
| 617 | let total_errors = [] |
Bram Moolenaar | 3ed9efc | 2020-03-26 16:50:57 +0100 | [diff] [blame] | 618 | let g:run_nr = 1 |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 619 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 620 | " A test can set g:test_is_flaky to retry running the test. |
| 621 | let g:test_is_flaky = 0 |
Bram Moolenaar | 3cdcb09 | 2020-03-18 19:18:10 +0100 | [diff] [blame] | 622 | |
Drew Vogel | ea67ba7 | 2025-05-07 22:05:17 +0200 | [diff] [blame] | 623 | let g:check_screendump_called = v:false |
| 624 | |
Milly | baab7c0 | 2024-10-28 21:56:14 +0100 | [diff] [blame] | 625 | " A test can set g:max_run_nr to change the max retry count. |
| 626 | let g:max_run_nr = 5 |
| 627 | if has('mac') |
| 628 | let g:max_run_nr = 10 |
| 629 | endif |
| 630 | |
| 631 | " By default, give up if the same error occurs. A test can set |
| 632 | " g:giveup_same_error to 0 to not give up on the same error and keep trying. |
| 633 | let g:giveup_same_error = 1 |
| 634 | |
Bram Moolenaar | 5fbbec1 | 2022-09-03 22:08:11 +0100 | [diff] [blame] | 635 | let starttime = strftime("%H:%M:%S") |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 636 | call RunTheTest(g:testfunc) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 637 | |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 638 | " Repeat a flaky test. Give up when: |
Bram Moolenaar | 6ca6ca4 | 2020-07-27 19:47:07 +0200 | [diff] [blame] | 639 | " - $TEST_NO_RETRY is not empty |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 640 | " - it fails again with the same message |
Bram Moolenaar | 1bc353b | 2019-09-01 14:45:28 +0200 | [diff] [blame] | 641 | " - it fails five times (with a different message) |
Bram Moolenaar | dbc0d21 | 2018-11-16 18:22:45 +0100 | [diff] [blame] | 642 | if len(v:errors) > 0 |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 643 | \ && $TEST_NO_RETRY == '' |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 644 | \ && g:test_is_flaky |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 645 | while 1 |
Bram Moolenaar | 06d32a0 | 2022-09-03 13:58:47 +0100 | [diff] [blame] | 646 | call add(s:messages, 'Found errors in ' .. g:testfunc .. ':') |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 647 | call extend(s:messages, v:errors) |
Bram Moolenaar | 15e737f | 2017-03-18 21:22:47 +0100 | [diff] [blame] | 648 | |
Bram Moolenaar | 65258d3 | 2022-09-09 15:09:59 +0100 | [diff] [blame] | 649 | let endtime = strftime("%H:%M:%S") |
Milly | baab7c0 | 2024-10-28 21:56:14 +0100 | [diff] [blame] | 650 | if has('reltime') |
| 651 | let suffix = $' in{reltimestr(reltime(g:func_start))} seconds' |
| 652 | else |
| 653 | let suffix = '' |
| 654 | endif |
| 655 | call add(total_errors, $'Run {g:run_nr}, {starttime} - {endtime}{suffix}:') |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 656 | call extend(total_errors, v:errors) |
Bram Moolenaar | 5505860 | 2017-11-21 15:14:51 +0100 | [diff] [blame] | 657 | |
Milly | baab7c0 | 2024-10-28 21:56:14 +0100 | [diff] [blame] | 658 | if g:run_nr >= g:max_run_nr || g:giveup_same_error && prev_error == v:errors[0] |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 659 | call add(total_errors, 'Flaky test failed too often, giving up') |
| 660 | let v:errors = total_errors |
| 661 | break |
| 662 | endif |
| 663 | |
| 664 | call add(s:messages, 'Flaky test failed, running it again') |
| 665 | |
| 666 | " Flakiness is often caused by the system being very busy. Sleep a |
| 667 | " couple of seconds to have a higher chance of succeeding the second |
| 668 | " time. |
Milly | baab7c0 | 2024-10-28 21:56:14 +0100 | [diff] [blame] | 669 | let delay = g:run_nr * 2 |
| 670 | exe 'sleep' delay |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 671 | |
| 672 | let prev_error = v:errors[0] |
| 673 | let v:errors = [] |
Bram Moolenaar | 3ed9efc | 2020-03-26 16:50:57 +0100 | [diff] [blame] | 674 | let g:run_nr += 1 |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 675 | |
Bram Moolenaar | 5fbbec1 | 2022-09-03 22:08:11 +0100 | [diff] [blame] | 676 | let starttime = strftime("%H:%M:%S") |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 677 | call RunTheTest(g:testfunc) |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 678 | |
| 679 | if len(v:errors) == 0 |
| 680 | " Test passed on rerun. |
| 681 | break |
| 682 | endif |
| 683 | endwhile |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 684 | endif |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 685 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 686 | call AfterTheTest(g:testfunc) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 687 | endfor |
| 688 | |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 689 | call FinishTesting() |
Bram Moolenaar | cc28e2d | 2016-11-17 17:56:13 +0100 | [diff] [blame] | 690 | |
| 691 | " vim: shiftwidth=2 sts=2 expandtab |