xref: /vim-8.2.3635/src/testdir/runtest.vim (revision 8903676d)
143345546SBram Moolenaar" This script is sourced while editing the .vim file with the tests.
243345546SBram Moolenaar" When the script is successful the .res file will be created.
343345546SBram Moolenaar" Errors are appended to the test.log file.
443345546SBram Moolenaar"
5befb366aSBram Moolenaar" To execute only specific test functions, add a second argument.  It will be
6befb366aSBram Moolenaar" matched against the names of the Test_ funtion.  E.g.:
7befb366aSBram Moolenaar"	../vim -u NONE -S runtest.vim test_channel.vim open_delay
8befb366aSBram Moolenaar" The output can be found in the "messages" file.
9befb366aSBram Moolenaar"
1043345546SBram Moolenaar" The test script may contain anything, only functions that start with
1143345546SBram Moolenaar" "Test_" are special.  These will be invoked and should contain assert
1243345546SBram Moolenaar" functions.  See test_assert.vim for an example.
1343345546SBram Moolenaar"
1443345546SBram Moolenaar" It is possible to source other files that contain "Test_" functions.  This
1543345546SBram Moolenaar" can speed up testing, since Vim does not need to restart.  But be careful
1643345546SBram Moolenaar" that the tests do not interfere with each other.
1743345546SBram Moolenaar"
1843345546SBram Moolenaar" If an error cannot be detected properly with an assert function add the
1943345546SBram Moolenaar" error to the v:errors list:
2043345546SBram Moolenaar"   call add(v:errors, 'test foo failed: Cannot find xyz')
2143345546SBram Moolenaar"
2243345546SBram Moolenaar" If preparation for each Test_ function is needed, define a SetUp function.
2343345546SBram Moolenaar" It will be called before each Test_ function.
2443345546SBram Moolenaar"
2543345546SBram Moolenaar" If cleanup after each Test_ function is needed, define a TearDown function.
2643345546SBram Moolenaar" It will be called after each Test_ function.
2700af60bbSBram Moolenaar"
2800af60bbSBram Moolenaar" When debugging a test it can be useful to add messages to v:errors:
2900af60bbSBram Moolenaar" 	call add(v:errors, "this happened")
3000af60bbSBram Moolenaar
3143345546SBram Moolenaar
3243345546SBram Moolenaar" Without the +eval feature we can't run these tests, bail out.
334686b323SBram Moolenaarso small.vim
3443345546SBram Moolenaar
3543345546SBram Moolenaar" Check that the screen size is at least 24 x 80 characters.
3643345546SBram Moolenaarif &lines < 24 || &columns < 80
3743345546SBram Moolenaar  let error = 'Screen size too small! Tests require at least 24 lines with 80 characters'
3843345546SBram Moolenaar  echoerr error
3943345546SBram Moolenaar  split test.log
4043345546SBram Moolenaar  $put =error
4143345546SBram Moolenaar  w
4243345546SBram Moolenaar  cquit
4343345546SBram Moolenaarendif
4443345546SBram Moolenaar
4589b10421SBram Moolenaar" Common with all tests on all systems.
4689b10421SBram Moolenaarsource setup.vim
4789b10421SBram Moolenaar
48c0662466SBram Moolenaar" For consistency run all tests with 'nocompatible' set.
49c0662466SBram Moolenaar" This also enables use of line continuation.
50c0662466SBram Moolenaarset nocp viminfo+=nviminfo
51c0662466SBram Moolenaar
52eb992cb9SBram Moolenaar" Use utf-8 or latin1 by default, instead of whatever the system default
53ac105ed3SBram Moolenaar" happens to be.  Individual tests can overrule this at the top of the file.
54ac105ed3SBram Moolenaarif has('multi_byte')
55ac105ed3SBram Moolenaar  set encoding=utf-8
56ac105ed3SBram Moolenaarelse
57ac105ed3SBram Moolenaar  set encoding=latin1
58ac105ed3SBram Moolenaarendif
59ac105ed3SBram Moolenaar
607a073549SBram Moolenaar" Avoid stopping at the "hit enter" prompt
617a073549SBram Moolenaarset nomore
627a073549SBram Moolenaar
63c0662466SBram Moolenaar" Output all messages in English.
64c0662466SBram Moolenaarlang mess C
65c0662466SBram Moolenaar
66f60b796fSBram Moolenaar" Always use forward slashes.
67f60b796fSBram Moolenaarset shellslash
68f60b796fSBram Moolenaar
6928fb79dbSBram Moolenaarlet s:srcdir = expand('%:p:h:h')
7028fb79dbSBram Moolenaar
718e8df251SBram Moolenaar" Prepare for calling test_garbagecollect_now().
72ebf7dfa6SBram Moolenaarlet v:testing = 1
73ebf7dfa6SBram Moolenaar
7428fb79dbSBram Moolenaar" Support function: get the alloc ID by name.
7528fb79dbSBram Moolenaarfunction GetAllocId(name)
7628fb79dbSBram Moolenaar  exe 'split ' . s:srcdir . '/alloc.h'
77065ee9aeSBram Moolenaar  let top = search('typedef enum')
78065ee9aeSBram Moolenaar  if top == 0
79065ee9aeSBram Moolenaar    call add(v:errors, 'typedef not found in alloc.h')
80065ee9aeSBram Moolenaar  endif
8128fb79dbSBram Moolenaar  let lnum = search('aid_' . a:name . ',')
8228fb79dbSBram Moolenaar  if lnum == 0
8328fb79dbSBram Moolenaar    call add(v:errors, 'Alloc ID ' . a:name . ' not defined')
8428fb79dbSBram Moolenaar  endif
8528fb79dbSBram Moolenaar  close
86065ee9aeSBram Moolenaar  return lnum - top - 1
8728fb79dbSBram Moolenaarendfunc
8828fb79dbSBram Moolenaar
8942205551SBram Moolenaarfunc RunTheTest(test)
90b5760a1cSBram Moolenaar  echo 'Executing ' . a:test
91e5f2a075SBram Moolenaar
92e5f2a075SBram Moolenaar  " Avoid stopping at the "hit enter" prompt
93e5f2a075SBram Moolenaar  set nomore
94e5f2a075SBram Moolenaar
95e5f2a075SBram Moolenaar  " Avoid a three second wait when a message is about to be overwritten by the
96e5f2a075SBram Moolenaar  " mode message.
97e5f2a075SBram Moolenaar  set noshowmode
98e5f2a075SBram Moolenaar
99eb992cb9SBram Moolenaar  " Clear any overrides.
100eb992cb9SBram Moolenaar  call test_override('ALL', 0)
101eb992cb9SBram Moolenaar
102cf1ba35fSBram Moolenaar  " Some tests wipe out buffers.  To be consistent, always wipe out all
103cf1ba35fSBram Moolenaar  " buffers.
104cf1ba35fSBram Moolenaar  %bwipe!
105cf1ba35fSBram Moolenaar
106209d3874SBram Moolenaar  " The test may change the current directory. Save and restore the
107209d3874SBram Moolenaar  " directory after executing the test.
108209d3874SBram Moolenaar  let save_cwd = getcwd()
109209d3874SBram Moolenaar
110b5760a1cSBram Moolenaar  if exists("*SetUp")
111cc28e2d0SBram Moolenaar    try
112b5760a1cSBram Moolenaar      call SetUp()
113cc28e2d0SBram Moolenaar    catch
114cc28e2d0SBram Moolenaar      call add(v:errors, 'Caught exception in SetUp() before ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
115cc28e2d0SBram Moolenaar    endtry
116b5760a1cSBram Moolenaar  endif
117b5760a1cSBram Moolenaar
118b5760a1cSBram Moolenaar  call add(s:messages, 'Executing ' . a:test)
119b5760a1cSBram Moolenaar  let s:done += 1
120f204e05aSBram Moolenaar
121f204e05aSBram Moolenaar  if a:test =~ 'Test_nocatch_'
122f204e05aSBram Moolenaar    " Function handles errors itself.  This avoids skipping commands after the
123f204e05aSBram Moolenaar    " error.
124f204e05aSBram Moolenaar    exe 'call ' . a:test
125f204e05aSBram Moolenaar  else
126b5760a1cSBram Moolenaar    try
127*8903676dSBram Moolenaar      let s:test = a:test
128*8903676dSBram Moolenaar      au VimLeavePre * call EarlyExit(s:test)
129b5760a1cSBram Moolenaar      exe 'call ' . a:test
130*8903676dSBram Moolenaar      au! VimLeavePre
131dac1947bSBram Moolenaar    catch /^\cskipped/
132dac1947bSBram Moolenaar      call add(s:messages, '    Skipped')
133dac1947bSBram Moolenaar      call add(s:skipped, 'SKIPPED ' . a:test . ': ' . substitute(v:exception, '^\S*\s\+', '',  ''))
134b5760a1cSBram Moolenaar    catch
135b5760a1cSBram Moolenaar      call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
136b5760a1cSBram Moolenaar    endtry
137f204e05aSBram Moolenaar  endif
138b5760a1cSBram Moolenaar
139b5760a1cSBram Moolenaar  if exists("*TearDown")
140cc28e2d0SBram Moolenaar    try
141b5760a1cSBram Moolenaar      call TearDown()
142cc28e2d0SBram Moolenaar    catch
143cc28e2d0SBram Moolenaar      call add(v:errors, 'Caught exception in TearDown() after ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint)
144cc28e2d0SBram Moolenaar    endtry
145b5760a1cSBram Moolenaar  endif
1467cba71d7SBram Moolenaar
147cf1ba35fSBram Moolenaar  " Clear any autocommands
148cf1ba35fSBram Moolenaar  au!
149cf1ba35fSBram Moolenaar
150ce11de87SBram Moolenaar  " Close any extra tab pages and windows and make the current one not modified.
151ce11de87SBram Moolenaar  while tabpagenr('$') > 1
152cf1ba35fSBram Moolenaar    quit!
153ce11de87SBram Moolenaar  endwhile
154ce11de87SBram Moolenaar
155358308ddSBram Moolenaar  while 1
156358308ddSBram Moolenaar    let wincount = winnr('$')
157358308ddSBram Moolenaar    if wincount == 1
158358308ddSBram Moolenaar      break
159358308ddSBram Moolenaar    endif
1607cba71d7SBram Moolenaar    bwipe!
161358308ddSBram Moolenaar    if wincount == winnr('$')
162358308ddSBram Moolenaar      " Did not manage to close a window.
163358308ddSBram Moolenaar      only!
164358308ddSBram Moolenaar      break
165358308ddSBram Moolenaar    endif
1667cba71d7SBram Moolenaar  endwhile
167209d3874SBram Moolenaar
168209d3874SBram Moolenaar  exe 'cd ' . save_cwd
169b5760a1cSBram Moolenaarendfunc
17028fb79dbSBram Moolenaar
17142205551SBram Moolenaarfunc AfterTheTest()
17243345546SBram Moolenaar  if len(v:errors) > 0
17300af60bbSBram Moolenaar    let s:fail += 1
17400af60bbSBram Moolenaar    call add(s:errors, 'Found errors in ' . s:test . ':')
17500af60bbSBram Moolenaar    call extend(s:errors, v:errors)
17643345546SBram Moolenaar    let v:errors = []
17743345546SBram Moolenaar  endif
17842205551SBram Moolenaarendfunc
17942205551SBram Moolenaar
180*8903676dSBram Moolenaarfunc EarlyExit(test)
181*8903676dSBram Moolenaar  " It's OK for the test we use to test the quit detection.
182*8903676dSBram Moolenaar  if a:test != 'Test_zz_quit_detected()'
183*8903676dSBram Moolenaar    call add(v:errors, 'Test caused Vim to exit: ' . a:test)
184*8903676dSBram Moolenaar  endif
185*8903676dSBram Moolenaar
186*8903676dSBram Moolenaar  call FinishTesting()
187*8903676dSBram Moolenaarendfunc
188*8903676dSBram Moolenaar
18942205551SBram Moolenaar" This function can be called by a test if it wants to abort testing.
19042205551SBram Moolenaarfunc FinishTesting()
19142205551SBram Moolenaar  call AfterTheTest()
19243345546SBram Moolenaar
193fc4ad616SBram Moolenaar  " Don't write viminfo on exit.
194fc4ad616SBram Moolenaar  set viminfo=
195fc4ad616SBram Moolenaar
196d1ee0043SBram Moolenaar  " Clean up files created by setup.vim
197d1ee0043SBram Moolenaar  call delete('XfakeHOME', 'rf')
198d1ee0043SBram Moolenaar
19900af60bbSBram Moolenaar  if s:fail == 0
20043345546SBram Moolenaar    " Success, create the .res file so that make knows it's done.
20100af60bbSBram Moolenaar    exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
20243345546SBram Moolenaar    write
20343345546SBram Moolenaar  endif
20443345546SBram Moolenaar
20500af60bbSBram Moolenaar  if len(s:errors) > 0
20643345546SBram Moolenaar    " Append errors to test.log
20743345546SBram Moolenaar    split test.log
20843345546SBram Moolenaar    call append(line('$'), '')
20900af60bbSBram Moolenaar    call append(line('$'), 'From ' . g:testname . ':')
21000af60bbSBram Moolenaar    call append(line('$'), s:errors)
21143345546SBram Moolenaar    write
21243345546SBram Moolenaar  endif
21343345546SBram Moolenaar
21429f9ed20SBram Moolenaar  if s:done == 0
21529f9ed20SBram Moolenaar    let message = 'NO tests executed'
21629f9ed20SBram Moolenaar  else
21700af60bbSBram Moolenaar    let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
21829f9ed20SBram Moolenaar  endif
219096c8bb4SBram Moolenaar  echo message
22000af60bbSBram Moolenaar  call add(s:messages, message)
22100af60bbSBram Moolenaar  if s:fail > 0
22200af60bbSBram Moolenaar    let message = s:fail . ' FAILED:'
223096c8bb4SBram Moolenaar    echo message
22400af60bbSBram Moolenaar    call add(s:messages, message)
22500af60bbSBram Moolenaar    call extend(s:messages, s:errors)
22643345546SBram Moolenaar  endif
22743345546SBram Moolenaar
228dac1947bSBram Moolenaar  " Add SKIPPED messages
229dac1947bSBram Moolenaar  call extend(s:messages, s:skipped)
230dac1947bSBram Moolenaar
231dac1947bSBram Moolenaar  " Append messages to the file "messages"
232096c8bb4SBram Moolenaar  split messages
233096c8bb4SBram Moolenaar  call append(line('$'), '')
23400af60bbSBram Moolenaar  call append(line('$'), 'From ' . g:testname . ':')
23500af60bbSBram Moolenaar  call append(line('$'), s:messages)
236096c8bb4SBram Moolenaar  write
237096c8bb4SBram Moolenaar
23843345546SBram Moolenaar  qall!
23942205551SBram Moolenaarendfunc
24042205551SBram Moolenaar
24142205551SBram Moolenaar" Source the test script.  First grab the file name, in case the script
24242205551SBram Moolenaar" navigates away.  g:testname can be used by the tests.
24342205551SBram Moolenaarlet g:testname = expand('%')
24442205551SBram Moolenaarlet s:done = 0
24542205551SBram Moolenaarlet s:fail = 0
24642205551SBram Moolenaarlet s:errors = []
24742205551SBram Moolenaarlet s:messages = []
24842205551SBram Moolenaarlet s:skipped = []
24942205551SBram Moolenaarif expand('%') =~ 'test_vimscript.vim'
25042205551SBram Moolenaar  " this test has intentional s:errors, don't use try/catch.
25142205551SBram Moolenaar  source %
25242205551SBram Moolenaarelse
25342205551SBram Moolenaar  try
25442205551SBram Moolenaar    source %
25542205551SBram Moolenaar  catch
25642205551SBram Moolenaar    let s:fail += 1
25742205551SBram Moolenaar    call add(s:errors, 'Caught exception: ' . v:exception . ' @ ' . v:throwpoint)
25842205551SBram Moolenaar  endtry
25942205551SBram Moolenaarendif
26042205551SBram Moolenaar
26142205551SBram Moolenaar" Names of flaky tests.
26242205551SBram Moolenaarlet s:flaky = [
26342205551SBram Moolenaar      \ 'Test_client_server()',
26442205551SBram Moolenaar      \ 'Test_close_and_exit_cb()',
26542205551SBram Moolenaar      \ 'Test_collapse_buffers()',
26642205551SBram Moolenaar      \ 'Test_communicate()',
26765873846SBram Moolenaar      \ 'Test_cwd()',
26842205551SBram Moolenaar      \ 'Test_exit_callback_interval()',
26942205551SBram Moolenaar      \ 'Test_nb_basic()',
27042205551SBram Moolenaar      \ 'Test_oneshot()',
2711eca6f13SBram Moolenaar      \ 'Test_out_cb()',
27224820691SBram Moolenaar      \ 'Test_paused()',
27342205551SBram Moolenaar      \ 'Test_pipe_through_sort_all()',
27442205551SBram Moolenaar      \ 'Test_pipe_through_sort_some()',
27542205551SBram Moolenaar      \ 'Test_quoteplus()',
2767dd48506SBram Moolenaar      \ 'Test_quotestar()',
27742205551SBram Moolenaar      \ 'Test_reltime()',
278bfbea567SBram Moolenaar      \ 'Test_repeat_three()',
279f204e05aSBram Moolenaar      \ 'Test_terminal_composing_unicode()',
28075a60f74SBram Moolenaar      \ 'Test_terminal_noblock()',
2817dd88c51SBram Moolenaar      \ 'Test_terminal_redir_file()',
28224820691SBram Moolenaar      \ 'Test_terminal_tmap()',
283d09be321SBram Moolenaar      \ 'Test_with_partial_callback()',
28442205551SBram Moolenaar      \ ]
28542205551SBram Moolenaar
28642205551SBram Moolenaar" Locate Test_ functions and execute them.
28742205551SBram Moolenaarredir @q
28842205551SBram Moolenaarsilent function /^Test_
28942205551SBram Moolenaarredir END
29042205551SBram Moolenaarlet s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
29142205551SBram Moolenaar
29242205551SBram Moolenaar" If there is an extra argument filter the function names against it.
29342205551SBram Moolenaarif argc() > 1
29442205551SBram Moolenaar  let s:tests = filter(s:tests, 'v:val =~ argv(1)')
29542205551SBram Moolenaarendif
29642205551SBram Moolenaar
29742205551SBram Moolenaar" Execute the tests in alphabetical order.
29842205551SBram Moolenaarfor s:test in sort(s:tests)
2994a6fcf80SBram Moolenaar  " Silence, please!
3004a6fcf80SBram Moolenaar  set belloff=all
3014a6fcf80SBram Moolenaar
30242205551SBram Moolenaar  call RunTheTest(s:test)
30342205551SBram Moolenaar
30442205551SBram Moolenaar  if len(v:errors) > 0 && index(s:flaky, s:test) >= 0
3056caf606bSBram Moolenaar    call add(s:messages, 'Found errors in ' . s:test . ':')
3066caf606bSBram Moolenaar    call extend(s:messages, v:errors)
30742205551SBram Moolenaar    call add(s:messages, 'Flaky test failed, running it again')
30815e737f7SBram Moolenaar    let first_run = v:errors
30915e737f7SBram Moolenaar
31055058601SBram Moolenaar    " Flakiness is often caused by the system being very busy.  Sleep a couple
31155058601SBram Moolenaar    " of seconds to have a higher chance of succeeding the second time.
31255058601SBram Moolenaar    sleep 2
31355058601SBram Moolenaar
31442205551SBram Moolenaar    let v:errors = []
31542205551SBram Moolenaar    call RunTheTest(s:test)
31615e737f7SBram Moolenaar    if len(v:errors) > 0
31715e737f7SBram Moolenaar      let second_run = v:errors
31815e737f7SBram Moolenaar      let v:errors = ['First run:']
31915e737f7SBram Moolenaar      call extend(v:errors, first_run)
32015e737f7SBram Moolenaar      call add(v:errors, 'Second run:')
32115e737f7SBram Moolenaar      call extend(v:errors, second_run)
32215e737f7SBram Moolenaar    endif
32342205551SBram Moolenaar  endif
32442205551SBram Moolenaar
32542205551SBram Moolenaar  call AfterTheTest()
32642205551SBram Moolenaarendfor
32742205551SBram Moolenaar
32842205551SBram Moolenaarcall FinishTesting()
329cc28e2d0SBram Moolenaar
330cc28e2d0SBram Moolenaar" vim: shiftwidth=2 sts=2 expandtab
331