diff options
author | Tim Burke <tim.burke@gmail.com> | 2015-05-17 23:51:37 -0700 |
---|---|---|
committer | Tim Burke <tim.burke@gmail.com> | 2015-07-07 15:39:02 -0700 |
commit | cf0b6c03df5b014e8c21ffcb0fe92f58e11c7ae3 (patch) | |
tree | 6d3d02d39244773e2c23397b8310c9b95854c2c3 /tests/unit/utils.py | |
parent | 92b82777523b0858d6f20fc49ea60f7edebb2dca (diff) | |
download | python-swiftclient-cf0b6c03df5b014e8c21ffcb0fe92f58e11c7ae3.tar.gz |
Properly test raw writes in Python 3
Previously we were trying to test writing bytes in Python 3 using only
native (unicode) string objects. That doesn't test what we thought we
were testing.
Change-Id: I10a0a38143d7f7d850ab9a7005ad87f5d314c375
Diffstat (limited to 'tests/unit/utils.py')
-rw-r--r-- | tests/unit/utils.py | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/tests/unit/utils.py b/tests/unit/utils.py index 955296e..aced173 100644 --- a/tests/unit/utils.py +++ b/tests/unit/utils.py @@ -381,28 +381,27 @@ class MockHttpTest(testtools.TestCase): reload_module(c) -class CaptureStreamBuffer(object): +class CaptureStreamPrinter(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 + CaptureStreamPrinter is used for testing unicode writing for PY3. Anything + written here is encoded as utf-8 and written to the parent CaptureStream """ def __init__(self, captured_stream): self._captured_stream = captured_stream - def write(self, bytes_data): + def write(self, 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)) - ) + data if isinstance(data, six.binary_type) else data.encode('utf8')) class CaptureStream(object): def __init__(self, stream): self.stream = stream - self._capture = six.StringIO() - self._buffer = CaptureStreamBuffer(self) + self._buffer = six.BytesIO() + self._capture = CaptureStreamPrinter(self._buffer) self.streams = [self._capture] @property @@ -425,11 +424,11 @@ class CaptureStream(object): stream.writelines(*args, **kwargs) def getvalue(self): - return self._capture.getvalue() + return self._buffer.getvalue() def clear(self): - self._capture.truncate(0) - self._capture.seek(0) + self._buffer.truncate(0) + self._buffer.seek(0) class CaptureOutput(object): @@ -467,11 +466,11 @@ class CaptureOutput(object): @property def out(self): - return self._out.getvalue() + return self._out.getvalue().decode('utf8') @property def err(self): - return self._err.getvalue() + return self._err.getvalue().decode('utf8') def clear(self): self._out.clear() |