diff options
author | Thomi Richards <thomi.richards@canonical.com> | 2013-11-19 11:16:41 +1300 |
---|---|---|
committer | Thomi Richards <thomi.richards@canonical.com> | 2013-11-19 11:16:41 +1300 |
commit | 66ed68e1d151baf71e63d11a44ce773b6f40b427 (patch) | |
tree | 91fb4e9a4cadcccf0dd0478deabe80da47e9075f | |
parent | 56063023c343ee0f25be349edab11eecbbc22e5a (diff) | |
download | subunit-66ed68e1d151baf71e63d11a44ce773b6f40b427.tar.gz |
Add support for passing mime-type on the command-line.
-rw-r--r-- | python/subunit/_output.py | 14 | ||||
-rw-r--r-- | python/subunit/tests/test_output_filter.py | 27 |
2 files changed, 36 insertions, 5 deletions
diff --git a/python/subunit/_output.py b/python/subunit/_output.py index b4df54c..4bd93d1 100644 --- a/python/subunit/_output.py +++ b/python/subunit/_output.py @@ -52,6 +52,13 @@ def parse_arguments(args=None, ParserClass=ArgumentParser): type=file, help="Attach a file to the result stream for this test." ) + common_args.add_argument( + "--mimetype", + help="The mime type to send with this file. This is only used if the "\ + "--attach-file argument is used. This argument is optional. If it is "\ + "not specified, the file will be sent wihtout a mime type.", + default=None + ) sub_parsers = parser.add_subparsers(dest="action") final_state = "This is a final action: No more actions may be generated " \ @@ -108,7 +115,12 @@ def get_output_stream_writer(): def generate_bytestream(args, output_writer): output_writer.startTestRun() if args.attach_file: - write_chunked_file(args.attach_file, args.test_id, output_writer) + write_chunked_file( + args.attach_file, + args.test_id, + output_writer, + args.mimetype, + ) output_writer.status( test_id=args.test_id, test_status=translate_command_name(args.action), diff --git a/python/subunit/tests/test_output_filter.py b/python/subunit/tests/test_output_filter.py index ef6dc9a..72ede6a 100644 --- a/python/subunit/tests/test_output_filter.py +++ b/python/subunit/tests/test_output_filter.py @@ -83,6 +83,13 @@ class OutputFilterArgumentTests(TestCase): self.assertThat(args.attach_file, IsInstance(file)) self.assertThat(args.attach_file.name, Equals(tmp_file.name)) + def test_all_commands_accept_mimetype_argument(self): + for command in self._all_supported_commands: + args = safe_parse_arguments( + args=[command, 'foo', '--mimetype', "text/plain"] + ) + self.assertThat(args.mimetype, Equals("text/plain")) + class ByteStreamCompatibilityTests(TestCase): @@ -193,7 +200,7 @@ class ByteStreamCompatibilityTests(TestCase): class FileChunkingTests(TestCase): - def _write_chunk_file(self, file_data, chunk_size): + def _write_chunk_file(self, file_data, chunk_size, mimetype=None): """Write chunked data to a subunit stream, return a StreamResult object.""" stream = BytesIO() output_writer = StreamResultToBytes(output_stream=stream) @@ -202,7 +209,7 @@ class FileChunkingTests(TestCase): f.write(file_data) f.seek(0) - write_chunked_file(f, 'foo_test', output_writer, chunk_size) + write_chunked_file(f, 'foo_test', output_writer, chunk_size, mimetype) stream.seek(0) @@ -225,6 +232,17 @@ class FileChunkingTests(TestCase): ]) ) + def test_file_mimetype_is_honored(self): + result = self._write_chunk_file("SomeData", 1024, "text/plain") + self.assertThat( + result._events, + MatchesListwise([ + MatchesCall(call='status', file_bytes='SomeData', mime_type="text/plain"), + MatchesCall(call='status', file_bytes='', mime_type="text/plain"), + ]) + ) + + class MatchesCall(Matcher): _position_lookup = { @@ -253,8 +271,9 @@ class MatchesCall(Matcher): def match(self, call_tuple): for k,v in self._filters.items(): try: - if call_tuple[self._position_lookup[k]] != v: - return Mismatch("Value for key is %r, not %r" % (self._position_lookup[k], v)) + pos = self._position_lookup[k] + if call_tuple[pos] != v: + return Mismatch("Value for key is %r, not %r" % (call_tuple[pos], v)) except IndexError: return Mismatch("Key %s is not present." % k) |