selftest.testlist: Add read_testlist.
authorJelmer Vernooij <jelmer@samba.org>
Sun, 4 Mar 2012 02:24:10 +0000 (03:24 +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 e535194..c377012 100644 (file)
 
 """Selftest test list management."""
 
-__all__ = ['find_in_list', 'read_test_regexes']
+__all__ = ['find_in_list', 'read_test_regexes', 'read_testlist']
 
 import re
+import sys
 
 def find_in_list(list, fullname):
     """Find test in list.
@@ -53,3 +54,34 @@ def read_test_regexes(f):
             yield l, None
         else:
             yield test.strip(), reason.strip()
+
+
+def should_run_test(tests, name):
+    if tests == []:
+        return True
+    for test in tests:
+        if re.match(test, name):
+            return True
+    return False
+
+
+def read_testlist(inf, outf):
+    """Read a list of tests from a file.
+
+    :param inf: File-like object to read from.
+    :param outf: File-like object to write to.
+    :return: Iterator over tuples describing tests
+    """
+    while True:
+        l = inf.readline()
+        if l == '':
+            return
+        if l.startswith("-- TEST") and l.endswith(" --\n"):
+            supports_loadlist = l.startswith("-- TEST-LOADLIST")
+            supports_idlist = l.startswith("-- TEST-IDLIST")
+            name = inf.readline().rstrip("\n")
+            env = inf.readline().rstrip("\n")
+            cmdline = inf.readline().rstrip("\n")
+            yield (name, env, cmdline, supports_loadlist, supports_idlist)
+        else:
+            outf.write(l)
index 5f03887..0e7f68b 100644 (file)
@@ -22,6 +22,7 @@
 from selftest.testlist import (
     find_in_list,
     read_test_regexes,
+    read_testlist,
     )
 
 from cStringIO import StringIO
@@ -53,3 +54,20 @@ class ReadTestRegexesTests(unittest.TestCase):
         f = StringIO(" foo # because\nbar\n")
         self.assertEquals([("foo", "because"), ("bar", None)],
             list(read_test_regexes(f)))
+
+
+class ReadTestlistTests(unittest.TestCase):
+
+    def test_read_list(self):
+        inf = StringIO("-- TEST --\nfoo\nbar\nbla\n")
+        outf = StringIO()
+        self.assertEquals([('foo', 'bar', 'bla', False, False)],
+                list(read_testlist(inf, outf)))
+        self.assertEquals("", outf.getvalue())
+
+    def test_read_list_passes_through(self):
+        inf = StringIO("MORENOISE\n-- TEST --\nfoo\nbar\nbla\nNOISE\n")
+        outf = StringIO()
+        self.assertEquals([('foo', 'bar', 'bla', False, False)],
+                list(read_testlist(inf, outf)))
+        self.assertEquals("MORENOISE\nNOISE\n", outf.getvalue())