[PATCH 4/7] Convert the repository name test into a regex

David Shea dshea at redhat.com
Tue Apr 15 18:22:29 UTC 2014


Add a test for the regex
---
 pyanaconda/regexes.py               |  4 +++
 pyanaconda/ui/gui/spokes/source.py  |  6 ++--
 tests/regex_tests/repo_name_test.py | 66 +++++++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+), 3 deletions(-)
 create mode 100644 tests/regex_tests/repo_name_test.py

diff --git a/pyanaconda/regexes.py b/pyanaconda/regexes.py
index 96d22c1..9081329 100644
--- a/pyanaconda/regexes.py
+++ b/pyanaconda/regexes.py
@@ -128,3 +128,7 @@ URL_PARSE = re.compile(r'^(?P<protocol>' + URL_SCHEME_PATTERN_WITHOUT_ANCHORS +
                        r'(?P<path>/(?:' + URL_PATH_CHAR + r')*)?' +
                        r'(?:\?(?P<query>(?:' + URL_PATH_CHAR + r'|\?)*))?' +
                        r'(?:#(?P<fragment>(?:' + URL_PATH_CHAR + r'|\?)*))?$')
+
+
+# Valid characters for repository names
+REPO_NAME_VALID = re.compile(r'^[a-zA-Z0-9_.:-]+$')
diff --git a/pyanaconda/ui/gui/spokes/source.py b/pyanaconda/ui/gui/spokes/source.py
index 12c2143..edc900f 100644
--- a/pyanaconda/ui/gui/spokes/source.py
+++ b/pyanaconda/ui/gui/spokes/source.py
@@ -25,7 +25,7 @@ import time
 import logging
 log = logging.getLogger("anaconda")
 
-import os, signal, string
+import os, signal
 
 from gi.repository import GLib
 
@@ -42,6 +42,7 @@ from pyanaconda.iutil import ProxyString, ProxyStringError, cmp_obj_attrs
 from pyanaconda.ui.gui.utils import gtk_call_once, really_hide, really_show
 from pyanaconda.threads import threadMgr, AnacondaThread
 from pyanaconda.packaging import PayloadError, MetadataError
+from pyanaconda.regexes import REPO_NAME_VALID
 from pyanaconda import constants
 
 from blivet.util import get_mount_paths
@@ -1107,8 +1108,7 @@ class SourceSpoke(NormalSpoke):
         if name == "":
             return N_("Empty repository name.")
 
-        allowed_chars = string.ascii_letters + string.digits + '-_.:'
-        if [c for c in name if c not in allowed_chars]:
+        if not REPO_NAME_VALID.match(name):
             return N_("Invalid repository name.")
 
         cnames = [constants.BASE_REPO_NAME] + \
diff --git a/tests/regex_tests/repo_name_test.py b/tests/regex_tests/repo_name_test.py
new file mode 100644
index 0000000..10f55a8
--- /dev/null
+++ b/tests/regex_tests/repo_name_test.py
@@ -0,0 +1,66 @@
+#!/usr/bin/python
+# vim:set fileencoding=utf-8
+#
+# Copyright (C) 2014  Red Hat, Inc.
+#
+# This copyrighted material is made available to anyone wishing to use,
+# modify, copy, or redistribute it subject to the terms and conditions of
+# the GNU General Public License v.2, or (at your option) any later version.
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY expressed or implied, including the implied warranties of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+# Public License for more details.  You should have received a copy of the
+# GNU General Public License along with this program; if not, write to the
+# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
+# source code or documentation are not subject to the GNU General Public
+# License and may only be used or replicated with the express permission of
+# Red Hat, Inc.
+#
+# Red Hat Author(s): David Shea <dshea at redhat.com>
+#
+import unittest
+
+from pyanaconda.regexes import REPO_NAME_VALID
+
+def _run_tests(testcase, expression, goodlist, badlist):
+    got_error = False
+    for good in goodlist:
+        try:
+            testcase.assertIsNotNone(expression.match(good))
+        except AssertionError:
+            got_error = True
+            print("Good string %s did not match expression" % good)
+
+    for bad in badlist:
+        try:
+            testcase.assertIsNone(expression.match(bad))
+        except AssertionError:
+            got_error = True
+            print("Bad string %s matched expression" % bad)
+
+    if got_error:
+        testcase.fail()
+
+class RepoNameTestCase(unittest.TestCase):
+    def reponame_test(self):
+        good_tests = [
+                'reponame',
+                'repoName',
+                'repo-name',
+                'repo.name',
+                'repo_name',
+                'repo:name',
+                'rep0Name',
+                '0repoName',
+                'repoName0'
+                ]
+
+        bad_tests = [
+                'repo name',
+                'répo name',
+                '@repo name',
+                '[reponame]'
+                ]
+
+        _run_tests(self, REPO_NAME_VALID, good_tests, bad_tests)
-- 
1.9.0



More information about the anaconda-patches mailing list