blob: ebc692b478d92f90d035cce29be4baa992b0b238 [file] [log] [blame]
Bram Moolenaar43345542015-11-29 17:35:35 +01001" 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 Moolenaarbefb3662016-02-20 14:41:40 +01005" To execute only specific test functions, add a second argument. It will be
Bram Moolenaare219f732019-11-30 15:34:08 +01006" matched against the names of the Test_ function. E.g.:
Bram Moolenaarbefb3662016-02-20 14:41:40 +01007" ../vim -u NONE -S runtest.vim test_channel.vim open_delay
8" The output can be found in the "messages" file.
9"
Bram Moolenaarce436de2020-03-21 15:17:20 +010010" 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 Moolenaardae453f2021-08-07 17:20:16 +020016" 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 Moolenaar6ca6ca42020-07-27 19:47:07 +020019" While working on a test you can make $TEST_NO_RETRY non-empty to not retry:
20" export TEST_NO_RETRY=yes
21"
Bram Moolenaarce436de2020-03-21 15:17:20 +010022" 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 Moolenaar43345542015-11-29 17:35:35 +010029" 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 Moolenaar00af60b2016-02-13 14:06:14 +010046"
47" When debugging a test it can be useful to add messages to v:errors:
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010048" call add(v:errors, "this happened")
Bram Moolenaar00af60b2016-02-13 14:06:14 +010049
Bram Moolenaar43345542015-11-29 17:35:35 +010050
51" Without the +eval feature we can't run these tests, bail out.
Bram Moolenaarb96a32e2020-08-13 18:59:55 +020052silent! while 0
53 qa!
54silent! endwhile
Bram Moolenaar43345542015-11-29 17:35:35 +010055
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020056" In the GUI we can always change the screen size.
57if has('gui_running')
58 set columns=80 lines=25
59endif
60
Bram Moolenaar43345542015-11-29 17:35:35 +010061" Check that the screen size is at least 24 x 80 characters.
62if &lines < 24 || &columns < 80
Bram Moolenaar0b5dc642019-08-11 22:56:15 +020063 let error = 'Screen size too small! Tests require at least 24 lines with 80 characters, got ' .. &lines .. ' lines with ' .. &columns .. ' characters'
Bram Moolenaar43345542015-11-29 17:35:35 +010064 echoerr error
65 split test.log
66 $put =error
Bram Moolenaar45aa07d2019-06-15 18:20:38 +020067 write
68 split messages
Bram Moolenaar0b5dc642019-08-11 22:56:15 +020069 call append(line('$'), '')
70 call append(line('$'), 'From ' . expand('%') . ':')
Bram Moolenaar45aa07d2019-06-15 18:20:38 +020071 call append(line('$'), error)
72 write
73 qa!
Bram Moolenaar43345542015-11-29 17:35:35 +010074endif
75
Bram Moolenaar75ee5442019-06-06 18:05:25 +020076if has('reltime')
77 let s:start_time = reltime()
78endif
79
Bakudankun29f3a452021-12-11 12:28:08 +000080" Always use forward slashes.
81set shellslash
82
Bram Moolenaar89b10422016-07-12 22:51:22 +020083" Common with all tests on all systems.
84source setup.vim
85
Bram Moolenaarc0662462015-12-30 15:49:05 +010086" For consistency run all tests with 'nocompatible' set.
87" This also enables use of line continuation.
88set nocp viminfo+=nviminfo
89
Bram Moolenaar30276f22019-01-24 17:59:39 +010090" Use utf-8 by default, instead of whatever the system default happens to be.
Bram Moolenaared79d1e2019-02-22 14:38:58 +010091" Individual tests can overrule this at the top of the file and use
92" g:orig_encoding if needed.
93let g:orig_encoding = &encoding
Bram Moolenaar30276f22019-01-24 17:59:39 +010094set encoding=utf-8
Bram Moolenaarac105ed2016-07-21 20:33:32 +020095
Bram Moolenaard8f27b32018-10-07 15:42:07 +020096" 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 Brabandt4b2c8042021-11-03 22:31:44 +000099if has("win32")
100 " replace any '/' directory separators by '\\'
101 let s:test_script_fname = substitute(expand('%'), '/', '\\', 'g')
102else
103 let s:test_script_fname = expand('%')
104endif
Bram Moolenaard8f27b32018-10-07 15:42:07 +0200105au! SwapExists * call HandleSwapExists()
106func HandleSwapExists()
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +0200107 if exists('g:ignoreSwapExists')
108 return
109 endif
Bram Moolenaarb073da82019-07-13 14:47:26 +0200110 " Ignore finding a swap file for the test script (the user might be
Bram Moolenaard8f27b32018-10-07 15:42:07 +0200111 " editing it and do ":make test_name") and the output file.
Bram Moolenaarb073da82019-07-13 14:47:26 +0200112 " Report finding another swap file and chose 'q' to avoid getting stuck.
Bram Moolenaard8f27b32018-10-07 15:42:07 +0200113 if expand('<afile>') == 'messages' || expand('<afile>') =~ s:test_script_fname
114 let v:swapchoice = 'e'
Bram Moolenaarb073da82019-07-13 14:47:26 +0200115 else
116 call assert_report('Unexpected swap file: ' .. v:swapname)
117 let v:swapchoice = 'q'
Bram Moolenaard8f27b32018-10-07 15:42:07 +0200118 endif
119endfunc
120
Bram Moolenaar7a073542017-02-01 23:17:36 +0100121" Avoid stopping at the "hit enter" prompt
122set nomore
123
Bram Moolenaarc0662462015-12-30 15:49:05 +0100124" Output all messages in English.
125lang mess C
126
Bram Moolenaar10e1d012020-07-18 22:03:11 +0200127" suppress menu translation
128if has('gui_running') && exists('did_install_default_menus')
129 source $VIMRUNTIME/delmenu.vim
130 set langmenu=none
131 source $VIMRUNTIME/menu.vim
132endif
133
Bram Moolenaar28fb79d2016-01-09 22:28:33 +0100134let s:srcdir = expand('%:p:h:h')
135
Bram Moolenaar2690b5a2020-07-22 18:14:58 +0200136if has('win32')
137 " avoid prompt that is long or contains a line break
138 let $PROMPT = '$P$G'
Bram Moolenaar45df2a02020-07-29 15:03:01 +0200139 " 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"
142else
143 let s:t_bold = &t_md
144 let s:t_normal = &t_me
Bram Moolenaar2690b5a2020-07-22 18:14:58 +0200145endif
146
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100147if has('mac')
Dominique Pelle923dce22021-11-21 11:36:04 +0000148 " In macOS, when starting a shell in a terminal, a bash deprecation warning
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100149 " message is displayed. This breaks the terminal test. Disable the warning
150 " message.
151 let $BASH_SILENCE_DEPRECATION_WARNING = 1
152endif
153
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200154" Prepare for calling test_garbagecollect_now().
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200155let v:testing = 1
156
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100157" By default, copy each buffer line into allocated memory, so that valgrind can
158" detect accessing memory before and after it.
159call test_override('alloc_lines', 1)
160
Bram Moolenaar28fb79d2016-01-09 22:28:33 +0100161" Support function: get the alloc ID by name.
162function GetAllocId(name)
163 exe 'split ' . s:srcdir . '/alloc.h'
Bram Moolenaar065ee9a2016-01-15 20:53:38 +0100164 let top = search('typedef enum')
165 if top == 0
166 call add(v:errors, 'typedef not found in alloc.h')
167 endif
Bram Moolenaar28fb79d2016-01-09 22:28:33 +0100168 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 Moolenaar065ee9a2016-01-15 20:53:38 +0100173 return lnum - top - 1
Bram Moolenaar28fb79d2016-01-09 22:28:33 +0100174endfunc
175
Bram Moolenaar42205552017-03-18 19:42:22 +0100176func RunTheTest(test)
Bram Moolenaar4c868302021-03-22 16:19:45 +0100177 echoconsole 'Executing ' . a:test
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200178 if has('reltime')
179 let func_start = reltime()
180 endif
Bram Moolenaare5f2a072017-02-01 22:31:49 +0100181
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 Moolenaarfa4873c2022-06-30 22:13:59 +0100189 " Clear any overrides, except "alloc_lines".
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100190 call test_override('ALL', 0)
191
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200192 " Some tests wipe out buffers. To be consistent, always wipe out all
193 " buffers.
194 %bwipe!
195
Bram Moolenaar209d3872017-11-16 21:52:51 +0100196 " The test may change the current directory. Save and restore the
197 " directory after executing the test.
198 let save_cwd = getcwd()
199
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100200 if exists("*SetUp")
Bram Moolenaarcc28e2d2016-11-17 17:56:13 +0100201 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 Moolenaarb5760a12016-03-03 13:10:44 +0100206 endif
207
Bram Moolenaar8bea1712022-06-15 20:49:35 +0100208 au VimLeavePre * call EarlyExit(g:testfunc)
Bram Moolenaarf204e052017-10-26 17:14:01 +0200209 if a:test =~ 'Test_nocatch_'
210 " Function handles errors itself. This avoids skipping commands after the
211 " error.
Bram Moolenaar776b9542021-03-10 22:27:48 +0100212 let g:skipped_reason = ''
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100213 exe 'call ' . a:test
Bram Moolenaar776b9542021-03-10 22:27:48 +0100214 if g:skipped_reason != ''
215 call add(s:messages, ' Skipped')
216 call add(s:skipped, 'SKIPPED ' . a:test . ': ' . g:skipped_reason)
217 endif
Bram Moolenaarf204e052017-10-26 17:14:01 +0200218 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 Moolenaar8bea1712022-06-15 20:49:35 +0100228 au! VimLeavePre
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100229
Bram Moolenaara22c56a2022-09-20 15:10:31 +0100230 if a:test =~ '_terminal_'
231 " Terminal tests sometimes hang, give extra information
232 echoconsole 'After executing ' .. a:test
233 endif
234
Bram Moolenaar8ad16da2019-01-06 15:29:57 +0100235 " In case 'insertmode' was set and something went wrong, make sure it is
236 " reset to avoid trouble with anything else.
237 set noinsertmode
238
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100239 if exists("*TearDown")
Bram Moolenaarcc28e2d2016-11-17 17:56:13 +0100240 try
241 call TearDown()
242 catch
243 call add(v:errors, 'Caught exception in TearDown() after ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
244 endtry
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100245 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200246
Bram Moolenaar0b5dc642019-08-11 22:56:15 +0200247 " Clear any autocommands and put back the catch-all for SwapExists.
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200248 au!
Bram Moolenaard8f27b32018-10-07 15:42:07 +0200249 au SwapExists * call HandleSwapExists()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200250
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200251 " Check for and close any stray popup windows.
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100252 if has('popupwin')
Bram Moolenaarf05a1e52022-08-02 11:48:53 +0100253 call assert_equal([], popup_list(), 'Popup is still present')
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200254 call popup_clear(1)
Bram Moolenaarae943152019-06-16 22:54:14 +0200255 endif
256
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100257 if filereadable('guidialogfile')
Bram Moolenaar217ea512022-06-14 17:13:59 +0100258 call add(v:errors, "Unexpected dialog: " .. readfile('guidialogfile')->join('<NL>'))
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100259 call delete('guidialogfile')
260 endif
261
Bram Moolenaarce11de82017-10-26 22:00:00 +0200262 " Close any extra tab pages and windows and make the current one not modified.
263 while tabpagenr('$') > 1
Bram Moolenaarbdf931c2020-10-01 22:37:40 +0200264 let winid = win_getid()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200265 quit!
Bram Moolenaarbdf931c2020-10-01 22:37:40 +0200266 if winid == win_getid()
267 echoerr 'Could not quit window'
268 break
269 endif
Bram Moolenaarce11de82017-10-26 22:00:00 +0200270 endwhile
271
Bram Moolenaar358308d2016-08-24 21:21:26 +0200272 while 1
273 let wincount = winnr('$')
274 if wincount == 1
275 break
276 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200277 bwipe!
Bram Moolenaar358308d2016-08-24 21:21:26 +0200278 if wincount == winnr('$')
279 " Did not manage to close a window.
280 only!
281 break
282 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200283 endwhile
Bram Moolenaar209d3872017-11-16 21:52:51 +0100284
285 exe 'cd ' . save_cwd
Bram Moolenaar640d4f02019-06-10 17:43:46 +0200286
Bram Moolenaara22c56a2022-09-20 15:10:31 +0100287 if a:test =~ '_terminal_'
288 " Terminal tests sometimes hang, give extra information
289 echoconsole 'Finished ' . a:test
290 endif
291
Bram Moolenaar640d4f02019-06-10 17:43:46 +0200292 let message = 'Executed ' . a:test
293 if has('reltime')
Bram Moolenaar8d943792020-06-21 20:39:37 +0200294 let message ..= repeat(' ', 50 - len(message))
295 let time = reltime(func_start)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100296 if reltimefloat(time) > 0.1
Bram Moolenaar45df2a02020-07-29 15:03:01 +0200297 let message = s:t_bold .. message
Bram Moolenaar8d943792020-06-21 20:39:37 +0200298 endif
299 let message ..= ' in ' .. reltimestr(time) .. ' seconds'
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100300 if reltimefloat(time) > 0.1
Bram Moolenaar45df2a02020-07-29 15:03:01 +0200301 let message ..= s:t_normal
Bram Moolenaar8d943792020-06-21 20:39:37 +0200302 endif
Bram Moolenaar640d4f02019-06-10 17:43:46 +0200303 endif
304 call add(s:messages, message)
305 let s:done += 1
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100306endfunc
Bram Moolenaar28fb79d2016-01-09 22:28:33 +0100307
Bram Moolenaarce436de2020-03-21 15:17:20 +0100308func AfterTheTest(func_name)
Bram Moolenaar42205552017-03-18 19:42:22 +0100309 if len(v:errors) > 0
Bram Moolenaarce436de2020-03-21 15:17:20 +0100310 if match(s:may_fail_list, '^' .. a:func_name) >= 0
311 let s:fail_expected += 1
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200312 call add(s:errors_expected, 'Found errors in ' . g:testfunc . ':')
Bram Moolenaarce436de2020-03-21 15:17:20 +0100313 call extend(s:errors_expected, v:errors)
314 else
315 let s:fail += 1
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200316 call add(s:errors, 'Found errors in ' . g:testfunc . ':')
Bram Moolenaarce436de2020-03-21 15:17:20 +0100317 call extend(s:errors, v:errors)
318 endif
Bram Moolenaar42205552017-03-18 19:42:22 +0100319 let v:errors = []
320 endif
321endfunc
322
Bram Moolenaar89036762018-06-12 14:58:39 +0200323func EarlyExit(test)
324 " It's OK for the test we use to test the quit detection.
325 if a:test != 'Test_zz_quit_detected()'
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000326 call add(v:errors, v:errmsg)
Bram Moolenaar89036762018-06-12 14:58:39 +0200327 call add(v:errors, 'Test caused Vim to exit: ' . a:test)
328 endif
329
330 call FinishTesting()
331endfunc
332
Bram Moolenaar42205552017-03-18 19:42:22 +0100333" This function can be called by a test if it wants to abort testing.
334func FinishTesting()
Bram Moolenaarce436de2020-03-21 15:17:20 +0100335 call AfterTheTest('')
Bram Moolenaar42205552017-03-18 19:42:22 +0100336
337 " Don't write viminfo on exit.
338 set viminfo=
339
Bram Moolenaard1ee0042017-07-29 20:39:53 +0200340 " Clean up files created by setup.vim
341 call delete('XfakeHOME', 'rf')
342
Bram Moolenaarce436de2020-03-21 15:17:20 +0100343 if s:fail == 0 && s:fail_expected == 0
Bram Moolenaar42205552017-03-18 19:42:22 +0100344 " Success, create the .res file so that make knows it's done.
345 exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
346 write
347 endif
348
349 if len(s:errors) > 0
350 " Append errors to test.log
351 split test.log
352 call append(line('$'), '')
353 call append(line('$'), 'From ' . g:testname . ':')
354 call append(line('$'), s:errors)
355 write
356 endif
357
Bram Moolenaar29f9ed22018-04-10 19:20:31 +0200358 if s:done == 0
Bram Moolenaar7b666c72019-09-27 21:25:00 +0200359 if s:filtered > 0
Bram Moolenaardae453f2021-08-07 17:20:16 +0200360 if $TEST_FILTER != ''
361 let message = "NO tests match $TEST_FILTER: '" .. $TEST_FILTER .. "'"
362 else
363 let message = "ALL tests match $TEST_SKIP_PAT: '" .. $TEST_SKIP_PAT .. "'"
364 endif
Bram Moolenaar7b666c72019-09-27 21:25:00 +0200365 else
366 let message = 'NO tests executed'
367 endif
Bram Moolenaar29f9ed22018-04-10 19:20:31 +0200368 else
Bram Moolenaar7b666c72019-09-27 21:25:00 +0200369 if s:filtered > 0
Bram Moolenaardae453f2021-08-07 17:20:16 +0200370 call add(s:messages, "Filtered " .. s:filtered .. " tests with $TEST_FILTER and $TEST_SKIP_PAT")
Bram Moolenaar7b666c72019-09-27 21:25:00 +0200371 endif
Bram Moolenaar29f9ed22018-04-10 19:20:31 +0200372 let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
373 endif
Bram Moolenaar7b666c72019-09-27 21:25:00 +0200374 if s:done > 0 && has('reltime')
Bram Moolenaar45df2a02020-07-29 15:03:01 +0200375 let message = s:t_bold .. message .. repeat(' ', 40 - len(message))
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200376 let message ..= ' in ' .. reltimestr(reltime(s:start_time)) .. ' seconds'
Bram Moolenaar45df2a02020-07-29 15:03:01 +0200377 let message ..= s:t_normal
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200378 endif
Bram Moolenaar42205552017-03-18 19:42:22 +0100379 echo message
380 call add(s:messages, message)
381 if s:fail > 0
382 let message = s:fail . ' FAILED:'
383 echo message
384 call add(s:messages, message)
385 call extend(s:messages, s:errors)
386 endif
Bram Moolenaarce436de2020-03-21 15:17:20 +0100387 if s:fail_expected > 0
388 let message = s:fail_expected . ' FAILED (matching $TEST_MAY_FAIL):'
389 echo message
390 call add(s:messages, message)
391 call extend(s:messages, s:errors_expected)
392 endif
Bram Moolenaar42205552017-03-18 19:42:22 +0100393
394 " Add SKIPPED messages
395 call extend(s:messages, s:skipped)
396
397 " Append messages to the file "messages"
398 split messages
399 call append(line('$'), '')
400 call append(line('$'), 'From ' . g:testname . ':')
401 call append(line('$'), s:messages)
402 write
403
404 qall!
405endfunc
406
Bram Moolenaar43345542015-11-29 17:35:35 +0100407" Source the test script. First grab the file name, in case the script
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100408" navigates away. g:testname can be used by the tests.
409let g:testname = expand('%')
410let s:done = 0
411let s:fail = 0
Bram Moolenaarce436de2020-03-21 15:17:20 +0100412let s:fail_expected = 0
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100413let s:errors = []
Bram Moolenaarce436de2020-03-21 15:17:20 +0100414let s:errors_expected = []
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100415let s:messages = []
Bram Moolenaardac19472016-09-03 22:35:40 +0200416let s:skipped = []
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100417if expand('%') =~ 'test_vimscript.vim'
Bram Moolenaarce436de2020-03-21 15:17:20 +0100418 " this test has intentional errors, don't use try/catch.
Bram Moolenaar4686b322015-12-28 14:44:10 +0100419 source %
Bram Moolenaara2cce862016-01-02 19:50:04 +0100420else
421 try
422 source %
Bram Moolenaar9c0cec62019-06-06 13:38:15 +0200423 catch /^\cskipped/
424 call add(s:messages, ' Skipped')
425 call add(s:skipped, 'SKIPPED ' . expand('%') . ': ' . substitute(v:exception, '^\S*\s\+', '', ''))
Bram Moolenaara2cce862016-01-02 19:50:04 +0100426 catch
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100427 let s:fail += 1
428 call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
Bram Moolenaara2cce862016-01-02 19:50:04 +0100429 endtry
430endif
Bram Moolenaar43345542015-11-29 17:35:35 +0100431
Bram Moolenaar7e0be3e2022-03-20 13:40:41 +0000432" Delete the .res file, it may change behavior for completion
433call delete(fnamemodify(g:testname, ':r') .. '.res')
434
Bram Moolenaar43345542015-11-29 17:35:35 +0100435" Locate Test_ functions and execute them.
436redir @q
Bram Moolenaar93bf5582016-02-18 22:25:47 +0100437silent function /^Test_
Bram Moolenaar43345542015-11-29 17:35:35 +0100438redir END
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +0100439let s:tests = split(substitute(@q, '\(function\|def\) \(\k*()\)', '\2', 'g'))
Bram Moolenaar43345542015-11-29 17:35:35 +0100440
Bram Moolenaarbefb3662016-02-20 14:41:40 +0100441" If there is an extra argument filter the function names against it.
442if argc() > 1
443 let s:tests = filter(s:tests, 'v:val =~ argv(1)')
444endif
445
Bram Moolenaara7f6c3c2019-09-27 15:34:16 +0200446" If the environment variable $TEST_FILTER is set then filter the function
447" names against it.
Bram Moolenaar7b666c72019-09-27 21:25:00 +0200448let s:filtered = 0
Bram Moolenaara7f6c3c2019-09-27 15:34:16 +0200449if $TEST_FILTER != ''
Bram Moolenaar7b666c72019-09-27 21:25:00 +0200450 let s:filtered = len(s:tests)
Bram Moolenaara7f6c3c2019-09-27 15:34:16 +0200451 let s:tests = filter(s:tests, 'v:val =~ $TEST_FILTER')
Bram Moolenaar7b666c72019-09-27 21:25:00 +0200452 let s:filtered -= len(s:tests)
Bram Moolenaara7f6c3c2019-09-27 15:34:16 +0200453endif
454
Bram Moolenaarce436de2020-03-21 15:17:20 +0100455let s:may_fail_list = []
456if $TEST_MAY_FAIL != ''
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200457 " Split the list at commas and add () to make it match g:testfunc.
Bram Moolenaarce436de2020-03-21 15:17:20 +0100458 let s:may_fail_list = split($TEST_MAY_FAIL, ',')->map({i, v -> v .. '()'})
459endif
460
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100461" Execute the tests in alphabetical order.
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200462for g:testfunc in sort(s:tests)
Bram Moolenaardae453f2021-08-07 17:20:16 +0200463 if $TEST_SKIP_PAT != '' && g:testfunc =~ $TEST_SKIP_PAT
464 call add(s:messages, g:testfunc .. ' matches $TEST_SKIP_PAT')
465 let s:filtered += 1
466 continue
467 endif
468
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200469 " Silence, please!
470 set belloff=all
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100471 let prev_error = ''
472 let total_errors = []
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +0100473 let g:run_nr = 1
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200474
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200475 " A test can set g:test_is_flaky to retry running the test.
476 let g:test_is_flaky = 0
Bram Moolenaar3cdcb092020-03-18 19:18:10 +0100477
Bram Moolenaar5fbbec12022-09-03 22:08:11 +0100478 let starttime = strftime("%H:%M:%S")
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200479 call RunTheTest(g:testfunc)
Bram Moolenaar43345542015-11-29 17:35:35 +0100480
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100481 " Repeat a flaky test. Give up when:
Bram Moolenaar6ca6ca42020-07-27 19:47:07 +0200482 " - $TEST_NO_RETRY is not empty
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100483 " - it fails again with the same message
Bram Moolenaar1bc353b2019-09-01 14:45:28 +0200484 " - it fails five times (with a different message)
Bram Moolenaardbc0d212018-11-16 18:22:45 +0100485 if len(v:errors) > 0
Bram Moolenaar622b3562020-07-27 20:02:41 +0200486 \ && $TEST_NO_RETRY == ''
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100487 \ && g:test_is_flaky
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100488 while 1
Bram Moolenaar06d32a02022-09-03 13:58:47 +0100489 call add(s:messages, 'Found errors in ' .. g:testfunc .. ':')
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100490 call extend(s:messages, v:errors)
Bram Moolenaar15e737f2017-03-18 21:22:47 +0100491
Bram Moolenaar65258d32022-09-09 15:09:59 +0100492 let endtime = strftime("%H:%M:%S")
493 call add(total_errors, $'Run {g:run_nr}, {starttime} - {endtime}:')
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100494 call extend(total_errors, v:errors)
Bram Moolenaar55058602017-11-21 15:14:51 +0100495
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100496 if g:run_nr >= 5 || prev_error == v:errors[0]
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100497 call add(total_errors, 'Flaky test failed too often, giving up')
498 let v:errors = total_errors
499 break
500 endif
501
502 call add(s:messages, 'Flaky test failed, running it again')
503
504 " Flakiness is often caused by the system being very busy. Sleep a
505 " couple of seconds to have a higher chance of succeeding the second
506 " time.
507 sleep 2
508
509 let prev_error = v:errors[0]
510 let v:errors = []
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +0100511 let g:run_nr += 1
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100512
Bram Moolenaar5fbbec12022-09-03 22:08:11 +0100513 let starttime = strftime("%H:%M:%S")
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200514 call RunTheTest(g:testfunc)
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100515
516 if len(v:errors) == 0
517 " Test passed on rerun.
518 break
519 endif
520 endwhile
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100521 endif
Bram Moolenaar43345542015-11-29 17:35:35 +0100522
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200523 call AfterTheTest(g:testfunc)
Bram Moolenaar43345542015-11-29 17:35:35 +0100524endfor
525
Bram Moolenaar42205552017-03-18 19:42:22 +0100526call FinishTesting()
Bram Moolenaarcc28e2d2016-11-17 17:56:13 +0100527
528" vim: shiftwidth=2 sts=2 expandtab