# HG changeset patch # User Mads Kiilerich # Date 1602429038 -7200 # Node ID bf2286a3fc7e8426a757fcf07e58cf0b72f8a721 # Parent 07cb7b42057ec5c12b49e4f5874a74edc838d9d7 tests: simplify test_lib mocking of routing.url Mock internals of the url generator to avoid import tricks. diff -r 07cb7b42057e -r bf2286a3fc7e kallithea/lib/helpers.py --- a/kallithea/lib/helpers.py Sat Oct 10 20:33:27 2020 +0200 +++ b/kallithea/lib/helpers.py Sun Oct 11 17:10:38 2020 +0200 @@ -985,18 +985,15 @@ if email_address == _def: return default - # re-import url so tests can mock it - from kallithea.config.routing import url from kallithea.model.db import User parsed_url = urllib.parse.urlparse(url.current(qualified=True)) - url = (c.visual.gravatar_url or User.DEFAULT_GRAVATAR_URL) \ + return (c.visual.gravatar_url or User.DEFAULT_GRAVATAR_URL) \ .replace('{email}', email_address) \ .replace('{md5email}', hashlib.md5(safe_bytes(email_address).lower()).hexdigest()) \ .replace('{netloc}', parsed_url.netloc) \ .replace('{scheme}', parsed_url.scheme) \ .replace('{size}', str(size)) - return url def changed_tooltip(nodes): @@ -1125,15 +1122,14 @@ """ def _replace(match_obj): - url = match_obj.group('url') - if url is not None: - return '%(url)s' % {'url': url} + match_url = match_obj.group('url') + if match_url is not None: + return '%(url)s' % {'url': match_url} mention = match_obj.group('mention') if mention is not None: return '%s' % mention hash_ = match_obj.group('hash') if hash_ is not None and repo_name is not None: - from kallithea.config.routing import url # doh, we need to re-import url to mock it later return '%(hash)s' % { 'url': url('changeset_home', repo_name=repo_name, revision=hash_), 'hash': hash_, diff -r 07cb7b42057e -r bf2286a3fc7e kallithea/tests/other/test_libs.py --- a/kallithea/tests/other/test_libs.py Sat Oct 10 20:33:27 2020 +0200 +++ b/kallithea/tests/other/test_libs.py Sun Oct 11 17:10:38 2020 +0200 @@ -71,26 +71,6 @@ ] -class FakeUrlGenerator(object): - - def __init__(self, current_url=None, default_route=None, **routes): - """Initialize using specified 'current' URL template, - default route template, and all other aguments describing known - routes (format: route=template)""" - self.current_url = current_url - self.default_route = default_route - self.routes = routes - - def __call__(self, route_name, *args, **kwargs): - if route_name in self.routes: - return self.routes[route_name] % kwargs - - return self.default_route % kwargs - - def current(self, *args, **kwargs): - return self.current_url % kwargs - - class TestLibs(base.TestController): @base.parametrize('test_url,expected,expected_creds', TEST_URLS) @@ -239,8 +219,7 @@ return _c - fake_url = FakeUrlGenerator(current_url='https://example.com') - with mock.patch('kallithea.config.routing.url', fake_url): + with mock.patch('kallithea.config.routing.url.current', lambda *a, **b: 'https://example.com'): fake = fake_tmpl_context(_url='http://example.com/{email}') with mock.patch('tg.tmpl_context', fake): from kallithea.config.routing import url @@ -338,8 +317,10 @@ ]) def test_urlify_text(self, sample, expected): expected = self._quick_url(expected) - fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s') - with mock.patch('kallithea.config.routing.url', fake_url): + + with mock.patch('kallithea.config.routing.UrlGenerator.__call__', + lambda self, name, **kwargs: dict(changeset_home='/%(repo_name)s/changeset/%(revision)s')[name] % kwargs, + ): from kallithea.lib.helpers import urlify_text assert urlify_text(sample, 'repo_name') == expected @@ -393,8 +374,9 @@ def test_urlify_test(self, sample, expected, url_): expected = self._quick_url(expected, tmpl="""%s""", url_=url_) - fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s') - with mock.patch('kallithea.config.routing.url', fake_url): + with mock.patch('kallithea.config.routing.UrlGenerator.__call__', + lambda self, name, **kwargs: dict(changeset_home='/%(repo_name)s/changeset/%(revision)s')[name] % kwargs, + ): from kallithea.lib.helpers import urlify_text assert urlify_text(sample, 'repo_name', stylize=True) == expected @@ -406,8 +388,9 @@ """ yo"""), ]) def test_urlify_link(self, sample, expected): - fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s') - with mock.patch('kallithea.config.routing.url', fake_url): + with mock.patch('kallithea.config.routing.UrlGenerator.__call__', + lambda self, name, **kwargs: dict(changeset_home='/%(repo_name)s/changeset/%(revision)s')[name] % kwargs, + ): from kallithea.lib.helpers import urlify_text assert urlify_text(sample, 'repo_name', link_='#the-link') == expected