blob: e120e7f23b58461f5cca2347cefc5847f768589c [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
6" matched against the names of the Test_ funtion. E.g.:
7" ../vim -u NONE -S runtest.vim test_channel.vim open_delay
8" The output can be found in the "messages" file.
9"
Bram Moolenaar43345542015-11-29 17:35:35 +010010" The test script may contain anything, only functions that start with
11" "Test_" are special. These will be invoked and should contain assert
12" functions. See test_assert.vim for an example.
13"
14" It is possible to source other files that contain "Test_" functions. This
15" can speed up testing, since Vim does not need to restart. But be careful
16" that the tests do not interfere with each other.
17"
18" If an error cannot be detected properly with an assert function add the
19" error to the v:errors list:
20" call add(v:errors, 'test foo failed: Cannot find xyz')
21"
22" If preparation for each Test_ function is needed, define a SetUp function.
23" It will be called before each Test_ function.
24"
25" If cleanup after each Test_ function is needed, define a TearDown function.
26" It will be called after each Test_ function.
Bram Moolenaar00af60b2016-02-13 14:06:14 +010027"
28" When debugging a test it can be useful to add messages to v:errors:
Bram Moolenaar8ad16da2019-01-06 15:29:57 +010029" call add(v:errors, "this happened")
Bram Moolenaar00af60b2016-02-13 14:06:14 +010030
Bram Moolenaar43345542015-11-29 17:35:35 +010031
32" Without the +eval feature we can't run these tests, bail out.
Bram Moolenaar4686b322015-12-28 14:44:10 +010033so small.vim
Bram Moolenaar43345542015-11-29 17:35:35 +010034
35" Check that the screen size is at least 24 x 80 characters.
36if &lines < 24 || &columns < 80
37 let error = 'Screen size too small! Tests require at least 24 lines with 80 characters'
38 echoerr error
39 split test.log
40 $put =error
Bram Moolenaar45aa07d2019-06-15 18:20:38 +020041 write
42 split messages
43 call append(line('$'), error)
44 write
45 qa!
Bram Moolenaar43345542015-11-29 17:35:35 +010046endif
47
Bram Moolenaar75ee5442019-06-06 18:05:25 +020048if has('reltime')
49 let s:start_time = reltime()
50endif
51
Bram Moolenaar89b10422016-07-12 22:51:22 +020052" Common with all tests on all systems.
53source setup.vim
54
Bram Moolenaarc0662462015-12-30 15:49:05 +010055" For consistency run all tests with 'nocompatible' set.
56" This also enables use of line continuation.
57set nocp viminfo+=nviminfo
58
Bram Moolenaar30276f22019-01-24 17:59:39 +010059" Use utf-8 by default, instead of whatever the system default happens to be.
Bram Moolenaared79d1e2019-02-22 14:38:58 +010060" Individual tests can overrule this at the top of the file and use
61" g:orig_encoding if needed.
62let g:orig_encoding = &encoding
Bram Moolenaar30276f22019-01-24 17:59:39 +010063set encoding=utf-8
Bram Moolenaarac105ed2016-07-21 20:33:32 +020064
Bram Moolenaard8f27b32018-10-07 15:42:07 +020065" REDIR_TEST_TO_NULL has a very permissive SwapExists autocommand which is for
66" the test_name.vim file itself. Replace it here with a more restrictive one,
67" so we still catch mistakes.
68let s:test_script_fname = expand('%')
69au! SwapExists * call HandleSwapExists()
70func HandleSwapExists()
71 " Only ignore finding a swap file for the test script (the user might be
72 " editing it and do ":make test_name") and the output file.
73 if expand('<afile>') == 'messages' || expand('<afile>') =~ s:test_script_fname
74 let v:swapchoice = 'e'
75 endif
76endfunc
77
Bram Moolenaar7a073542017-02-01 23:17:36 +010078" Avoid stopping at the "hit enter" prompt
79set nomore
80
Bram Moolenaarc0662462015-12-30 15:49:05 +010081" Output all messages in English.
82lang mess C
83
Bram Moolenaarf60b7962016-01-16 22:47:23 +010084" Always use forward slashes.
85set shellslash
86
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010087let s:srcdir = expand('%:p:h:h')
88
Bram Moolenaar8e8df252016-05-25 21:23:21 +020089" Prepare for calling test_garbagecollect_now().
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +020090let v:testing = 1
91
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010092" Support function: get the alloc ID by name.
93function GetAllocId(name)
94 exe 'split ' . s:srcdir . '/alloc.h'
Bram Moolenaar065ee9a2016-01-15 20:53:38 +010095 let top = search('typedef enum')
96 if top == 0
97 call add(v:errors, 'typedef not found in alloc.h')
98 endif
Bram Moolenaar28fb79d2016-01-09 22:28:33 +010099 let lnum = search('aid_' . a:name . ',')
100 if lnum == 0
101 call add(v:errors, 'Alloc ID ' . a:name . ' not defined')
102 endif
103 close
Bram Moolenaar065ee9a2016-01-15 20:53:38 +0100104 return lnum - top - 1
Bram Moolenaar28fb79d2016-01-09 22:28:33 +0100105endfunc
106
Bram Moolenaar42205552017-03-18 19:42:22 +0100107func RunTheTest(test)
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100108 echo 'Executing ' . a:test
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200109 if has('reltime')
110 let func_start = reltime()
111 endif
Bram Moolenaare5f2a072017-02-01 22:31:49 +0100112
113 " Avoid stopping at the "hit enter" prompt
114 set nomore
115
116 " Avoid a three second wait when a message is about to be overwritten by the
117 " mode message.
118 set noshowmode
119
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100120 " Clear any overrides.
121 call test_override('ALL', 0)
122
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200123 " Some tests wipe out buffers. To be consistent, always wipe out all
124 " buffers.
125 %bwipe!
126
Bram Moolenaar209d3872017-11-16 21:52:51 +0100127 " The test may change the current directory. Save and restore the
128 " directory after executing the test.
129 let save_cwd = getcwd()
130
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100131 if exists("*SetUp")
Bram Moolenaarcc28e2d2016-11-17 17:56:13 +0100132 try
133 call SetUp()
134 catch
135 call add(v:errors, 'Caught exception in SetUp() before ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
136 endtry
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100137 endif
138
Bram Moolenaarf204e052017-10-26 17:14:01 +0200139 if a:test =~ 'Test_nocatch_'
140 " Function handles errors itself. This avoids skipping commands after the
141 " error.
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100142 exe 'call ' . a:test
Bram Moolenaarf204e052017-10-26 17:14:01 +0200143 else
144 try
Bram Moolenaar89036762018-06-12 14:58:39 +0200145 let s:test = a:test
146 au VimLeavePre * call EarlyExit(s:test)
Bram Moolenaarf204e052017-10-26 17:14:01 +0200147 exe 'call ' . a:test
Bram Moolenaar89036762018-06-12 14:58:39 +0200148 au! VimLeavePre
Bram Moolenaarf204e052017-10-26 17:14:01 +0200149 catch /^\cskipped/
150 call add(s:messages, ' Skipped')
151 call add(s:skipped, 'SKIPPED ' . a:test . ': ' . substitute(v:exception, '^\S*\s\+', '', ''))
152 catch
153 call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
154 endtry
155 endif
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100156
Bram Moolenaar8ad16da2019-01-06 15:29:57 +0100157 " In case 'insertmode' was set and something went wrong, make sure it is
158 " reset to avoid trouble with anything else.
159 set noinsertmode
160
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100161 if exists("*TearDown")
Bram Moolenaarcc28e2d2016-11-17 17:56:13 +0100162 try
163 call TearDown()
164 catch
165 call add(v:errors, 'Caught exception in TearDown() after ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
166 endtry
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100167 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200168
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200169 " Clear any autocommands
170 au!
Bram Moolenaard8f27b32018-10-07 15:42:07 +0200171 au SwapExists * call HandleSwapExists()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200172
Bram Moolenaarae943152019-06-16 22:54:14 +0200173 " Close any stray popup windows
174 if has('textprop')
175 call popup_clear()
176 endif
177
Bram Moolenaarce11de82017-10-26 22:00:00 +0200178 " Close any extra tab pages and windows and make the current one not modified.
179 while tabpagenr('$') > 1
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200180 quit!
Bram Moolenaarce11de82017-10-26 22:00:00 +0200181 endwhile
182
Bram Moolenaar358308d2016-08-24 21:21:26 +0200183 while 1
184 let wincount = winnr('$')
185 if wincount == 1
186 break
187 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200188 bwipe!
Bram Moolenaar358308d2016-08-24 21:21:26 +0200189 if wincount == winnr('$')
190 " Did not manage to close a window.
191 only!
192 break
193 endif
Bram Moolenaar7cba71d2016-08-02 23:04:49 +0200194 endwhile
Bram Moolenaar209d3872017-11-16 21:52:51 +0100195
196 exe 'cd ' . save_cwd
Bram Moolenaar640d4f02019-06-10 17:43:46 +0200197
198 let message = 'Executed ' . a:test
199 if has('reltime')
200 let message ..= ' in ' .. reltimestr(reltime(func_start)) .. ' seconds'
201 endif
202 call add(s:messages, message)
203 let s:done += 1
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100204endfunc
Bram Moolenaar28fb79d2016-01-09 22:28:33 +0100205
Bram Moolenaar42205552017-03-18 19:42:22 +0100206func AfterTheTest()
207 if len(v:errors) > 0
208 let s:fail += 1
209 call add(s:errors, 'Found errors in ' . s:test . ':')
210 call extend(s:errors, v:errors)
211 let v:errors = []
212 endif
213endfunc
214
Bram Moolenaar89036762018-06-12 14:58:39 +0200215func EarlyExit(test)
216 " It's OK for the test we use to test the quit detection.
217 if a:test != 'Test_zz_quit_detected()'
218 call add(v:errors, 'Test caused Vim to exit: ' . a:test)
219 endif
220
221 call FinishTesting()
222endfunc
223
Bram Moolenaar42205552017-03-18 19:42:22 +0100224" This function can be called by a test if it wants to abort testing.
225func FinishTesting()
226 call AfterTheTest()
227
228 " Don't write viminfo on exit.
229 set viminfo=
230
Bram Moolenaard1ee0042017-07-29 20:39:53 +0200231 " Clean up files created by setup.vim
232 call delete('XfakeHOME', 'rf')
233
Bram Moolenaar42205552017-03-18 19:42:22 +0100234 if s:fail == 0
235 " Success, create the .res file so that make knows it's done.
236 exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
237 write
238 endif
239
240 if len(s:errors) > 0
241 " Append errors to test.log
242 split test.log
243 call append(line('$'), '')
244 call append(line('$'), 'From ' . g:testname . ':')
245 call append(line('$'), s:errors)
246 write
247 endif
248
Bram Moolenaar29f9ed22018-04-10 19:20:31 +0200249 if s:done == 0
250 let message = 'NO tests executed'
251 else
252 let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
253 endif
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200254 if has('reltime')
255 let message ..= ' in ' .. reltimestr(reltime(s:start_time)) .. ' seconds'
256 endif
Bram Moolenaar42205552017-03-18 19:42:22 +0100257 echo message
258 call add(s:messages, message)
259 if s:fail > 0
260 let message = s:fail . ' FAILED:'
261 echo message
262 call add(s:messages, message)
263 call extend(s:messages, s:errors)
264 endif
265
266 " Add SKIPPED messages
267 call extend(s:messages, s:skipped)
268
269 " Append messages to the file "messages"
270 split messages
271 call append(line('$'), '')
272 call append(line('$'), 'From ' . g:testname . ':')
273 call append(line('$'), s:messages)
274 write
275
276 qall!
277endfunc
278
Bram Moolenaar43345542015-11-29 17:35:35 +0100279" Source the test script. First grab the file name, in case the script
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100280" navigates away. g:testname can be used by the tests.
281let g:testname = expand('%')
282let s:done = 0
283let s:fail = 0
284let s:errors = []
285let s:messages = []
Bram Moolenaardac19472016-09-03 22:35:40 +0200286let s:skipped = []
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100287if expand('%') =~ 'test_vimscript.vim'
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100288 " this test has intentional s:errors, don't use try/catch.
Bram Moolenaar4686b322015-12-28 14:44:10 +0100289 source %
Bram Moolenaara2cce862016-01-02 19:50:04 +0100290else
291 try
292 source %
Bram Moolenaar9c0cec62019-06-06 13:38:15 +0200293 catch /^\cskipped/
294 call add(s:messages, ' Skipped')
295 call add(s:skipped, 'SKIPPED ' . expand('%') . ': ' . substitute(v:exception, '^\S*\s\+', '', ''))
Bram Moolenaara2cce862016-01-02 19:50:04 +0100296 catch
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100297 let s:fail += 1
298 call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
Bram Moolenaara2cce862016-01-02 19:50:04 +0100299 endtry
300endif
Bram Moolenaar43345542015-11-29 17:35:35 +0100301
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100302" Names of flaky tests.
Bram Moolenaardbc0d212018-11-16 18:22:45 +0100303let s:flaky_tests = [
Bram Moolenaarc0f05d02018-11-16 17:44:48 +0100304 \ 'Test_call()',
305 \ 'Test_channel_handler()',
Bram Moolenaar42205552017-03-18 19:42:22 +0100306 \ 'Test_client_server()',
Bram Moolenaar6fe2eb42017-01-29 21:49:51 +0100307 \ 'Test_close_and_exit_cb()',
Bram Moolenaarc0f05d02018-11-16 17:44:48 +0100308 \ 'Test_close_callback()',
309 \ 'Test_close_handle()',
310 \ 'Test_close_lambda()',
Bram Moolenaard80232b2018-12-15 17:46:23 +0100311 \ 'Test_close_output_buffer()',
Bram Moolenaarc0f05d02018-11-16 17:44:48 +0100312 \ 'Test_close_partial()',
Bram Moolenaarb2455592017-02-01 18:00:13 +0100313 \ 'Test_collapse_buffers()',
314 \ 'Test_communicate()',
Bram Moolenaar65873842018-03-25 17:12:58 +0200315 \ 'Test_cwd()',
Bram Moolenaar218959b2018-11-11 18:51:42 +0100316 \ 'Test_diff_screen()',
Bram Moolenaarc0f05d02018-11-16 17:44:48 +0100317 \ 'Test_exit_callback()',
Bram Moolenaar0529b3e2017-03-16 22:30:37 +0100318 \ 'Test_exit_callback_interval()',
Bram Moolenaarb2455592017-02-01 18:00:13 +0100319 \ 'Test_nb_basic()',
Bram Moolenaard512e172017-02-27 21:35:53 +0100320 \ 'Test_oneshot()',
Bram Moolenaarc0f05d02018-11-16 17:44:48 +0100321 \ 'Test_open_delay()',
Bram Moolenaar1eca6f12017-12-05 14:04:27 +0100322 \ 'Test_out_cb()',
Bram Moolenaar24820692017-12-02 16:38:12 +0100323 \ 'Test_paused()',
Bram Moolenaarc79d6aa2016-09-25 22:27:37 +0200324 \ 'Test_pipe_through_sort_all()',
Bram Moolenaar4e032e12017-02-01 20:48:13 +0100325 \ 'Test_pipe_through_sort_some()',
Bram Moolenaar142ae732018-08-19 17:04:01 +0200326 \ 'Test_popup_and_window_resize()',
Bram Moolenaar0fbff642017-03-05 14:30:52 +0100327 \ 'Test_quoteplus()',
Bram Moolenaar7dd48502017-03-19 20:04:22 +0100328 \ 'Test_quotestar()',
Bram Moolenaarc0f05d02018-11-16 17:44:48 +0100329 \ 'Test_raw_one_time_callback()',
Bram Moolenaarb2455592017-02-01 18:00:13 +0100330 \ 'Test_reltime()',
Bram Moolenaarbfbea562018-02-12 21:31:35 +0100331 \ 'Test_repeat_three()',
Bram Moolenaarc0f05d02018-11-16 17:44:48 +0100332 \ 'Test_server_crash()',
333 \ 'Test_terminal_ansicolors_default()',
334 \ 'Test_terminal_ansicolors_func()',
335 \ 'Test_terminal_ansicolors_global()',
Bram Moolenaarf204e052017-10-26 17:14:01 +0200336 \ 'Test_terminal_composing_unicode()',
Bram Moolenaar38767892019-02-21 18:17:14 +0100337 \ 'Test_terminal_does_not_truncate_last_newlines()',
Bram Moolenaarc0f05d02018-11-16 17:44:48 +0100338 \ 'Test_terminal_env()',
339 \ 'Test_terminal_hide_buffer()',
340 \ 'Test_terminal_make_change()',
Bram Moolenaar3615abb2019-02-10 23:04:12 +0100341 \ 'Test_terminal_no_cmd()',
Bram Moolenaar75a60f72017-09-07 22:24:41 +0200342 \ 'Test_terminal_noblock()',
Bram Moolenaar7dd88c52017-11-04 20:46:40 +0100343 \ 'Test_terminal_redir_file()',
Bram Moolenaarc0f05d02018-11-16 17:44:48 +0100344 \ 'Test_terminal_response_to_control_sequence()',
345 \ 'Test_terminal_scrollback()',
346 \ 'Test_terminal_split_quit()',
347 \ 'Test_terminal_termwinkey()',
348 \ 'Test_terminal_termwinsize_mininmum()',
349 \ 'Test_terminal_termwinsize_option_fixed()',
350 \ 'Test_terminal_termwinsize_option_zero()',
Bram Moolenaar24820692017-12-02 16:38:12 +0100351 \ 'Test_terminal_tmap()',
Bram Moolenaarc0f05d02018-11-16 17:44:48 +0100352 \ 'Test_terminal_wall()',
353 \ 'Test_terminal_wipe_buffer()',
354 \ 'Test_terminal_wqall()',
355 \ 'Test_two_channels()',
356 \ 'Test_unlet_handle()',
Bram Moolenaard09be322017-07-30 21:37:58 +0200357 \ 'Test_with_partial_callback()',
Bram Moolenaarc0f05d02018-11-16 17:44:48 +0100358 \ 'Test_zero_reply()',
359 \ 'Test_zz1_terminal_in_gui()',
Bram Moolenaare1c8c7a2016-09-11 16:48:50 +0200360 \ ]
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100361
Bram Moolenaardbc0d212018-11-16 18:22:45 +0100362" Pattern indicating a common flaky test failure.
Bram Moolenaar447f6ce2018-11-16 18:50:19 +0100363let s:flaky_errors_re = 'StopVimInTerminal\|VerifyScreenDump'
Bram Moolenaardbc0d212018-11-16 18:22:45 +0100364
Bram Moolenaar43345542015-11-29 17:35:35 +0100365" Locate Test_ functions and execute them.
366redir @q
Bram Moolenaar93bf5582016-02-18 22:25:47 +0100367silent function /^Test_
Bram Moolenaar43345542015-11-29 17:35:35 +0100368redir END
Bram Moolenaar00af60b2016-02-13 14:06:14 +0100369let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
Bram Moolenaar43345542015-11-29 17:35:35 +0100370
Bram Moolenaarbefb3662016-02-20 14:41:40 +0100371" If there is an extra argument filter the function names against it.
372if argc() > 1
373 let s:tests = filter(s:tests, 'v:val =~ argv(1)')
374endif
375
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100376" Execute the tests in alphabetical order.
Bram Moolenaar93bf5582016-02-18 22:25:47 +0100377for s:test in sort(s:tests)
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200378 " Silence, please!
379 set belloff=all
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100380 let prev_error = ''
381 let total_errors = []
382 let run_nr = 1
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200383
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100384 call RunTheTest(s:test)
Bram Moolenaar43345542015-11-29 17:35:35 +0100385
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100386 " Repeat a flaky test. Give up when:
387 " - it fails again with the same message
388 " - it fails five times (with a different mesage)
Bram Moolenaardbc0d212018-11-16 18:22:45 +0100389 if len(v:errors) > 0
390 \ && (index(s:flaky_tests, s:test) >= 0
391 \ || v:errors[0] =~ s:flaky_errors_re)
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100392 while 1
393 call add(s:messages, 'Found errors in ' . s:test . ':')
394 call extend(s:messages, v:errors)
Bram Moolenaar15e737f2017-03-18 21:22:47 +0100395
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100396 call add(total_errors, 'Run ' . run_nr . ':')
397 call extend(total_errors, v:errors)
Bram Moolenaar55058602017-11-21 15:14:51 +0100398
Bram Moolenaarf77af0e2018-11-16 16:52:16 +0100399 if run_nr == 5 || prev_error == v:errors[0]
400 call add(total_errors, 'Flaky test failed too often, giving up')
401 let v:errors = total_errors
402 break
403 endif
404
405 call add(s:messages, 'Flaky test failed, running it again')
406
407 " Flakiness is often caused by the system being very busy. Sleep a
408 " couple of seconds to have a higher chance of succeeding the second
409 " time.
410 sleep 2
411
412 let prev_error = v:errors[0]
413 let v:errors = []
414 let run_nr += 1
415
416 call RunTheTest(s:test)
417
418 if len(v:errors) == 0
419 " Test passed on rerun.
420 break
421 endif
422 endwhile
Bram Moolenaarb5760a12016-03-03 13:10:44 +0100423 endif
Bram Moolenaar43345542015-11-29 17:35:35 +0100424
Bram Moolenaar42205552017-03-18 19:42:22 +0100425 call AfterTheTest()
Bram Moolenaar43345542015-11-29 17:35:35 +0100426endfor
427
Bram Moolenaar42205552017-03-18 19:42:22 +0100428call FinishTesting()
Bram Moolenaarcc28e2d2016-11-17 17:56:13 +0100429
430" vim: shiftwidth=2 sts=2 expandtab