selftest.testlist: Add read_test_regexes.
authorJelmer Vernooij <jelmer@samba.org>
Sun, 4 Mar 2012 02:12:35 +0000 (03:12 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Sun, 4 Mar 2012 17:02:06 +0000 (18:02 +0100)
selftest/testlist.py
selftest/tests/test_testlist.py

index 8fb31fd..e535194 100644 (file)
 
 """Selftest test list management."""
 
-__all__ = ['find_in_list']
+__all__ = ['find_in_list', 'read_test_regexes']
 
 import re
 
 def find_in_list(list, fullname):
     """Find test in list.
 
+    :param list: List with 2-tuples with regex and reason
     """
     for (regex, reason) in list:
         if re.match(regex, fullname):
@@ -34,3 +35,21 @@ def find_in_list(list, fullname):
             else:
                 return ""
     return None
+
+
+def read_test_regexes(f):
+    """Read tuples with regular expression and optional string from a file.
+
+    :param f: File-like object to read from
+    :return: Iterator over tuples with regular expression and test name
+    """
+    for l in f.readlines():
+        l = l.strip()
+        if l[0] == "#":
+            continue
+        try:
+            (test, reason) = l.split("#", 1)
+        except ValueError:
+            yield l, None
+        else:
+            yield test.strip(), reason.strip()
index e62300c..5f03887 100644 (file)
 
 from selftest.testlist import (
     find_in_list,
+    read_test_regexes,
     )
 
+from cStringIO import StringIO
+
 import unittest
 
 
@@ -34,3 +37,19 @@ class FindInListTests(unittest.TestCase):
     def test_no_reason(self):
         self.assertEquals("because",
             find_in_list([("foo.*bar", "because")], "foo.bla.bar"))
+
+
+class ReadTestRegexesTests(unittest.TestCase):
+
+    def test_comment(self):
+        f = StringIO("# I am a comment\n # I am also a comment\n")
+        self.assertEquals([], list(read_test_regexes(f)))
+
+    def test_no_reason(self):
+        f = StringIO(" foo\n")
+        self.assertEquals([("foo", None)], list(read_test_regexes(f)))
+
+    def test_reason(self):
+        f = StringIO(" foo # because\nbar\n")
+        self.assertEquals([("foo", "because"), ("bar", None)],
+            list(read_test_regexes(f)))