Add support for passing mime-type on the command-line.
authorThomi Richards <thomi.richards@canonical.com>
Mon, 18 Nov 2013 22:16:41 +0000 (11:16 +1300)
committerThomi Richards <thomi.richards@canonical.com>
Mon, 18 Nov 2013 22:16:41 +0000 (11:16 +1300)
python/subunit/_output.py
python/subunit/tests/test_output_filter.py

index b4df54cab1c5241eafbeb068c7a1ead00e8c5d64..4bd93d1aa00504ab0d7898148829c2ce0a9b8198 100644 (file)
@@ -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),
index ef6dc9a2ee7c1982e084002f79b072c65bac77e4..72ede6ad15e24e93d18f2012fa96bbc2afb50d84 100644 (file)
@@ -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)