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