__init__.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 __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 MockVimModule
  24. MockVimModule()
  25. import contextlib
  26. import functools
  27. import os
  28. import requests
  29. import time
  30. import warnings
  31. from ycm.client.base_request import BaseRequest
  32. from ycm.tests import test_utils
  33. from ycm.youcompleteme import YouCompleteMe
  34. from ycmd.utils import CloseStandardStreams, WaitUntilProcessIsTerminated
  35. # The default options which are required for a working YouCompleteMe object.
  36. DEFAULT_CLIENT_OPTIONS = {
  37. # YCM options
  38. 'g:ycm_log_level': 'info',
  39. 'g:ycm_keep_logfiles': 0,
  40. 'g:ycm_extra_conf_vim_data': [],
  41. 'g:ycm_server_python_interpreter': '',
  42. 'g:ycm_show_diagnostics_ui': 1,
  43. 'g:ycm_enable_diagnostic_signs': 1,
  44. 'g:ycm_enable_diagnostic_highlighting': 0,
  45. 'g:ycm_echo_current_diagnostic': 1,
  46. 'g:ycm_filter_diagnostics': {},
  47. 'g:ycm_always_populate_location_list': 0,
  48. 'g:ycm_collect_identifiers_from_tags_files': 0,
  49. 'g:ycm_seed_identifiers_with_syntax': 0,
  50. 'g:ycm_goto_buffer_command': 'same-buffer',
  51. # ycmd options
  52. 'g:ycm_auto_trigger': 1,
  53. 'g:ycm_min_num_of_chars_for_completion': 2,
  54. 'g:ycm_semantic_triggers': {},
  55. 'g:ycm_filetype_specific_completion_to_disable': { 'gitcommit': 1 },
  56. 'g:ycm_max_num_candidates': 50,
  57. 'g:ycm_max_diagnostics_to_display': 30
  58. }
  59. def PathToTestFile( *args ):
  60. dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) )
  61. return os.path.join( dir_of_current_script, 'testdata', *args )
  62. @contextlib.contextmanager
  63. def UserOptions( options ):
  64. old_vim_options = test_utils.VIM_OPTIONS.copy()
  65. test_utils.VIM_OPTIONS.update( DEFAULT_CLIENT_OPTIONS )
  66. test_utils.VIM_OPTIONS.update( options )
  67. try:
  68. yield
  69. finally:
  70. test_utils.VIM_OPTIONS = old_vim_options
  71. def _IsReady():
  72. return BaseRequest().GetDataFromHandler( 'ready' )
  73. def WaitUntilReady( timeout = 5 ):
  74. expiration = time.time() + timeout
  75. while True:
  76. try:
  77. if time.time() > expiration:
  78. raise RuntimeError( 'Waited for the server to be ready '
  79. 'for {0} seconds, aborting.'.format( timeout ) )
  80. if _IsReady():
  81. return
  82. except requests.exceptions.ConnectionError:
  83. pass
  84. finally:
  85. time.sleep( 0.1 )
  86. def StopServer( ycm ):
  87. try:
  88. ycm.OnVimLeave()
  89. WaitUntilProcessIsTerminated( ycm._server_popen )
  90. CloseStandardStreams( ycm._server_popen )
  91. except Exception:
  92. pass
  93. def setUpPackage():
  94. # We treat warnings as errors in our tests because warnings raised inside Vim
  95. # will interrupt user workflow with a traceback and we don't want that.
  96. warnings.filterwarnings( 'error' )
  97. # We ignore warnings from nose as we are not interested in them.
  98. warnings.filterwarnings( 'ignore', module = 'nose' )
  99. def tearDownPackage():
  100. warnings.resetwarnings()
  101. def YouCompleteMeInstance( custom_options = {} ):
  102. """Defines a decorator function for tests that passes a unique YouCompleteMe
  103. instance as a parameter. This instance is initialized with the default options
  104. `DEFAULT_CLIENT_OPTIONS`. Use the optional parameter |custom_options| to give
  105. additional options and/or override the already existing ones.
  106. Do NOT attach it to test generators but directly to the yielded tests.
  107. Example usage:
  108. from ycm.tests import YouCompleteMeInstance
  109. @YouCompleteMeInstance( { 'log_level': 'debug',
  110. 'keep_logfiles': 1 } )
  111. def Debug_test( ycm ):
  112. ...
  113. """
  114. def Decorator( test ):
  115. @functools.wraps( test )
  116. def Wrapper( *args, **kwargs ):
  117. with UserOptions( custom_options ):
  118. ycm = YouCompleteMe()
  119. WaitUntilReady()
  120. ycm.CheckIfServerIsReady()
  121. try:
  122. test( ycm, *args, **kwargs )
  123. finally:
  124. StopServer( ycm )
  125. return Wrapper
  126. return Decorator