diff options
| author | Thomi Richards <thomi.richards@canonical.com> | 2013-11-19 09:55:44 +1300 |
|---|---|---|
| committer | Thomi Richards <thomi.richards@canonical.com> | 2013-11-19 09:55:44 +1300 |
| commit | 2b4a6de5804fb0b4cc207d384f8d6aac9f0c2a67 (patch) | |
| tree | 1811fc885b93182c3235bd60400f5f3e1a2ed429 /python | |
| parent | f9b9c8ccebc2f7a9a42caabbfb11a81db02cfc99 (diff) | |
| download | subunit-git-2b4a6de5804fb0b4cc207d384f8d6aac9f0c2a67.tar.gz | |
Allow customisation of argument parser class used, so we can write failing tests for command line arguments not yet supported. Have failing test for attaching files.
Diffstat (limited to 'python')
| -rw-r--r-- | python/subunit/_output.py | 15 | ||||
| -rw-r--r-- | python/subunit/tests/test_output_filter.py | 48 |
2 files changed, 39 insertions, 24 deletions
diff --git a/python/subunit/_output.py b/python/subunit/_output.py index 4889e6f..b3a5bba 100644 --- a/python/subunit/_output.py +++ b/python/subunit/_output.py @@ -27,20 +27,25 @@ def output_main(): return 0 -def parse_arguments(args=None): +def parse_arguments(args=None, ParserClass=ArgumentParser): """Parse arguments from the command line. If specified, args must be a list of strings, similar to sys.argv[1:]. + ParserClass can be specified to override the class we use to parse the + command-line arguments. This is useful for testing. + """ - parser = ArgumentParser( + parser = ParserClass( prog='subunit-output', description="A tool to generate a subunit result byte-stream", ) - common_args = ArgumentParser(add_help=False) - common_args.add_argument("test_id", help="""A string that uniquely - identifies this test.""") + common_args = ParserClass(add_help=False) + common_args.add_argument( + "test_id", + help="A string that uniquely identifies this test." + ) sub_parsers = parser.add_subparsers(dest="action") final_state = "This is a final action: No more actions may be generated " \ diff --git a/python/subunit/tests/test_output_filter.py b/python/subunit/tests/test_output_filter.py index 4031449..c9059df 100644 --- a/python/subunit/tests/test_output_filter.py +++ b/python/subunit/tests/test_output_filter.py @@ -15,10 +15,11 @@ # -from io import BytesIO - +import argparse from collections import namedtuple import datetime +from functools import partial +from io import BytesIO from testtools import TestCase from testtools.matchers import ( Equals, @@ -36,37 +37,46 @@ from subunit._output import ( ) import subunit._output as _o + +class SafeArgumentParser(argparse.ArgumentParser): + + def exit(self, status=0, message=""): + raise RuntimeError("ArgumentParser requested to exit with status "\ + " %d and message %r" % (status, message)) + + +safe_parse_arguments = partial(parse_arguments, ParserClass=SafeArgumentParser) + + class OutputFilterArgumentTests(TestCase): """Tests for the command line argument parser.""" + _all_supported_commands = ('start', 'pass', 'fail', 'skip', 'exists') + def _test_command(self, command, test_id): - args = parse_arguments(args=[command, test_id]) + args = safe_parse_arguments(args=[command, test_id]) self.assertThat(args.action, Equals(command)) self.assertThat(args.test_id, Equals(test_id)) - def test_can_parse_start_test(self): - self._test_command('start', self.getUniqueString()) - - def test_can_parse_pass_test(self): - self._test_command('pass', self.getUniqueString()) - - def test_can_parse_fail_test(self): - self._test_command('fail', self.getUniqueString()) - - def test_can_parse_skip_test(self): - self._test_command('skip', self.getUniqueString()) - - def test_can_parse_exists(self): - self._test_command('exists', self.getUniqueString()) + def test_can_parse_all_commands_with_test_id(self): + for command in self._all_supported_commands: + self._test_command(command, self.getUniqueString()) def test_command_translation(self): self.assertThat(translate_command_name('start'), Equals('inprogress')) self.assertThat(translate_command_name('pass'), Equals('success')) - for command in ('fail', 'skip'): + for command in ('fail', 'skip', 'exists'): self.assertThat(translate_command_name(command), Equals(command)) + def test_all_commands_parse_file_attachment(self): + for command in self._all_supported_commands: + args = safe_parse_arguments( + args=[command, 'foo', '--attach-file', '/some/path'] + ) + self.assertThat(args.attach_file, Equals('/some/path')) + class ByteStreamCompatibilityTests(TestCase): @@ -88,7 +98,7 @@ class ByteStreamCompatibilityTests(TestCase): stream = BytesIO() for command_list in commands: - args = parse_arguments(command_list) + args = safe_parse_arguments(command_list) output_writer = StreamResultToBytes(output_stream=stream) generate_bytestream(args, output_writer) |
