changeset 3385:d21c762f5c4b beta

revision extraction function shouldn't be so eager, just extract commits that are surrounded with spaces, or they are at beginning of the whole string - wrote tests for that - used parametrized for other tests
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 19 Feb 2013 20:35:25 +0100
parents 95c38de476a7
children 934f1fd5d549
files rhodecode/lib/helpers.py rhodecode/tests/test_libs.py
diffstat 2 files changed, 78 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/lib/helpers.py	Tue Feb 19 19:48:45 2013 +0100
+++ b/rhodecode/lib/helpers.py	Tue Feb 19 20:35:25 2013 +0100
@@ -996,24 +996,29 @@
     :param text_:
     :param repository:
     """
-
-    URL_PAT = re.compile(r'([0-9a-fA-F]{12,})')
+    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)')
 
     def url_func(match_obj):
         rev = match_obj.groups()[0]
         pref = ''
+        suf = ''
         if match_obj.group().startswith(' '):
             pref = ' '
+        if match_obj.group().endswith(' '):
+            suf = ' '
         tmpl = (
         '%(pref)s<a class="%(cls)s" href="%(url)s">'
         '%(rev)s'
         '</a>'
+        '%(suf)s'
         )
         return tmpl % {
          'pref': pref,
          'cls': 'revision-link',
          'url': url('changeset_home', repo_name=repository, revision=rev),
          'rev': rev,
+         'suf': suf
         }
 
     newtext = URL_PAT.sub(url_func, text_)
--- a/rhodecode/tests/test_libs.py	Tue Feb 19 19:48:45 2013 +0100
+++ b/rhodecode/tests/test_libs.py	Tue Feb 19 20:35:25 2013 +0100
@@ -66,41 +66,37 @@
 
 class TestLibs(unittest.TestCase):
 
-    def test_uri_filter(self):
+    @parameterized.expand(TEST_URLS)
+    def test_uri_filter(self, test_url, expected, expected_creds):
         from rhodecode.lib.utils2 import uri_filter
+        self.assertEqual(uri_filter(test_url), expected)
 
-        for url in TEST_URLS:
-            self.assertEqual(uri_filter(url[0]), url[1])
+    @parameterized.expand(TEST_URLS)
+    def test_credentials_filter(self, test_url, expected, expected_creds):
+        from rhodecode.lib.utils2 import credentials_filter
+        self.assertEqual(credentials_filter(test_url), expected_creds)
 
-    def test_credentials_filter(self):
-        from rhodecode.lib.utils2 import credentials_filter
-
-        for url in TEST_URLS:
-            self.assertEqual(credentials_filter(url[0]), url[2])
-
-    def test_str2bool(self):
+    @parameterized.expand([('t', True),
+                           ('true', True),
+                           ('y', True),
+                           ('yes', True),
+                           ('on', True),
+                           ('1', True),
+                           ('Y', True),
+                           ('yeS', True),
+                           ('Y', True),
+                           ('TRUE', True),
+                           ('T', True),
+                           ('False', False),
+                           ('F', False),
+                           ('FALSE', False),
+                           ('0', False),
+                           ('-1', False),
+                           ('', False)
+    ])
+    def test_str2bool(self, str_bool, expected):
         from rhodecode.lib.utils2 import str2bool
-        test_cases = [
-            ('t', True),
-            ('true', True),
-            ('y', True),
-            ('yes', True),
-            ('on', True),
-            ('1', True),
-            ('Y', True),
-            ('yeS', True),
-            ('Y', True),
-            ('TRUE', True),
-            ('T', True),
-            ('False', False),
-            ('F', False),
-            ('FALSE', False),
-            ('0', False),
-            ('-1', False),
-            ('', False), ]
-
-        for case in test_cases:
-            self.assertEqual(str2bool(case[0]), case[1])
+        self.assertEqual(str2bool(str_bool), expected)
 
     def test_mention_extractor(self):
         from rhodecode.lib.utils2 import extract_mentioned_users
@@ -212,3 +208,47 @@
                 em = 'test@foo.com'
                 grav = gravatar_url(email_address=em, size=24)
                 assert grav == 'https://server.com/%s/%s' % (_md5(em), 24)
+
+    @parameterized.expand([
+      ("",
+       ""),
+      ("git-svn-id: https://svn.apache.org/repos/asf/libcloud/trunk@1441655 13f79535-47bb-0310-9956-ffa450edef68",
+       "git-svn-id: https://svn.apache.org/repos/asf/libcloud/trunk@1441655 13f79535-47bb-0310-9956-ffa450edef68"),
+      ("from rev 000000000000",
+       "from rev url[000000000000]"),
+      ("from rev 000000000000123123 also rev 000000000000",
+       "from rev url[000000000000123123] also rev url[000000000000]"),
+      ("this should-000 00",
+       "this should-000 00"),
+      ("longtextffffffffff rev 123123123123",
+       "longtextffffffffff rev url[123123123123]"),
+      ("rev ffffffffffffffffffffffffffffffffffffffffffffffffff",
+       "rev ffffffffffffffffffffffffffffffffffffffffffffffffff"),
+      ("ffffffffffff some text traalaa",
+       "url[ffffffffffff] some text traalaa"),
+       ("""Multi line
+       123123123123 
+       some text 123123123123""",
+       """Multi line
+       url[123123123123] 
+       some text url[123123123123]""")
+    ])
+    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)
+
+        with mock.patch('pylons.url', fake_url):
+            from rhodecode.lib.helpers import urlify_changesets
+            self.assertEqual(urlify_changesets(sample, 'repo_name'), expected)