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') |
| 58 | set columns=80 lines=25 |
| 59 | endif |
| 60 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 61 | " Check that the screen size is at least 24 x 80 characters. |
| 62 | if &lines < 24 || &columns < 80 |
Bram Moolenaar | 0b5dc64 | 2019-08-11 22:56:15 +0200 | [diff] [blame] | 63 | 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] | 64 | echoerr error |
| 65 | split test.log |
| 66 | $put =error |
Bram Moolenaar | 45aa07d | 2019-06-15 18:20:38 +0200 | [diff] [blame] | 67 | write |
| 68 | split messages |
Bram Moolenaar | 0b5dc64 | 2019-08-11 22:56:15 +0200 | [diff] [blame] | 69 | call append(line('$'), '') |
| 70 | call append(line('$'), 'From ' . expand('%') . ':') |
Bram Moolenaar | 45aa07d | 2019-06-15 18:20:38 +0200 | [diff] [blame] | 71 | call append(line('$'), error) |
| 72 | write |
| 73 | qa! |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 74 | endif |
| 75 | |
Bram Moolenaar | 75ee544 | 2019-06-06 18:05:25 +0200 | [diff] [blame] | 76 | if has('reltime') |
| 77 | let s:start_time = reltime() |
| 78 | endif |
| 79 | |
Bakudankun | 29f3a45 | 2021-12-11 12:28:08 +0000 | [diff] [blame] | 80 | " Always use forward slashes. |
| 81 | set shellslash |
| 82 | |
Bram Moolenaar | 89b1042 | 2016-07-12 22:51:22 +0200 | [diff] [blame] | 83 | " Common with all tests on all systems. |
| 84 | source setup.vim |
| 85 | |
Bram Moolenaar | c066246 | 2015-12-30 15:49:05 +0100 | [diff] [blame] | 86 | " For consistency run all tests with 'nocompatible' set. |
| 87 | " This also enables use of line continuation. |
| 88 | set nocp viminfo+=nviminfo |
| 89 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 90 | " 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] | 91 | " Individual tests can overrule this at the top of the file and use |
| 92 | " g:orig_encoding if needed. |
| 93 | let g:orig_encoding = &encoding |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 94 | set encoding=utf-8 |
Bram Moolenaar | ac105ed | 2016-07-21 20:33:32 +0200 | [diff] [blame] | 95 | |
Bram Moolenaar | d8f27b3 | 2018-10-07 15:42:07 +0200 | [diff] [blame] | 96 | " REDIR_TEST_TO_NULL has a very permissive SwapExists autocommand which is for |
| 97 | " the test_name.vim file itself. Replace it here with a more restrictive one, |
| 98 | " so we still catch mistakes. |
Christian Brabandt | 4b2c804 | 2021-11-03 22:31:44 +0000 | [diff] [blame] | 99 | if has("win32") |
| 100 | " replace any '/' directory separators by '\\' |
| 101 | let s:test_script_fname = substitute(expand('%'), '/', '\\', 'g') |
| 102 | else |
| 103 | let s:test_script_fname = expand('%') |
| 104 | endif |
Bram Moolenaar | d8f27b3 | 2018-10-07 15:42:07 +0200 | [diff] [blame] | 105 | au! SwapExists * call HandleSwapExists() |
| 106 | func HandleSwapExists() |
Bram Moolenaar | 8ce4b7e | 2020-08-07 18:12:18 +0200 | [diff] [blame] | 107 | if exists('g:ignoreSwapExists') |
| 108 | return |
| 109 | endif |
Bram Moolenaar | b073da8 | 2019-07-13 14:47:26 +0200 | [diff] [blame] | 110 | " 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] | 111 | " editing it and do ":make test_name") and the output file. |
Bram Moolenaar | b073da8 | 2019-07-13 14:47:26 +0200 | [diff] [blame] | 112 | " Report finding another swap file and chose 'q' to avoid getting stuck. |
Bram Moolenaar | d8f27b3 | 2018-10-07 15:42:07 +0200 | [diff] [blame] | 113 | if expand('<afile>') == 'messages' || expand('<afile>') =~ s:test_script_fname |
| 114 | let v:swapchoice = 'e' |
Bram Moolenaar | b073da8 | 2019-07-13 14:47:26 +0200 | [diff] [blame] | 115 | else |
| 116 | call assert_report('Unexpected swap file: ' .. v:swapname) |
| 117 | let v:swapchoice = 'q' |
Bram Moolenaar | d8f27b3 | 2018-10-07 15:42:07 +0200 | [diff] [blame] | 118 | endif |
| 119 | endfunc |
| 120 | |
Bram Moolenaar | 7a07354 | 2017-02-01 23:17:36 +0100 | [diff] [blame] | 121 | " Avoid stopping at the "hit enter" prompt |
| 122 | set nomore |
| 123 | |
Bram Moolenaar | c066246 | 2015-12-30 15:49:05 +0100 | [diff] [blame] | 124 | " Output all messages in English. |
| 125 | lang mess C |
| 126 | |
Bram Moolenaar | 10e1d01 | 2020-07-18 22:03:11 +0200 | [diff] [blame] | 127 | " suppress menu translation |
| 128 | if has('gui_running') && exists('did_install_default_menus') |
| 129 | source $VIMRUNTIME/delmenu.vim |
| 130 | set langmenu=none |
| 131 | source $VIMRUNTIME/menu.vim |
| 132 | endif |
| 133 | |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 134 | let s:srcdir = expand('%:p:h:h') |
| 135 | |
Bram Moolenaar | 2690b5a | 2020-07-22 18:14:58 +0200 | [diff] [blame] | 136 | if has('win32') |
| 137 | " avoid prompt that is long or contains a line break |
| 138 | let $PROMPT = '$P$G' |
Bram Moolenaar | 45df2a0 | 2020-07-29 15:03:01 +0200 | [diff] [blame] | 139 | " On MS-Windows t_md and t_me are Vim specific escape sequences. |
| 140 | let s:t_bold = "\x1b[1m" |
| 141 | let s:t_normal = "\x1b[m" |
| 142 | else |
| 143 | let s:t_bold = &t_md |
| 144 | let s:t_normal = &t_me |
Bram Moolenaar | 2690b5a | 2020-07-22 18:14:58 +0200 | [diff] [blame] | 145 | endif |
| 146 | |
Bram Moolenaar | 4b2ce12 | 2020-11-21 21:41:41 +0100 | [diff] [blame] | 147 | if has('mac') |
Dominique Pelle | 923dce2 | 2021-11-21 11:36:04 +0000 | [diff] [blame] | 148 | " 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] | 149 | " message is displayed. This breaks the terminal test. Disable the warning |
| 150 | " message. |
| 151 | let $BASH_SILENCE_DEPRECATION_WARNING = 1 |
| 152 | endif |
| 153 | |
Bram Moolenaar | 8e8df25 | 2016-05-25 21:23:21 +0200 | [diff] [blame] | 154 | " Prepare for calling test_garbagecollect_now(). |
Bram Moolenaar | ebf7dfa | 2016-04-14 12:46:51 +0200 | [diff] [blame] | 155 | let v:testing = 1 |
| 156 | |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 157 | " By default, copy each buffer line into allocated memory, so that valgrind can |
| 158 | " detect accessing memory before and after it. |
| 159 | call test_override('alloc_lines', 1) |
| 160 | |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 161 | " Support function: get the alloc ID by name. |
| 162 | function GetAllocId(name) |
| 163 | exe 'split ' . s:srcdir . '/alloc.h' |
Bram Moolenaar | 065ee9a | 2016-01-15 20:53:38 +0100 | [diff] [blame] | 164 | let top = search('typedef enum') |
| 165 | if top == 0 |
| 166 | call add(v:errors, 'typedef not found in alloc.h') |
| 167 | endif |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 168 | let lnum = search('aid_' . a:name . ',') |
| 169 | if lnum == 0 |
| 170 | call add(v:errors, 'Alloc ID ' . a:name . ' not defined') |
| 171 | endif |
| 172 | close |
Bram Moolenaar | 065ee9a | 2016-01-15 20:53:38 +0100 | [diff] [blame] | 173 | return lnum - top - 1 |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 174 | endfunc |
| 175 | |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 176 | func RunTheTest(test) |
Bram Moolenaar | 4c86830 | 2021-03-22 16:19:45 +0100 | [diff] [blame] | 177 | echoconsole 'Executing ' . a:test |
Bram Moolenaar | 75ee544 | 2019-06-06 18:05:25 +0200 | [diff] [blame] | 178 | if has('reltime') |
| 179 | let func_start = reltime() |
| 180 | endif |
Bram Moolenaar | e5f2a07 | 2017-02-01 22:31:49 +0100 | [diff] [blame] | 181 | |
| 182 | " Avoid stopping at the "hit enter" prompt |
| 183 | set nomore |
| 184 | |
| 185 | " Avoid a three second wait when a message is about to be overwritten by the |
| 186 | " mode message. |
| 187 | set noshowmode |
| 188 | |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 189 | " Clear any overrides, except "alloc_lines". |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 190 | call test_override('ALL', 0) |
| 191 | |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 192 | " Some tests wipe out buffers. To be consistent, always wipe out all |
| 193 | " buffers. |
| 194 | %bwipe! |
| 195 | |
Bram Moolenaar | 209d387 | 2017-11-16 21:52:51 +0100 | [diff] [blame] | 196 | " The test may change the current directory. Save and restore the |
| 197 | " directory after executing the test. |
| 198 | let save_cwd = getcwd() |
| 199 | |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 200 | if exists("*SetUp") |
Bram Moolenaar | cc28e2d | 2016-11-17 17:56:13 +0100 | [diff] [blame] | 201 | try |
| 202 | call SetUp() |
| 203 | catch |
| 204 | call add(v:errors, 'Caught exception in SetUp() before ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) |
| 205 | endtry |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 206 | endif |
| 207 | |
Bram Moolenaar | 8bea171 | 2022-06-15 20:49:35 +0100 | [diff] [blame] | 208 | au VimLeavePre * call EarlyExit(g:testfunc) |
Bram Moolenaar | f204e05 | 2017-10-26 17:14:01 +0200 | [diff] [blame] | 209 | if a:test =~ 'Test_nocatch_' |
| 210 | " Function handles errors itself. This avoids skipping commands after the |
| 211 | " error. |
Bram Moolenaar | 776b954 | 2021-03-10 22:27:48 +0100 | [diff] [blame] | 212 | let g:skipped_reason = '' |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 213 | exe 'call ' . a:test |
Bram Moolenaar | 776b954 | 2021-03-10 22:27:48 +0100 | [diff] [blame] | 214 | if g:skipped_reason != '' |
| 215 | call add(s:messages, ' Skipped') |
| 216 | call add(s:skipped, 'SKIPPED ' . a:test . ': ' . g:skipped_reason) |
| 217 | endif |
Bram Moolenaar | f204e05 | 2017-10-26 17:14:01 +0200 | [diff] [blame] | 218 | else |
| 219 | try |
| 220 | exe 'call ' . a:test |
| 221 | catch /^\cskipped/ |
| 222 | call add(s:messages, ' Skipped') |
| 223 | call add(s:skipped, 'SKIPPED ' . a:test . ': ' . substitute(v:exception, '^\S*\s\+', '', '')) |
| 224 | catch |
| 225 | call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) |
| 226 | endtry |
| 227 | endif |
Bram Moolenaar | 8bea171 | 2022-06-15 20:49:35 +0100 | [diff] [blame] | 228 | au! VimLeavePre |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 229 | |
Bram Moolenaar | 8ad16da | 2019-01-06 15:29:57 +0100 | [diff] [blame] | 230 | " In case 'insertmode' was set and something went wrong, make sure it is |
| 231 | " reset to avoid trouble with anything else. |
| 232 | set noinsertmode |
| 233 | |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 234 | if exists("*TearDown") |
Bram Moolenaar | cc28e2d | 2016-11-17 17:56:13 +0100 | [diff] [blame] | 235 | try |
| 236 | call TearDown() |
| 237 | catch |
| 238 | call add(v:errors, 'Caught exception in TearDown() after ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) |
| 239 | endtry |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 240 | endif |
Bram Moolenaar | 7cba71d | 2016-08-02 23:04:49 +0200 | [diff] [blame] | 241 | |
Bram Moolenaar | 0b5dc64 | 2019-08-11 22:56:15 +0200 | [diff] [blame] | 242 | " Clear any autocommands and put back the catch-all for SwapExists. |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 243 | au! |
Bram Moolenaar | d8f27b3 | 2018-10-07 15:42:07 +0200 | [diff] [blame] | 244 | au SwapExists * call HandleSwapExists() |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 245 | |
Bram Moolenaar | ef6b979 | 2020-05-13 16:34:15 +0200 | [diff] [blame] | 246 | " Check for and close any stray popup windows. |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 247 | if has('popupwin') |
Bram Moolenaar | f05a1e5 | 2022-08-02 11:48:53 +0100 | [diff] [blame] | 248 | call assert_equal([], popup_list(), 'Popup is still present') |
Bram Moolenaar | 03a9f84 | 2020-05-13 13:40:16 +0200 | [diff] [blame] | 249 | call popup_clear(1) |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 250 | endif |
| 251 | |
Bram Moolenaar | 2d12c25 | 2022-06-13 21:42:45 +0100 | [diff] [blame] | 252 | if filereadable('guidialogfile') |
Bram Moolenaar | 217ea51 | 2022-06-14 17:13:59 +0100 | [diff] [blame] | 253 | call add(v:errors, "Unexpected dialog: " .. readfile('guidialogfile')->join('<NL>')) |
Bram Moolenaar | 2d12c25 | 2022-06-13 21:42:45 +0100 | [diff] [blame] | 254 | call delete('guidialogfile') |
| 255 | endif |
| 256 | |
Bram Moolenaar | ce11de8 | 2017-10-26 22:00:00 +0200 | [diff] [blame] | 257 | " Close any extra tab pages and windows and make the current one not modified. |
| 258 | while tabpagenr('$') > 1 |
Bram Moolenaar | bdf931c | 2020-10-01 22:37:40 +0200 | [diff] [blame] | 259 | let winid = win_getid() |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 260 | quit! |
Bram Moolenaar | bdf931c | 2020-10-01 22:37:40 +0200 | [diff] [blame] | 261 | if winid == win_getid() |
| 262 | echoerr 'Could not quit window' |
| 263 | break |
| 264 | endif |
Bram Moolenaar | ce11de8 | 2017-10-26 22:00:00 +0200 | [diff] [blame] | 265 | endwhile |
| 266 | |
Bram Moolenaar | 358308d | 2016-08-24 21:21:26 +0200 | [diff] [blame] | 267 | while 1 |
| 268 | let wincount = winnr('$') |
| 269 | if wincount == 1 |
| 270 | break |
| 271 | endif |
Bram Moolenaar | 7cba71d | 2016-08-02 23:04:49 +0200 | [diff] [blame] | 272 | bwipe! |
Bram Moolenaar | 358308d | 2016-08-24 21:21:26 +0200 | [diff] [blame] | 273 | if wincount == winnr('$') |
| 274 | " Did not manage to close a window. |
| 275 | only! |
| 276 | break |
| 277 | endif |
Bram Moolenaar | 7cba71d | 2016-08-02 23:04:49 +0200 | [diff] [blame] | 278 | endwhile |
Bram Moolenaar | 209d387 | 2017-11-16 21:52:51 +0100 | [diff] [blame] | 279 | |
| 280 | exe 'cd ' . save_cwd |
Bram Moolenaar | 640d4f0 | 2019-06-10 17:43:46 +0200 | [diff] [blame] | 281 | |
| 282 | let message = 'Executed ' . a:test |
| 283 | if has('reltime') |
Bram Moolenaar | 8d94379 | 2020-06-21 20:39:37 +0200 | [diff] [blame] | 284 | let message ..= repeat(' ', 50 - len(message)) |
| 285 | let time = reltime(func_start) |
| 286 | if has('float') && reltimefloat(time) > 0.1 |
Bram Moolenaar | 45df2a0 | 2020-07-29 15:03:01 +0200 | [diff] [blame] | 287 | let message = s:t_bold .. message |
Bram Moolenaar | 8d94379 | 2020-06-21 20:39:37 +0200 | [diff] [blame] | 288 | endif |
| 289 | let message ..= ' in ' .. reltimestr(time) .. ' seconds' |
| 290 | if has('float') && reltimefloat(time) > 0.1 |
Bram Moolenaar | 45df2a0 | 2020-07-29 15:03:01 +0200 | [diff] [blame] | 291 | let message ..= s:t_normal |
Bram Moolenaar | 8d94379 | 2020-06-21 20:39:37 +0200 | [diff] [blame] | 292 | endif |
Bram Moolenaar | 640d4f0 | 2019-06-10 17:43:46 +0200 | [diff] [blame] | 293 | endif |
| 294 | call add(s:messages, message) |
| 295 | let s:done += 1 |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 296 | endfunc |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 297 | |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 298 | func AfterTheTest(func_name) |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 299 | if len(v:errors) > 0 |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 300 | if match(s:may_fail_list, '^' .. a:func_name) >= 0 |
| 301 | let s:fail_expected += 1 |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 302 | call add(s:errors_expected, 'Found errors in ' . g:testfunc . ':') |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 303 | call extend(s:errors_expected, v:errors) |
| 304 | else |
| 305 | let s:fail += 1 |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 306 | call add(s:errors, 'Found errors in ' . g:testfunc . ':') |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 307 | call extend(s:errors, v:errors) |
| 308 | endif |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 309 | let v:errors = [] |
| 310 | endif |
| 311 | endfunc |
| 312 | |
Bram Moolenaar | 8903676 | 2018-06-12 14:58:39 +0200 | [diff] [blame] | 313 | func EarlyExit(test) |
| 314 | " It's OK for the test we use to test the quit detection. |
| 315 | if a:test != 'Test_zz_quit_detected()' |
Bram Moolenaar | 1c67f3a | 2021-12-30 13:32:09 +0000 | [diff] [blame] | 316 | call add(v:errors, v:errmsg) |
Bram Moolenaar | 8903676 | 2018-06-12 14:58:39 +0200 | [diff] [blame] | 317 | call add(v:errors, 'Test caused Vim to exit: ' . a:test) |
| 318 | endif |
| 319 | |
| 320 | call FinishTesting() |
| 321 | endfunc |
| 322 | |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 323 | " This function can be called by a test if it wants to abort testing. |
| 324 | func FinishTesting() |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 325 | call AfterTheTest('') |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 326 | |
| 327 | " Don't write viminfo on exit. |
| 328 | set viminfo= |
| 329 | |
Bram Moolenaar | d1ee004 | 2017-07-29 20:39:53 +0200 | [diff] [blame] | 330 | " Clean up files created by setup.vim |
| 331 | call delete('XfakeHOME', 'rf') |
| 332 | |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 333 | if s:fail == 0 && s:fail_expected == 0 |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 334 | " Success, create the .res file so that make knows it's done. |
| 335 | exe 'split ' . fnamemodify(g:testname, ':r') . '.res' |
| 336 | write |
| 337 | endif |
| 338 | |
| 339 | if len(s:errors) > 0 |
| 340 | " Append errors to test.log |
| 341 | split test.log |
| 342 | call append(line('$'), '') |
| 343 | call append(line('$'), 'From ' . g:testname . ':') |
| 344 | call append(line('$'), s:errors) |
| 345 | write |
| 346 | endif |
| 347 | |
Bram Moolenaar | 29f9ed2 | 2018-04-10 19:20:31 +0200 | [diff] [blame] | 348 | if s:done == 0 |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 349 | if s:filtered > 0 |
Bram Moolenaar | dae453f | 2021-08-07 17:20:16 +0200 | [diff] [blame] | 350 | if $TEST_FILTER != '' |
| 351 | let message = "NO tests match $TEST_FILTER: '" .. $TEST_FILTER .. "'" |
| 352 | else |
| 353 | let message = "ALL tests match $TEST_SKIP_PAT: '" .. $TEST_SKIP_PAT .. "'" |
| 354 | endif |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 355 | else |
| 356 | let message = 'NO tests executed' |
| 357 | endif |
Bram Moolenaar | 29f9ed2 | 2018-04-10 19:20:31 +0200 | [diff] [blame] | 358 | else |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 359 | if s:filtered > 0 |
Bram Moolenaar | dae453f | 2021-08-07 17:20:16 +0200 | [diff] [blame] | 360 | 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] | 361 | endif |
Bram Moolenaar | 29f9ed2 | 2018-04-10 19:20:31 +0200 | [diff] [blame] | 362 | let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test') |
| 363 | endif |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 364 | if s:done > 0 && has('reltime') |
Bram Moolenaar | 45df2a0 | 2020-07-29 15:03:01 +0200 | [diff] [blame] | 365 | let message = s:t_bold .. message .. repeat(' ', 40 - len(message)) |
Bram Moolenaar | 75ee544 | 2019-06-06 18:05:25 +0200 | [diff] [blame] | 366 | let message ..= ' in ' .. reltimestr(reltime(s:start_time)) .. ' seconds' |
Bram Moolenaar | 45df2a0 | 2020-07-29 15:03:01 +0200 | [diff] [blame] | 367 | let message ..= s:t_normal |
Bram Moolenaar | 75ee544 | 2019-06-06 18:05:25 +0200 | [diff] [blame] | 368 | endif |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 369 | echo message |
| 370 | call add(s:messages, message) |
| 371 | if s:fail > 0 |
| 372 | let message = s:fail . ' FAILED:' |
| 373 | echo message |
| 374 | call add(s:messages, message) |
| 375 | call extend(s:messages, s:errors) |
| 376 | endif |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 377 | if s:fail_expected > 0 |
| 378 | let message = s:fail_expected . ' FAILED (matching $TEST_MAY_FAIL):' |
| 379 | echo message |
| 380 | call add(s:messages, message) |
| 381 | call extend(s:messages, s:errors_expected) |
| 382 | endif |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 383 | |
| 384 | " Add SKIPPED messages |
| 385 | call extend(s:messages, s:skipped) |
| 386 | |
| 387 | " Append messages to the file "messages" |
| 388 | split messages |
| 389 | call append(line('$'), '') |
| 390 | call append(line('$'), 'From ' . g:testname . ':') |
| 391 | call append(line('$'), s:messages) |
| 392 | write |
| 393 | |
| 394 | qall! |
| 395 | endfunc |
| 396 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 397 | " 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] | 398 | " navigates away. g:testname can be used by the tests. |
| 399 | let g:testname = expand('%') |
| 400 | let s:done = 0 |
| 401 | let s:fail = 0 |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 402 | let s:fail_expected = 0 |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 403 | let s:errors = [] |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 404 | let s:errors_expected = [] |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 405 | let s:messages = [] |
Bram Moolenaar | dac1947 | 2016-09-03 22:35:40 +0200 | [diff] [blame] | 406 | let s:skipped = [] |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 407 | if expand('%') =~ 'test_vimscript.vim' |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 408 | " this test has intentional errors, don't use try/catch. |
Bram Moolenaar | 4686b32 | 2015-12-28 14:44:10 +0100 | [diff] [blame] | 409 | source % |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 410 | else |
| 411 | try |
| 412 | source % |
Bram Moolenaar | 9c0cec6 | 2019-06-06 13:38:15 +0200 | [diff] [blame] | 413 | catch /^\cskipped/ |
| 414 | call add(s:messages, ' Skipped') |
| 415 | call add(s:skipped, 'SKIPPED ' . expand('%') . ': ' . substitute(v:exception, '^\S*\s\+', '', '')) |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 416 | catch |
Bram Moolenaar | 00af60b | 2016-02-13 14:06:14 +0100 | [diff] [blame] | 417 | let s:fail += 1 |
| 418 | call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint) |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 419 | endtry |
| 420 | endif |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 421 | |
Bram Moolenaar | 7e0be3e | 2022-03-20 13:40:41 +0000 | [diff] [blame] | 422 | " Delete the .res file, it may change behavior for completion |
| 423 | call delete(fnamemodify(g:testname, ':r') .. '.res') |
| 424 | |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 425 | " Locate Test_ functions and execute them. |
| 426 | redir @q |
Bram Moolenaar | 93bf558 | 2016-02-18 22:25:47 +0100 | [diff] [blame] | 427 | silent function /^Test_ |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 428 | redir END |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 429 | let s:tests = split(substitute(@q, '\(function\|def\) \(\k*()\)', '\2', 'g')) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 430 | |
Bram Moolenaar | befb366 | 2016-02-20 14:41:40 +0100 | [diff] [blame] | 431 | " If there is an extra argument filter the function names against it. |
| 432 | if argc() > 1 |
| 433 | let s:tests = filter(s:tests, 'v:val =~ argv(1)') |
| 434 | endif |
| 435 | |
Bram Moolenaar | a7f6c3c | 2019-09-27 15:34:16 +0200 | [diff] [blame] | 436 | " If the environment variable $TEST_FILTER is set then filter the function |
| 437 | " names against it. |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 438 | let s:filtered = 0 |
Bram Moolenaar | a7f6c3c | 2019-09-27 15:34:16 +0200 | [diff] [blame] | 439 | if $TEST_FILTER != '' |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 440 | let s:filtered = len(s:tests) |
Bram Moolenaar | a7f6c3c | 2019-09-27 15:34:16 +0200 | [diff] [blame] | 441 | let s:tests = filter(s:tests, 'v:val =~ $TEST_FILTER') |
Bram Moolenaar | 7b666c7 | 2019-09-27 21:25:00 +0200 | [diff] [blame] | 442 | let s:filtered -= len(s:tests) |
Bram Moolenaar | a7f6c3c | 2019-09-27 15:34:16 +0200 | [diff] [blame] | 443 | endif |
| 444 | |
Bram Moolenaar | ce436de | 2020-03-21 15:17:20 +0100 | [diff] [blame] | 445 | let s:may_fail_list = [] |
| 446 | if $TEST_MAY_FAIL != '' |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 447 | " 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] | 448 | let s:may_fail_list = split($TEST_MAY_FAIL, ',')->map({i, v -> v .. '()'}) |
| 449 | endif |
| 450 | |
Bram Moolenaar | cfc0a35 | 2016-01-09 20:23:00 +0100 | [diff] [blame] | 451 | " Execute the tests in alphabetical order. |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 452 | for g:testfunc in sort(s:tests) |
Bram Moolenaar | dae453f | 2021-08-07 17:20:16 +0200 | [diff] [blame] | 453 | if $TEST_SKIP_PAT != '' && g:testfunc =~ $TEST_SKIP_PAT |
| 454 | call add(s:messages, g:testfunc .. ' matches $TEST_SKIP_PAT') |
| 455 | let s:filtered += 1 |
| 456 | continue |
| 457 | endif |
| 458 | |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 459 | " Silence, please! |
| 460 | set belloff=all |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 461 | let prev_error = '' |
| 462 | let total_errors = [] |
Bram Moolenaar | 3ed9efc | 2020-03-26 16:50:57 +0100 | [diff] [blame] | 463 | let g:run_nr = 1 |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 464 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 465 | " A test can set g:test_is_flaky to retry running the test. |
| 466 | let g:test_is_flaky = 0 |
Bram Moolenaar | 3cdcb09 | 2020-03-18 19:18:10 +0100 | [diff] [blame] | 467 | |
Bram Moolenaar | 06d32a0 | 2022-09-03 13:58:47 +0100 | [diff] [blame] | 468 | let starttime = strftime("%T") |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 469 | call RunTheTest(g:testfunc) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 470 | |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 471 | " Repeat a flaky test. Give up when: |
Bram Moolenaar | 6ca6ca4 | 2020-07-27 19:47:07 +0200 | [diff] [blame] | 472 | " - $TEST_NO_RETRY is not empty |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 473 | " - it fails again with the same message |
Bram Moolenaar | 1bc353b | 2019-09-01 14:45:28 +0200 | [diff] [blame] | 474 | " - it fails five times (with a different message) |
Bram Moolenaar | dbc0d21 | 2018-11-16 18:22:45 +0100 | [diff] [blame] | 475 | if len(v:errors) > 0 |
Bram Moolenaar | 622b356 | 2020-07-27 20:02:41 +0200 | [diff] [blame] | 476 | \ && $TEST_NO_RETRY == '' |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 477 | \ && g:test_is_flaky |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 478 | while 1 |
Bram Moolenaar | 06d32a0 | 2022-09-03 13:58:47 +0100 | [diff] [blame] | 479 | call add(s:messages, 'Found errors in ' .. g:testfunc .. ':') |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 480 | call extend(s:messages, v:errors) |
Bram Moolenaar | 15e737f | 2017-03-18 21:22:47 +0100 | [diff] [blame] | 481 | |
Bram Moolenaar | 06d32a0 | 2022-09-03 13:58:47 +0100 | [diff] [blame] | 482 | call add(total_errors, starttime .. ' Run ' .. g:run_nr .. ':') |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 483 | call extend(total_errors, v:errors) |
Bram Moolenaar | 5505860 | 2017-11-21 15:14:51 +0100 | [diff] [blame] | 484 | |
Bram Moolenaar | 96ba25a | 2022-07-04 17:34:33 +0100 | [diff] [blame] | 485 | if g:run_nr >= 5 || prev_error == v:errors[0] |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 486 | call add(total_errors, 'Flaky test failed too often, giving up') |
| 487 | let v:errors = total_errors |
| 488 | break |
| 489 | endif |
| 490 | |
| 491 | call add(s:messages, 'Flaky test failed, running it again') |
| 492 | |
| 493 | " Flakiness is often caused by the system being very busy. Sleep a |
| 494 | " couple of seconds to have a higher chance of succeeding the second |
| 495 | " time. |
| 496 | sleep 2 |
| 497 | |
| 498 | let prev_error = v:errors[0] |
| 499 | let v:errors = [] |
Bram Moolenaar | 3ed9efc | 2020-03-26 16:50:57 +0100 | [diff] [blame] | 500 | let g:run_nr += 1 |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 501 | |
Bram Moolenaar | 06d32a0 | 2022-09-03 13:58:47 +0100 | [diff] [blame] | 502 | let starttime = strftime("%T") |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 503 | call RunTheTest(g:testfunc) |
Bram Moolenaar | f77af0e | 2018-11-16 16:52:16 +0100 | [diff] [blame] | 504 | |
| 505 | if len(v:errors) == 0 |
| 506 | " Test passed on rerun. |
| 507 | break |
| 508 | endif |
| 509 | endwhile |
Bram Moolenaar | b5760a1 | 2016-03-03 13:10:44 +0100 | [diff] [blame] | 510 | endif |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 511 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 512 | call AfterTheTest(g:testfunc) |
Bram Moolenaar | 4334554 | 2015-11-29 17:35:35 +0100 | [diff] [blame] | 513 | endfor |
| 514 | |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 515 | call FinishTesting() |
Bram Moolenaar | cc28e2d | 2016-11-17 17:56:13 +0100 | [diff] [blame] | 516 | |
| 517 | " vim: shiftwidth=2 sts=2 expandtab |