youcompleteme_test.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. # Copyright (C) 2016-2017 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 __future__ import unicode_literals
  18. from __future__ import print_function
  19. from __future__ import division
  20. from __future__ import absolute_import
  21. # Not installing aliases from python-future; it's unreliable and slow.
  22. from builtins import * # noqa
  23. from ycm.tests.test_utils import ( ExtendedMock, MockVimBuffers, MockVimModule,
  24. VimBuffer )
  25. MockVimModule()
  26. import os
  27. import sys
  28. from hamcrest import ( assert_that, contains, empty, is_in, is_not, has_length,
  29. matches_regexp )
  30. from mock import call, MagicMock, patch
  31. from ycm.tests import StopServer, YouCompleteMeInstance
  32. from ycmd.responses import ServerError
  33. @YouCompleteMeInstance()
  34. def YouCompleteMe_YcmCoreNotImported_test( ycm ):
  35. assert_that( 'ycm_core', is_not( is_in( sys.modules ) ) )
  36. @YouCompleteMeInstance()
  37. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  38. def RunNotifyUserIfServerCrashed( ycm, test, post_vim_message ):
  39. StopServer( ycm )
  40. ycm._logger = MagicMock( autospec = True )
  41. ycm._server_popen = MagicMock( autospec = True )
  42. ycm._server_popen.poll.return_value = test[ 'return_code' ]
  43. ycm._server_popen.stderr.read.return_value = test[ 'stderr_output' ]
  44. ycm._NotifyUserIfServerCrashed()
  45. assert_that( ycm._logger.method_calls,
  46. has_length( len( test[ 'expected_logs' ] ) ) )
  47. ycm._logger.error.assert_has_calls(
  48. [ call( log ) for log in test[ 'expected_logs' ] ] )
  49. post_vim_message.assert_has_exact_calls( [
  50. call( test[ 'expected_vim_message' ] )
  51. ] )
  52. def YouCompleteMe_NotifyUserIfServerCrashed_UnexpectedCore_test():
  53. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  54. "Unexpected error while loading the YCM core library. "
  55. "Use the ':YcmToggleLogs' command to check the logs." )
  56. RunNotifyUserIfServerCrashed( {
  57. 'return_code': 3,
  58. 'stderr_output' : '',
  59. 'expected_logs': [ message ],
  60. 'expected_vim_message': message
  61. } )
  62. def YouCompleteMe_NotifyUserIfServerCrashed_MissingCore_test():
  63. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  64. "YCM core library not detected; you need to compile YCM before "
  65. "using it. Follow the instructions in the documentation." )
  66. RunNotifyUserIfServerCrashed( {
  67. 'return_code': 4,
  68. 'stderr_output': '',
  69. 'expected_logs': [ message ],
  70. 'expected_vim_message': message
  71. } )
  72. def YouCompleteMe_NotifyUserIfServerCrashed_Python2Core_test():
  73. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  74. "YCM core library compiled for Python 2 but loaded in Python 3. "
  75. "Set the 'g:ycm_server_python_interpreter' option to a Python 2 "
  76. "interpreter path." )
  77. RunNotifyUserIfServerCrashed( {
  78. 'return_code': 5,
  79. 'stderr_output': '',
  80. 'expected_logs': [ message ],
  81. 'expected_vim_message': message
  82. } )
  83. def YouCompleteMe_NotifyUserIfServerCrashed_Python3Core_test():
  84. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  85. "YCM core library compiled for Python 3 but loaded in Python 2. "
  86. "Set the 'g:ycm_server_python_interpreter' option to a Python 3 "
  87. "interpreter path." )
  88. RunNotifyUserIfServerCrashed( {
  89. 'return_code': 6,
  90. 'stderr_output': '',
  91. 'expected_logs': [ message ],
  92. 'expected_vim_message': message
  93. } )
  94. def YouCompleteMe_NotifyUserIfServerCrashed_OutdatedCore_test():
  95. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  96. "YCM core library too old; PLEASE RECOMPILE by running the "
  97. "install.py script. See the documentation for more details." )
  98. RunNotifyUserIfServerCrashed( {
  99. 'return_code': 7,
  100. 'stderr_output': '',
  101. 'expected_logs': [ message ],
  102. 'expected_vim_message': message
  103. } )
  104. def YouCompleteMe_NotifyUserIfServerCrashed_UnexpectedExitCode_test():
  105. message = ( "The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). "
  106. "Unexpected exit code 1. Use the ':YcmToggleLogs' command to "
  107. "check the logs." )
  108. RunNotifyUserIfServerCrashed( {
  109. 'return_code': 1,
  110. 'stderr_output': 'First line\r\n'
  111. 'Second line',
  112. 'expected_logs': [ 'First line\n'
  113. 'Second line',
  114. message ],
  115. 'expected_vim_message': message
  116. } )
  117. @YouCompleteMeInstance()
  118. def YouCompleteMe_DebugInfo_ServerRunning_test( ycm ):
  119. current_buffer = VimBuffer( 'current_buffer' )
  120. with MockVimBuffers( [ current_buffer ], current_buffer ):
  121. assert_that(
  122. ycm.DebugInfo(),
  123. matches_regexp(
  124. 'Client logfile: .+\n'
  125. 'Server Python interpreter: .+\n'
  126. 'Server Python version: .+\n'
  127. 'Server has Clang support compiled in: (True|False)\n'
  128. 'Clang version: .+\n'
  129. 'No extra configuration file found\n'
  130. 'Server running at: .+\n'
  131. 'Server process ID: \d+\n'
  132. 'Server logfiles:\n'
  133. ' .+\n'
  134. ' .+' )
  135. )
  136. @YouCompleteMeInstance()
  137. def YouCompleteMe_DebugInfo_ServerNotRunning_test( ycm ):
  138. StopServer( ycm )
  139. current_buffer = VimBuffer( 'current_buffer' )
  140. with MockVimBuffers( [ current_buffer ], current_buffer ):
  141. assert_that(
  142. ycm.DebugInfo(),
  143. matches_regexp(
  144. 'Client logfile: .+\n'
  145. 'Server errored, no debug info from server\n'
  146. 'Server running at: .+\n'
  147. 'Server process ID: \d+\n'
  148. 'Server logfiles:\n'
  149. ' .+\n'
  150. ' .+' )
  151. )
  152. @YouCompleteMeInstance()
  153. def YouCompleteMe_OnVimLeave_RemoveClientLogfileByDefault_test( ycm ):
  154. client_logfile = ycm._client_logfile
  155. assert_that( os.path.isfile( client_logfile ),
  156. 'Logfile {0} does not exist.'.format( client_logfile ) )
  157. ycm.OnVimLeave()
  158. assert_that( not os.path.isfile( client_logfile ),
  159. 'Logfile {0} was not removed.'.format( client_logfile ) )
  160. @YouCompleteMeInstance( { 'keep_logfiles': 1 } )
  161. def YouCompleteMe_OnVimLeave_KeepClientLogfile_test( ycm ):
  162. client_logfile = ycm._client_logfile
  163. assert_that( os.path.isfile( client_logfile ),
  164. 'Logfile {0} does not exist.'.format( client_logfile ) )
  165. ycm.OnVimLeave()
  166. assert_that( os.path.isfile( client_logfile ),
  167. 'Logfile {0} was removed.'.format( client_logfile ) )
  168. @YouCompleteMeInstance()
  169. @patch( 'ycm.vimsupport.CloseBuffersForFilename', new_callable = ExtendedMock )
  170. @patch( 'ycm.vimsupport.OpenFilename', new_callable = ExtendedMock )
  171. def YouCompleteMe_ToggleLogs_WithParameters_test( ycm,
  172. open_filename,
  173. close_buffers_for_filename ):
  174. logfile_buffer = VimBuffer( ycm._client_logfile, window = 1 )
  175. with MockVimBuffers( [ logfile_buffer ], logfile_buffer ):
  176. ycm.ToggleLogs( os.path.basename( ycm._client_logfile ),
  177. 'nonexisting_logfile',
  178. os.path.basename( ycm._server_stdout ) )
  179. open_filename.assert_has_exact_calls( [
  180. call( ycm._server_stdout, { 'size': 12,
  181. 'watch': True,
  182. 'fix': True,
  183. 'focus': False,
  184. 'position': 'end' } )
  185. ] )
  186. close_buffers_for_filename.assert_has_exact_calls( [
  187. call( ycm._client_logfile )
  188. ] )
  189. @YouCompleteMeInstance()
  190. @patch( 'ycm.vimsupport.PostVimMessage' )
  191. def YouCompleteMe_ToggleLogs_WithoutParameters_test( ycm, post_vim_message ):
  192. # We test on a Python buffer because the Python completer has subserver
  193. # logfiles.
  194. python_buffer = VimBuffer( 'buffer.py', filetype = 'python' )
  195. with MockVimBuffers( [ python_buffer ], python_buffer ):
  196. ycm.ToggleLogs()
  197. assert_that(
  198. # Argument passed to PostVimMessage.
  199. post_vim_message.call_args[ 0 ][ 0 ],
  200. matches_regexp(
  201. 'Available logfiles are:\n'
  202. 'jedihttp_\d+_stderr_.+.log\n'
  203. 'jedihttp_\d+_stdout_.+.log\n'
  204. 'ycm_.+.log\n'
  205. 'ycmd_\d+_stderr_.+.log\n'
  206. 'ycmd_\d+_stdout_.+.log' )
  207. )
  208. @YouCompleteMeInstance()
  209. def YouCompleteMe_GetDefinedSubcommands_ListFromServer_test( ycm ):
  210. current_buffer = VimBuffer( 'buffer' )
  211. with MockVimBuffers( [ current_buffer ], current_buffer ):
  212. with patch( 'ycm.client.base_request.JsonFromFuture',
  213. return_value = [ 'SomeCommand', 'AnotherCommand' ] ):
  214. assert_that(
  215. ycm.GetDefinedSubcommands(),
  216. contains(
  217. 'SomeCommand',
  218. 'AnotherCommand'
  219. )
  220. )
  221. @YouCompleteMeInstance()
  222. @patch( 'ycm.client.base_request._logger', autospec = True )
  223. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  224. def YouCompleteMe_GetDefinedSubcommands_ErrorFromServer_test( ycm,
  225. post_vim_message,
  226. logger ):
  227. current_buffer = VimBuffer( 'buffer' )
  228. with MockVimBuffers( [ current_buffer ], current_buffer ):
  229. with patch( 'ycm.client.base_request.JsonFromFuture',
  230. side_effect = ServerError( 'Server error' ) ):
  231. result = ycm.GetDefinedSubcommands()
  232. logger.exception.assert_called_with( 'Error while handling server response' )
  233. post_vim_message.assert_has_exact_calls( [
  234. call( 'Server error', truncate = False )
  235. ] )
  236. assert_that( result, empty() )
  237. @YouCompleteMeInstance()
  238. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  239. def YouCompleteMe_ShowDetailedDiagnostic_MessageFromServer_test(
  240. ycm, post_vim_message ):
  241. current_buffer = VimBuffer( 'buffer' )
  242. with MockVimBuffers( [ current_buffer ], current_buffer ):
  243. with patch( 'ycm.client.base_request.JsonFromFuture',
  244. return_value = { 'message': 'some_detailed_diagnostic' } ):
  245. ycm.ShowDetailedDiagnostic(),
  246. post_vim_message.assert_has_exact_calls( [
  247. call( 'some_detailed_diagnostic', warning = False )
  248. ] )
  249. @YouCompleteMeInstance()
  250. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  251. def YouCompleteMe_ShowDiagnostics_FiletypeNotSupported_test( ycm,
  252. post_vim_message ):
  253. current_buffer = VimBuffer( 'buffer', filetype = 'not_supported' )
  254. with MockVimBuffers( [ current_buffer ], current_buffer ):
  255. ycm.ShowDiagnostics()
  256. post_vim_message.assert_called_once_with(
  257. 'Native filetype completion not supported for current file, '
  258. 'cannot force recompilation.', warning = False )
  259. @YouCompleteMeInstance()
  260. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  261. return_value = True )
  262. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  263. @patch( 'ycm.vimsupport.SetLocationList', new_callable = ExtendedMock )
  264. def YouCompleteMe_ShowDiagnostics_NoDiagnosticsDetected_test(
  265. ycm, set_location_list, post_vim_message, *args ):
  266. current_buffer = VimBuffer( 'buffer', filetype = 'cpp' )
  267. with MockVimBuffers( [ current_buffer ], current_buffer ):
  268. with patch( 'ycm.client.event_notification.EventNotification.Response',
  269. return_value = {} ):
  270. ycm.ShowDiagnostics()
  271. post_vim_message.assert_has_exact_calls( [
  272. call( 'Forcing compilation, this will block Vim until done.',
  273. warning = False ),
  274. call( 'Diagnostics refreshed', warning = False ),
  275. call( 'No warnings or errors detected.', warning = False )
  276. ] )
  277. set_location_list.assert_called_once_with( [] )
  278. @YouCompleteMeInstance( { 'log_level': 'debug',
  279. 'keep_logfiles': 1,
  280. 'open_loclist_on_ycm_diags': 0 } )
  281. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  282. return_value = True )
  283. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  284. @patch( 'ycm.vimsupport.SetLocationList', new_callable = ExtendedMock )
  285. def YouCompleteMe_ShowDiagnostics_DiagnosticsFound_DoNotOpenLocationList_test(
  286. ycm, set_location_list, post_vim_message, *args ):
  287. diagnostic = {
  288. 'kind': 'ERROR',
  289. 'text': 'error text',
  290. 'location': {
  291. 'filepath': 'buffer',
  292. 'column_num': 2,
  293. 'line_num': 19
  294. }
  295. }
  296. current_buffer = VimBuffer( 'buffer', filetype = 'cpp', number = 3 )
  297. with MockVimBuffers( [ current_buffer ], current_buffer ):
  298. with patch( 'ycm.client.event_notification.EventNotification.Response',
  299. return_value = [ diagnostic ] ):
  300. ycm.ShowDiagnostics()
  301. post_vim_message.assert_has_exact_calls( [
  302. call( 'Forcing compilation, this will block Vim until done.',
  303. warning = False ),
  304. call( 'Diagnostics refreshed', warning = False )
  305. ] )
  306. set_location_list.assert_called_once_with( [ {
  307. 'bufnr': 3,
  308. 'lnum': 19,
  309. 'col': 2,
  310. 'text': 'error text',
  311. 'type': 'E',
  312. 'valid': 1
  313. } ] )
  314. @YouCompleteMeInstance( { 'open_loclist_on_ycm_diags': 1 } )
  315. @patch( 'ycm.youcompleteme.YouCompleteMe.FiletypeCompleterExistsForFiletype',
  316. return_value = True )
  317. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  318. @patch( 'ycm.vimsupport.SetLocationList', new_callable = ExtendedMock )
  319. @patch( 'ycm.vimsupport.OpenLocationList', new_callable = ExtendedMock )
  320. def YouCompleteMe_ShowDiagnostics_DiagnosticsFound_OpenLocationList_test(
  321. ycm, open_location_list, set_location_list, post_vim_message, *args ):
  322. diagnostic = {
  323. 'kind': 'ERROR',
  324. 'text': 'error text',
  325. 'location': {
  326. 'filepath': 'buffer',
  327. 'column_num': 2,
  328. 'line_num': 19
  329. }
  330. }
  331. current_buffer = VimBuffer( 'buffer', filetype = 'cpp', number = 3 )
  332. with MockVimBuffers( [ current_buffer ], current_buffer ):
  333. with patch( 'ycm.client.event_notification.EventNotification.Response',
  334. return_value = [ diagnostic ] ):
  335. ycm.ShowDiagnostics()
  336. post_vim_message.assert_has_exact_calls( [
  337. call( 'Forcing compilation, this will block Vim until done.',
  338. warning = False ),
  339. call( 'Diagnostics refreshed', warning = False )
  340. ] )
  341. set_location_list.assert_called_once_with( [ {
  342. 'bufnr': 3,
  343. 'lnum': 19,
  344. 'col': 2,
  345. 'text': 'error text',
  346. 'type': 'E',
  347. 'valid': 1
  348. } ] )
  349. open_location_list.assert_called_once_with( focus = True )