Python 3 compatibility fixes.
authorThomi Richards <thomi.richards@canonical.com>
Tue, 19 Nov 2013 02:28:28 +0000 (15:28 +1300)
committerThomi Richards <thomi.richards@canonical.com>
Tue, 19 Nov 2013 02:28:28 +0000 (15:28 +1300)
python/subunit/_output.py
python/subunit/tests/test_output_filter.py

index b3ab675d25daf2e5be5748d805978600a453ce74..dd81b87b9bb83d0df053fff1fdaa95b12aa1a14b 100644 (file)
@@ -17,7 +17,7 @@ from argparse import ArgumentParser
 import datetime
 from functools import partial
 from sys import stdout
-from string import split
+from testtools.compat import _b
 
 from subunit.v2 import StreamResultToBytes
 
@@ -55,7 +55,7 @@ def parse_arguments(args=None, ParserClass=ArgumentParser):
     )
     common_args.add_argument(
         "--attach-file",
-        type=file,
+        type=open,
         help="Attach a file to the result stream for this test."
     )
     common_args.add_argument(
@@ -68,7 +68,7 @@ def parse_arguments(args=None, ParserClass=ArgumentParser):
     common_args.add_argument(
         "--tags",
         help="A comma-separated list of tags to associate with this test.",
-        type=partial(split, sep=','),
+        type=lambda s: s.split(','),
         default=None
     )
     sub_parsers = parser.add_subparsers(
@@ -165,7 +165,7 @@ def generate_bytestream(args, output_writer):
 def write_chunked_file(file_obj, test_id, output_writer, chunk_size=1024,
                        mime_type=None):
     reader = partial(file_obj.read, chunk_size)
-    for chunk in iter(reader, ''):
+    for chunk in iter(reader, _b('')):
         output_writer.status(
             test_id=test_id,
             file_name=file_obj.name,
@@ -176,7 +176,7 @@ def write_chunked_file(file_obj, test_id, output_writer, chunk_size=1024,
     output_writer.status(
         test_id=test_id,
         file_name=file_obj.name,
-        file_bytes='',
+        file_bytes=_b(''),
         mime_type=mime_type,
         eof=True,
     )
index fac47ffcc53d595a4d462d1e6ecf179a8c33f863..be42ea65a7769ee1f5a9a02bb09593423aac4be8 100644 (file)
@@ -21,6 +21,7 @@ from functools import partial
 from io import BytesIO
 from tempfile import NamedTemporaryFile
 from testtools import TestCase
+from testtools.compat import _b
 from testtools.matchers import (
     Equals,
     IsInstance,
@@ -102,7 +103,6 @@ class OutputFilterArgumentParserTests(TestCase):
                 args = safe_parse_arguments(
                     args=[command, 'foo', '--attach-file', tmp_file.name]
                 )
-                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):
@@ -298,26 +298,26 @@ class FileChunkingTests(TestCase):
         return result
 
     def test_file_chunk_size_is_honored(self):
-        result = self._write_chunk_file("Hello", 1)
+        result = self._write_chunk_file(_b("Hello"), 1)
         self.assertThat(
             result._events,
             MatchesListwise([
-                MatchesCall(call='status', file_bytes='H', mime_type=None, eof=False),
-                MatchesCall(call='status', file_bytes='e', mime_type=None, eof=False),
-                MatchesCall(call='status', file_bytes='l', mime_type=None, eof=False),
-                MatchesCall(call='status', file_bytes='l', mime_type=None, eof=False),
-                MatchesCall(call='status', file_bytes='o', mime_type=None, eof=False),
-                MatchesCall(call='status', file_bytes='', mime_type=None, eof=True),
+                MatchesCall(call='status', file_bytes=_b('H'), mime_type=None, eof=False),
+                MatchesCall(call='status', file_bytes=_b('e'), mime_type=None, eof=False),
+                MatchesCall(call='status', file_bytes=_b('l'), mime_type=None, eof=False),
+                MatchesCall(call='status', file_bytes=_b('l'), mime_type=None, eof=False),
+                MatchesCall(call='status', file_bytes=_b('o'), mime_type=None, eof=False),
+                MatchesCall(call='status', file_bytes=_b(''), mime_type=None, eof=True),
             ])
         )
 
     def test_file_mimetype_is_honored(self):
-        result = self._write_chunk_file("SomeData", 1024, "text/plain")
+        result = self._write_chunk_file(_b("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"),
+                MatchesCall(call='status', file_bytes=_b('SomeData'), mime_type="text/plain"),
+                MatchesCall(call='status', file_bytes=_b(''), mime_type="text/plain"),
             ])
         )
 
@@ -339,10 +339,10 @@ class MatchesCall(Matcher):
         }
 
     def __init__(self, **kwargs):
-        unknown_kwargs = filter(
+        unknown_kwargs = list(filter(
             lambda k: k not in self._position_lookup,
             kwargs
-        )
+        ))
         if unknown_kwargs:
             raise ValueError("Unknown keywords: %s" % ','.join(unknown_kwargs))
         self._filters = kwargs