diff options
author | Guido Günther <agx@sigxcpu.org> | 2015-01-25 14:37:59 +0100 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2015-01-25 15:10:47 +0100 |
commit | 59402f62fe9e46ed7b0e29d9cf6a56177a57a73d (patch) | |
tree | c4ffed30d7c0409b061544802854d611ef51e1f2 /tests/testutils | |
parent | 0104bae2b38fa14dd82feae22927f141dbcc0a17 (diff) |
testutils: Move log test helpers to separate class
so we can use them in the unit tests as well
Diffstat (limited to 'tests/testutils')
-rw-r--r-- | tests/testutils/__init__.py | 2 | ||||
-rw-r--r-- | tests/testutils/gbplogtester.py | 52 |
2 files changed, 54 insertions, 0 deletions
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() |