testutils: Move log test helpers to separate class
authorGuido Günther <agx@sigxcpu.org>
Sun, 25 Jan 2015 13:37:59 +0000 (14:37 +0100)
committerGuido Günther <agx@sigxcpu.org>
Sun, 25 Jan 2015 14:10:47 +0000 (15:10 +0100)
so we can use them in the unit tests as well

tests/component/__init__.py
tests/testutils/__init__.py
tests/testutils/gbplogtester.py [new file with mode: 0644]

index cfa042235f3decaf13533f0a960becf453f50896..f6b1224fcd574bce535f3dec2d7123a8e8bd67ad 100644 (file)
@@ -27,6 +27,7 @@ import tempfile
 from StringIO import StringIO
 from nose import SkipTest
 from nose.tools import eq_, ok_     # pylint: disable=E0611
+from .. testutils import GbpLogTester
 
 import gbp.log
 from  gbp.git import GitRepository, GitRepositoryError
@@ -76,7 +77,7 @@ class ComponentTestGitRepository(GitRepository):
                            "Consider doing 'git submodule update'" % __name__)
 
 
-class ComponentTestBase(object):
+class ComponentTestBase(GbpLogTester):
     """Base class for testing cmdline tools of git-buildpackage"""
 
     @classmethod
@@ -103,8 +104,7 @@ class ComponentTestBase(object):
         """Object initialization"""
         self._orig_dir = None
         self._tmpdir = None
-        self._log = None
-        self._loghandler = None
+        GbpLogTester.__init__(self)
 
     def setup(self):
         """Test case setup"""
@@ -163,39 +163,3 @@ class ComponentTestBase(object):
                 cls.check_files(files, local_f)
             if dirs is not None:
                 cls.check_files(dirs, local_d)
-
-    def _capture_log(self, capture=True):
-        """ Capture log"""
-        if capture and self._log is None:
-            self._log = StringIO()
-            self._loghandler = gbp.log.GbpStreamHandler(self._log, False)
-            self._loghandler.addFilter(gbp.log.GbpFilter([gbp.log.WARNING,
-                                                          gbp.log.ERROR]))
-            for hdl in gbp.log.LOGGER.handlers:
-                gbp.log.LOGGER.removeHandler(hdl)
-            gbp.log.LOGGER.addHandler(self._loghandler)
-        elif self._log is not None:
-            gbp.log.LOGGER.removeHandler(self._loghandler)
-            self._loghandler.close()
-            self._loghandler = None
-            self._log.close()
-            self._log = None
-
-    def _get_log(self):
-        """Get the captured log output"""
-        self._log.seek(0)
-        return self._log.readlines()
-
-    def _check_log(self, linenum, regex):
-        """Check that the specified line on log matches expectations"""
-        if self._log is None:
-            raise Exception("BUG in unittests: no log captured!")
-        output = self._get_log()[linenum].strip()
-        ok_(re.match(regex, output),
-            "Log entry '%s' doesn't match '%s'" % (output, regex))
-
-    def _clear_log(self):
-        """Clear the mock strerr"""
-        if self._log is not None:
-            self._log.seek(0)
-            self._log.truncate()
index 2d389a5ff9caa44941f8ec761d491f7f590b8e7a..2a13218a9dc52321da6eec8d818c6ee523fcaf49 100644 (file)
@@ -13,6 +13,8 @@ import gbp.deb.git
 import gbp.errors
 from gbp.deb.changelog import ChangeLog
 
+from gbplogtester import GbpLogTester
+
 class DebianGitTestRepo(unittest.TestCase):
     """Scratch repo for a single unit test"""
 
diff --git a/tests/testutils/gbplogtester.py b/tests/testutils/gbplogtester.py
new file mode 100644 (file)
index 0000000..20b522d
--- /dev/null
@@ -0,0 +1,52 @@
+# vim: set fileencoding=utf-8 :
+
+import re
+from StringIO import StringIO
+from nose.tools import eq_, ok_     # pylint: disable=E0611
+
+import gbp.log
+
+class GbpLogTester(object):
+    """
+    Helper class for tests that need to capture logging output
+    """
+    def __init__(self):
+        """Object initialization"""
+        self._log = None
+        self._loghandler = None
+
+    def _capture_log(self, capture=True):
+        """ Capture log"""
+        if capture and self._log is None:
+            self._log = StringIO()
+            self._loghandler = gbp.log.GbpStreamHandler(self._log, False)
+            self._loghandler.addFilter(gbp.log.GbpFilter([gbp.log.WARNING,
+                                                          gbp.log.ERROR]))
+            for hdl in gbp.log.LOGGER.handlers:
+                gbp.log.LOGGER.removeHandler(hdl)
+            gbp.log.LOGGER.addHandler(self._loghandler)
+        elif self._log is not None:
+            gbp.log.LOGGER.removeHandler(self._loghandler)
+            self._loghandler.close()
+            self._loghandler = None
+            self._log.close()
+            self._log = None
+
+    def _get_log(self):
+        """Get the captured log output"""
+        self._log.seek(0)
+        return self._log.readlines()
+
+    def _check_log(self, linenum, regex):
+        """Check that the specified line on log matches expectations"""
+        if self._log is None:
+            raise Exception("BUG in unittests: no log captured!")
+        output = self._get_log()[linenum].strip()
+        ok_(re.match(regex, output),
+            "Log entry '%s' doesn't match '%s'" % (output, regex))
+
+    def _clear_log(self):
+        """Clear the mock strerr"""
+        if self._log is not None:
+            self._log.seek(0)
+            self._log.truncate()