summaryrefslogtreecommitdiff
path: root/tests/unit/utils.py
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2015-02-03 18:55:47 +0000
committerGerrit Code Review <review@openstack.org>2015-02-03 18:55:47 +0000
commiteb28d8aff6d6b6e985e76643bee7dbf729055c82 (patch)
treee8d2a07545e676cb5d916dfe0b217084fe57139b /tests/unit/utils.py
parent21473f1bc475fa69aa9d1cdd6b60cc827c4f7f1b (diff)
parentbd42c2b00d0e4a18d15fd494bd9b9101742c4a37 (diff)
downloadpython-swiftclient-eb28d8aff6d6b6e985e76643bee7dbf729055c82.tar.gz
Merge "This patch fixes downloading files to stdout."
Diffstat (limited to 'tests/unit/utils.py')
-rw-r--r--tests/unit/utils.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/unit/utils.py b/tests/unit/utils.py
index 201a8a8..2467ca6 100644
--- a/tests/unit/utils.py
+++ b/tests/unit/utils.py
@@ -370,13 +370,41 @@ class MockHttpTest(testtools.TestCase):
reload_module(c)
+class CaptureStreamBuffer(object):
+ """
+ CaptureStreamBuffer is used for testing raw byte writing for PY3. Anything
+ written here is decoded as utf-8 and written to the parent CaptureStream
+ """
+ def __init__(self, captured_stream):
+ self._captured_stream = captured_stream
+
+ def write(self, bytes_data):
+ # No encoding, just convert the raw bytes into a str for testing
+ # The below call also validates that we have a byte string.
+ self._captured_stream.write(
+ ''.join(map(chr, bytes_data))
+ )
+
+
class CaptureStream(object):
def __init__(self, stream):
self.stream = stream
self._capture = six.StringIO()
+ self._buffer = CaptureStreamBuffer(self)
self.streams = [self.stream, self._capture]
+ @property
+ def buffer(self):
+ if six.PY3:
+ return self._buffer
+ else:
+ raise AttributeError(
+ 'Output stream has no attribute "buffer" in Python2')
+
+ def flush(self):
+ pass
+
def write(self, *args, **kwargs):
for stream in self.streams:
stream.write(*args, **kwargs)