youcompleteme_test.py 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. # Copyright (C) 2016-2018 YouCompleteMe contributors
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from ycm.client.messages_request import MessagesPoll
  18. from ycm.tests.test_utils import ( ExtendedMock,
  19. MockVimBuffers,
  20. MockVimModule,
  21. Version,
  22. VimBuffer,
  23. VimProp,
  24. VimSign )
  25. MockVimModule()
  26. import os
  27. import sys
  28. from hamcrest import ( assert_that, contains_exactly, empty, equal_to,
  29. has_entries, is_in, is_not, matches_regexp )
  30. from unittest.mock import call, MagicMock, patch
  31. from unittest import TestCase
  32. from ycm.paths import _PathToPythonUsedDuringBuild
  33. from ycm.vimsupport import SetVariableValue
  34. from ycm.tests import ( StopServer,
  35. test_utils,
  36. UserOptions,
  37. WaitUntilReady,
  38. YouCompleteMeInstance )
  39. from ycm.client.base_request import _LoadExtraConfFile
  40. from ycm.youcompleteme import YouCompleteMe
  41. from ycmd.responses import ServerError
  42. from ycm.tests.mock_utils import ( MockAsyncServerResponseDone,
  43. MockAsyncServerResponseInProgress,
  44. MockAsyncServerResponseException )
  45. def RunNotifyUserIfServerCrashed( ycm, post_vim_message, test ):
  46. StopServer( ycm )
  47. ycm._logger = MagicMock( autospec = True )
  48. ycm._server_popen = MagicMock( autospec = True )
  49. ycm._server_popen.poll.return_value = test[ 'return_code' ]
  50. ycm.OnFileReadyToParse()
  51. assert_that( ycm._logger.error.call_args[ 0 ][ 0 ],
  52. test[ 'expected_message' ] )
  53. assert_that( post_vim_message.call_args[ 0 ][ 0 ],
  54. test[ 'expected_message' ] )
  55. def YouCompleteMe_UpdateDiagnosticInterface( ycm, post_vim_message, *args ):
  56. contents = "\nint main() { int x, y; x == y }"
  57. # List of diagnostics returned by ycmd for the above code.
  58. diagnostics = [ {
  59. 'kind': 'ERROR',
  60. 'text': "expected ';' after expression (fix available) "
  61. "[expected_semi_after_expr]",
  62. 'location': {
  63. 'filepath': 'buffer',
  64. 'line_num': 2,
  65. 'column_num': 31
  66. },
  67. # Looks strange but this is really what ycmd is returning.
  68. 'location_extent': {
  69. 'start': {
  70. 'filepath': '',
  71. 'line_num': 2,
  72. 'column_num': 31,
  73. },
  74. 'end': {
  75. 'filepath': '',
  76. 'line_num': 2,
  77. 'column_num': 32,
  78. }
  79. },
  80. 'ranges': [ {
  81. 'start': {
  82. 'line_num': 2,
  83. 'column_num': 31,
  84. 'filepath': 'buffer'
  85. },
  86. 'end': {
  87. 'line_num': 2,
  88. 'column_num': 31,
  89. 'filepath': 'buffer'
  90. }
  91. } ],
  92. 'fixit_available': False
  93. }, {
  94. 'kind': 'WARNING',
  95. 'text': 'equality comparison result unused',
  96. 'location': {
  97. 'filepath': 'buffer',
  98. 'line_num': 2,
  99. 'column_num': 31,
  100. },
  101. 'location_extent': {
  102. 'start': {
  103. 'filepath': 'buffer',
  104. 'line_num': 2,
  105. 'column_num': 31,
  106. },
  107. 'end': {
  108. 'filepath': 'buffer',
  109. 'line_num': 2,
  110. 'column_num': 32,
  111. }
  112. },
  113. 'ranges': [ {
  114. 'start': {
  115. 'filepath': 'buffer',
  116. 'line_num': 2,
  117. 'column_num': 24,
  118. },
  119. 'end': {
  120. 'filepath': 'buffer',
  121. 'line_num': 2,
  122. 'column_num': 30,
  123. }
  124. } ],
  125. 'fixit_available': False
  126. } ]
  127. current_buffer = VimBuffer( 'buffer',
  128. filetype = 'c',
  129. contents = contents.splitlines(),
  130. number = 5 )
  131. test_utils.VIM_SIGNS = []
  132. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 2, 1 ) ):
  133. with patch( 'ycm.client.event_notification.EventNotification.Response',
  134. return_value = diagnostics ):
  135. ycm.OnFileReadyToParse()
  136. ycm.HandleFileParseRequest( block = True )
  137. # The error on the current line is echoed, not the warning.
  138. post_vim_message.assert_called_once_with(
  139. "expected ';' after expression (fix available) "
  140. "[expected_semi_after_expr]",
  141. truncate = True, warning = False )
  142. # Error match is added after warning matches.
  143. assert_that(
  144. test_utils.VIM_PROPS_FOR_BUFFER,
  145. has_entries( {
  146. current_buffer.number: contains_exactly(
  147. VimProp( 'YcmWarningProperty', 2, 31, 2, 32 ),
  148. VimProp( 'YcmWarningProperty', 2, 24, 2, 30 ),
  149. VimProp( 'YcmErrorProperty', 2, 31, 2, 32 ),
  150. VimProp( 'YcmErrorProperty', 2, 31, 2, 31 ),
  151. )
  152. } )
  153. )
  154. # Only the error sign is placed.
  155. assert_that(
  156. test_utils.VIM_SIGNS,
  157. contains_exactly(
  158. VimSign( 2, 'YcmError', 5 )
  159. )
  160. )
  161. # The error is not echoed again when moving the cursor along the line.
  162. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 2, 2 ) ):
  163. post_vim_message.reset_mock()
  164. ycm.OnCursorMoved()
  165. post_vim_message.assert_not_called()
  166. # The error is cleared when moving the cursor to another line.
  167. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 2 ) ):
  168. post_vim_message.reset_mock()
  169. ycm.OnCursorMoved()
  170. post_vim_message.assert_called_once_with( "", warning = False )
  171. # The error is echoed when moving the cursor back.
  172. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 2, 2 ) ):
  173. post_vim_message.reset_mock()
  174. ycm.OnCursorMoved()
  175. post_vim_message.assert_called_once_with(
  176. "expected ';' after expression (fix available) "
  177. "[expected_semi_after_expr]",
  178. truncate = True, warning = False )
  179. with patch( 'ycm.client.event_notification.EventNotification.Response',
  180. return_value = diagnostics[ 1 : ] ):
  181. ycm.OnFileReadyToParse()
  182. ycm.HandleFileParseRequest( block = True )
  183. assert_that(
  184. test_utils.VIM_PROPS_FOR_BUFFER,
  185. has_entries( {
  186. current_buffer.number: contains_exactly(
  187. VimProp( 'YcmWarningProperty', 2, 31, 2, 32 ),
  188. VimProp( 'YcmWarningProperty', 2, 24, 2, 30 )
  189. )
  190. } )
  191. )
  192. assert_that(
  193. test_utils.VIM_SIGNS,
  194. contains_exactly(
  195. VimSign( 2, 'YcmWarning', 5 )
  196. )
  197. )
  198. class YouCompleteMeTest( TestCase ):
  199. @YouCompleteMeInstance()
  200. def test_YouCompleteMe_YcmCoreNotImported( self, ycm ):
  201. assert_that( 'ycm_core', is_not( is_in( sys.modules ) ) )
  202. @patch( 'ycm.vimsupport.PostVimMessage' )
  203. def test_YouCompleteMe_InvalidPythonInterpreterPath( self, post_vim_message ):
  204. with UserOptions( {
  205. 'g:ycm_server_python_interpreter': '/invalid/python/path' } ):
  206. try:
  207. ycm = YouCompleteMe()
  208. assert_that( ycm.IsServerAlive(), equal_to( False ) )
  209. post_vim_message.assert_called_once_with(
  210. "Unable to start the ycmd server. "
  211. "Path in 'g:ycm_server_python_interpreter' option does not point "
  212. "to a valid Python 3.6+. "
  213. "Correct the error then restart the server with "
  214. "':YcmRestartServer'." )
  215. post_vim_message.reset_mock()
  216. SetVariableValue( 'g:ycm_server_python_interpreter',
  217. _PathToPythonUsedDuringBuild() )
  218. ycm.RestartServer()
  219. assert_that( ycm.IsServerAlive(), equal_to( True ) )
  220. post_vim_message.assert_called_once_with( 'Restarting ycmd server...' )
  221. finally:
  222. WaitUntilReady()
  223. StopServer( ycm )
  224. @patch( 'ycmd.utils.PathToFirstExistingExecutable', return_value = None )
  225. @patch( 'ycm.paths._EndsWithPython', return_value = False )
  226. @patch( 'ycm.vimsupport.PostVimMessage' )
  227. def test_YouCompleteMe_NoPythonInterpreterFound(
  228. self, post_vim_message, *args ):
  229. with UserOptions( {} ):
  230. try:
  231. with patch( 'ycmd.utils.ReadFile', side_effect = IOError ):
  232. ycm = YouCompleteMe()
  233. assert_that( ycm.IsServerAlive(), equal_to( False ) )
  234. post_vim_message.assert_called_once_with(
  235. "Unable to start the ycmd server. Cannot find Python 3.6+. "
  236. "Set the 'g:ycm_server_python_interpreter' option to a Python "
  237. "interpreter path. "
  238. "Correct the error then restart the server with "
  239. "':YcmRestartServer'." )
  240. post_vim_message.reset_mock()
  241. SetVariableValue( 'g:ycm_server_python_interpreter',
  242. _PathToPythonUsedDuringBuild() )
  243. ycm.RestartServer()
  244. assert_that( ycm.IsServerAlive(), equal_to( True ) )
  245. post_vim_message.assert_called_once_with( 'Restarting ycmd server...' )
  246. finally:
  247. WaitUntilReady()
  248. StopServer( ycm )
  249. @YouCompleteMeInstance()
  250. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  251. def test_YouCompleteMe_NotifyUserIfServerCrashed_UnexpectedCore(
  252. self, ycm, post_vim_message ):
  253. message = (
  254. "The ycmd server SHUT DOWN \\(restart with ':YcmRestartServer'\\). "
  255. "Unexpected error while loading the YCM core library. Type "
  256. "':YcmToggleLogs ycmd_\\d+_stderr_.+.log' to check the logs." )
  257. RunNotifyUserIfServerCrashed( ycm, post_vim_message, {
  258. 'return_code': 3,
  259. 'expected_message': matches_regexp( message )
  260. } )
  261. @YouCompleteMeInstance()
  262. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  263. def test_YouCompleteMe_NotifyUserIfServerCrashed_MissingCore(
  264. self, ycm, post_vim_message ):
  265. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  266. "YCM core library not detected; you need to compile YCM before "
  267. "using it. Follow the instructions in the documentation." )
  268. RunNotifyUserIfServerCrashed( ycm, post_vim_message, {
  269. 'return_code': 4,
  270. 'expected_message': equal_to( message )
  271. } )
  272. @YouCompleteMeInstance()
  273. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  274. def test_YouCompleteMe_NotifyUserIfServerCrashed_OutdatedCore(
  275. self, ycm, post_vim_message ):
  276. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  277. "YCM core library too old; PLEASE RECOMPILE by running the "
  278. "install.py script. See the documentation for more details." )
  279. RunNotifyUserIfServerCrashed( ycm, post_vim_message, {
  280. 'return_code': 7,
  281. 'expected_message': equal_to( message )
  282. } )
  283. @YouCompleteMeInstance()
  284. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  285. def test_YouCompleteMe_NotifyUserIfServerCrashed_UnexpectedExitCode(
  286. self, ycm, post_vim_message ):
  287. message = (
  288. "The ycmd server SHUT DOWN \\(restart with ':YcmRestartServer'\\). "
  289. "Unexpected exit code 1. Type "
  290. "':YcmToggleLogs ycmd_\\d+_stderr_.+.log' to check the logs." )
  291. RunNotifyUserIfServerCrashed( ycm, post_vim_message, {
  292. 'return_code': 1,
  293. 'expected_message': matches_regexp( message )
  294. } )
  295. @YouCompleteMeInstance( { 'g:ycm_extra_conf_vim_data': [ 'tempname()' ] } )
  296. @patch( 'ycm.vimsupport.VimSupportsPopupWindows', return_value=True )
  297. def test_YouCompleteMe_DebugInfo_ServerRunning( self, ycm, *args ):
  298. dir_of_script = os.path.dirname( os.path.abspath( __file__ ) )
  299. buf_name = os.path.join( dir_of_script, 'testdata', 'test.cpp' )
  300. extra_conf = os.path.join( dir_of_script, 'testdata', '.ycm_extra_conf.py' )
  301. _LoadExtraConfFile( extra_conf )
  302. current_buffer = VimBuffer( buf_name, filetype = 'cpp' )
  303. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  304. assert_that(
  305. ycm.DebugInfo(),
  306. matches_regexp(
  307. 'Client logfile: .+\n'
  308. 'Server Python interpreter: .+\n'
  309. 'Server Python version: .+\n'
  310. 'Server has Clang support compiled in: (False|True)\n'
  311. 'Clang version: .+\n'
  312. 'Extra configuration file found and loaded\n'
  313. 'Extra configuration path: .*testdata[/\\\\]\\.ycm_extra_conf\\.py\n'
  314. '[\\w\\W]*'
  315. 'Server running at: .+\n'
  316. 'Server process ID: \\d+\n'
  317. 'Server logfiles:\n'
  318. ' .+\n'
  319. ' .+' )
  320. )
  321. @YouCompleteMeInstance()
  322. @patch( 'ycm.vimsupport.VimSupportsPopupWindows', return_value=True )
  323. def test_YouCompleteMe_DebugInfo_ServerNotRunning( self, ycm, *args ):
  324. StopServer( ycm )
  325. current_buffer = VimBuffer( 'current_buffer' )
  326. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  327. assert_that(
  328. ycm.DebugInfo(),
  329. matches_regexp(
  330. 'Client logfile: .+\n'
  331. 'Server errored, no debug info from server\n'
  332. 'Server running at: .+\n'
  333. 'Server process ID: \\d+\n'
  334. 'Server logfiles:\n'
  335. ' .+\n'
  336. ' .+' )
  337. )
  338. @YouCompleteMeInstance()
  339. def test_YouCompleteMe_OnVimLeave_RemoveClientLogfileByDefault( self, ycm ):
  340. client_logfile = ycm._client_logfile
  341. assert_that( os.path.isfile( client_logfile ),
  342. f'Logfile { client_logfile } does not exist.' )
  343. ycm.OnVimLeave()
  344. assert_that( not os.path.isfile( client_logfile ),
  345. f'Logfile { client_logfile } was not removed.' )
  346. @YouCompleteMeInstance( { 'g:ycm_keep_logfiles': 1 } )
  347. def test_YouCompleteMe_OnVimLeave_KeepClientLogfile( self, ycm ):
  348. client_logfile = ycm._client_logfile
  349. assert_that( os.path.isfile( client_logfile ),
  350. f'Logfile { client_logfile } does not exist.' )
  351. ycm.OnVimLeave()
  352. assert_that( os.path.isfile( client_logfile ),
  353. f'Logfile { client_logfile } was removed.' )
  354. @YouCompleteMeInstance()
  355. @patch( 'ycm.vimsupport.CloseBuffersForFilename',
  356. new_callable = ExtendedMock )
  357. @patch( 'ycm.vimsupport.OpenFilename', new_callable = ExtendedMock )
  358. def test_YouCompleteMe_ToggleLogs_WithParameters(
  359. self, ycm, open_filename, close_buffers_for_filename ):
  360. logfile_buffer = VimBuffer( ycm._client_logfile )
  361. with MockVimBuffers( [ logfile_buffer ], [ logfile_buffer ] ):
  362. ycm.ToggleLogs( 90,
  363. 'botright vertical',
  364. os.path.basename( ycm._client_logfile ),
  365. 'nonexisting_logfile',
  366. os.path.basename( ycm._server_stdout ) )
  367. open_filename.assert_has_exact_calls( [
  368. call( ycm._server_stdout, { 'size': 90,
  369. 'watch': True,
  370. 'fix': True,
  371. 'focus': False,
  372. 'position': 'end',
  373. 'mods': 'botright vertical' } )
  374. ] )
  375. close_buffers_for_filename.assert_has_exact_calls( [
  376. call( ycm._client_logfile )
  377. ] )
  378. @YouCompleteMeInstance()
  379. # Select the second item of the list which is the ycmd stderr logfile.
  380. @patch( 'ycm.vimsupport.SelectFromList', return_value = 1 )
  381. @patch( 'ycm.vimsupport.OpenFilename', new_callable = ExtendedMock )
  382. def test_YouCompleteMe_ToggleLogs_WithoutParameters_SelectLogfileNotAlreadyOpen( # noqa
  383. self, ycm, open_filename, *args ):
  384. current_buffer = VimBuffer( 'current_buffer' )
  385. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  386. ycm.ToggleLogs( 0, '' )
  387. open_filename.assert_has_exact_calls( [
  388. call( ycm._server_stderr, { 'size': 12,
  389. 'watch': True,
  390. 'fix': True,
  391. 'focus': False,
  392. 'position': 'end',
  393. 'mods': '' } )
  394. ] )
  395. @YouCompleteMeInstance()
  396. # Select the third item of the list which is the ycmd stdout logfile.
  397. @patch( 'ycm.vimsupport.SelectFromList', return_value = 2 )
  398. @patch( 'ycm.vimsupport.CloseBuffersForFilename',
  399. new_callable = ExtendedMock )
  400. def test_YouCompleteMe_ToggleLogs_WithoutParameters_SelectLogfileAlreadyOpen(
  401. self, ycm, close_buffers_for_filename, *args ):
  402. logfile_buffer = VimBuffer( ycm._server_stdout )
  403. with MockVimBuffers( [ logfile_buffer ], [ logfile_buffer ] ):
  404. ycm.ToggleLogs( 0, '' )
  405. close_buffers_for_filename.assert_has_exact_calls( [
  406. call( ycm._server_stdout )
  407. ] )
  408. @YouCompleteMeInstance()
  409. @patch( 'ycm.vimsupport.SelectFromList',
  410. side_effect = RuntimeError( 'Error message' ) )
  411. @patch( 'ycm.vimsupport.PostVimMessage' )
  412. def test_YouCompleteMe_ToggleLogs_WithoutParameters_NoSelection(
  413. self, ycm, post_vim_message, *args ):
  414. current_buffer = VimBuffer( 'current_buffer' )
  415. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  416. ycm.ToggleLogs( 0, '' )
  417. assert_that(
  418. # Argument passed to PostVimMessage.
  419. post_vim_message.call_args[ 0 ][ 0 ],
  420. equal_to( 'Error message' )
  421. )
  422. @YouCompleteMeInstance()
  423. def test_YouCompleteMe_GetDefinedSubcommands_ListFromServer( self, ycm ):
  424. current_buffer = VimBuffer( 'buffer' )
  425. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  426. with patch( 'ycm.client.base_request._JsonFromFuture',
  427. return_value = [ 'SomeCommand', 'AnotherCommand' ] ):
  428. assert_that(
  429. ycm.GetDefinedSubcommands(),
  430. contains_exactly(
  431. 'SomeCommand',
  432. 'AnotherCommand'
  433. )
  434. )
  435. @YouCompleteMeInstance()
  436. @patch( 'ycm.client.base_request._logger', autospec = True )
  437. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  438. def test_YouCompleteMe_GetDefinedSubcommands_ErrorFromServer(
  439. self, ycm, post_vim_message, logger ):
  440. current_buffer = VimBuffer( 'buffer' )
  441. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  442. with patch( 'ycm.client.base_request._JsonFromFuture',
  443. side_effect = ServerError( 'Server error' ) ):
  444. result = ycm.GetDefinedSubcommands()
  445. logger.exception.assert_called_with(
  446. 'Error while handling server response' )
  447. post_vim_message.assert_has_exact_calls( [
  448. call( 'Server error', truncate = False )
  449. ] )
  450. assert_that( result, empty() )
  451. @YouCompleteMeInstance()
  452. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  453. def test_YouCompleteMe_ShowDetailedDiagnostic_MessageFromServer(
  454. self, ycm, post_vim_message ):
  455. current_buffer = VimBuffer( 'buffer' )
  456. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  457. with patch( 'ycm.client.base_request._JsonFromFuture',
  458. return_value = { 'message': 'some_detailed_diagnostic' } ):
  459. ycm.ShowDetailedDiagnostic( False ),
  460. post_vim_message.assert_has_exact_calls( [
  461. call( 'some_detailed_diagnostic', warning = False )
  462. ] )
  463. @YouCompleteMeInstance()
  464. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  465. def test_YouCompleteMe_ShowDetailedDiagnostic_Exception(
  466. self, ycm, post_vim_message ):
  467. current_buffer = VimBuffer( 'buffer' )
  468. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  469. with patch( 'ycm.client.base_request._JsonFromFuture',
  470. side_effect = RuntimeError( 'Some exception' ) ):
  471. ycm.ShowDetailedDiagnostic( False ),
  472. post_vim_message.assert_has_exact_calls( [
  473. call( 'Some exception', truncate = False )
  474. ] )
  475. @YouCompleteMeInstance()
  476. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  477. def test_YouCompleteMe_ShowDiagnostics_FiletypeNotSupported(
  478. self, ycm, post_vim_message ):
  479. current_buffer = VimBuffer( 'buffer', filetype = 'not_supported' )
  480. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  481. ycm.ShowDiagnostics()
  482. post_vim_message.assert_called_once_with(
  483. 'Native filetype completion not supported for current file, '
  484. 'cannot force recompilation.', warning = False )
  485. @YouCompleteMeInstance()
  486. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  487. return_value = True )
  488. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  489. @patch( 'ycm.vimsupport.SetLocationListForWindow',
  490. new_callable = ExtendedMock )
  491. def test_YouCompleteMe_ShowDiagnostics_NoDiagnosticsDetected(
  492. self,
  493. ycm,
  494. set_location_list_for_window,
  495. post_vim_message,
  496. *args ):
  497. current_buffer = VimBuffer( 'buffer', filetype = 'cpp' )
  498. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  499. with patch( 'ycm.client.event_notification.EventNotification.Response',
  500. return_value = {} ):
  501. ycm.ShowDiagnostics()
  502. post_vim_message.assert_has_exact_calls( [
  503. call( 'Forcing compilation, this will block Vim until done.',
  504. warning = False ),
  505. call( 'Diagnostics refreshed', warning = False ),
  506. call( 'No warnings or errors detected.', warning = False )
  507. ] )
  508. set_location_list_for_window.assert_called_once_with( 1, [], 1 )
  509. @YouCompleteMeInstance( { 'g:ycm_log_level': 'debug',
  510. 'g:ycm_keep_logfiles': 1,
  511. 'g:ycm_open_loclist_on_ycm_diags': 0 } )
  512. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  513. return_value = True )
  514. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  515. @patch( 'ycm.vimsupport.SetLocationListForWindow',
  516. new_callable = ExtendedMock )
  517. def test_YouCompleteMe_ShowDiagnostics_DiagnosticsFound_DoNotOpenLocationList(
  518. self,
  519. ycm,
  520. set_location_list_for_window,
  521. post_vim_message,
  522. *args ):
  523. diagnostic = {
  524. 'kind': 'ERROR',
  525. 'text': 'error text',
  526. 'location': {
  527. 'filepath': 'buffer',
  528. 'line_num': 19,
  529. 'column_num': 2
  530. }
  531. }
  532. current_buffer = VimBuffer( 'buffer',
  533. filetype = 'cpp',
  534. number = 3 )
  535. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  536. with patch( 'ycm.client.event_notification.EventNotification.Response',
  537. return_value = [ diagnostic ] ):
  538. ycm.ShowDiagnostics()
  539. post_vim_message.assert_has_exact_calls( [
  540. call( 'Forcing compilation, this will block Vim until done.',
  541. warning = False ),
  542. call( 'Diagnostics refreshed', warning = False )
  543. ] )
  544. set_location_list_for_window.assert_called_once_with( 1, [ {
  545. 'bufnr': 3,
  546. 'lnum': 19,
  547. 'col': 2,
  548. 'text': 'error text',
  549. 'type': 'E',
  550. 'valid': 1
  551. } ], 0 )
  552. @YouCompleteMeInstance( { 'g:ycm_open_loclist_on_ycm_diags': 1 } )
  553. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  554. return_value = True )
  555. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  556. @patch( 'ycm.vimsupport.SetLocationListForWindow',
  557. new_callable = ExtendedMock )
  558. @patch( 'ycm.vimsupport.OpenLocationList', new_callable = ExtendedMock )
  559. def test_YouCompleteMe_ShowDiagnostics_DiagnosticsFound_OpenLocationList(
  560. self,
  561. ycm,
  562. open_location_list,
  563. set_location_list_for_window,
  564. post_vim_message,
  565. *args ):
  566. diagnostic = {
  567. 'kind': 'ERROR',
  568. 'text': 'error text',
  569. 'location': {
  570. 'filepath': 'buffer',
  571. 'line_num': 19,
  572. 'column_num': 2
  573. }
  574. }
  575. current_buffer = VimBuffer( 'buffer',
  576. filetype = 'cpp',
  577. number = 3 )
  578. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  579. with patch( 'ycm.client.event_notification.EventNotification.Response',
  580. return_value = [ diagnostic ] ):
  581. ycm.ShowDiagnostics()
  582. post_vim_message.assert_has_exact_calls( [
  583. call( 'Forcing compilation, this will block Vim until done.',
  584. warning = False ),
  585. call( 'Diagnostics refreshed', warning = False )
  586. ] )
  587. set_location_list_for_window.assert_called_once_with( 1, [ {
  588. 'bufnr': 3,
  589. 'lnum': 19,
  590. 'col': 2,
  591. 'text': 'error text',
  592. 'type': 'E',
  593. 'valid': 1
  594. } ], 1 )
  595. open_location_list.assert_called_once_with( focus = True )
  596. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  597. 'g:ycm_enable_diagnostic_signs': 1,
  598. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  599. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  600. return_value = True )
  601. @patch( 'ycm.client.event_notification.EventNotification.Done',
  602. return_value = True )
  603. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  604. def test_YouCompleteMe_UpdateDiagnosticInterface_OldVim(
  605. self, ycm, post_vim_message, *args ):
  606. YouCompleteMe_UpdateDiagnosticInterface( ycm, post_vim_message )
  607. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  608. 'g:ycm_enable_diagnostic_signs': 1,
  609. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  610. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  611. return_value = True )
  612. @patch( 'ycm.tests.test_utils.VIM_VERSION', Version( 8, 1, 614 ) )
  613. @patch( 'ycm.client.event_notification.EventNotification.Done',
  614. return_value = True )
  615. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  616. def test_YouCompleteMe_UpdateDiagnosticInterface_NewVim(
  617. self, ycm, post_vim_message, *args ):
  618. YouCompleteMe_UpdateDiagnosticInterface( ycm, post_vim_message )
  619. @YouCompleteMeInstance( { 'g:ycm_enable_diagnostic_highlighting': 1 } )
  620. def test_YouCompleteMe_UpdateMatches_ClearDiagnosticMatchesInNewBuffer(
  621. self, ycm ):
  622. current_buffer = VimBuffer( 'buffer',
  623. filetype = 'c',
  624. contents = '\n\n\n\n',
  625. number = 5 )
  626. test_utils.VIM_PROPS_FOR_BUFFER[ current_buffer.number ] = [
  627. VimProp( 'YcmWarningProperty', 3, 5, 3, 7 ),
  628. VimProp( 'YcmWarningProperty', 3, 3, 3, 9 ),
  629. VimProp( 'YcmErrorProperty', 3, 8 )
  630. ]
  631. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  632. ycm.UpdateMatches()
  633. assert_that( test_utils.VIM_PROPS_FOR_BUFFER[ current_buffer.number ],
  634. empty() )
  635. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  636. 'g:ycm_always_populate_location_list': 1,
  637. 'g:ycm_show_diagnostics_ui': 0,
  638. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  639. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  640. return_value = True )
  641. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  642. def test_YouCompleteMe_AsyncDiagnosticUpdate_UserDisabled(
  643. self,
  644. ycm,
  645. post_vim_message,
  646. *args ):
  647. diagnostics = [
  648. {
  649. 'kind': 'ERROR',
  650. 'text': 'error text in current buffer',
  651. 'location': {
  652. 'filepath': '/current',
  653. 'line_num': 1,
  654. 'column_num': 1
  655. },
  656. 'location_extent': {
  657. 'start': {
  658. 'filepath': '/current',
  659. 'line_num': 1,
  660. 'column_num': 1,
  661. },
  662. 'end': {
  663. 'filepath': '/current',
  664. 'line_num': 1,
  665. 'column_num': 1,
  666. }
  667. },
  668. 'ranges': []
  669. },
  670. ]
  671. current_buffer = VimBuffer( '/current',
  672. filetype = 'ycmtest',
  673. contents = [ 'current' ] * 10,
  674. number = 1 )
  675. buffers = [ current_buffer ]
  676. windows = [ current_buffer ]
  677. # Register each buffer internally with YCM
  678. for current in buffers:
  679. with MockVimBuffers( buffers, [ current ] ):
  680. ycm.OnFileReadyToParse()
  681. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  682. new_callable = ExtendedMock ) as set_location_list_for_window:
  683. with MockVimBuffers( buffers, windows ):
  684. ycm.UpdateWithNewDiagnosticsForFile( '/current', diagnostics )
  685. post_vim_message.assert_has_exact_calls( [] )
  686. set_location_list_for_window.assert_has_exact_calls( [] )
  687. assert_that(
  688. test_utils.VIM_PROPS_FOR_BUFFER,
  689. empty()
  690. )
  691. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  692. 'g:ycm_always_populate_location_list': 1,
  693. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  694. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  695. return_value = True )
  696. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  697. def test_YouCompleteMe_AsyncDiagnosticUpdate_SingleFile(
  698. self,
  699. ycm,
  700. post_vim_message,
  701. *args ):
  702. # This test simulates asynchronous diagnostic updates associated with a
  703. # single file (e.g. Translation Unit), but where the actual errors refer to
  704. # other open files and other non-open files. This is not strictly invalid,
  705. # nor is it completely normal, but it is supported and does work.
  706. # Contrast with the next test which sends the diagnostics filewise, which
  707. # is what the language server protocol will do.
  708. diagnostics = [
  709. {
  710. 'kind': 'ERROR',
  711. 'text': 'error text in current buffer',
  712. 'location': {
  713. 'filepath': '/current',
  714. 'line_num': 1,
  715. 'column_num': 1
  716. },
  717. 'location_extent': {
  718. 'start': {
  719. 'filepath': '/current',
  720. 'line_num': 1,
  721. 'column_num': 1,
  722. },
  723. 'end': {
  724. 'filepath': '/current',
  725. 'line_num': 1,
  726. 'column_num': 1,
  727. }
  728. },
  729. 'ranges': []
  730. },
  731. {
  732. 'kind': 'ERROR',
  733. 'text': 'error text in hidden buffer',
  734. 'location': {
  735. 'filepath': '/has_diags',
  736. 'line_num': 4,
  737. 'column_num': 2
  738. },
  739. 'location_extent': {
  740. 'start': {
  741. 'filepath': '/has_diags',
  742. 'line_num': 4,
  743. 'column_num': 2,
  744. },
  745. 'end': {
  746. 'filepath': '/has_diags',
  747. 'line_num': 4,
  748. 'column_num': 2,
  749. }
  750. },
  751. 'ranges': []
  752. },
  753. {
  754. 'kind': 'ERROR',
  755. 'text': 'error text in buffer not open in Vim',
  756. 'location': {
  757. 'filepath': '/not_open',
  758. 'line_num': 8,
  759. 'column_num': 4
  760. },
  761. 'location_extent': {
  762. 'start': {
  763. 'filepath': '/not_open',
  764. 'line_num': 8,
  765. 'column_num': 4,
  766. },
  767. 'end': {
  768. 'filepath': '/not_open',
  769. 'line_num': 8,
  770. 'column_num': 4,
  771. }
  772. },
  773. 'ranges': []
  774. }
  775. ]
  776. current_buffer = VimBuffer( '/current',
  777. filetype = 'ycmtest',
  778. contents = [ 'current' ] * 10,
  779. number = 1 )
  780. no_diags_buffer = VimBuffer( '/no_diags',
  781. filetype = 'ycmtest',
  782. contents = [ 'nodiags' ] * 10,
  783. number = 2 )
  784. hidden_buffer = VimBuffer( '/has_diags',
  785. filetype = 'ycmtest',
  786. contents = [ 'hasdiags' ] * 10,
  787. number = 3 )
  788. buffers = [ current_buffer, no_diags_buffer, hidden_buffer ]
  789. windows = [ current_buffer, no_diags_buffer ]
  790. # Register each buffer internally with YCM
  791. for current in buffers:
  792. with MockVimBuffers( buffers, [ current ] ):
  793. ycm.OnFileReadyToParse()
  794. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  795. new_callable = ExtendedMock ) as set_location_list_for_window:
  796. with MockVimBuffers( buffers, windows ):
  797. ycm.UpdateWithNewDiagnosticsForFile( '/current', diagnostics )
  798. # We update the diagnostic on the current cursor position
  799. post_vim_message.assert_has_exact_calls( [
  800. call( "error text in current buffer", truncate = True, warning = False ),
  801. ] )
  802. # Ensure we included all the diags though
  803. set_location_list_for_window.assert_has_exact_calls( [
  804. call( 1, [
  805. {
  806. 'lnum': 1,
  807. 'col': 1,
  808. 'bufnr': 1,
  809. 'valid': 1,
  810. 'type': 'E',
  811. 'text': 'error text in current buffer',
  812. },
  813. {
  814. 'lnum': 4,
  815. 'col': 2,
  816. 'bufnr': 3,
  817. 'valid': 1,
  818. 'type': 'E',
  819. 'text': 'error text in hidden buffer',
  820. },
  821. {
  822. 'lnum': 8,
  823. 'col': 4,
  824. 'bufnr': -1, # sic: Our mocked bufnr function actually returns -1,
  825. # even though YCM is passing "create if needed".
  826. # FIXME? we shouldn't do that, and we should pass
  827. # filename instead
  828. 'valid': 1,
  829. 'type': 'E',
  830. 'text': 'error text in buffer not open in Vim'
  831. }
  832. ], False )
  833. ] )
  834. assert_that(
  835. test_utils.VIM_PROPS_FOR_BUFFER,
  836. has_entries( {
  837. 1: contains_exactly(
  838. VimProp( 'YcmErrorProperty', 1, 1, 1, 1 )
  839. )
  840. } )
  841. )
  842. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  843. 'g:ycm_always_populate_location_list': 1,
  844. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  845. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  846. return_value = True )
  847. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  848. def test_YouCompleteMe_AsyncDiagnosticUpdate_PerFile(
  849. self,
  850. ycm,
  851. post_vim_message,
  852. *args ):
  853. # This test simulates asynchronous diagnostic updates which are delivered
  854. # per file, including files which are open and files which are not.
  855. # Ordered to ensure that the calls to update are deterministic
  856. diagnostics_per_file = [
  857. ( '/current', [ {
  858. 'kind': 'ERROR',
  859. 'text': 'error text in current buffer',
  860. 'location': {
  861. 'filepath': '/current',
  862. 'line_num': 1,
  863. 'column_num': 1
  864. },
  865. 'location_extent': {
  866. 'start': {
  867. 'filepath': '/current',
  868. 'line_num': 1,
  869. 'column_num': 1,
  870. },
  871. 'end': {
  872. 'filepath': '/current',
  873. 'line_num': 1,
  874. 'column_num': 1,
  875. }
  876. },
  877. 'ranges': [],
  878. } ] ),
  879. ( '/separate_window', [ {
  880. 'kind': 'ERROR',
  881. 'text': 'error text in a buffer open in a separate window',
  882. 'location': {
  883. 'filepath': '/separate_window',
  884. 'line_num': 3,
  885. 'column_num': 3
  886. },
  887. 'location_extent': {
  888. 'start': {
  889. 'filepath': '/separate_window',
  890. 'line_num': 3,
  891. 'column_num': 3,
  892. },
  893. 'end': {
  894. 'filepath': '/separate_window',
  895. 'line_num': 3,
  896. 'column_num': 3,
  897. }
  898. },
  899. 'ranges': []
  900. } ] ),
  901. ( '/hidden', [ {
  902. 'kind': 'ERROR',
  903. 'text': 'error text in hidden buffer',
  904. 'location': {
  905. 'filepath': '/hidden',
  906. 'line_num': 4,
  907. 'column_num': 2
  908. },
  909. 'location_extent': {
  910. 'start': {
  911. 'filepath': '/hidden',
  912. 'line_num': 4,
  913. 'column_num': 2,
  914. },
  915. 'end': {
  916. 'filepath': '/hidden',
  917. 'line_num': 4,
  918. 'column_num': 2,
  919. }
  920. },
  921. 'ranges': []
  922. } ] ),
  923. ( '/not_open', [ {
  924. 'kind': 'ERROR',
  925. 'text': 'error text in buffer not open in Vim',
  926. 'location': {
  927. 'filepath': '/not_open',
  928. 'line_num': 8,
  929. 'column_num': 4
  930. },
  931. 'location_extent': {
  932. 'start': {
  933. 'filepath': '/not_open',
  934. 'line_num': 8,
  935. 'column_num': 4,
  936. },
  937. 'end': {
  938. 'filepath': '/not_open',
  939. 'line_num': 8,
  940. 'column_num': 4,
  941. }
  942. },
  943. 'ranges': []
  944. } ] )
  945. ]
  946. current_buffer = VimBuffer( '/current',
  947. filetype = 'ycmtest',
  948. contents = [ 'current' ] * 10,
  949. number = 1 )
  950. no_diags_buffer = VimBuffer( '/no_diags',
  951. filetype = 'ycmtest',
  952. contents = [ 'no_diags' ] * 10,
  953. number = 2 )
  954. separate_window = VimBuffer( '/separate_window',
  955. filetype = 'ycmtest',
  956. contents = [ 'separate_window' ] * 10,
  957. number = 3 )
  958. hidden_buffer = VimBuffer( '/hidden',
  959. filetype = 'ycmtest',
  960. contents = [ 'hidden' ] * 10,
  961. number = 4 )
  962. buffers = [
  963. current_buffer,
  964. no_diags_buffer,
  965. separate_window,
  966. hidden_buffer
  967. ]
  968. windows = [
  969. current_buffer,
  970. no_diags_buffer,
  971. separate_window
  972. ]
  973. # Register each buffer internally with YCM
  974. for current in buffers:
  975. with MockVimBuffers( buffers, [ current ] ):
  976. ycm.OnFileReadyToParse()
  977. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  978. new_callable = ExtendedMock ) as set_location_list_for_window:
  979. with MockVimBuffers( buffers, windows ):
  980. for filename, diagnostics in diagnostics_per_file:
  981. ycm.UpdateWithNewDiagnosticsForFile( filename, diagnostics )
  982. # We update the diagnostic on the current cursor position
  983. post_vim_message.assert_has_exact_calls( [
  984. call( "error text in current buffer", truncate = True, warning = False ),
  985. ] )
  986. # Ensure we included all the diags though
  987. set_location_list_for_window.assert_has_exact_calls( [
  988. call( 1, [
  989. {
  990. 'lnum': 1,
  991. 'col': 1,
  992. 'bufnr': 1,
  993. 'valid': 1,
  994. 'type': 'E',
  995. 'text': 'error text in current buffer',
  996. },
  997. ], False ),
  998. call( 3, [
  999. {
  1000. 'lnum': 3,
  1001. 'col': 3,
  1002. 'bufnr': 3,
  1003. 'valid': 1,
  1004. 'type': 'E',
  1005. 'text': 'error text in a buffer open in a separate window',
  1006. },
  1007. ], False )
  1008. ] )
  1009. assert_that(
  1010. test_utils.VIM_PROPS_FOR_BUFFER,
  1011. has_entries( {
  1012. 1: contains_exactly(
  1013. VimProp( 'YcmErrorProperty', 1, 1, 1, 1 )
  1014. ),
  1015. 3: contains_exactly(
  1016. VimProp( 'YcmErrorProperty', 3, 3, 3, 3 )
  1017. )
  1018. } )
  1019. )
  1020. @YouCompleteMeInstance()
  1021. def test_YouCompleteMe_OnPeriodicTick_ServerNotRunning( self, ycm ):
  1022. with patch.object( ycm, 'IsServerAlive', return_value = False ):
  1023. assert_that( ycm.OnPeriodicTick(), equal_to( False ) )
  1024. @YouCompleteMeInstance()
  1025. def test_YouCompleteMe_OnPeriodicTick_ServerNotReady( self, ycm ):
  1026. with patch.object( ycm, 'IsServerAlive', return_value = True ):
  1027. with patch.object( ycm, 'IsServerReady', return_value = False ):
  1028. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1029. @YouCompleteMeInstance()
  1030. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  1031. return_value = True )
  1032. @patch( 'ycm.client.base_request._ValidateResponseObject',
  1033. return_value = True )
  1034. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1035. def test_YouCompleteMe_OnPeriodicTick_DontRetry(
  1036. self,
  1037. ycm,
  1038. post_data_to_handler_async,
  1039. *args ):
  1040. current_buffer = VimBuffer( '/current',
  1041. filetype = 'ycmtest',
  1042. number = 1 )
  1043. # Create the request and make the first poll; we expect no response
  1044. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1045. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1046. post_data_to_handler_async.assert_called()
  1047. assert ycm._message_poll_requests[ 'ycmtest' ] is not None
  1048. post_data_to_handler_async.reset_mock()
  1049. # OK that sent the request, now poll to check if it is complete (say it is
  1050. # not)
  1051. with MockVimBuffers( [ current_buffer ],
  1052. [ current_buffer ],
  1053. ( 1, 1 ) ) as v:
  1054. mock_response = MockAsyncServerResponseInProgress()
  1055. with patch.dict( ycm._message_poll_requests, {} ):
  1056. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll(
  1057. v.current.buffer )
  1058. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1059. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1060. poll_again = ycm.OnPeriodicTick()
  1061. mock_future.done.assert_called()
  1062. mock_future.result.assert_not_called()
  1063. assert_that( poll_again, equal_to( True ) )
  1064. # Poll again, but return a response (telling us to stop polling)
  1065. with MockVimBuffers( [ current_buffer ],
  1066. [ current_buffer ],
  1067. ( 1, 1 ) ) as v:
  1068. mock_response = MockAsyncServerResponseDone( False )
  1069. with patch.dict( ycm._message_poll_requests, {} ):
  1070. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll(
  1071. v.current.buffer )
  1072. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1073. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1074. poll_again = ycm.OnPeriodicTick()
  1075. mock_future.done.assert_called()
  1076. mock_future.result.assert_called()
  1077. post_data_to_handler_async.assert_not_called()
  1078. # We reset and don't poll anymore
  1079. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is None )
  1080. assert_that( poll_again, equal_to( False ) )
  1081. @YouCompleteMeInstance()
  1082. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  1083. return_value = True )
  1084. @patch( 'ycm.client.base_request._ValidateResponseObject',
  1085. return_value = True )
  1086. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1087. def test_YouCompleteMe_OnPeriodicTick_Exception(
  1088. self,
  1089. ycm,
  1090. post_data_to_handler_async,
  1091. *args ):
  1092. current_buffer = VimBuffer( '/current',
  1093. filetype = 'ycmtest',
  1094. number = 1 )
  1095. # Create the request and make the first poll; we expect no response
  1096. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1097. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1098. post_data_to_handler_async.assert_called()
  1099. post_data_to_handler_async.reset_mock()
  1100. # Poll again, but return an exception response
  1101. with MockVimBuffers( [ current_buffer ],
  1102. [ current_buffer ],
  1103. ( 1, 1 ) ) as v:
  1104. mock_response = MockAsyncServerResponseException(
  1105. RuntimeError( 'test' ) )
  1106. with patch.dict( ycm._message_poll_requests, {} ):
  1107. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll(
  1108. v.current.buffer )
  1109. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1110. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1111. assert_that( ycm.OnPeriodicTick(), equal_to( False ) )
  1112. mock_future.done.assert_called()
  1113. mock_future.result.assert_called()
  1114. post_data_to_handler_async.assert_not_called()
  1115. # We reset and don't poll anymore
  1116. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is None )
  1117. @YouCompleteMeInstance()
  1118. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  1119. return_value = True )
  1120. @patch( 'ycm.client.base_request._ValidateResponseObject',
  1121. return_value = True )
  1122. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1123. @patch( 'ycm.client.messages_request._HandlePollResponse' )
  1124. def test_YouCompleteMe_OnPeriodicTick_ValidResponse(
  1125. self, ycm, handle_poll_response, post_data_to_handler_async, *args ):
  1126. current_buffer = VimBuffer( '/current',
  1127. filetype = 'ycmtest',
  1128. number = 1 )
  1129. # Create the request and make the first poll; we expect no response
  1130. with MockVimBuffers( [ current_buffer ],
  1131. [ current_buffer ],
  1132. ( 1, 1 ) ):
  1133. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1134. post_data_to_handler_async.assert_called()
  1135. post_data_to_handler_async.reset_mock()
  1136. # Poll again, and return a _proper_ response (finally!).
  1137. # Note, _HandlePollResponse is tested independently (for simplicity)
  1138. with MockVimBuffers( [ current_buffer ],
  1139. [ current_buffer ],
  1140. ( 1, 1 ) ) as v:
  1141. mock_response = MockAsyncServerResponseDone( [] )
  1142. with patch.dict( ycm._message_poll_requests, {} ):
  1143. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll(
  1144. v.current.buffer )
  1145. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1146. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1147. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1148. handle_poll_response.assert_called()
  1149. mock_future.done.assert_called()
  1150. mock_future.result.assert_called()
  1151. post_data_to_handler_async.assert_called() # Poll again!
  1152. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is not None )
  1153. @YouCompleteMeInstance()
  1154. @patch( 'ycm.client.completion_request.CompletionRequest.OnCompleteDone' )
  1155. def test_YouCompleteMe_OnCompleteDone_CompletionRequest(
  1156. self, ycm, on_complete_done ):
  1157. current_buffer = VimBuffer( 'current_buffer' )
  1158. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1159. ycm.SendCompletionRequest()
  1160. ycm.OnCompleteDone()
  1161. on_complete_done.assert_called()
  1162. @YouCompleteMeInstance()
  1163. @patch( 'ycm.client.completion_request.CompletionRequest.OnCompleteDone' )
  1164. def test_YouCompleteMe_OnCompleteDone_NoCompletionRequest(
  1165. self, ycm, on_complete_done ):
  1166. ycm.OnCompleteDone()
  1167. on_complete_done.assert_not_called()
  1168. @YouCompleteMeInstance()
  1169. def test_YouCompleteMe_ShouldResendFileParseRequest_NoParseRequest(
  1170. self, ycm ):
  1171. current_buffer = VimBuffer( 'current_buffer' )
  1172. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  1173. assert_that( ycm.ShouldResendFileParseRequest(), equal_to( False ) )