summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomi Richards <thomi.richards@canonical.com>2013-11-25 12:17:11 +1300
committerThomi Richards <thomi.richards@canonical.com>2013-11-25 12:17:11 +1300
commitbc9d2e6733469d0482a6fdd56019b862514ba396 (patch)
treecae7897ac8ad8cbe3556040368e280368bea8760
parent9e919f3695634681d2ac59e533ff321ba8c5815e (diff)
downloadsubunit-bc9d2e6733469d0482a6fdd56019b862514ba396.tar.gz
Add a few more tests for error cases in option parser.
-rw-r--r--python/subunit/_output.py7
-rw-r--r--python/subunit/tests/test_output_filter.py39
2 files changed, 35 insertions, 11 deletions
diff --git a/python/subunit/_output.py b/python/subunit/_output.py
index 6f111cc..bdea14f 100644
--- a/python/subunit/_output.py
+++ b/python/subunit/_output.py
@@ -43,8 +43,9 @@ def parse_arguments(args=None, ParserClass=OptionParser):
command-line arguments. This is useful for testing.
"""
parser = ParserClass(
- prog='subunit-output',
+ prog="subunit-output",
description="A tool to generate a subunit v2 result byte-stream",
+ usage="subunit-output [-h] [status test_id] [options]",
)
parser.set_default('tags', None)
@@ -131,6 +132,8 @@ def parse_arguments(args=None, ParserClass=OptionParser):
parser.error("Cannot open %s (%s)" % (options.attach_file, e.strerror))
if options.tags and not options.action:
parser.error("Cannot specify --tags without a status command")
+ if not (options.attach_file or options.action):
+ parser.error("Must specify either --attach-file or a status command")
return options
@@ -139,6 +142,8 @@ def status_action(option, opt_str, value, parser, status_name):
if getattr(parser.values, "action", None) is not None:
raise OptionValueError("argument %s: Only one status may be specified at once." % option)
+ if len(parser.rargs) == 0:
+ raise OptionValueError("argument %s: must specify a single TEST_ID.")
parser.values.action = status_name
parser.values.test_id = parser.rargs.pop(0)
diff --git a/python/subunit/tests/test_output_filter.py b/python/subunit/tests/test_output_filter.py
index 658174c..21d8172 100644
--- a/python/subunit/tests/test_output_filter.py
+++ b/python/subunit/tests/test_output_filter.py
@@ -52,7 +52,19 @@ class SafeOptionParser(optparse.OptionParser):
safe_parse_arguments = partial(parse_arguments, ParserClass=SafeOptionParser)
-class TestStatusArgParserTests(WithScenarios, TestCase):
+class TestCaseWithPatchedStderr(TestCase):
+
+ def setUp(self):
+ super(TestCaseWithPatchedStderr, self).setUp()
+ # prevent OptionParser from printing to stderr:
+ if sys.version[0] > '2':
+ self._stderr = StringIO()
+ else:
+ self._stderr = BytesIO()
+ self.patch(optparse.sys, 'stderr', self._stderr)
+
+
+class TestStatusArgParserTests(WithScenarios, TestCaseWithPatchedStderr):
scenarios = [
(cmd, dict(command=cmd, option='--' + cmd)) for cmd in (
@@ -122,17 +134,16 @@ class TestStatusArgParserTests(WithScenarios, TestCase):
self.assertThat(args.file_name, Equals("foo"))
+ def test_requires_test_id(self):
+ fn = lambda: safe_parse_arguments(args=[self.option])
+ self.assertThat(
+ fn,
+ raises(RuntimeError('subunit-output: error: argument %s: must '
+ 'specify a single TEST_ID.\n'))
+ )
-class ArgParserTests(TestCase):
- def setUp(self):
- super(ArgParserTests, self).setUp()
- # prevent OptionParser from printing to stderr:
- if sys.version[0] > '2':
- self._stderr = StringIO()
- else:
- self._stderr = BytesIO()
- self.patch(optparse.sys, 'stderr', self._stderr)
+class ArgParserTests(TestCaseWithPatchedStderr):
def test_can_parse_attach_file_without_test_id(self):
with NamedTemporaryFile() as tmp_file:
@@ -141,6 +152,14 @@ class ArgParserTests(TestCase):
)
self.assertThat(args.attach_file.name, Equals(tmp_file.name))
+ def test_must_specify_argument(self):
+ fn = lambda: safe_parse_arguments([])
+ self.assertThat(
+ fn,
+ raises(RuntimeError('subunit-output: error: Must specify either '
+ '--attach-file or a status command\n'))
+ )
+
def test_cannot_specify_more_than_one_status_command(self):
fn = lambda: safe_parse_arguments(['--fail', 'foo', '--skip', 'bar'])
self.assertThat(