Skip to content

Commit

Permalink
More: Import latest YCM test framework changes
Browse files Browse the repository at this point in the history
  • Loading branch information
puremourning committed Jan 15, 2020
1 parent d711d65 commit f6eccf1
Showing 1 changed file with 74 additions and 57 deletions.
131 changes: 74 additions & 57 deletions tests/lib/run_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"
" To execute only specific test functions, add a second argument. It will be
" matched against the names of the Test_ funtion. E.g.:
" ../vim -u NONE -S runtest.vim test_channel.vim open_delay
" ../vim -Nu NONE vimrc -S lib/run_test.vim test_channel.vim open_delay
" The output can be found in the "messages" file.
"
" The test script may contain anything, only functions that start with
Expand All @@ -26,8 +26,16 @@
" It will be called after each Test_ function.
"
" When debugging a test it can be useful to add messages to v:errors:
" call add(v:errors, "this happened")
" call add(v:errors, "this happened")
"
" But for real debug logging:
" call ch_log( ",,,message..." )
" Then view it in 'debuglog'

" Let a test take up to 1 minute
let s:single_test_timeout = 60000

" Restrict the runtimepath to the exact minimum needed for testing
set rtp=$PWD/lib,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after
if has('packages')
let &packpath = &rtp
Expand All @@ -37,7 +45,7 @@ call ch_logfile( 'debuglog', 'w' )

" For consistency run all tests with 'nocompatible' set.
" This also enables use of line continuation.
set nocp viminfo+=nviminfo
set nocp

" Use utf-8 by default, instead of whatever the system default happens to be.
" Individual tests can overrule this at the top of the file.
Expand All @@ -47,11 +55,23 @@ set encoding=utf-8
set nomore

" Output all messages in English.
lang mess C
lang messages C

" Always use forward slashes.
set shellslash

func s:TestFailed()
let log = readfile( expand( '~/.vimspector.log' ) )
let logfile = s:testid_filesafe . '_vimspector.log.testlog'
call writefile( log, logfile, 's' )
call add( s:messages, 'Wrote log for failed test: ' . logfile )
endfunc

func! Abort( timer_id )
call assert_report( 'Test timed out!!!' )
qa!
endfunc

func RunTheTest(test)
echo 'Executing ' . a:test

Expand Down Expand Up @@ -107,40 +127,38 @@ func RunTheTest(test)

call add(s:messages, 'Executing ' . a:test)
let s:done += 1
let timer = timer_start( s:single_test_timeout, funcref( 'Abort' ) )

if a:test =~ 'Test_nocatch_'
" Function handles errors itself. This avoids skipping commands after the
" error.
try
let s:test = a:test
let s:testid = g:testpath . ':' . a:test

let test_filesafe = substitute( a:test, '[)(,:]', '_', 'g' )
let s:testid_filesafe = g:testpath . '_' . test_filesafe

au VimLeavePre * call EarlyExit(s:test)
exe 'call ' . a:test
else
try
let s:test = a:test
let s:testid = g:testpath . ':' . a:test
let test_filesafe = substitute( a:test, ')', '_', 'g' )
let test_filesafe = substitute( test_filesafe, '(', '_', 'g' )
let test_filesafe = substitute( test_filesafe, ',', '_', 'g' )
let test_filesafe = substitute( test_filesafe, ':', '_', 'g' )
let s:testid_filesafe = g:testpath . '_' . test_filesafe
au VimLeavePre * call EarlyExit(s:test)
exe 'call ' . a:test
au! VimLeavePre
catch /^\cskipped/
call add(s:messages, ' Skipped')
call add(s:skipped,
\ 'SKIPPED ' . a:test
\ . ': '
\ . substitute(v:exception, '^\S*\s\+', '', ''))
catch
call add(v:errors,
\ 'Caught exception in ' . a:test
\ . ': '
\ . v:exception
\ . ' @ '
\ . g:testpath
\ . ':'
\ . v:throwpoint)
endtry
endif
au! VimLeavePre
catch /^\cskipped/
call add(s:messages, ' Skipped')
call add(s:skipped,
\ 'SKIPPED ' . a:test
\ . ': '
\ . substitute(v:exception, '^\S*\s\+', '', ''))
catch
call add(v:errors,
\ 'Caught exception in ' . a:test
\ . ': '
\ . v:exception
\ . ' @ '
\ . g:testpath
\ . ':'
\ . v:throwpoint)

call s:TestFailed()
endtry

call timer_stop( timer )

" In case 'insertmode' was set and something went wrong, make sure it is
" reset to avoid trouble with anything else.
Expand Down Expand Up @@ -203,23 +221,16 @@ endfunc
func AfterTheTest()
if len(v:errors) > 0
let s:fail += 1
call s:TestFailed()
call add(s:errors, 'Found errors in ' . s:testid . ':')
call extend(s:errors, v:errors)
let v:errors = []

let log = readfile( expand( '~/.vimspector.log' ) )
let logfile = s:testid_filesafe . '.vimspector.log'
call writefile( log, logfile, 's' )
call add( s:messages, 'Wrote log for failed test: ' . logfile )
endif
endfunc

func EarlyExit(test)
" It's OK for the test we use to test the quit detection.
if a:test != 'Test_zz_quit_detected()'
call add(v:errors, 'Test caused Vim to exit: ' . a:test)
endif

call add(v:errors, 'Test caused Vim to exit: ' . a:test)
call FinishTesting()
endfunc

Expand All @@ -232,17 +243,19 @@ func FinishTesting()

if s:fail == 0
" Success, create the .res file so that make knows it's done.
exe 'split ' . fnamemodify(g:testname, ':r') . '.res'
write
call writefile( [], g:testname . '.res', 's' )
endif

if len(s:errors) > 0
" Append errors to test.log
split test.log
call append(line('$'), '')
call append(line('$'), 'From ' . g:testpath . ':')
call append(line('$'), s:errors)
write
let l = []
if filereadable( 'test.log' )
let l = readfile( 'test.log' )
endif
call writefile( l->extend( [ '', 'From ' . g:testpath . ':' ] )
\ ->extend( s:errors ),
\ 'test.log',
\ 's' )
endif

if s:done == 0
Expand All @@ -263,11 +276,14 @@ func FinishTesting()
call extend(s:messages, s:skipped)

" Append messages to the file "messages"
split messages
call append(line('$'), '')
call append(line('$'), 'From ' . g:testpath . ':')
call append(line('$'), s:messages)
write
let l = []
if filereadable( 'messages' )
let l = readfile( 'messages' )
endif
call writefile( l->extend( [ '', 'From ' . g:testpath . ':' ] )
\ ->extend( s:messages ),
\ 'messages',
\ 's' )

if s:fail > 0
cquit!
Expand Down Expand Up @@ -316,6 +332,7 @@ endif
for s:test in sort(s:tests)
" Silence, please!
set belloff=all

call RunTheTest(s:test)
call AfterTheTest()
endfor
Expand Down

0 comments on commit f6eccf1

Please sign in to comment.