diff options
author | Guido Günther <agx@sigxcpu.org> | 2015-03-27 13:15:17 +0100 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2015-03-27 13:36:03 +0100 |
commit | 4a53ad73eb4468796317bd1e44273a80b3ec9385 (patch) | |
tree | 3f51de49bfedb9937951ef37c63e1ef39e8e30ba /tests/testutils | |
parent | 430be3831596a072f67c4da84a2244da1205e9e6 (diff) |
testutils: Add a context manager to capture stderr
and use it to test the help output
Diffstat (limited to 'tests/testutils')
-rw-r--r-- | tests/testutils/__init__.py | 3 | ||||
-rw-r--r-- | tests/testutils/capture.py | 29 |
2 files changed, 31 insertions, 1 deletions
diff --git a/tests/testutils/__init__.py b/tests/testutils/__init__.py index c94af648..092d377c 100644 --- a/tests/testutils/__init__.py +++ b/tests/testutils/__init__.py @@ -13,9 +13,10 @@ from gbp.deb.changelog import ChangeLog from . gbplogtester import GbpLogTester from . debiangittestrepo import DebianGitTestRepo +from . capture import capture_stderr __all__ = ['GbpLogTester', 'DebianGitTestRepo', 'OsReleaseFile', - 'MockedChangeLog', 'get_dch_default_urgency' ] + 'MockedChangeLog', 'get_dch_default_urgency', 'capture_stderr' ] class OsReleaseFile(object): """Repesents a simple file with key-value pairs""" diff --git a/tests/testutils/capture.py b/tests/testutils/capture.py new file mode 100644 index 00000000..60d5a12e --- /dev/null +++ b/tests/testutils/capture.py @@ -0,0 +1,29 @@ +# vim: set fileencoding=utf-8 : + +import sys +from contextlib import contextmanager +from StringIO import StringIO + + +class StderrCapture(StringIO): + def save(self): + self.safed = sys.stderr + sys.stderr = self + + def restore(self): + if self.safed is not None: + sys.stderr = self.safed + self.safed = None + + def output(self): + self.seek(0) + return self.read() + + +@contextmanager +def capture_stderr(): + """Capture an output and return it's content""" + c = StderrCapture() + c.save() + yield c + c.restore() |