youcompleteme_test.py 15 KB

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