youcompleteme_test.py 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  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 vim
  27. import os
  28. import sys
  29. from hamcrest import ( assert_that, contains_exactly, empty, equal_to,
  30. has_entries, is_in, is_not, matches_regexp )
  31. from unittest.mock import call, MagicMock, patch
  32. from unittest import TestCase
  33. from ycm.paths import _PathToPythonUsedDuringBuild
  34. from ycm.vimsupport import SetVariableValue
  35. from ycm.tests import ( StopServer,
  36. test_utils,
  37. UserOptions,
  38. WaitUntilReady,
  39. YouCompleteMeInstance )
  40. from ycm.client.base_request import _LoadExtraConfFile
  41. from ycm.youcompleteme import YouCompleteMe
  42. from ycmd.responses import ServerError
  43. from ycm.tests.mock_utils import ( MockAsyncServerResponseDone,
  44. MockAsyncServerResponseInProgress,
  45. MockAsyncServerResponseException )
  46. def RunNotifyUserIfServerCrashed( ycm, post_vim_message, test ):
  47. StopServer( ycm )
  48. ycm._logger = MagicMock( autospec = True )
  49. ycm._server_popen = MagicMock( autospec = True )
  50. ycm._server_popen.poll.return_value = test[ 'return_code' ]
  51. ycm.OnFileReadyToParse()
  52. assert_that( ycm._logger.error.call_args[ 0 ][ 0 ],
  53. test[ 'expected_message' ] )
  54. assert_that( post_vim_message.call_args[ 0 ][ 0 ],
  55. test[ 'expected_message' ] )
  56. def YouCompleteMe_UpdateDiagnosticInterface( ycm, post_vim_message, *args ):
  57. contents = "\nint main() { int x, y; x == y }"
  58. # List of diagnostics returned by ycmd for the above code.
  59. diagnostics = [ {
  60. 'kind': 'ERROR',
  61. 'text': "expected ';' after expression (fix available) "
  62. "[expected_semi_after_expr]",
  63. 'location': {
  64. 'filepath': 'buffer',
  65. 'line_num': 2,
  66. 'column_num': 31
  67. },
  68. 'location_extent': {
  69. 'start': {
  70. 'filepath': 'buffer',
  71. 'line_num': 2,
  72. 'column_num': 31,
  73. },
  74. 'end': {
  75. 'filepath': 'buffer',
  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. set_location_list_for_window.assert_called_once_with(
  503. vim.current.window,
  504. [],
  505. 1 )
  506. post_vim_message.assert_has_exact_calls( [
  507. call( 'Forcing compilation, this will block Vim until done.',
  508. warning = False ),
  509. call( 'Diagnostics refreshed', warning = False ),
  510. call( 'No warnings or errors detected.', warning = False )
  511. ] )
  512. @YouCompleteMeInstance( { 'g:ycm_log_level': 'debug',
  513. 'g:ycm_keep_logfiles': 1,
  514. 'g:ycm_open_loclist_on_ycm_diags': 0 } )
  515. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  516. return_value = True )
  517. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  518. @patch( 'ycm.vimsupport.SetLocationListForWindow',
  519. new_callable = ExtendedMock )
  520. def test_YouCompleteMe_ShowDiagnostics_DiagnosticsFound_DoNotOpenLocationList(
  521. self,
  522. ycm,
  523. set_location_list_for_window,
  524. post_vim_message,
  525. *args ):
  526. diagnostic = {
  527. 'kind': 'ERROR',
  528. 'text': 'error text',
  529. 'location': {
  530. 'filepath': 'buffer',
  531. 'line_num': 19,
  532. 'column_num': 2
  533. },
  534. 'location_extent': {
  535. 'start': {
  536. 'filepath': 'buffer',
  537. 'line_num': 19,
  538. 'column_num': 2
  539. },
  540. 'end': {
  541. 'filepath': 'buffer',
  542. 'line_num': 19,
  543. 'column_num': 3
  544. }
  545. }
  546. }
  547. current_buffer = VimBuffer( 'buffer',
  548. filetype = 'cpp',
  549. number = 3 )
  550. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  551. with patch( 'ycm.client.event_notification.EventNotification.Response',
  552. return_value = [ diagnostic ] ):
  553. ycm.ShowDiagnostics()
  554. set_location_list_for_window.assert_called_once_with(
  555. vim.current.window, [ {
  556. 'bufnr': 3,
  557. 'lnum': 19,
  558. 'col': 2,
  559. 'text': 'error text',
  560. 'type': 'E',
  561. 'valid': 1
  562. } ], 0 )
  563. post_vim_message.assert_has_exact_calls( [
  564. call( 'Forcing compilation, this will block Vim until done.',
  565. warning = False ),
  566. call( 'Diagnostics refreshed', warning = False )
  567. ] )
  568. @YouCompleteMeInstance( { 'g:ycm_open_loclist_on_ycm_diags': 1 } )
  569. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  570. return_value = True )
  571. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  572. @patch( 'ycm.vimsupport.SetLocationListForWindow',
  573. new_callable = ExtendedMock )
  574. @patch( 'ycm.vimsupport.OpenLocationList', new_callable = ExtendedMock )
  575. def test_YouCompleteMe_ShowDiagnostics_DiagnosticsFound_OpenLocationList(
  576. self,
  577. ycm,
  578. open_location_list,
  579. set_location_list_for_window,
  580. post_vim_message,
  581. *args ):
  582. diagnostic = {
  583. 'kind': 'ERROR',
  584. 'text': 'error text',
  585. 'location': {
  586. 'filepath': 'buffer',
  587. 'line_num': 19,
  588. 'column_num': 2
  589. },
  590. 'location_extent': {
  591. 'start': {
  592. 'filepath': 'buffer',
  593. 'line_num': 19,
  594. 'column_num': 2
  595. },
  596. 'end': {
  597. 'filepath': 'buffer',
  598. 'line_num': 19,
  599. 'column_num': 2
  600. }
  601. }
  602. }
  603. current_buffer = VimBuffer( 'buffer',
  604. filetype = 'cpp',
  605. number = 3 )
  606. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  607. with patch( 'ycm.client.event_notification.EventNotification.Response',
  608. return_value = [ diagnostic ] ):
  609. ycm.ShowDiagnostics()
  610. set_location_list_for_window.assert_called_once_with(
  611. vim.current.window, [ {
  612. 'bufnr': 3,
  613. 'lnum': 19,
  614. 'col': 2,
  615. 'text': 'error text',
  616. 'type': 'E',
  617. 'valid': 1
  618. } ], 1 )
  619. post_vim_message.assert_has_exact_calls( [
  620. call( 'Forcing compilation, this will block Vim until done.',
  621. warning = False ),
  622. call( 'Diagnostics refreshed', warning = False )
  623. ] )
  624. open_location_list.assert_called_once_with( focus = True )
  625. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  626. 'g:ycm_enable_diagnostic_signs': 1,
  627. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  628. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  629. return_value = True )
  630. @patch( 'ycm.client.event_notification.EventNotification.Done',
  631. return_value = True )
  632. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  633. def test_YouCompleteMe_UpdateDiagnosticInterface_OldVim(
  634. self, ycm, post_vim_message, *args ):
  635. YouCompleteMe_UpdateDiagnosticInterface( ycm, post_vim_message )
  636. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  637. 'g:ycm_enable_diagnostic_signs': 1,
  638. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  639. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  640. return_value = True )
  641. @patch( 'ycm.tests.test_utils.VIM_VERSION', Version( 8, 1, 614 ) )
  642. @patch( 'ycm.client.event_notification.EventNotification.Done',
  643. return_value = True )
  644. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  645. def test_YouCompleteMe_UpdateDiagnosticInterface_NewVim(
  646. self, ycm, post_vim_message, *args ):
  647. YouCompleteMe_UpdateDiagnosticInterface( ycm, post_vim_message )
  648. @YouCompleteMeInstance( { 'g:ycm_enable_diagnostic_highlighting': 1 } )
  649. def test_YouCompleteMe_UpdateMatches_ClearDiagnosticMatchesInNewBuffer(
  650. self, ycm ):
  651. current_buffer = VimBuffer( 'buffer',
  652. filetype = 'c',
  653. contents = '\n\n\n\n',
  654. number = 5 )
  655. test_utils.VIM_PROPS_FOR_BUFFER[ current_buffer.number ] = [
  656. VimProp( 'YcmWarningProperty', 3, 5, 3, 7 ),
  657. VimProp( 'YcmWarningProperty', 3, 3, 3, 9 ),
  658. VimProp( 'YcmErrorProperty', 3, 8, 3, 9 )
  659. ]
  660. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  661. ycm.UpdateMatches()
  662. assert_that( test_utils.VIM_PROPS_FOR_BUFFER[ current_buffer.number ],
  663. empty() )
  664. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  665. 'g:ycm_always_populate_location_list': 1,
  666. 'g:ycm_show_diagnostics_ui': 0,
  667. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  668. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  669. return_value = True )
  670. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  671. def test_YouCompleteMe_AsyncDiagnosticUpdate_UserDisabled(
  672. self,
  673. ycm,
  674. post_vim_message,
  675. *args ):
  676. diagnostics = [
  677. {
  678. 'kind': 'ERROR',
  679. 'text': 'error text in current buffer',
  680. 'location': {
  681. 'filepath': '/current',
  682. 'line_num': 1,
  683. 'column_num': 1
  684. },
  685. 'location_extent': {
  686. 'start': {
  687. 'filepath': '/current',
  688. 'line_num': 1,
  689. 'column_num': 1,
  690. },
  691. 'end': {
  692. 'filepath': '/current',
  693. 'line_num': 1,
  694. 'column_num': 1,
  695. }
  696. },
  697. 'ranges': []
  698. },
  699. ]
  700. current_buffer = VimBuffer( '/current',
  701. filetype = 'ycmtest',
  702. contents = [ 'current' ] * 10,
  703. number = 1 )
  704. buffers = [ current_buffer ]
  705. windows = [ current_buffer ]
  706. # Register each buffer internally with YCM
  707. for current in buffers:
  708. with MockVimBuffers( buffers, [ current ] ):
  709. ycm.OnFileReadyToParse()
  710. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  711. new_callable = ExtendedMock ) as set_location_list_for_window:
  712. with MockVimBuffers( buffers, windows ):
  713. ycm.UpdateWithNewDiagnosticsForFile( '/current', diagnostics )
  714. post_vim_message.assert_has_exact_calls( [] )
  715. set_location_list_for_window.assert_has_exact_calls( [] )
  716. assert_that(
  717. test_utils.VIM_PROPS_FOR_BUFFER,
  718. empty()
  719. )
  720. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  721. 'g:ycm_always_populate_location_list': 1,
  722. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  723. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  724. return_value = True )
  725. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  726. def test_YouCompleteMe_AsyncDiagnosticUpdate_SingleFile(
  727. self,
  728. ycm,
  729. post_vim_message,
  730. *args ):
  731. # This test simulates asynchronous diagnostic updates associated with a
  732. # single file (e.g. Translation Unit), but where the actual errors refer to
  733. # other open files and other non-open files. This is not strictly invalid,
  734. # nor is it completely normal, but it is supported and does work.
  735. # Contrast with the next test which sends the diagnostics filewise, which
  736. # is what the language server protocol will do.
  737. diagnostics = [
  738. {
  739. 'kind': 'ERROR',
  740. 'text': 'error text in current buffer',
  741. 'location': {
  742. 'filepath': '/current',
  743. 'line_num': 1,
  744. 'column_num': 1
  745. },
  746. 'location_extent': {
  747. 'start': {
  748. 'filepath': '/current',
  749. 'line_num': 1,
  750. 'column_num': 1,
  751. },
  752. 'end': {
  753. 'filepath': '/current',
  754. 'line_num': 1,
  755. 'column_num': 1,
  756. }
  757. },
  758. 'ranges': []
  759. },
  760. {
  761. 'kind': 'ERROR',
  762. 'text': 'error text in hidden buffer',
  763. 'location': {
  764. 'filepath': '/has_diags',
  765. 'line_num': 4,
  766. 'column_num': 2
  767. },
  768. 'location_extent': {
  769. 'start': {
  770. 'filepath': '/has_diags',
  771. 'line_num': 4,
  772. 'column_num': 2,
  773. },
  774. 'end': {
  775. 'filepath': '/has_diags',
  776. 'line_num': 4,
  777. 'column_num': 2,
  778. }
  779. },
  780. 'ranges': []
  781. },
  782. {
  783. 'kind': 'ERROR',
  784. 'text': 'error text in buffer not open in Vim',
  785. 'location': {
  786. 'filepath': '/not_open',
  787. 'line_num': 8,
  788. 'column_num': 4
  789. },
  790. 'location_extent': {
  791. 'start': {
  792. 'filepath': '/not_open',
  793. 'line_num': 8,
  794. 'column_num': 4,
  795. },
  796. 'end': {
  797. 'filepath': '/not_open',
  798. 'line_num': 8,
  799. 'column_num': 4,
  800. }
  801. },
  802. 'ranges': []
  803. }
  804. ]
  805. current_buffer = VimBuffer( '/current',
  806. filetype = 'ycmtest',
  807. contents = [ 'current' ] * 10,
  808. number = 1 )
  809. no_diags_buffer = VimBuffer( '/no_diags',
  810. filetype = 'ycmtest',
  811. contents = [ 'nodiags' ] * 10,
  812. number = 2 )
  813. hidden_buffer = VimBuffer( '/has_diags',
  814. filetype = 'ycmtest',
  815. contents = [ 'hasdiags' ] * 10,
  816. number = 3 )
  817. buffers = [ current_buffer, no_diags_buffer, hidden_buffer ]
  818. windows = [ current_buffer, no_diags_buffer ]
  819. # Register each buffer internally with YCM
  820. for current in buffers:
  821. with MockVimBuffers( buffers, [ current ] ):
  822. ycm.OnFileReadyToParse()
  823. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  824. new_callable = ExtendedMock ) as set_location_list_for_window:
  825. with MockVimBuffers( buffers, windows ):
  826. ycm.UpdateWithNewDiagnosticsForFile( '/current', diagnostics )
  827. # Ensure we included all the diags though
  828. set_location_list_for_window.assert_has_exact_calls( [
  829. call( vim.current.window, [
  830. {
  831. 'lnum': 1,
  832. 'col': 1,
  833. 'bufnr': 1,
  834. 'valid': 1,
  835. 'type': 'E',
  836. 'text': 'error text in current buffer',
  837. },
  838. {
  839. 'lnum': 4,
  840. 'col': 2,
  841. 'bufnr': 3,
  842. 'valid': 1,
  843. 'type': 'E',
  844. 'text': 'error text in hidden buffer',
  845. },
  846. {
  847. 'lnum': 8,
  848. 'col': 4,
  849. 'bufnr': -1, # sic: Our mocked bufnr function actually returns -1,
  850. # even though YCM is passing "create if needed".
  851. # FIXME? we shouldn't do that, and we should pass
  852. # filename instead
  853. 'valid': 1,
  854. 'type': 'E',
  855. 'text': 'error text in buffer not open in Vim'
  856. }
  857. ], False )
  858. ] )
  859. # We update the diagnostic on the current cursor position
  860. post_vim_message.assert_has_exact_calls( [
  861. call( "error text in current buffer", truncate = True, warning = False ),
  862. ] )
  863. assert_that(
  864. test_utils.VIM_PROPS_FOR_BUFFER,
  865. has_entries( {
  866. 1: contains_exactly(
  867. VimProp( 'YcmErrorProperty', 1, 1, 1, 1 )
  868. )
  869. } )
  870. )
  871. @YouCompleteMeInstance( { 'g:ycm_echo_current_diagnostic': 1,
  872. 'g:ycm_always_populate_location_list': 1,
  873. 'g:ycm_enable_diagnostic_highlighting': 1 } )
  874. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  875. return_value = True )
  876. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  877. def test_YouCompleteMe_AsyncDiagnosticUpdate_PerFile(
  878. self,
  879. ycm,
  880. post_vim_message,
  881. *args ):
  882. # This test simulates asynchronous diagnostic updates which are delivered
  883. # per file, including files which are open and files which are not.
  884. # Ordered to ensure that the calls to update are deterministic
  885. diagnostics_per_file = [
  886. ( '/current', [ {
  887. 'kind': 'ERROR',
  888. 'text': 'error text in current buffer',
  889. 'location': {
  890. 'filepath': '/current',
  891. 'line_num': 1,
  892. 'column_num': 1
  893. },
  894. 'location_extent': {
  895. 'start': {
  896. 'filepath': '/current',
  897. 'line_num': 1,
  898. 'column_num': 1,
  899. },
  900. 'end': {
  901. 'filepath': '/current',
  902. 'line_num': 1,
  903. 'column_num': 1,
  904. }
  905. },
  906. 'ranges': [],
  907. } ] ),
  908. ( '/separate_window', [ {
  909. 'kind': 'ERROR',
  910. 'text': 'error text in a buffer open in a separate window',
  911. 'location': {
  912. 'filepath': '/separate_window',
  913. 'line_num': 3,
  914. 'column_num': 3
  915. },
  916. 'location_extent': {
  917. 'start': {
  918. 'filepath': '/separate_window',
  919. 'line_num': 3,
  920. 'column_num': 3,
  921. },
  922. 'end': {
  923. 'filepath': '/separate_window',
  924. 'line_num': 3,
  925. 'column_num': 3,
  926. }
  927. },
  928. 'ranges': []
  929. } ] ),
  930. ( '/hidden', [ {
  931. 'kind': 'ERROR',
  932. 'text': 'error text in hidden buffer',
  933. 'location': {
  934. 'filepath': '/hidden',
  935. 'line_num': 4,
  936. 'column_num': 2
  937. },
  938. 'location_extent': {
  939. 'start': {
  940. 'filepath': '/hidden',
  941. 'line_num': 4,
  942. 'column_num': 2,
  943. },
  944. 'end': {
  945. 'filepath': '/hidden',
  946. 'line_num': 4,
  947. 'column_num': 2,
  948. }
  949. },
  950. 'ranges': []
  951. } ] ),
  952. ( '/not_open', [ {
  953. 'kind': 'ERROR',
  954. 'text': 'error text in buffer not open in Vim',
  955. 'location': {
  956. 'filepath': '/not_open',
  957. 'line_num': 8,
  958. 'column_num': 4
  959. },
  960. 'location_extent': {
  961. 'start': {
  962. 'filepath': '/not_open',
  963. 'line_num': 8,
  964. 'column_num': 4,
  965. },
  966. 'end': {
  967. 'filepath': '/not_open',
  968. 'line_num': 8,
  969. 'column_num': 4,
  970. }
  971. },
  972. 'ranges': []
  973. } ] )
  974. ]
  975. current_buffer = VimBuffer( '/current',
  976. filetype = 'ycmtest',
  977. contents = [ 'current' ] * 10,
  978. number = 1 )
  979. no_diags_buffer = VimBuffer( '/no_diags',
  980. filetype = 'ycmtest',
  981. contents = [ 'no_diags' ] * 10,
  982. number = 2 )
  983. separate_window = VimBuffer( '/separate_window',
  984. filetype = 'ycmtest',
  985. contents = [ 'separate_window' ] * 10,
  986. number = 3 )
  987. hidden_buffer = VimBuffer( '/hidden',
  988. filetype = 'ycmtest',
  989. contents = [ 'hidden' ] * 10,
  990. number = 4 )
  991. buffers = [
  992. current_buffer,
  993. no_diags_buffer,
  994. separate_window,
  995. hidden_buffer
  996. ]
  997. windows = [
  998. current_buffer,
  999. no_diags_buffer,
  1000. separate_window
  1001. ]
  1002. # Register each buffer internally with YCM
  1003. for current in buffers:
  1004. with MockVimBuffers( buffers, [ current ] ):
  1005. ycm.OnFileReadyToParse()
  1006. with patch( 'ycm.vimsupport.SetLocationListForWindow',
  1007. new_callable = ExtendedMock ) as set_location_list_for_window:
  1008. with MockVimBuffers( buffers, windows ):
  1009. for filename, diagnostics in diagnostics_per_file:
  1010. ycm.UpdateWithNewDiagnosticsForFile( filename, diagnostics )
  1011. # Ensure we included all the diags though
  1012. set_location_list_for_window.assert_has_exact_calls( [
  1013. call( vim.windows[ 0 ], [
  1014. {
  1015. 'lnum': 1,
  1016. 'col': 1,
  1017. 'bufnr': 1,
  1018. 'valid': 1,
  1019. 'type': 'E',
  1020. 'text': 'error text in current buffer',
  1021. },
  1022. ], False ),
  1023. call( vim.windows[ 2 ], [
  1024. {
  1025. 'lnum': 3,
  1026. 'col': 3,
  1027. 'bufnr': 3,
  1028. 'valid': 1,
  1029. 'type': 'E',
  1030. 'text': 'error text in a buffer open in a separate window',
  1031. },
  1032. ], False )
  1033. ] )
  1034. # We update the diagnostic on the current cursor position
  1035. post_vim_message.assert_has_exact_calls( [
  1036. call( "error text in current buffer", truncate = True, warning = False ),
  1037. ] )
  1038. assert_that(
  1039. test_utils.VIM_PROPS_FOR_BUFFER,
  1040. has_entries( {
  1041. 1: contains_exactly(
  1042. VimProp( 'YcmErrorProperty', 1, 1, 1, 1 )
  1043. ),
  1044. 3: contains_exactly(
  1045. VimProp( 'YcmErrorProperty', 3, 3, 3, 3 )
  1046. )
  1047. } )
  1048. )
  1049. @YouCompleteMeInstance()
  1050. def test_YouCompleteMe_OnPeriodicTick_ServerNotRunning( self, ycm ):
  1051. with patch.object( ycm, 'IsServerAlive', return_value = False ):
  1052. assert_that( ycm.OnPeriodicTick(), equal_to( False ) )
  1053. @YouCompleteMeInstance()
  1054. def test_YouCompleteMe_OnPeriodicTick_ServerNotReady( self, ycm ):
  1055. with patch.object( ycm, 'IsServerAlive', return_value = True ):
  1056. with patch.object( ycm, 'IsServerReady', return_value = False ):
  1057. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1058. @YouCompleteMeInstance()
  1059. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  1060. return_value = True )
  1061. @patch( 'ycm.client.base_request._ValidateResponseObject',
  1062. return_value = True )
  1063. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1064. def test_YouCompleteMe_OnPeriodicTick_DontRetry(
  1065. self,
  1066. ycm,
  1067. post_data_to_handler_async,
  1068. *args ):
  1069. current_buffer = VimBuffer( '/current',
  1070. filetype = 'ycmtest',
  1071. number = 1 )
  1072. # Create the request and make the first poll; we expect no response
  1073. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1074. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1075. post_data_to_handler_async.assert_called()
  1076. assert ycm._message_poll_requests[ 'ycmtest' ] is not None
  1077. post_data_to_handler_async.reset_mock()
  1078. # OK that sent the request, now poll to check if it is complete (say it is
  1079. # not)
  1080. with MockVimBuffers( [ current_buffer ],
  1081. [ current_buffer ],
  1082. ( 1, 1 ) ) as v:
  1083. mock_response = MockAsyncServerResponseInProgress()
  1084. with patch.dict( ycm._message_poll_requests, {} ):
  1085. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll(
  1086. v.current.buffer )
  1087. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1088. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1089. poll_again = ycm.OnPeriodicTick()
  1090. mock_future.done.assert_called()
  1091. mock_future.result.assert_not_called()
  1092. assert_that( poll_again, equal_to( True ) )
  1093. # Poll again, but return a response (telling us to stop polling)
  1094. with MockVimBuffers( [ current_buffer ],
  1095. [ current_buffer ],
  1096. ( 1, 1 ) ) as v:
  1097. mock_response = MockAsyncServerResponseDone( False )
  1098. with patch.dict( ycm._message_poll_requests, {} ):
  1099. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll(
  1100. v.current.buffer )
  1101. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1102. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1103. poll_again = ycm.OnPeriodicTick()
  1104. mock_future.done.assert_called()
  1105. mock_future.result.assert_called()
  1106. post_data_to_handler_async.assert_not_called()
  1107. # We reset and don't poll anymore
  1108. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is None )
  1109. assert_that( poll_again, equal_to( False ) )
  1110. @YouCompleteMeInstance()
  1111. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  1112. return_value = True )
  1113. @patch( 'ycm.client.base_request._ValidateResponseObject',
  1114. return_value = True )
  1115. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1116. def test_YouCompleteMe_OnPeriodicTick_Exception(
  1117. self,
  1118. ycm,
  1119. post_data_to_handler_async,
  1120. *args ):
  1121. current_buffer = VimBuffer( '/current',
  1122. filetype = 'ycmtest',
  1123. number = 1 )
  1124. # Create the request and make the first poll; we expect no response
  1125. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1126. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1127. post_data_to_handler_async.assert_called()
  1128. post_data_to_handler_async.reset_mock()
  1129. # Poll again, but return an exception response
  1130. with MockVimBuffers( [ current_buffer ],
  1131. [ current_buffer ],
  1132. ( 1, 1 ) ) as v:
  1133. mock_response = MockAsyncServerResponseException(
  1134. RuntimeError( 'test' ) )
  1135. with patch.dict( ycm._message_poll_requests, {} ):
  1136. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll(
  1137. v.current.buffer )
  1138. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1139. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1140. assert_that( ycm.OnPeriodicTick(), equal_to( False ) )
  1141. mock_future.done.assert_called()
  1142. mock_future.result.assert_called()
  1143. post_data_to_handler_async.assert_not_called()
  1144. # We reset and don't poll anymore
  1145. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is None )
  1146. @YouCompleteMeInstance()
  1147. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  1148. return_value = True )
  1149. @patch( 'ycm.client.base_request._ValidateResponseObject',
  1150. return_value = True )
  1151. @patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync' )
  1152. @patch( 'ycm.client.messages_request._HandlePollResponse' )
  1153. def test_YouCompleteMe_OnPeriodicTick_ValidResponse(
  1154. self, ycm, handle_poll_response, post_data_to_handler_async, *args ):
  1155. current_buffer = VimBuffer( '/current',
  1156. filetype = 'ycmtest',
  1157. number = 1 )
  1158. # Create the request and make the first poll; we expect no response
  1159. with MockVimBuffers( [ current_buffer ],
  1160. [ current_buffer ],
  1161. ( 1, 1 ) ):
  1162. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1163. post_data_to_handler_async.assert_called()
  1164. post_data_to_handler_async.reset_mock()
  1165. # Poll again, and return a _proper_ response (finally!).
  1166. # Note, _HandlePollResponse is tested independently (for simplicity)
  1167. with MockVimBuffers( [ current_buffer ],
  1168. [ current_buffer ],
  1169. ( 1, 1 ) ) as v:
  1170. mock_response = MockAsyncServerResponseDone( [] )
  1171. with patch.dict( ycm._message_poll_requests, {} ):
  1172. ycm._message_poll_requests[ 'ycmtest' ] = MessagesPoll(
  1173. v.current.buffer )
  1174. ycm._message_poll_requests[ 'ycmtest' ]._response_future = mock_response
  1175. mock_future = ycm._message_poll_requests[ 'ycmtest' ]._response_future
  1176. assert_that( ycm.OnPeriodicTick(), equal_to( True ) )
  1177. handle_poll_response.assert_called()
  1178. mock_future.done.assert_called()
  1179. mock_future.result.assert_called()
  1180. post_data_to_handler_async.assert_called() # Poll again!
  1181. assert_that( ycm._message_poll_requests[ 'ycmtest' ] is not None )
  1182. @YouCompleteMeInstance()
  1183. @patch( 'ycm.client.completion_request.CompletionRequest.OnCompleteDone' )
  1184. def test_YouCompleteMe_OnCompleteDone_CompletionRequest(
  1185. self, ycm, on_complete_done ):
  1186. current_buffer = VimBuffer( 'current_buffer' )
  1187. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  1188. ycm.SendCompletionRequest()
  1189. ycm.OnCompleteDone()
  1190. on_complete_done.assert_called()
  1191. @YouCompleteMeInstance()
  1192. @patch( 'ycm.client.completion_request.CompletionRequest.OnCompleteDone' )
  1193. def test_YouCompleteMe_OnCompleteDone_NoCompletionRequest(
  1194. self, ycm, on_complete_done ):
  1195. ycm.OnCompleteDone()
  1196. on_complete_done.assert_not_called()
  1197. @YouCompleteMeInstance()
  1198. def test_YouCompleteMe_ShouldResendFileParseRequest_NoParseRequest(
  1199. self, ycm ):
  1200. current_buffer = VimBuffer( 'current_buffer' )
  1201. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  1202. assert_that( ycm.ShouldResendFileParseRequest(), equal_to( False ) )