changeset 3405:a9adca4ba3c9 beta

fixed urlify changesets regex + tests
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 26 Feb 2013 00:11:59 +0100
parents 7854097b189c
children 4e9f00ddde4a
files rhodecode/lib/helpers.py rhodecode/tests/test_libs.py
diffstat 2 files changed, 55 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/lib/helpers.py	Mon Feb 25 23:17:33 2013 +0100
+++ b/rhodecode/lib/helpers.py	Tue Feb 26 00:11:59 2013 +0100
@@ -1004,21 +1004,16 @@
     :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<a class="%(cls)s" href="%(url)s">'
-        '%(rev)s'
-        '</a>'
-        '%(suf)s'
+        '%(rev)s</a>%(suf)s'
         )
         return tmpl % {
          'pref': pref,
--- a/rhodecode/tests/test_libs.py	Mon Feb 25 23:17:33 2013 +0100
+++ b/rhodecode/tests/test_libs.py	Tue Feb 26 00:11:59 2013 +0100
@@ -209,6 +209,21 @@
                 grav = gravatar_url(email_address=em, size=24)
                 assert grav == 'https://server.com/%s/%s' % (_md5(em), 24)
 
+    def _quick_url(self, text, tmpl="""<a class="revision-link" href="%s">%s</a>""", url_=None):
+        """
+        Changes `some text url[foo]` => `some text <a href="/">foo</a>
+
+        :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 @@
        "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 = """<a class="revision-link" href="/some-url">%s</a>"""
-            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="""<a href="%s">%s</a>""", url_=url_)
+        self.assertEqual(urlify_text(sample), expected)