Browse code

Fixes failing tests with special chars for PY3

Florent Viard authored on 2017/05/30 18:40:27
Showing 2 changed files
... ...
@@ -22,6 +22,13 @@ from S3.ExitCodes import *
22 22
 
23 23
 PY3 = (sys.version_info >= (3,0))
24 24
 
25
+try:
26
+    unicode
27
+except NameError:
28
+    # python 3 support
29
+    # In python 3, unicode -> str, and str -> bytes
30
+    unicode = str
31
+
25 32
 count_pass = 0
26 33
 count_fail = 0
27 34
 count_skip = 0
... ...
@@ -100,6 +107,19 @@ if have_encoding:
100 100
     enc_pattern = patterns[encoding]
101 101
 else:
102 102
     print(encoding + " specific files not found.")
103
+
104
+def unicodise(string):
105
+    if type(string) == unicode:
106
+        return string
107
+
108
+    return unicode(string, "UTF-8", "replace")
109
+
110
+def deunicodise(string):
111
+    if type(string) != unicode:
112
+        return string
113
+
114
+    return string.encode("UTF-8", "replace")
115
+
103 116
 # Minio: disable encoding tests
104 117
 have_encoding = False
105 118
 
... ...
@@ -143,7 +163,7 @@ def test(label, cmd_args = [], retcode = 0, must_find = [], must_not_find = [],
143 143
         return 0
144 144
     def compile_list(_list, regexps = False):
145 145
         if regexps == False:
146
-            _list = [re.escape(item.encode(encoding, "replace")).decode(encoding) for item in _list]
146
+            _list = [re.escape(item) for item in _list]
147 147
 
148 148
         return [re.compile(item, re.MULTILINE) for item in _list]
149 149
 
... ...
@@ -184,6 +204,7 @@ def test(label, cmd_args = [], retcode = 0, must_find = [], must_not_find = [],
184 184
     not_find_list_patterns.extend(must_not_find_re)
185 185
 
186 186
     for index in range(len(find_list)):
187
+        stdout = unicodise(stdout)
187 188
         match = find_list[index].search(stdout)
188 189
         if not match:
189 190
             return failure("pattern not found: %s" % find_list_patterns[index])
... ...
@@ -384,6 +405,7 @@ test_s3cmd("Put from stdin", ['put', '-', '%s/single-file/single-file.txt' % pbu
384 384
 f.close()
385 385
 
386 386
 ## ====== Multipart put
387
+os.system('mkdir -p testsuite-out')
387 388
 os.system('dd if=/dev/urandom of=testsuite-out/urandom.bin bs=1M count=16 > /dev/null 2>&1')
388 389
 test_s3cmd("Put multipart", ['put', '--multipart-chunk-size-mb=5', 'testsuite-out/urandom.bin', '%s/urandom.bin' % pbucket(1)],
389 390
            must_not_find = ['abortmp'])
... ...
@@ -20,6 +20,15 @@ import S3.Exceptions
20 20
 import S3.Config
21 21
 from S3.ExitCodes import *
22 22
 
23
+PY3 = (sys.version_info >= (3,0))
24
+
25
+try:
26
+    unicode
27
+except NameError:
28
+    # python 3 support
29
+    # In python 3, unicode -> str, and str -> bytes
30
+    unicode = str
31
+
23 32
 count_pass = 0
24 33
 count_fail = 0
25 34
 count_skip = 0
... ...
@@ -99,6 +108,18 @@ if have_encoding:
99 99
 else:
100 100
     print(encoding + " specific files not found.")
101 101
 
102
+def unicodise(string):
103
+    if type(string) == unicode:
104
+        return string
105
+
106
+    return unicode(string, "UTF-8", "replace")
107
+
108
+def deunicodise(string):
109
+    if type(string) != unicode:
110
+        return string
111
+
112
+    return string.encode("UTF-8", "replace")
113
+
102 114
 if not os.path.isdir('testsuite/crappy-file-name'):
103 115
     os.system("tar xvz -C testsuite -f testsuite/crappy-file-name.tar.gz")
104 116
     # TODO: also unpack if the tarball is newer than the directory timestamp
... ...
@@ -139,7 +160,7 @@ def test(label, cmd_args = [], retcode = 0, must_find = [], must_not_find = [],
139 139
         return 0
140 140
     def compile_list(_list, regexps = False):
141 141
         if regexps == False:
142
-            _list = [re.escape(item.encode(encoding, "replace")) for item in _list]
142
+            _list = [re.escape(item) for item in _list]
143 143
 
144 144
         return [re.compile(item, re.MULTILINE) for item in _list]
145 145
 
... ...
@@ -180,6 +201,7 @@ def test(label, cmd_args = [], retcode = 0, must_find = [], must_not_find = [],
180 180
     not_find_list_patterns.extend(must_not_find_re)
181 181
 
182 182
     for index in range(len(find_list)):
183
+        stdout = unicodise(stdout)
183 184
         match = find_list[index].search(stdout)
184 185
         if not match:
185 186
             return failure("pattern not found: %s" % find_list_patterns[index])