__init__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright (C) 2016 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 MockVimModule
  25. MockVimModule()
  26. import functools
  27. import os
  28. import requests
  29. import time
  30. from ycm.client.base_request import BaseRequest
  31. from ycm.youcompleteme import YouCompleteMe
  32. from ycmd import user_options_store
  33. from ycmd.utils import WaitUntilProcessIsTerminated
  34. # The default options which are only relevant to the client, not the server and
  35. # thus are not part of default_options.json, but are required for a working
  36. # YouCompleteMe object.
  37. DEFAULT_CLIENT_OPTIONS = {
  38. 'log_level': 'info',
  39. 'keep_logfiles': 0,
  40. 'extra_conf_vim_data': [],
  41. 'show_diagnostics_ui': 1,
  42. 'enable_diagnostic_signs': 1,
  43. 'enable_diagnostic_highlighting': 0,
  44. 'always_populate_location_list': 0,
  45. }
  46. def PathToTestFile( *args ):
  47. dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) )
  48. return os.path.join( dir_of_current_script, 'testdata', *args )
  49. def _MakeUserOptions( custom_options = {} ):
  50. options = dict( user_options_store.DefaultOptions() )
  51. options.update( DEFAULT_CLIENT_OPTIONS )
  52. options.update( custom_options )
  53. return options
  54. def _IsReady():
  55. return BaseRequest.GetDataFromHandler( 'ready' )
  56. def _WaitUntilReady( timeout = 5 ):
  57. expiration = time.time() + timeout
  58. while True:
  59. try:
  60. if time.time() > expiration:
  61. raise RuntimeError( 'Waited for the server to be ready '
  62. 'for {0} seconds, aborting.'.format( timeout ) )
  63. if _IsReady():
  64. return
  65. except requests.exceptions.ConnectionError:
  66. pass
  67. finally:
  68. time.sleep( 0.1 )
  69. def StopServer( ycm ):
  70. try:
  71. ycm.OnVimLeave()
  72. WaitUntilProcessIsTerminated( ycm._server_popen )
  73. except Exception:
  74. pass
  75. def YouCompleteMeInstance( custom_options = {} ):
  76. """Defines a decorator function for tests that passes a unique YouCompleteMe
  77. instance as a parameter. This instance is initialized with the default options
  78. `DEFAULT_CLIENT_OPTIONS`. Use the optional parameter |custom_options| to give
  79. additional options and/or override the already existing ones.
  80. Do NOT attach it to test generators but directly to the yielded tests.
  81. Example usage:
  82. from ycm.tests import YouCompleteMeInstance
  83. @YouCompleteMeInstance( { 'log_level': 'debug',
  84. 'keep_logfiles': 1 } )
  85. def Debug_test( ycm ):
  86. ...
  87. """
  88. def Decorator( test ):
  89. @functools.wraps( test )
  90. def Wrapper( *args, **kwargs ):
  91. ycm = YouCompleteMe( _MakeUserOptions( custom_options ) )
  92. _WaitUntilReady()
  93. try:
  94. test( ycm, *args, **kwargs )
  95. finally:
  96. StopServer( ycm )
  97. return Wrapper
  98. return Decorator