Skip to content

Commit

Permalink
Port to unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
bstaletic committed Sep 10, 2021
1 parent 518275b commit 3f4b323
Show file tree
Hide file tree
Showing 24 changed files with 5,808 additions and 5,717 deletions.
7 changes: 0 additions & 7 deletions pytest.ini

This file was deleted.

2 changes: 0 additions & 2 deletions python/test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ codecov >= 2.0.5
coverage <5.0
click <8.0.0
covimerage >= 0.2.0
pytest
pytest-cov
111 changes: 110 additions & 1 deletion python/ycm/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,118 @@
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.

import os
from ycm.tests.conftest import * # noqa
from ycm.tests.test_utils import MockVimModule
MockVimModule()

import contextlib
import functools
import time
from urllib.error import HTTPError, URLError

from ycm.client.base_request import BaseRequest
from ycm.tests import test_utils
from ycm.youcompleteme import YouCompleteMe
from ycmd.utils import CloseStandardStreams, WaitUntilProcessIsTerminated


def PathToTestFile( *args ):
dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) )
return os.path.join( dir_of_current_script, 'testdata', *args )


# The default options which are required for a working YouCompleteMe object.
DEFAULT_CLIENT_OPTIONS = {
# YCM options
'g:ycm_log_level': 'info',
'g:ycm_keep_logfiles': 0,
'g:ycm_extra_conf_vim_data': [],
'g:ycm_server_python_interpreter': '',
'g:ycm_show_diagnostics_ui': 1,
'g:ycm_enable_diagnostic_signs': 1,
'g:ycm_enable_diagnostic_highlighting': 0,
'g:ycm_echo_current_diagnostic': 1,
'g:ycm_filter_diagnostics': {},
'g:ycm_always_populate_location_list': 0,
'g:ycm_collect_identifiers_from_tags_files': 0,
'g:ycm_seed_identifiers_with_syntax': 0,
'g:ycm_goto_buffer_command': 'same-buffer',
'g:ycm_update_diagnostics_in_insert_mode': 1,
# ycmd options
'g:ycm_auto_trigger': 1,
'g:ycm_min_num_of_chars_for_completion': 2,
'g:ycm_semantic_triggers': {},
'g:ycm_filetype_specific_completion_to_disable': { 'gitcommit': 1 },
'g:ycm_max_num_candidates': 50,
'g:ycm_max_diagnostics_to_display': 30,
'g:ycm_disable_signature_help': 0,
}


@contextlib.contextmanager
def UserOptions( options ):
old_vim_options = test_utils.VIM_OPTIONS.copy()
test_utils.VIM_OPTIONS.update( DEFAULT_CLIENT_OPTIONS )
test_utils.VIM_OPTIONS.update( options )
try:
yield
finally:
test_utils.VIM_OPTIONS = old_vim_options


def _IsReady():
return BaseRequest().GetDataFromHandler( 'ready' )


def WaitUntilReady( timeout = 5 ):
expiration = time.time() + timeout
while True:
try:
if time.time() > expiration:
raise RuntimeError( 'Waited for the server to be ready '
f'for { timeout } seconds, aborting.' )
if _IsReady():
return
except ( URLError, HTTPError ):
pass
finally:
time.sleep( 0.1 )


def StopServer( ycm ):
try:
ycm.OnVimLeave()
WaitUntilProcessIsTerminated( ycm._server_popen )
CloseStandardStreams( ycm._server_popen )
except Exception:
pass


def YouCompleteMeInstance( custom_options = {} ):
"""Defines a decorator function for tests that passes a unique YouCompleteMe
instance as a parameter. This instance is initialized with the default options
`DEFAULT_CLIENT_OPTIONS`. Use the optional parameter |custom_options| to give
additional options and/or override the already existing ones.
Example usage:
from ycm.tests import YouCompleteMeInstance
@YouCompleteMeInstance( { 'log_level': 'debug',
'keep_logfiles': 1 } )
def Debug_test( ycm ):
...
"""
def Decorator( test ):
@functools.wraps( test )
def Wrapper( *args, **kwargs ):
with UserOptions( custom_options ):
ycm = YouCompleteMe()
WaitUntilReady()
ycm.CheckIfServerIsReady()
try:
test_utils.VIM_MATCHES_FOR_WINDOW.clear()
return test( args[ 0 ], ycm, *args[ 1: ], **kwargs )
finally:
StopServer( ycm )
return Wrapper
return Decorator
Loading

0 comments on commit 3f4b323

Please sign in to comment.