__init__.py 3.5 KB

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