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 " \
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),
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):
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)
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)
])
)
+ 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 = {
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)