28
28
"""Unit test fileio functions"""
29
29
30
30
import binascii
31
+ import codecs
31
32
import os
32
33
import sys
33
34
import unittest
34
35
35
36
# NOTE: wrap next imports in try except to prevent autopep8 shuffling up
36
37
try :
37
- from tests .support import MigTestCase , cleanpath , temppath , testmain
38
+ from tests .support import PY2 , MigTestCase , cleanpath , temppath , testmain
38
39
import mig .shared .fileio as fileio
39
40
except ImportError as ioe :
40
41
print ("Failed to import mig core modules: %s" % ioe )
43
44
DUMMY_BYTES = binascii .unhexlify ('DEADBEEF' ) # 4 bytes
44
45
DUMMY_BYTES_LENGTH = 4
45
46
DUMMY_UNICODE = u'UniCode123½¾µßðþđŋħĸþł@ª€£$¥©®'
46
- DUMMY_UNICODE_LENGTH = len (DUMMY_UNICODE )
47
+ DUMMY_UNICODE_BYTES = bytearray (DUMMY_UNICODE , 'utf8' )
48
+ DUMMY_UNICODE_BYTES_LENGTH = len (DUMMY_UNICODE_BYTES )
47
49
DUMMY_FILE_WRITECHUNK = 'fileio/write_chunk'
48
50
DUMMY_FILE_WRITEFILE = 'fileio/write_file'
49
51
50
52
assert isinstance (DUMMY_BYTES , bytes )
51
53
54
+ def as_unicode_string (value ):
55
+ assert isinstance (value , bytearray )
56
+ return unicode (codecs .decode (value , 'utf8' )) if PY2 else str (value , 'utf8' )
57
+
58
+ class TextFile :
59
+ def __init__ (self , path , mode = 'r' ):
60
+ self ._file = None
61
+ self ._path = path
62
+ self ._mode = "%s%s" % (mode , 'b' ) if 'b' not in mode else mode
63
+
64
+ def read (self , size ):
65
+ content = self ._file .read (size )
66
+ # always read back the content as though it was raw bytes
67
+ return bytearray (content )
68
+
69
+ def __enter__ (self ):
70
+ self ._file = open (self ._path , mode = self ._mode )
71
+ return self
72
+
73
+ def __exit__ (self , * args ):
74
+ self ._file .close ()
75
+ if args [1 ] is not None :
76
+ raise args [1 ]
77
+
52
78
53
79
class MigSharedFileio__write_chunk (MigTestCase ):
54
80
# TODO: Add docstrings to this class and its methods
@@ -58,9 +84,7 @@ def setUp(self):
58
84
cleanpath (os .path .dirname (DUMMY_FILE_WRITECHUNK ), self )
59
85
60
86
def test_return_false_on_invalid_data (self ):
61
- # NOTE: we make sure to disable any forced stringification here
62
- did_succeed = fileio .write_chunk (self .tmp_path , 1234 , 0 , self .logger ,
63
- force_string = False )
87
+ did_succeed = fileio .write_chunk (self .tmp_path , 1234 , 0 , self .logger )
64
88
self .assertFalse (did_succeed )
65
89
66
90
def test_return_false_on_invalid_offset (self ):
@@ -110,25 +134,24 @@ def test_store_bytes_in_text_mode(self):
110
134
self .assertEqual (len (content ), DUMMY_BYTES_LENGTH )
111
135
self .assertEqual (content [:], DUMMY_BYTES )
112
136
113
- @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
114
137
def test_store_unicode (self ):
115
- fileio .write_chunk (self .tmp_path , DUMMY_UNICODE , 0 , self .logger ,
116
- mode = 'r+' )
138
+ did_succeed = fileio .write_chunk (self .tmp_path , DUMMY_UNICODE , 0 , self .logger ,
139
+ mode = 'r+' )
140
+ self .assertTrue (did_succeed )
117
141
118
- with open (self .tmp_path , 'r' ) as file :
142
+ with TextFile (self .tmp_path ) as file :
119
143
content = file .read (1024 )
120
- self .assertEqual (len (content ), DUMMY_UNICODE_LENGTH )
121
- self .assertEqual (content [:], DUMMY_UNICODE )
144
+ self .assertEqual (len (content ), DUMMY_UNICODE_BYTES_LENGTH )
145
+ self .assertEqual (content [:], DUMMY_UNICODE_BYTES )
122
146
123
- @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
124
147
def test_store_unicode_in_binary_mode (self ):
125
148
fileio .write_chunk (self .tmp_path , DUMMY_UNICODE , 0 , self .logger ,
126
149
mode = 'r+b' )
127
150
128
- with open (self .tmp_path , 'r' ) as file :
151
+ with TextFile (self .tmp_path ) as file :
129
152
content = file .read (1024 )
130
- self .assertEqual (len (content ), DUMMY_UNICODE_LENGTH )
131
- self .assertEqual (content [:], DUMMY_UNICODE )
153
+ self .assertEqual (len (content ), DUMMY_UNICODE_BYTES_LENGTH )
154
+ self .assertEqual (as_unicode_string ( content [:]) , DUMMY_UNICODE )
132
155
133
156
134
157
class MigSharedFileio__write_file (MigTestCase ):
@@ -138,15 +161,14 @@ def setUp(self):
138
161
cleanpath (os .path .dirname (DUMMY_FILE_WRITEFILE ), self )
139
162
140
163
def test_return_false_on_invalid_data (self ):
141
- # NOTE: we make sure to disable any forced stringification here
142
- did_succeed = fileio .write_file (1234 , self .tmp_path , self .logger ,
143
- force_string = False )
164
+ did_succeed = fileio .write_file (1234 , self .tmp_path , self .logger )
144
165
self .assertFalse (did_succeed )
145
166
146
167
def test_return_false_on_invalid_dir (self ):
147
168
os .makedirs (self .tmp_path )
148
169
149
- did_succeed = fileio .write_file (DUMMY_BYTES , self .tmp_path , self .logger )
170
+ did_succeed = fileio .write_file (
171
+ DUMMY_BYTES , self .tmp_path , self .logger )
150
172
self .assertFalse (did_succeed )
151
173
152
174
def test_return_false_on_missing_dir (self ):
@@ -155,20 +177,16 @@ def test_return_false_on_missing_dir(self):
155
177
self .assertFalse (did_succeed )
156
178
157
179
def test_creates_directory (self ):
158
- # TODO: temporarily use empty string to avoid any byte/unicode issues
159
- # did_succeed = fileio.write_file(DUMMY_BYTES, self.tmp_path, self.logger)
160
- did_succeed = fileio .write_file ('' , self .tmp_path , self .logger )
180
+ did_succeed = fileio .write_file (
181
+ DUMMY_BYTES , self .tmp_path , self .logger )
161
182
self .assertTrue (did_succeed )
162
183
163
184
path_kind = self .assertPathExists (DUMMY_FILE_WRITEFILE )
164
185
self .assertEqual (path_kind , "file" )
165
186
166
187
def test_store_bytes (self ):
167
- mode = 'w'
168
- # TODO: remove next once we have auto adjust mode in write helper
169
- mode = fileio ._auto_adjust_mode (DUMMY_BYTES , mode )
170
- did_succeed = fileio .write_file (DUMMY_BYTES , self .tmp_path , self .logger ,
171
- mode = mode )
188
+ did_succeed = fileio .write_file (
189
+ DUMMY_BYTES , self .tmp_path , self .logger )
172
190
self .assertTrue (did_succeed )
173
191
174
192
with open (self .tmp_path , 'rb' ) as file :
@@ -211,4 +229,4 @@ def test_store_unicode_in_binary_mode(self):
211
229
212
230
213
231
if __name__ == '__main__' :
214
- testmain ()
232
+ testmain (failfast = True )
0 commit comments