aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2016-12-26 20:15:17 +0100
committerGuido Günther <agx@sigxcpu.org>2016-12-26 20:15:17 +0100
commit9e44a205fbec7fd83ff4c41fa17a7e4c049ef1ba (patch)
treebefe94e0de99d3994ee0c9b06ffa2862df830e38
parent6382d56b09f88a1d9d62baf8c7969703126b6672 (diff)
test_supercommand: test --list-cmds
-rw-r--r--tests/16_test_supercommand.py15
-rw-r--r--tests/testutils/__init__.py5
-rw-r--r--tests/testutils/capture.py30
3 files changed, 41 insertions, 9 deletions
diff --git a/tests/16_test_supercommand.py b/tests/16_test_supercommand.py
index 65f4b105..d15ec797 100644
--- a/tests/16_test_supercommand.py
+++ b/tests/16_test_supercommand.py
@@ -15,10 +15,11 @@
# <http://www.gnu.org/licenses/>
"""Test L{gbp} command wrapper"""
-import sys
import unittest
import gbp.scripts.supercommand
+from tests.testutils import capture_stdout, capture_stderr
+
class TestSuperCommand(unittest.TestCase):
@@ -37,13 +38,19 @@ class TestSuperCommand(unittest.TestCase):
def test_invalid_command(self):
"""Test if we fail correctly with an invalid command"""
- old_stderr = sys.stderr
- with open('/dev/null', 'w') as sys.stderr:
+ with capture_stderr():
self.assertEqual(gbp.scripts.supercommand.supercommand(
['argv0', 'asdf']), 2)
self.assertEqual(gbp.scripts.supercommand.supercommand(
['argv0', 'asdf', '--verbose']), 2)
- sys.stderr = old_stderr
+
+ def test_list_commands(self):
+ """Invoking with --list-cmds must not raise an error"""
+ with capture_stdout() as out:
+ self.assertEqual(gbp.scripts.supercommand.supercommand(['argv0',
+ '--list-cmds']), 0)
+ for cmd in ['import-orig', 'create-remote-repo', 'pq']:
+ self.assertIn("%s - " % cmd, out.output())
def test_help_command(self):
"""Invoking with --help must not raise an error"""
diff --git a/tests/testutils/__init__.py b/tests/testutils/__init__.py
index cc5e4cb4..a4f52437 100644
--- a/tests/testutils/__init__.py
+++ b/tests/testutils/__init__.py
@@ -15,10 +15,11 @@ from gbp.deb.changelog import ChangeLog
from . gbplogtester import GbpLogTester
from . debiangittestrepo import DebianGitTestRepo
-from . capture import capture_stderr
+from . capture import capture_stdout, capture_stderr
__all__ = ['GbpLogTester', 'DebianGitTestRepo', 'OsReleaseFile',
- 'MockedChangeLog', 'get_dch_default_urgency', 'capture_stderr',
+ 'MockedChangeLog', 'get_dch_default_urgency',
+ 'capture_stderr', 'capture_stdout',
'ls_dir', 'ls_tar', 'ls_zip']
diff --git a/tests/testutils/capture.py b/tests/testutils/capture.py
index 31621f39..94aeac65 100644
--- a/tests/testutils/capture.py
+++ b/tests/testutils/capture.py
@@ -5,9 +5,9 @@ from contextlib import contextmanager
from six import StringIO
-class StderrCapture(StringIO):
+class _StderrCapture(StringIO):
def save(self):
- self.safed = sys.stderr
+ self.safed = sys.stdout
sys.stderr = self
def restore(self):
@@ -20,10 +20,34 @@ class StderrCapture(StringIO):
return self.read()
+class _StdoutCapture(StringIO):
+ def save(self):
+ self.safed = sys.stdout
+ sys.stdout = self
+
+ def restore(self):
+ if self.safed is not None:
+ sys.stdout = self.safed
+ self.safed = None
+
+ def output(self):
+ self.seek(0)
+ return self.read()
+
+
@contextmanager
def capture_stderr():
"""Capture an output and return its content"""
- c = StderrCapture()
+ c = _StderrCapture()
+ c.save()
+ yield c
+ c.restore()
+
+
+@contextmanager
+def capture_stdout():
+ """Capture an output and return its content"""
+ c = _StdoutCapture()
c.save()
yield c
c.restore()