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 | |
parent | 430be3831596a072f67c4da84a2244da1205e9e6 (diff) |
testutils: Add a context manager to capture stderr
and use it to test the help output
-rw-r--r-- | tests/11_test_dch_main.py | 6 | ||||
-rw-r--r-- | tests/testutils/__init__.py | 3 | ||||
-rw-r--r-- | tests/testutils/capture.py | 29 |
3 files changed, 35 insertions, 3 deletions
diff --git a/tests/11_test_dch_main.py b/tests/11_test_dch_main.py index 4f271310..538d2e84 100644 --- a/tests/11_test_dch_main.py +++ b/tests/11_test_dch_main.py @@ -4,7 +4,7 @@ from . import context from .testutils import (DebianGitTestRepo, OsReleaseFile, - get_dch_default_urgency) + get_dch_default_urgency, capture_stderr) from gbp.scripts import dch @@ -191,7 +191,9 @@ class TestScriptDch(DebianGitTestRepo): def test_dch_main_new_upstream_version_with_snapshot_release(self): """Test dch.py like gbp dch script does: new upstream version - snapshot - release""" options = ["--snapshot", "--release"] - self.assertRaises(SystemExit, self.run_dch, options) + with capture_stderr() as c: + self.assertRaises(SystemExit, self.run_dch, options) + self.assertTrue("'--snapshot' and '--release' are incompatible options" in c.output()) def test_dch_main_new_upstream_version_with_distribution(self): 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() |