Skip to content

Commit 0a8f411

Browse files
committed
fixup
1 parent 1b3b154 commit 0a8f411

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

mig/shared/fileio.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,6 @@
7474
exit(1)
7575

7676

77-
def _auto_adjust_mode(data, mode):
78-
"""Select suitable file open mode based on string type of data. I.e. whether
79-
to use binary or text mode depending on data in bytes or unicode format.
80-
"""
81-
82-
# NOTE: detect byte/unicode writes and handle explicitly in a portable way
83-
if isinstance(data, bytes):
84-
if 'b' not in mode:
85-
mode = "%sb" % mode # appended to avoid mode ordering error on PY2
86-
else:
87-
if 'b' in mode:
88-
mode = mode.strip('b')
89-
return mode
90-
91-
9277
def _is_string(value):
9378
return isinstance(value, unicode) if PY2 else isinstance(value, str)
9479

tests/test_mig_shared_fileio.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,16 @@
5252
assert isinstance(DUMMY_BYTES, bytes)
5353

5454

55-
def as_unicode_string(value):
55+
def _as_unicode_string(value):
5656
assert isinstance(value, bytearray)
5757
return unicode(codecs.decode(value, 'utf8')) if PY2 else str(value, 'utf8')
5858

5959

60-
class TextFile:
60+
class _ByteText:
61+
"""File-like object that allows interacting with a text file as a bytearray.
62+
63+
Supports reading and directly usable as a context manager."""
64+
6165
def __init__(self, path, mode='r'):
6266
self._file = None
6367
self._path = path
@@ -79,7 +83,8 @@ def __exit__(self, *args):
7983

8084

8185
class MigSharedFileio__write_chunk(MigTestCase):
82-
# TODO: Add docstrings to this class and its methods
86+
"""Coverage of the write_chunk() function."""
87+
8388
def setUp(self):
8489
super(MigSharedFileio__write_chunk, self).setUp()
8590
self.tmp_path = temppath(DUMMY_FILE_WRITECHUNK, self, skip_clean=True)
@@ -126,7 +131,6 @@ def test_store_bytes_at_offset(self):
126131
"expected a hole was left")
127132
self.assertEqual(content[3:], DUMMY_BYTES)
128133

129-
@unittest.skip("TODO: enable again - requires the temporarily disabled auto mode select")
130134
def test_store_bytes_in_text_mode(self):
131135
fileio.write_chunk(self.tmp_path, DUMMY_BYTES, 0, self.logger,
132136
mode="r+")
@@ -141,7 +145,7 @@ def test_store_unicode(self):
141145
mode='r+')
142146
self.assertTrue(did_succeed)
143147

144-
with TextFile(self.tmp_path) as file:
148+
with _ByteText(self.tmp_path) as file:
145149
content = file.read(1024)
146150
self.assertEqual(len(content), DUMMY_UNICODE_BYTES_LENGTH)
147151
self.assertEqual(content[:], DUMMY_UNICODE_BYTES)
@@ -150,13 +154,15 @@ def test_store_unicode_in_binary_mode(self):
150154
fileio.write_chunk(self.tmp_path, DUMMY_UNICODE, 0, self.logger,
151155
mode='r+b')
152156

153-
with TextFile(self.tmp_path) as file:
157+
with _ByteText(self.tmp_path) as file:
154158
content = file.read(1024)
155159
self.assertEqual(len(content), DUMMY_UNICODE_BYTES_LENGTH)
156-
self.assertEqual(as_unicode_string(content[:]), DUMMY_UNICODE)
160+
self.assertEqual(_as_unicode_string(content[:]), DUMMY_UNICODE)
157161

158162

159163
class MigSharedFileio__write_file(MigTestCase):
164+
"""Coverage of the write_file() function."""
165+
160166
def setUp(self):
161167
super(MigSharedFileio__write_file, self).setUp()
162168
self.tmp_path = temppath(DUMMY_FILE_WRITEFILE, self, skip_clean=True)
@@ -196,7 +202,6 @@ def test_store_bytes(self):
196202
self.assertEqual(len(content), DUMMY_BYTES_LENGTH)
197203
self.assertEqual(content[:], DUMMY_BYTES)
198204

199-
@unittest.skip("TODO: enable again - requires the temporarily disabled auto mode select")
200205
def test_store_bytes_in_text_mode(self):
201206
did_succeed = fileio.write_file(DUMMY_BYTES, self.tmp_path, self.logger,
202207
mode="w")
@@ -207,27 +212,25 @@ def test_store_bytes_in_text_mode(self):
207212
self.assertEqual(len(content), DUMMY_BYTES_LENGTH)
208213
self.assertEqual(content[:], DUMMY_BYTES)
209214

210-
@unittest.skip("TODO: enable again - requires the temporarily disabled auto mode select")
211215
def test_store_unicode(self):
212216
did_succeed = fileio.write_file(DUMMY_UNICODE, self.tmp_path,
213217
self.logger, mode='w')
214218
self.assertTrue(did_succeed)
215219

216-
with open(self.tmp_path, 'r') as file:
220+
with _ByteText(self.tmp_path, 'r') as file:
217221
content = file.read(1024)
218-
self.assertEqual(len(content), DUMMY_UNICODE_LENGTH)
219-
self.assertEqual(content[:], DUMMY_UNICODE)
222+
self.assertEqual(len(content), DUMMY_UNICODE_BYTES_LENGTH)
223+
self.assertEqual(content[:], DUMMY_UNICODE_BYTES)
220224

221-
@unittest.skip("TODO: enable again - requires the temporarily disabled auto mode select")
222225
def test_store_unicode_in_binary_mode(self):
223226
did_succeed = fileio.write_file(DUMMY_UNICODE, self.tmp_path,
224227
self.logger, mode='wb')
225228
self.assertTrue(did_succeed)
226229

227-
with open(self.tmp_path, 'r') as file:
230+
with _ByteText(self.tmp_path, 'r') as file:
228231
content = file.read(1024)
229-
self.assertEqual(len(content), DUMMY_UNICODE_LENGTH)
230-
self.assertEqual(content[:], DUMMY_UNICODE)
232+
self.assertEqual(len(content), DUMMY_UNICODE_BYTES_LENGTH)
233+
self.assertEqual(content[:], DUMMY_UNICODE_BYTES)
231234

232235

233236
if __name__ == '__main__':

0 commit comments

Comments
 (0)