52
52
assert isinstance (DUMMY_BYTES , bytes )
53
53
54
54
55
- def as_unicode_string (value ):
55
+ def _as_unicode_string (value ):
56
56
assert isinstance (value , bytearray )
57
57
return unicode (codecs .decode (value , 'utf8' )) if PY2 else str (value , 'utf8' )
58
58
59
59
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
+
61
65
def __init__ (self , path , mode = 'r' ):
62
66
self ._file = None
63
67
self ._path = path
@@ -79,7 +83,8 @@ def __exit__(self, *args):
79
83
80
84
81
85
class MigSharedFileio__write_chunk (MigTestCase ):
82
- # TODO: Add docstrings to this class and its methods
86
+ """Coverage of the write_chunk() function."""
87
+
83
88
def setUp (self ):
84
89
super (MigSharedFileio__write_chunk , self ).setUp ()
85
90
self .tmp_path = temppath (DUMMY_FILE_WRITECHUNK , self , skip_clean = True )
@@ -126,7 +131,6 @@ def test_store_bytes_at_offset(self):
126
131
"expected a hole was left" )
127
132
self .assertEqual (content [3 :], DUMMY_BYTES )
128
133
129
- @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
130
134
def test_store_bytes_in_text_mode (self ):
131
135
fileio .write_chunk (self .tmp_path , DUMMY_BYTES , 0 , self .logger ,
132
136
mode = "r+" )
@@ -141,7 +145,7 @@ def test_store_unicode(self):
141
145
mode = 'r+' )
142
146
self .assertTrue (did_succeed )
143
147
144
- with TextFile (self .tmp_path ) as file :
148
+ with _ByteText (self .tmp_path ) as file :
145
149
content = file .read (1024 )
146
150
self .assertEqual (len (content ), DUMMY_UNICODE_BYTES_LENGTH )
147
151
self .assertEqual (content [:], DUMMY_UNICODE_BYTES )
@@ -150,13 +154,15 @@ def test_store_unicode_in_binary_mode(self):
150
154
fileio .write_chunk (self .tmp_path , DUMMY_UNICODE , 0 , self .logger ,
151
155
mode = 'r+b' )
152
156
153
- with TextFile (self .tmp_path ) as file :
157
+ with _ByteText (self .tmp_path ) as file :
154
158
content = file .read (1024 )
155
159
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 )
157
161
158
162
159
163
class MigSharedFileio__write_file (MigTestCase ):
164
+ """Coverage of the write_file() function."""
165
+
160
166
def setUp (self ):
161
167
super (MigSharedFileio__write_file , self ).setUp ()
162
168
self .tmp_path = temppath (DUMMY_FILE_WRITEFILE , self , skip_clean = True )
@@ -196,7 +202,6 @@ def test_store_bytes(self):
196
202
self .assertEqual (len (content ), DUMMY_BYTES_LENGTH )
197
203
self .assertEqual (content [:], DUMMY_BYTES )
198
204
199
- @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
200
205
def test_store_bytes_in_text_mode (self ):
201
206
did_succeed = fileio .write_file (DUMMY_BYTES , self .tmp_path , self .logger ,
202
207
mode = "w" )
@@ -207,27 +212,25 @@ def test_store_bytes_in_text_mode(self):
207
212
self .assertEqual (len (content ), DUMMY_BYTES_LENGTH )
208
213
self .assertEqual (content [:], DUMMY_BYTES )
209
214
210
- @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
211
215
def test_store_unicode (self ):
212
216
did_succeed = fileio .write_file (DUMMY_UNICODE , self .tmp_path ,
213
217
self .logger , mode = 'w' )
214
218
self .assertTrue (did_succeed )
215
219
216
- with open (self .tmp_path , 'r' ) as file :
220
+ with _ByteText (self .tmp_path , 'r' ) as file :
217
221
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 )
220
224
221
- @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
222
225
def test_store_unicode_in_binary_mode (self ):
223
226
did_succeed = fileio .write_file (DUMMY_UNICODE , self .tmp_path ,
224
227
self .logger , mode = 'wb' )
225
228
self .assertTrue (did_succeed )
226
229
227
- with open (self .tmp_path , 'r' ) as file :
230
+ with _ByteText (self .tmp_path , 'r' ) as file :
228
231
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 )
231
234
232
235
233
236
if __name__ == '__main__' :
0 commit comments