From 59402f62fe9e46ed7b0e29d9cf6a56177a57a73d Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Sun, 25 Jan 2015 14:37:59 +0100 Subject: testutils: Move log test helpers to separate class so we can use them in the unit tests as well --- tests/component/__init__.py | 42 +++------------------------------ tests/testutils/__init__.py | 2 ++ tests/testutils/gbplogtester.py | 52 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 39 deletions(-) create mode 100644 tests/testutils/gbplogtester.py diff --git a/tests/component/__init__.py b/tests/component/__init__.py index cfa04223..f6b1224f 100644 --- a/tests/component/__init__.py +++ b/tests/component/__init__.py @@ -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() diff --git a/tests/testutils/__init__.py b/tests/testutils/__init__.py index 2d389a5f..2a13218a 100644 --- a/tests/testutils/__init__.py +++ b/tests/testutils/__init__.py @@ -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 index 00000000..20b522d6 --- /dev/null +++ b/tests/testutils/gbplogtester.py @@ -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() -- cgit v1.2.3