1
0

conftest.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. from ycm.tests.test_utils import MockVimModule
  18. MockVimModule()
  19. import contextlib
  20. import pytest
  21. import requests
  22. import time
  23. import warnings
  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 setUpAndTearDown():
  29. # We treat warnings as errors in our tests because warnings raised inside Vim
  30. # will interrupt user workflow with a traceback and we don't want that.
  31. warnings.filterwarnings( 'error' )
  32. try:
  33. yield
  34. finally:
  35. warnings.resetwarnings()
  36. # The default options which are required for a working YouCompleteMe object.
  37. DEFAULT_CLIENT_OPTIONS = {
  38. # YCM options
  39. 'g:ycm_log_level': 'info',
  40. 'g:ycm_keep_logfiles': 0,
  41. 'g:ycm_extra_conf_vim_data': [],
  42. 'g:ycm_server_python_interpreter': '',
  43. 'g:ycm_show_diagnostics_ui': 1,
  44. 'g:ycm_enable_diagnostic_signs': 1,
  45. 'g:ycm_enable_diagnostic_highlighting': 0,
  46. 'g:ycm_echo_current_diagnostic': 1,
  47. 'g:ycm_filter_diagnostics': {},
  48. 'g:ycm_always_populate_location_list': 0,
  49. 'g:ycm_collect_identifiers_from_tags_files': 0,
  50. 'g:ycm_seed_identifiers_with_syntax': 0,
  51. 'g:ycm_goto_buffer_command': 'same-buffer',
  52. # ycmd options
  53. 'g:ycm_auto_trigger': 1,
  54. 'g:ycm_min_num_of_chars_for_completion': 2,
  55. 'g:ycm_semantic_triggers': {},
  56. 'g:ycm_filetype_specific_completion_to_disable': { 'gitcommit': 1 },
  57. 'g:ycm_max_num_candidates': 50,
  58. 'g:ycm_max_diagnostics_to_display': 30,
  59. 'g:ycm_disable_signature_help': 0,
  60. }
  61. @contextlib.contextmanager
  62. def UserOptions( options ):
  63. old_vim_options = test_utils.VIM_OPTIONS.copy()
  64. test_utils.VIM_OPTIONS.update( DEFAULT_CLIENT_OPTIONS )
  65. test_utils.VIM_OPTIONS.update( options )
  66. try:
  67. yield
  68. finally:
  69. test_utils.VIM_OPTIONS = old_vim_options
  70. def _IsReady():
  71. return BaseRequest().GetDataFromHandler( 'ready' )
  72. def WaitUntilReady( timeout = 5 ):
  73. expiration = time.time() + timeout
  74. while True:
  75. try:
  76. if time.time() > expiration:
  77. raise RuntimeError( 'Waited for the server to be ready '
  78. f'for { timeout } seconds, aborting.' )
  79. if _IsReady():
  80. return
  81. except requests.exceptions.ConnectionError:
  82. pass
  83. finally:
  84. time.sleep( 0.1 )
  85. def StopServer( ycm ):
  86. try:
  87. ycm.OnVimLeave()
  88. WaitUntilProcessIsTerminated( ycm._server_popen )
  89. CloseStandardStreams( ycm._server_popen )
  90. except Exception:
  91. pass
  92. @pytest.fixture
  93. def ycm( request ):
  94. custom_options = request.param
  95. with UserOptions( custom_options ):
  96. ycm = YouCompleteMe()
  97. WaitUntilReady()
  98. ycm.CheckIfServerIsReady()
  99. try:
  100. test_utils.VIM_MATCHES_FOR_WINDOW.clear()
  101. yield ycm
  102. finally:
  103. StopServer( ycm )
  104. def YouCompleteMeInstance( custom_options = {} ):
  105. """Defines a decorator function for tests that passes a unique YouCompleteMe
  106. instance as a parameter. This instance is initialized with the default options
  107. `DEFAULT_CLIENT_OPTIONS`. Use the optional parameter |custom_options| to give
  108. additional options and/or override the already existing ones.
  109. Example usage:
  110. from ycm.tests import YouCompleteMeInstance
  111. @YouCompleteMeInstance( { 'log_level': 'debug',
  112. 'keep_logfiles': 1 } )
  113. def Debug_test( ycm ):
  114. ...
  115. """
  116. return pytest.mark.parametrize(
  117. 'ycm',
  118. [ custom_options ],
  119. ids = [ '' ],
  120. indirect = True )