8000 tests/run-tests: Make "regex'ed .exp" facility available to device te… · sparkfun/circuitpython@6ead9f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ead9f6

Browse files
committed
tests/run-tests: Make "regex'ed .exp" facility available to device tests.
Required to pass bytes_compare3.py (opptional warnings) on devices.
1 parent f2f761c commit 6ead9f6

File tree

1 file changed

+63
-58
lines changed

1 file changed

+63
-58
lines changed

tests/run-tests

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,35 @@ def rm_f(fname):
2626
if os.path.exists(fname):
2727
os.remove(fname)
2828

29+
30+
# unescape wanted regex chars and escape unwanted ones
31+
def convert_regex_escapes(line):
32+
cs = []
33+
escape = False
34+
for c in str(line, 'utf8'):
35+
if escape:
36+
escape = False
37+
cs.append(c)
38+
elif c == '\\':
39+
escape = True
40+
elif c in ('(', ')', '[', ']', '{', '}', '.', '*', '+', '^', '$'):
41+
cs.append('\\' + c)
42+
else:
43+
cs.append(c)
44+
# accept carriage-return(s) before final newline
45+
if cs[-1] == '\n':
46+
cs[-1] = '\r*\n'
47+
return bytes(''.join(cs), 'utf8')
48+
49+
2950
def run_micropython(pyb, args, test_file):
3051
special_tests = ('micropython/meminfo.py', 'basics/bytes_compare3.py')
52+
is_special = False
3153
if pyb is None:
3254
# run on PC
33-
if test_file.startswith(('cmdline/', 'feature_check/')) or test_file in special_tests :
55+
if test_file.startswith(('cmdline/', 'feature_check/')) or test_file in special_tests:
3456
# special handling for tests of the unix cmdline program
57+
is_special = True
3558

3659
# check for any cmdline options needed for this test
3760
args = [MICROPYTHON]
@@ -81,63 +104,6 @@ def run_micropython(pyb, args, test_file):
81104
except subprocess.CalledProcessError:
82105
return b'CRASH'
83106

84-
# unescape wanted regex chars and escape unwanted ones
85-
def convert_regex_escapes(line):
86-
cs = []
87-
escape = False
88-
for c in str(line, 'utf8'):
89-
if escape:
90-
escape = False
91-
cs.append(c)
92-
elif c == '\\':
93-
escape = True
94-
elif c in ('(', ')', '[', ']', '{', '}', '.', '*', '+', '^', '$'):
95-
cs.append('\\' + c)
96-
else:
97-
cs.append(c)
98-
# accept carriage-return(s) before final newline
99-
if cs[-1] == '\n':
100-
cs[-1] = '\r*\n'
101-
return bytes(''.join(cs), 'utf8')
102-
103-
# convert parts of the output that are not stable across runs
104-
with open(test_file + '.exp', 'rb') as f:
105-
lines_exp = []
106-
for line in f.readlines():
107-
if line == b'########\n':
108-
line = (line,)
109-
else:
110-
line = (line, re.compile(convert_regex_escapes(line)))
111-
lines_exp.append(line)
112-
lines_mupy = [line + b'\n' for line in output_mupy.split(b'\n')]
113-
if output_mupy.endswith(b'\n'):
114-
lines_mupy = lines_mupy[:-1] # remove erroneous last empty line
115-
i_mupy = 0
116-
for i in range(len(lines_exp)):
117-
if lines_exp[i][0] == b'########\n':
118-
# 8x #'s means match 0 or more whole lines
119-
line_exp = lines_exp[i + 1]
120-
skip = 0
121-
while i_mupy + skip < len(lines_mupy) and not line_exp[1].match(lines_mupy[i_mupy + skip]):
122-
skip += 1
123-
if i_mupy + skip >= len(lines_mupy):
124-
lines_mupy[i_mupy] = b'######## FAIL\n'
125-
break
126-
del lines_mupy[i_mupy:i_mupy + skip]
127-
lines_mupy.insert(i_mupy, b'########\n')
128-
i_mupy += 1
129-
else:
130-
# a regex
131-
if lines_exp[i][1].match(lines_mupy[i_mupy]):
132-
lines_mupy[i_mupy] = lines_exp[i][0]
133-
else:
134-
#print("don't match: %r %s" % (lines_exp[i][1], lines_mupy[i_mupy])) # DEBUG
135-
pass
136-
i_mupy += 1
137-
if i_mupy >= len(lines_mupy):
138-
break
139-
output_mupy = b''.join(lines_mupy)
140-
141107
else:
142108
# a standard test
143109
try:
@@ -160,6 +126,45 @@ def run_micropython(pyb, args, test_file):
160126
# canonical form for all ports/platforms is to use \n for end-of-line
161127
output_mupy = output_mupy.replace(b'\r\n', b'\n')
162128

129+
if is_special or test_file in special_tests:
130+
# convert parts of the output that are not stable across runs
131+
with open(test_file + '.exp', 'rb') as f:
132+
lines_exp = []
133+
for line in f.readlines():
134+
if line == b'########\n':
135+
line = (line,)
136+
else:
137+
line = (line, re.compile(convert_regex_escapes(line)))
138+
lines_exp.append(line)
139+
lines_mupy = [line + b'\n' for line in output_mupy.split(b'\n')]
140+
if output_mupy.endswith(b'\n'):
141+
lines_mupy = lines_mupy[:-1] # remove erroneous last empty line
142+
i_mupy = 0
143+
for i in range(len(lines_exp)):
144+
if lines_exp[i][0] == b'########\n':
145+
# 8x #'s means match 0 or more whole lines
146+
line_exp = lines_exp[i + 1]
147+
skip = 0
148+
while i_mupy + skip < len(lines_mupy) and not line_exp[1].match(lines_mupy[i_mupy + skip]):
149+
skip += 1
150+
if i_mupy + skip >= len(lines_mupy):
151+
lines_mupy[i_mupy] = b'######## FAIL\n'
152+
break
153+
del lines_mupy[i_mupy:i_mupy + skip]
154+
lines_mupy.insert(i_mupy, b'########\n')
155+
i_mupy += 1
156+
else:
157+
# a regex
158+
if lines_exp[i][1].match(lines_mupy[i_mupy]):
159+
lines_mupy[i_mupy] = lines_exp[i][0]
160+
else:
161+
#print("don't match: %r %s" % (lines_exp[i][1], lines_mupy[i_mupy])) # DEBUG
162+
pass
163+
i_mupy += 1
164+
if i_mupy >= len(lines_mupy):
165+
break
166+
output_mupy = b''.join(lines_mupy)
167+
163168
return output_mupy
164169

165170
def run_tests(pyb, tests, args):

0 commit comments

Comments
 (0)
0