__init__.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Copyright (C) 2016-2020 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. import os
  18. from ycm.tests.test_utils import MockVimModule
  19. MockVimModule()
  20. import contextlib
  21. import functools
  22. import time
  23. from urllib.error import HTTPError, URLError
  24. from ycm.client.base_request import BaseRequest
  25. from ycm.tests import test_utils
  26. from ycm.youcompleteme import YouCompleteMe
  27. from ycmd.utils import CloseStandardStreams, WaitUntilProcessIsTerminated
  28. def PathToTestFile( *args ):
  29. dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) )
  30. return os.path.join( dir_of_current_script, 'testdata', *args )
  31. # The default options which are required for a working YouCompleteMe object.
  32. DEFAULT_CLIENT_OPTIONS = {
  33. # YCM options
  34. 'g:ycm_log_level': 'info',
  35. 'g:ycm_keep_logfiles': 0,
  36. 'g:ycm_extra_conf_vim_data': [],
  37. 'g:ycm_server_python_interpreter': '',
  38. 'g:ycm_show_diagnostics_ui': 1,
  39. 'g:ycm_enable_diagnostic_signs': 1,
  40. 'g:ycm_enable_diagnostic_highlighting': 0,
  41. 'g:ycm_echo_current_diagnostic': 1,
  42. 'g:ycm_filter_diagnostics': {},
  43. 'g:ycm_always_populate_location_list': 0,
  44. 'g:ycm_open_loclist_on_ycm_diags': 1,
  45. 'g:ycm_collect_identifiers_from_tags_files': 0,
  46. 'g:ycm_seed_identifiers_with_syntax': 0,
  47. 'g:ycm_goto_buffer_command': 'same-buffer',
  48. 'g:ycm_update_diagnostics_in_insert_mode': 1,
  49. # ycmd options
  50. 'g:ycm_auto_trigger': 1,
  51. 'g:ycm_min_num_of_chars_for_completion': 2,
  52. 'g:ycm_semantic_triggers': {},
  53. 'g:ycm_filetype_specific_completion_to_disable': { 'gitcommit': 1 },
  54. 'g:ycm_max_num_candidates': 50,
  55. 'g:ycm_max_diagnostics_to_display': 30,
  56. 'g:ycm_disable_signature_help': 0,
  57. }
  58. @contextlib.contextmanager
  59. def UserOptions( options ):
  60. old_vim_options = test_utils.VIM_OPTIONS.copy()
  61. test_utils.VIM_OPTIONS.update( DEFAULT_CLIENT_OPTIONS )
  62. test_utils.VIM_OPTIONS.update( options )
  63. try:
  64. yield
  65. finally:
  66. test_utils.VIM_OPTIONS = old_vim_options
  67. def _IsReady():
  68. return BaseRequest().GetDataFromHandler( 'ready' )
  69. def WaitUntilReady( timeout = 5 ):
  70. expiration = time.time() + timeout
  71. while True:
  72. try:
  73. if time.time() > expiration:
  74. raise RuntimeError( 'Waited for the server to be ready '
  75. f'for { timeout } seconds, aborting.' )
  76. if _IsReady():
  77. return
  78. except ( URLError, HTTPError ):
  79. pass
  80. finally:
  81. time.sleep( 0.1 )
  82. def StopServer( ycm ):
  83. try:
  84. ycm.OnVimLeave()
  85. WaitUntilProcessIsTerminated( ycm._server_popen )
  86. CloseStandardStreams( ycm._server_popen )
  87. except Exception:
  88. pass
  89. def YouCompleteMeInstance( custom_options = {} ):
  90. """Defines a decorator function for tests that passes a unique YouCompleteMe
  91. instance as a parameter. This instance is initialized with the default options
  92. `DEFAULT_CLIENT_OPTIONS`. Use the optional parameter |custom_options| to give
  93. additional options and/or override the already existing ones.
  94. Example usage:
  95. from ycm.tests import YouCompleteMeInstance
  96. @YouCompleteMeInstance( { 'log_level': 'debug',
  97. 'keep_logfiles': 1 } )
  98. def Debug_test( ycm ):
  99. ...
  100. """
  101. def Decorator( test ):
  102. @functools.wraps( test )
  103. def Wrapper( test_case_instance, *args, **kwargs ):
  104. with UserOptions( custom_options ):
  105. ycm = YouCompleteMe()
  106. WaitUntilReady()
  107. ycm.CheckIfServerIsReady()
  108. try:
  109. test_utils.VIM_PROPS_FOR_BUFFER.clear()
  110. return test( test_case_instance, ycm, *args, **kwargs )
  111. finally:
  112. StopServer( ycm )
  113. return Wrapper
  114. return Decorator
  115. @contextlib.contextmanager
  116. def youcompleteme_instance( custom_options = {} ):
  117. """Defines a context manager to be used in case a shared YCM state
  118. between subtests is to be avoided, as could be the case with completion
  119. caching."""
  120. with UserOptions( custom_options ):
  121. ycm = YouCompleteMe()
  122. WaitUntilReady()
  123. try:
  124. test_utils.VIM_PROPS_FOR_BUFFER.clear()
  125. yield ycm
  126. finally:
  127. StopServer( ycm )