run_test.vim 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. "
  5. " 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 -Nu NONE vimrc -S lib/run_test.vim test_channel.vim open_delay
  8. " The output can be found in the "messages" file.
  9. "
  10. " 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.
  27. "
  28. " When debugging a test it can be useful to add messages to v:errors:
  29. " call add(v:errors, "this happened")
  30. "
  31. " But for real debug logging:
  32. " call ch_log( ",,,message..." )
  33. " Then view it in 'debuglog'
  34. " Let a test take up to 1 minute
  35. let s:single_test_timeout = 60000
  36. " Restrict the runtimepath to the exact minimum needed for testing
  37. set rtp=$PWD/lib,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after
  38. call ch_logfile( 'debuglog', 'w' )
  39. " For consistency run all tests with 'nocompatible' set.
  40. " This also enables use of line continuation.
  41. set nocp
  42. " Use utf-8 by default, instead of whatever the system default happens to be.
  43. " Individual tests can overrule this at the top of the file.
  44. set encoding=utf-8
  45. " Avoid stopping at the "hit enter" prompt
  46. set nomore
  47. " Output all messages in English.
  48. lang messages C
  49. " Always use forward slashes.
  50. set shellslash
  51. func s:TestFailed()
  52. if pyxeval( '"ycm_state" in globals()' )
  53. let logs = pyxeval( 'ycm_state.GetLogfiles()' )
  54. for log_name in sort( keys( logs ) )
  55. let log = readfile( logs[ log_name ] )
  56. let logfile = s:testid_filesafe . '_' . log_name . '.testlog'
  57. call writefile( log, logfile, 's' )
  58. call add( s:messages,
  59. \ 'Wrote '
  60. \ . log_name
  61. \ . ' log for failed test: '
  62. \ . logfile )
  63. call add( s:messages, '**** LOG FILE ' . log_name . ' ****' )
  64. call extend( s:messages, log )
  65. endfor
  66. endif
  67. endfunc
  68. func! Abort( timer_id )
  69. call assert_report( 'Test timed out!!!' )
  70. qa!
  71. endfunc
  72. func RunTheTest(test)
  73. echo 'Executing ' . a:test
  74. " Avoid stopping at the "hit enter" prompt
  75. set nomore
  76. " Avoid a three second wait when a message is about to be overwritten by the
  77. " mode message.
  78. set noshowmode
  79. " Clear any overrides.
  80. call test_override('ALL', 0)
  81. " Some tests wipe out buffers. To be consistent, always wipe out all
  82. " buffers.
  83. %bwipe!
  84. " The test may change the current directory. Save and restore the
  85. " directory after executing the test.
  86. let save_cwd = getcwd()
  87. if exists("*SetUp_" . a:test)
  88. try
  89. exe 'call SetUp_' . a:test
  90. catch
  91. call add(v:errors,
  92. \ 'Caught exception in SetUp_' . a:test . ' before '
  93. \ . a:test
  94. \ . ': '
  95. \ . v:exception
  96. \ . ' @ '
  97. \ . g:testpath
  98. \ . ':'
  99. \ . v:throwpoint)
  100. endtry
  101. endif
  102. if exists("*SetUp")
  103. try
  104. call SetUp()
  105. catch
  106. call add(v:errors,
  107. \ 'Caught exception in SetUp() before '
  108. \ . a:test
  109. \ . ': '
  110. \ . v:exception
  111. \ . ' @ '
  112. \ . g:testpath
  113. \ . ':'
  114. \ . v:throwpoint)
  115. endtry
  116. endif
  117. call add(s:messages, 'Executing ' . a:test)
  118. let s:done += 1
  119. let timer = timer_start( s:single_test_timeout, funcref( 'Abort' ) )
  120. try
  121. let s:test = a:test
  122. let s:testid = g:testpath . ':' . a:test
  123. let test_filesafe = substitute( a:test, '[)(,:]', '_', 'g' )
  124. let s:testid_filesafe = g:testpath . '_' . test_filesafe
  125. au VimLeavePre * call EarlyExit(s:test)
  126. exe 'call ' . a:test
  127. au! VimLeavePre
  128. catch /^\cskipped/
  129. call add(s:messages, ' Skipped')
  130. call add(s:skipped,
  131. \ 'SKIPPED ' . a:test
  132. \ . ': '
  133. \ . substitute(v:exception, '^\S*\s\+', '', ''))
  134. catch
  135. call add(v:errors,
  136. \ 'Caught exception in ' . a:test
  137. \ . ': '
  138. \ . v:exception
  139. \ . ' @ '
  140. \ . g:testpath
  141. \ . ':'
  142. \ . v:throwpoint)
  143. call s:TestFailed()
  144. endtry
  145. call timer_stop( timer )
  146. " In case 'insertmode' was set and something went wrong, make sure it is
  147. " reset to avoid trouble with anything else.
  148. set noinsertmode
  149. if exists("*TearDown")
  150. try
  151. call TearDown()
  152. catch
  153. call add(v:errors,
  154. \ 'Caught exception in TearDown() after ' . a:test
  155. \ . ': '
  156. \ . v:exception
  157. \ . ' @ '
  158. \ . g:testpath
  159. \ . ':'
  160. \ . v:throwpoint)
  161. endtry
  162. endif
  163. if exists("*TearDown_" . a:test)
  164. try
  165. exe 'call TearDown_' . a:test
  166. catch
  167. call add(v:errors,
  168. \ 'Caught exception in TearDown_' . a:test . ' after ' . a:test
  169. \ . ': '
  170. \ . v:exception
  171. \ . ' @ '
  172. \ . g:testpath
  173. \ . ':'
  174. \ . v:throwpoint)
  175. endtry
  176. endif
  177. " Clear any autocommands
  178. au!
  179. " Close any extra tab pages and windows and make the current one not modified.
  180. while tabpagenr('$') > 1
  181. quit!
  182. endwhile
  183. while 1
  184. let wincount = winnr('$')
  185. if wincount == 1
  186. break
  187. endif
  188. bwipe!
  189. if wincount == winnr('$')
  190. " Did not manage to close a window.
  191. only!
  192. break
  193. endif
  194. endwhile
  195. exe 'cd ' . save_cwd
  196. endfunc
  197. func AfterTheTest()
  198. if len(v:errors) > 0
  199. let s:fail += 1
  200. call s:TestFailed()
  201. call add(s:errors, 'Found errors in ' . s:testid . ':')
  202. call extend(s:errors, v:errors)
  203. let v:errors = []
  204. endif
  205. endfunc
  206. func EarlyExit(test)
  207. " It's OK for the test we use to test the quit detection.
  208. call add(v:errors, 'Test caused Vim to exit: ' . a:test)
  209. call FinishTesting()
  210. endfunc
  211. " This function can be called by a test if it wants to abort testing.
  212. func FinishTesting()
  213. call AfterTheTest()
  214. " Don't write viminfo on exit.
  215. set viminfo=
  216. if s:fail == 0
  217. " Success, create the .res file so that make knows it's done.
  218. call writefile( [], g:testname . '.res', 's' )
  219. endif
  220. if len(s:errors) > 0
  221. " Append errors to test.log
  222. let l = []
  223. if filereadable( 'test.log' )
  224. let l = readfile( 'test.log' )
  225. endif
  226. call writefile( l->extend( [ '', 'From ' . g:testpath . ':' ] )
  227. \ ->extend( s:errors ),
  228. \ 'test.log',
  229. \ 's' )
  230. endif
  231. if s:done == 0
  232. let message = 'NO tests executed'
  233. else
  234. let message = 'Executed ' . s:done . (s:done > 1 ? ' tests' : ' test')
  235. endif
  236. echo message
  237. call add(s:messages, message)
  238. if s:fail > 0
  239. let message = s:fail . ' FAILED:'
  240. echo message
  241. call add(s:messages, message)
  242. call extend(s:messages, s:errors)
  243. endif
  244. " Add SKIPPED messages
  245. call extend(s:messages, s:skipped)
  246. " Append messages to the file "messages"
  247. let l = []
  248. if filereadable( 'messages' )
  249. let l = readfile( 'messages' )
  250. endif
  251. call writefile( l->extend( [ '', 'From ' . g:testpath . ':' ] )
  252. \ ->extend( s:messages ),
  253. \ 'messages',
  254. \ 's' )
  255. if exists( '$COVERAGE' )
  256. pyx _cov.stop()
  257. pyx _cov.save()
  258. endif
  259. if s:fail > 0
  260. cquit!
  261. else
  262. qall!
  263. endif
  264. endfunc
  265. " Source the test script. First grab the file name, in case the script
  266. " navigates away. g:testname can be used by the tests.
  267. let g:testname = expand('%')
  268. let g:testpath = expand('%:p')
  269. let s:done = 0
  270. let s:fail = 0
  271. let s:errors = []
  272. let s:messages = []
  273. let s:skipped = []
  274. try
  275. source %
  276. catch
  277. let s:fail += 1
  278. call add(s:errors,
  279. \ 'Caught exception: ' .
  280. \ v:exception .
  281. \ ' @ ' . v:throwpoint)
  282. endtry
  283. " Names of flaky tests.
  284. let s:flaky_tests = []
  285. " Pattern indicating a common flaky test failure.
  286. let s:flaky_errors_re = '__does_not_match__'
  287. " Locate Test_ functions and execute them.
  288. redir @q
  289. silent function /^Test_
  290. redir END
  291. let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
  292. " If there is an extra argument filter the function names against it.
  293. if argc() > 1
  294. let s:tests = filter(s:tests, 'v:val =~ argv(1)')
  295. endif
  296. pyx <<EOF
  297. def _InitCoverage():
  298. import coverage
  299. cov = coverage.Coverage( data_file='.coverage.python' )
  300. cov.start()
  301. return cov
  302. import os
  303. if 'COVERAGE' in os.environ:
  304. _cov = _InitCoverage()
  305. EOF
  306. " Init covimerage
  307. if exists( '$COVERAGE' )
  308. profile start .vim_profile
  309. exe 'profile! file */youcompleteme.vim'
  310. endif
  311. " Execute the tests in alphabetical order.
  312. for s:test in sort(s:tests)
  313. " Silence, please!
  314. set belloff=all
  315. call RunTheTest(s:test)
  316. call AfterTheTest()
  317. endfor
  318. call FinishTesting()
  319. " vim: shiftwidth=2 sts=2 expandtab