__init__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. 'server_log_level': 'info',
  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 YouCompleteMeInstance( custom_options = {} ):
  69. """Defines a decorator function for tests that passes a unique YouCompleteMe
  70. instance as a parameter. This instance is initialized with the default options
  71. `DEFAULT_CLIENT_OPTIONS`. Use the optional parameter |custom_options| to give
  72. additional options and/or override the already existing ones.
  73. Do NOT attach it to test generators but directly to the yielded tests.
  74. Example usage:
  75. from ycm.tests import YouCompleteMeInstance
  76. @YouCompleteMeInstance( { 'server_log_level': 'debug',
  77. 'server_keep_logfiles': 1 } )
  78. def Debug_test( ycm ):
  79. ...
  80. """
  81. def Decorator( test ):
  82. @functools.wraps( test )
  83. def Wrapper( *args, **kwargs ):
  84. ycm = YouCompleteMe( _MakeUserOptions( custom_options ) )
  85. _WaitUntilReady()
  86. try:
  87. test( ycm, *args, **kwargs )
  88. finally:
  89. ycm.OnVimLeave()
  90. WaitUntilProcessIsTerminated( ycm._server_popen )
  91. return Wrapper
  92. return Decorator