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