-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathunit_tests.py
427 lines (351 loc) · 15.2 KB
/
unit_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# This Python file uses the following encoding: utf-8
import subprocess
import random
import sys
import os
import platform
import codecs
import time
codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)
def is_probably_on_windows():
p = platform.system().lower()
if p.find("windows") != -1:
return True
elif p.find("cygwin") != -1:
return True
return False
random.seed(125) # For deterministic test result comparisons.
LINUX_PYTHON_EXECS = [u"python2", u"python3"]
LINUX_RES_DIFF_SCRIPT_LOCATION = u"roberteldersoftwarediff.py"
WINDOWS_PYTHON_EXECS = [u"C:\\Python27\\python.exe", u"C:\\Python361\\python.exe"]
WINDOWS_RES_DIFF_SCRIPT_LOCATION = u".\\roberteldersoftwarediff.py"
TEST_INPUT_FILES_LOCATION = u"tests"
input_files = []
for (dirpath, dirnames, filenames) in os.walk(TEST_INPUT_FILES_LOCATION):
input_files.extend([dirpath + u"/" + f for f in filenames])
ENCODINGS = [
u"ascii",
u"big5",
u"big5hkscs",
u"cp037",
u"cp424",
u"cp437",
u"cp500",
u"cp720",
u"cp737",
u"cp775",
u"cp850",
u"cp852",
u"cp855",
u"cp856",
u"cp857",
u"cp858",
u"cp860",
u"cp861",
u"cp862",
u"cp863",
u"cp864",
u"cp865",
u"cp866",
u"cp869",
u"cp874",
u"cp875",
u"cp932",
u"cp949",
u"cp950",
u"cp1006",
u"cp1026",
u"cp1140",
u"cp1250",
u"cp1251",
u"cp1252",
u"cp1253",
u"cp1254",
u"cp1255",
u"cp1256",
u"cp1257",
u"cp1258",
u"euc_jp",
u"euc_jis_2004",
u"euc_jisx0213",
u"euc_kr",
u"gb2312",
u"gbk",
u"gb18030",
u"hz",
u"iso2022_jp",
u"iso2022_jp_1",
u"iso2022_jp_2",
u"iso2022_jp_2004",
u"iso2022_jp_3",
u"iso2022_jp_ext",
u"iso2022_kr",
u"latin_1",
u"iso8859_2",
u"iso8859_3",
u"iso8859_4",
u"iso8859_5",
u"iso8859_6",
u"iso8859_7",
u"iso8859_8",
u"iso8859_9",
u"iso8859_10",
u"iso8859_13",
u"iso8859_14",
u"iso8859_15",
u"iso8859_16",
u"johab",
u"koi8_r",
u"koi8_u",
u"mac_cyrillic",
u"mac_greek",
u"mac_iceland",
u"mac_latin2",
u"mac_roman",
u"mac_turkish",
u"ptcp154",
u"shift_jis",
u"shift_jis_2004",
u"shift_jisx0213",
u"utf_32",
u"utf_32_be",
u"utf_32_le",
u"utf_16",
u"utf_16_be",
u"utf_16_le",
u"utf_7",
u"utf_8",
u"utf_8_sig",
# Not really text encodings, but sure, why not?
# "idna", Does not work with ignore errors
u"palmos",
# "punycode", Throws UnicodeDecodeError, but doesn't seem to use ignore error handler
u"raw_unicode_escape",
# "rot_13", It is not supported by str.encode() (which only produces bytes output)
# See https://docs.python.org/3/library/codecs.html#text-transforms
# "undefined", This one always triggers error by definition.
u"unicode_escape",
u"unicode_internal",
# "base64_codec", Complains about always requiring strict errors.
# "bz2_codec", Complains about always requiring strict errors.
# "hex_codec", Complains about always requiring strict errors.
# "quopri_codec", Complains about always requiring strict errors.
# "string_escape" Not available in Python 3
# "uu_codec" Complains about always requiring strict errors.
# "zlib_codec" Complains about always requiring strict errors.
] + ([] if not is_probably_on_windows() else [
u"mbcs" # Windows only
])
PYTHON_EXECS = LINUX_PYTHON_EXECS
RES_DIFF_SCRIPT_LOCATION = LINUX_RES_DIFF_SCRIPT_LOCATION
if is_probably_on_windows():
PYTHON_EXECS = WINDOWS_PYTHON_EXECS
RES_DIFF_SCRIPT_LOCATION = WINDOWS_RES_DIFF_SCRIPT_LOCATION
params = []
def get_random_nonquote_character():
n = 34 # Quote
while n == 34:
n = random.randint(32, 126)
return chr(n)
def get_random_delimiter():
return "\"" + "".join([get_random_nonquote_character() for i in range(0, random.randint(0, 10))]) + "\""
def get_infile_param():
return [input_files[random.randint(0,len(input_files)-1)]]
def get_random_encoding():
return ENCODINGS[random.randint(0, len(ENCODINGS) -1)]
def get_output_encoding_param():
return ["--output-encoding", get_random_encoding()]
def get_oldfile_encoding_param():
return ["--oldfile-encoding", get_random_encoding()]
def get_newfile_encoding_param():
return ["--newfile-encoding", get_random_encoding()]
def get_parameters_encoding_param():
return ["--parameters-encoding", get_random_encoding()]
def get_delimiters_param():
return ["--delimiters", get_random_delimiter()]
def get_push_delimiters_param():
return ["--push-delimiters", get_random_delimiter()]
def get_pop_delimiters_param():
return ["--pop-delimiters", get_random_delimiter()]
def get_cols_param():
return ["--cols", str(random.randint(0,200))]
def get_lines_context_param():
return ["--lines-context", str(random.randint(0,200))]
def get_enable_windows_terminal_colours_param():
return ["--enable-windows-terminal-colours" ]
def get_disable_windows_terminal_colours_param():
return ["--disable-windows-terminal-colours" ]
def get_enable_ansi_param():
return ["--enable-ansi" ]
def get_disable_ansi_param():
return ["--disable-ansi" ]
def get_verbose_param():
return ["--verbose" ]
def get_infinite_context_param():
return ["--infinite-context" ]
def get_max_line_length_param():
return ["--max-line-length", str(random.randint(0,200))]
def get_oldfile_message_param():
return ["--oldfile-message", get_random_delimiter()]
def get_newfile_message_param():
return ["--newfile-message", get_random_delimiter()]
def get_disable_header_param():
return ["--disable-header"]
def get_enable_mark_param():
return ["--enable-mark"]
def get_disable_line_numbers_param():
return ["--disable-line-numbers"]
def get_disable_colours_param():
return ["--disable-colours"]
def get_include_delimiters_param():
return ["--include-delimiters"]
def get_show_byte_offsets_param():
return ["--show-byte-offsets"]
def get_outfile_param():
return ["--outfile", "tmp_outfile_test" if is_probably_on_windows() else "/tmp/tmp_outfile_test"]
def get_random_params():
params = []
# Two mandatory input files.
params += get_infile_param()
params += get_infile_param()
if random.randint(0, 1) == 0:
params += get_output_encoding_param()
if random.randint(0, 1) == 0:
params += get_oldfile_encoding_param()
if random.randint(0, 1) == 0:
params += get_newfile_encoding_param()
if random.randint(0, 1) == 0:
params += get_parameters_encoding_param()
if random.randint(0, 1) == 0:
params += get_delimiters_param()
if random.randint(0, 1) == 0:
params += get_push_delimiters_param()
if random.randint(0, 1) == 0:
params += get_pop_delimiters_param()
if random.randint(0, 1) == 0:
params += get_cols_param()
if random.randint(0, 1) == 0:
params += get_enable_windows_terminal_colours_param()
if random.randint(0, 1) == 0:
params += get_disable_windows_terminal_colours_param()
if random.randint(0, 1) == 0:
params += get_enable_ansi_param()
if random.randint(0, 1) == 0:
params += get_disable_ansi_param()
if random.randint(0, 1) == 0:
params += get_verbose_param()
if random.randint(0, 1) == 0:
params += get_infinite_context_param()
if random.randint(0, 1) == 0:
params += get_lines_context_param()
if random.randint(0, 1) == 0:
params += get_max_line_length_param()
if random.randint(0, 1) == 0:
params += get_oldfile_message_param()
if random.randint(0, 1) == 0:
params += get_newfile_message_param()
if random.randint(0, 1) == 0:
params += get_newfile_message_param()
if random.randint(0, 1) == 0:
params += get_disable_header_param()
if random.randint(0, 1) == 0:
params += get_enable_mark_param()
if random.randint(0, 1) == 0:
params += get_disable_line_numbers_param()
if random.randint(0, 1) == 0:
params += get_outfile_param()
return params
def get_special_case_params():
# The windows and unix specific tests should be tested on both unix and Windows to detect crashes.
special_cases = [
[u"noexist", u"noexist"],
[u"tests/ascii/ex1", u"noexist"],
[u"noexist", u"tests/ascii/ex1"],
[u"tests/ascii/ex1", u"tests/ascii/ex1", "--outfile", "/dev/null"],
[u"tests/ascii/ex1", u"tests/ascii/ex2"],
[u"tests/utf_8/ex3", u"tests/utf_8/ex4"],
[u"tests/utf_8/ex3", u"tests/utf_8/ex4", u"--oldfile-encoding", u"\"utf-8\"", u"--newfile-encoding", u"\"utf-8\""],
[u"tests/utf_8/ex3", u"tests/utf_8/ex4", u"--oldfile-encoding", u"\"utf-8\"", u"--newfile-encoding", u"\"utf-8\"", u"--output-encoding", u"\"utf-8\""],
[u"tests/ascii/ex5", u"tests/ascii/ex6"],
[u"tests/ascii/ex7", u"tests/ascii/ex8"],
[u"tests/ascii/a.json", u"tests/ascii/b.json"],
[u"tests/ascii/a.json", u"tests/ascii/b.json", u"--push-delimiters", u"\"{\"", u"\"[\"", u"--pop-delimiters", u"\"}\"", u"\"]\"", u"--include-delimiters"],
[u"tests/utf_8/fancy1", u"tests/utf_8/fancy2", u"--delimiters", u"日本国", u"--include-delimiters", u"--parameters-encoding", u"\"utf-8\"", u"--output-encoding", u"\"utf-8\"", u"--newfile-encoding", u"\"utf-8\"", u"--oldfile-encoding", u"\"utf-8\""],
[u"tests/utf_8/fancy1", u"tests/utf_8/fancy2", u"--delimiters", u"\"\\u65e5\\u672c\\u56fd\"", u"--include-delimiters", u"--parameters-encoding", u"\"utf-8\"", u"--output-encoding", u"\"utf-8\"", u"--newfile-encoding", u"\"utf-8\"", u"--oldfile-encoding", u"\"utf-8\""],
[u"tests/utf_8/this-is-encoded-in-utf-8", u"tests/utf_16/this-is-encoded-in-utf-16", u"--output-encoding", u"\"utf-8\"", u"--newfile-encoding", u"\"utf-16\"", u"--oldfile-encoding", u"\"utf-8\"", u"--enable-mark"],
[u"tests/ascii/a.html", u"tests/ascii/b.html", u"-m", u"html"]
]
return special_cases[random.randint(0, len(special_cases)-1)]
current_visual_param_number = 0
def get_visual_test_params(enc):
# Tests where a human should check the output to see if it looks ok.
all_cases = {
"utf-8": [
[u"tests/bytes-00-to-FF", u"tests/bytes-00-to-FF", u"--oldfile-encoding", u"latin_1", u"--newfile-encoding", u"latin_1", u"--output-encoding", u"utf-8", u"--infinite-context", u"--delimiters", u"\\n"],
[u"tests/bytes-00-to-FF", u"tests/bytes-00-to-FF", u"--oldfile-encoding", u"iso8859_2", u"--newfile-encoding", u"iso8859_2", u"--output-encoding", u"utf-8", u"--infinite-context", u"--delimiters", u"\\n"],
[u"tests/bytes-00-to-FF", u"tests/bytes-00-to-FF", u"--oldfile-encoding", u"iso8859_3", u"--newfile-encoding", u"iso8859_3", u"--output-encoding", u"utf-8", u"--infinite-context", u"--delimiters", u"\\n"],
[u"tests/ascii/a.json", u"tests/ascii/b.json"],
[u"tests/utf_8/delimit2", u"tests/utf_8/delimit1", u"--delimiters", u"\\u65e5\\u672c\\u56fd", u"--include-delimiters", u"--parameters-encoding", u"utf-8", u"--output-encoding", u"\"utf-8\"", u"--newfile-encoding", u"\"utf-8\"", u"--oldfile-encoding", u"\"utf-8\""],
[u"tests/utf_8/shrug", u"tests/utf_16/shrug", u"--output-encoding", u"utf-8", u"--newfile-encoding", u"utf-16", u"--oldfile-encoding", u"utf-8", u"--delimiters", u"\\n"],
[u"tests/big5hkscs/big5hkscs-test-big5hkscs", u"tests/utf_8/utf-8-test-big5hkscs", u"--oldfile-encoding", u"big5hkscs", u"--newfile-encoding", u"utf-8", u"--output-encoding", u"utf-8", u"--infinite-context", u"--delimiters", u"\\n"],
[u"tests/gb2312/gb2312-test-gb2312", u"tests/utf_16/utf-16-test-gb2312", u"--oldfile-encoding", u"gb2312", u"--newfile-encoding", u"utf-16", u"--output-encoding", u"utf-8", u"--infinite-context", u"--delimiters", u"\\n"],
[u"tests/cp866/cp866-test-cp866", u"tests/utf_32/utf-32-test-cp866", u"--oldfile-encoding", u"cp866", u"--newfile-encoding", u"utf-32", u"--output-encoding", u"utf-8", u"--infinite-context", u"--delimiters", u"\\n"],
[u"tests/utf_8/utf-8-hebrew-test-2", u"tests/utf_8/utf-8-hebrew-test-1", u"--oldfile-encoding", u"utf-8", u"--newfile-encoding", u"utf-8", u"--output-encoding", u"utf-8", u"--infinite-context", u"--delimiters", u"\\n"],
[u"tests/utf_8/utf-8-test-sanskrit-2", u"tests/utf_8/utf-8-test-sanskrit-1", u"--oldfile-encoding", u"utf-8", u"--newfile-encoding", u"utf-8", u"--output-encoding", u"utf-8", u"--infinite-context", u"--delimiters", u"\\n"]
],
"big5hkscs": [
[u"tests/big5hkscs/big5hkscs-test-big5hkscs", u"tests/utf_8/utf-8-test-big5hkscs", u"--oldfile-encoding", u"big5hkscs", u"--newfile-encoding", u"utf-8", u"--output-encoding", u"big5hkscs", u"--infinite-context", u"--delimiters", u"\\n"],
[u"tests/big5hkscs/big5hkscs-test-big5hkscs", u"tests/utf_32/utf-32-test-big5hkscs", u"--oldfile-encoding", u"big5hkscs", u"--newfile-encoding", u"utf-32", u"--output-encoding", u"big5hkscs", u"--infinite-context", u"--delimiters", u"\\n"]
],
"gb2312": [
[u"tests/gb2312/gb2312-test-gb2312", u"tests/utf_16/utf-16-test-gb2312", u"--oldfile-encoding", u"gb2312", u"--newfile-encoding", u"utf-16", u"--output-encoding", u"gb2312", u"--infinite-context", u"--delimiters", u"\\n"]
],
"cp866": [
[u"tests/cp866/cp866-test-cp866", u"tests/utf_32/utf-32-test-cp866", u"--oldfile-encoding", u"cp866", u"--newfile-encoding", u"utf-32", u"--output-encoding", u"cp866", u"--infinite-context", u"--delimiters", u"\\n"]
]
}
global current_visual_param_number
rtn = all_cases[enc][current_visual_param_number] if current_visual_param_number < len(all_cases[enc]) else None
current_visual_param_number += 1
return rtn;
def get_random_test_params():
if random.randint(0, 1) == 0:
return get_random_params()
else:
return get_special_case_params()
visual_mode = False
visual_test_encoding = "abcdef"
if len(sys.argv) > 1:
visual_test_encoding = sys.argv[1]
visual_mode = True
if visual_mode:
print(u"Running tests in visual mode. Expecting a human to watch results to see if they look fine.")
while True:
python_exec = PYTHON_EXECS[random.randint(0,len(PYTHON_EXECS)-1)]
params = []
if visual_mode:
p = get_visual_test_params(visual_test_encoding)
if p is None:
break
params = [python_exec, RES_DIFF_SCRIPT_LOCATION] + p
else:
params = [python_exec, RES_DIFF_SCRIPT_LOCATION] + get_random_test_params()
try:
print(u"Begin test. CMD is : " + (u" ".join(params)))
except:
print(u"Begin test. CMD as bytes: " + str([bytearray(b, "utf-8") for b in params]))
sys.stdout.flush()
if visual_mode:
time.sleep(1)
rtn = subprocess.call(params)
sys.stdout.flush()
if visual_mode:
time.sleep(1)
if rtn > 0:
# Stop and make the error obvious.
# If the error is not in the list of known error codes.
if not rtn in [100, 101, 102, 103, 104]:
print(u"Saw unexpected return code: " + str(rtn))
exit()
print(u"Pass")
sys.stdout.flush()