[Checkins] SVN: Sandbox/ulif/grok-testsetup/src/grok/testing.py Added BasicTestSetup in grok.testing.

Uli Fouquet uli at gnufix.de
Thu Dec 27 06:39:50 EST 2007


Log message for revision 82461:
  Added BasicTestSetup in grok.testing.

Changed:
  U   Sandbox/ulif/grok-testsetup/src/grok/testing.py

-=-
Modified: Sandbox/ulif/grok-testsetup/src/grok/testing.py
===================================================================
--- Sandbox/ulif/grok-testsetup/src/grok/testing.py	2007-12-27 11:38:38 UTC (rev 82460)
+++ Sandbox/ulif/grok-testsetup/src/grok/testing.py	2007-12-27 11:39:50 UTC (rev 82461)
@@ -17,6 +17,71 @@
 from martian import scan
 from grok import zcml
 
+from os import listdir
+import os.path
+import re
+
+class BasicTestSetup(object):
+
+    extensions = ['.rst', '.txt']
+
+    def __init__(self, package, filter_func=None, extensions=None, **kw):
+        self.package = package
+        self.filter_func = filter_func or self.isTestFile
+        self.extensions = extensions or self.extensions
+        self.additional_options = kw
+        return
+
+    def setUp(self, test):
+        pass
+
+    def tearDown(self, test):
+        pass
+
+    def fileContains(self, filename, regexp_list):
+        """Does a file contain lines matching every of the regular
+        expressions?
+        """
+        found_list = []
+        try:
+            for line in open(filename):
+                for regexp in regexp_list:
+                    if re.compile(regexp).match(line) and (
+                        regexp not in found_list):
+                        found_list.append(regexp)
+                if len(regexp_list) == len(found_list):
+                    break
+        except IOError:
+            # be gentle
+            pass
+        return len(regexp_list) == len(found_list)
+
+    def isTestFile(self, filepath):
+        """Return ``True`` if a file matches our expectations for a
+        doctest file.
+        """
+        if os.path.splitext(filepath)[1].lower() not in self.extensions:
+            return False
+        return True
+
+    def getDocTestFiles(self, dirpath=None, **kw):
+        """Find all doctest files filtered by filter_func.
+        """
+        if dirpath is None:
+            dirpath = os.path.dirname(self.package.__file__)
+        dirlist = []
+        for filename in listdir(dirpath):
+            abs_path = os.path.join(dirpath, filename)
+            if not os.path.isdir(abs_path):
+                if self.filter_func(abs_path):
+                    dirlist.append(abs_path)
+                continue
+            # Search subdirectories...
+            subdir_files = self.getDocTestFiles(dirpath=abs_path, **kw)
+            dirlist.extend(subdir_files)
+        return dirlist
+
+
 def grok(module_name):
     config = ConfigurationMachine()
     zcml.do_grok('grok.meta', config)



More information about the Checkins mailing list