# HG changeset patch # User Marcin Kuzminski # Date 2013-02-25 23:11:59 # Node ID a9adca4ba3c98a5f0e456dc66cfe6db11d0e7e80 # Parent 7854097b189c7b8639e52db7c557f9bbb84ff072 fixed urlify changesets regex + tests diff --git a/rhodecode/lib/helpers.py b/rhodecode/lib/helpers.py --- a/rhodecode/lib/helpers.py +++ b/rhodecode/lib/helpers.py @@ -1004,21 +1004,16 @@ def urlify_changesets(text_, repository) :param repository: repo name to build the URL with """ from pylons import url # doh, we need to re-import url to mock it later - URL_PAT = re.compile(r'(?:^|\s)([0-9a-fA-F]{12,40})(?:$|\s)') + URL_PAT = re.compile(r'(^|\s)([0-9a-fA-F]{12,40})($|\s)') def url_func(match_obj): - rev = match_obj.groups()[0] - pref = '' - suf = '' - if match_obj.group().startswith(' '): - pref = ' ' - if match_obj.group().endswith(' '): - suf = ' ' + rev = match_obj.groups()[1] + pref = match_obj.groups()[0] + suf = match_obj.groups()[2] + tmpl = ( '%(pref)s' - '%(rev)s' - '' - '%(suf)s' + '%(rev)s%(suf)s' ) return tmpl % { 'pref': pref, diff --git a/rhodecode/tests/test_libs.py b/rhodecode/tests/test_libs.py --- a/rhodecode/tests/test_libs.py +++ b/rhodecode/tests/test_libs.py @@ -209,6 +209,21 @@ class TestLibs(unittest.TestCase): grav = gravatar_url(email_address=em, size=24) assert grav == 'https://server.com/%s/%s' % (_md5(em), 24) + def _quick_url(self, text, tmpl="""%s""", url_=None): + """ + Changes `some text url[foo]` => `some text foo + + :param text: + """ + import re + #quickly change expected url[] into a link + URL_PAT = re.compile(r'(?:url\[)(.+?)(?:\])') + + def url_func(match_obj): + _url = match_obj.groups()[0] + return tmpl % (url_ or '/some-url', _url) + return URL_PAT.sub(url_func, text) + @parameterized.expand([ ("", ""), @@ -228,27 +243,48 @@ class TestLibs(unittest.TestCase): "url[ffffffffffff] some text traalaa"), ("""Multi line 123123123123 - some text 123123123123""", + some text 123123123123 + sometimes ! + """, """Multi line url[123123123123] - some text url[123123123123]""") + some text url[123123123123] + sometimes ! + """) ]) def test_urlify_changesets(self, sample, expected): - import re - def fake_url(self, *args, **kwargs): return '/some-url' - #quickly change expected url[] into a link - URL_PAT = re.compile(r'(?:url\[)(.+?)(?:\])') - - def url_func(match_obj): - _url = match_obj.groups()[0] - tmpl = """%s""" - return tmpl % _url - - expected = URL_PAT.sub(url_func, expected) + expected = self._quick_url(expected) with mock.patch('pylons.url', fake_url): from rhodecode.lib.helpers import urlify_changesets self.assertEqual(urlify_changesets(sample, 'repo_name'), expected) + + @parameterized.expand([ + ("", + "", + ""), + ("https://svn.apache.org/repos", + "url[https://svn.apache.org/repos]", + "https://svn.apache.org/repos"), + ("http://svn.apache.org/repos", + "url[http://svn.apache.org/repos]", + "http://svn.apache.org/repos"), + ("from rev a also rev http://google.com", + "from rev a also rev url[http://google.com]", + "http://google.com"), + ("""Multi line + https://foo.bar.com + some text lalala""", + """Multi line + url[https://foo.bar.com] + some text lalala""", + "https://foo.bar.com") + ]) + def test_urlify_test(self, sample, expected, url_): + from rhodecode.lib.helpers import urlify_text + expected = self._quick_url(expected, + tmpl="""%s""", url_=url_) + self.assertEqual(urlify_text(sample), expected)