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
|
"""
The test script will only run the one test on micropython. It is not a
framework that can be used for other tests. This has a reduced code footprint.
entrypoint API is almost the same as the framework.
Requirements
Python >= 3.10
pillow library
add the following to the CI prior to the test running
python3 -m pip install pillow
Example command line to run the test. I suggest doing this from the root of the
binding directory. It is just a simple location to do it from.
Paths that are passed in MUST be relative to the current working directory.
python3 lib/lv_bindings/lvgl/tests/micropy_test/__init__.py --artifact-path=lib/lv_bindings/lvgl/tests/micropy_test/artifacts --mpy-path=ports/unix/build-standard/micropython
"""
import os
import time
import signal
import argparse
import binascii
import unittest
import threading
import subprocess
from PIL import Image as Image
DEBUG = 0
debug_log = None
saved_test_data = []
def format_error_data(data):
output = ''
for line in data.split('\n'):
output += f'\033[31;1m{line}\033[0m\n'
return output
def log(*args):
args = ' '.join(repr(arg) for arg in args)
debug_log.write(args + '\n')
if DEBUG:
sys.stdout.write('\033[31;1m' + args + '\033[0m\n')
sys.stdout.flush()
BASE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__)))
TEST_PATH = os.path.join(BASE_PATH, 'micropy.py')
IMG_PATH = os.path.join(BASE_PATH, '../ref_imgs/binding.png')
CTRL_C = b'\x03' # 2 times to exit any running code
CTRL_D = b'\x04' # exit paste mode committing and running what has been pasted
CTRL_E = b'\x05' # enter paste mode
PASTE_PROMPT = b'==='
REPL_PROMPT = b'>>>'
os.environ['MICROPYINSPECT'] = '1'
class TestData:
def __init__(self):
self.watchdog_timer = time.time()
self.result = None
self.error_data = ''
self.event = threading.Event()
class MicroPython_Test(unittest.TestCase):
# these are here simply to make an IDE happy. Their values get dynamically
# set when the class gets constructed
process: subprocess.Popen = None
exit_event: threading.Event = None
@classmethod
def send(cls, cmd):
if cls.process is None:
return
log('<---', cmd)
cls.process.stdin.write(cmd)
cls.process.stdin.flush()
@classmethod
def read_until(cls, marker):
micropy_data = b''
error_data = b''
log('MARKER', marker)
logged = False
while (
not micropy_data.endswith(marker) and
not cls.exit_event.is_set()
):
try:
char = cls.process.stdout.read(1)
except: # NOQA
break
if char:
micropy_data += char
logged = False
else:
logged = True
log('--->', micropy_data)
if b'\nERROR END\n' in micropy_data:
error_data = micropy_data.split(b'\nERROR START\n')[-1].split(b'\nERROR END\n')[0]
micropy_data = b''
try:
log('---> ERROR: ', error_data.decode('utf-8'))
except UnicodeDecodeError:
log('---> ERROR: ', error_data)
logged = True
break
if not logged:
log('--->', micropy_data)
if b'\nERROR END\n' in micropy_data:
error_data = micropy_data.split(b'\nERROR START\n')[-1].split(b'\nERROR END\n')[0]
micropy_data = b''
try:
log('---> ERROR: ', error_data.decode('utf-8'))
except UnicodeDecodeError:
log('---> ERROR: ', error_data)
if cls.exit_event.is_set():
log('--EXIT EVENT SET')
saved_test_data.append((micropy_data, error_data))
return micropy_data.replace(marker, b''), error_data
@classmethod
def setUpClass(cls):
os.chdir(os.path.dirname(__file__))
cls.exit_event = threading.Event()
log(f'--SETTING UP')
cls.process = subprocess.Popen(
['bash'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
env=os.environ,
shell=True,
preexec_fn=os.setsid
)
log(f'--RUNNING MICROPYTHON ({MICROPYTHON_PATH})')
cls.send(b'cd ' + os.path.dirname(__file__).encode('utf-8') + b'\n')
cls.send(MICROPYTHON_PATH.encode('utf-8') + b'\n')
_, error_data = cls.read_until(b'>>>')
if error_data:
raise RuntimeError(error_data)
log('--MICROPYTHON STARTED')
@classmethod
def tearDownClass(cls):
log('--TEARDOWN STARTED')
if cls.process is not None:
cls.send(CTRL_C)
cls.send(CTRL_C)
cls.send(CTRL_D)
if not cls.process.stdin.closed:
cls.process.stdin.close()
os.killpg(os.getpgid(cls.process.pid), signal.SIGTERM)
cls.process.wait()
if not cls.process.stdout.closed:
cls.process.stdout.close()
if not cls.process.stderr.closed:
cls.process.stderr.close()
cls.process = None
log(f'--TEARDOWN FINISHED')
def test_1_image_compare(self):
image = Image.open(IMG_PATH)
res_img = image.convert('RGB')
image.close()
res_data = list(res_img.getdata())
res_img.close()
with open(TEST_PATH, 'rb') as f:
test_code = f.read()
log(f'--RUNNING TEST ({TEST_PATH})')
test_code = test_code.strip()
if self.__class__.process is None:
self.fail('MicroPython failure.')
self.send(CTRL_E)
_, error_data = self.read_until(PASTE_PROMPT)
if error_data:
self.fail(error_data)
test_code = test_code.split(b'\r\n')
for i, line in enumerate(test_code):
self.send(line + b'\n')
_, error_data = self.read_until(b'\n')
if error_data:
self.fail(error_data)
time.sleep(0.002)
# self.read_until(b'# end\n')
def _do(td: TestData):
self.send(CTRL_D)
td.error_data = b''
td.watchdog_timer = time.time()
td.result = []
try:
_, td.error_data = self.read_until(b'FRAME START\n')
td.watchdog_timer = time.time()
lne, td.error_data = self.read_until(b'\n')
while (
b'FRAME END' not in lne and
not td.error_data and
not self.__class__.exit_event.is_set()
):
td.watchdog_timer = time.time()
td.result.append(lne)
lne, td.error_data = self.read_until(b'\n')
if td.error_data:
return
if self.__class__.exit_event.is_set():
return
except: # NOQA
import traceback
traceback.print_exc()
td.error_data = traceback.format_exc()
return
td.event.set()
test_data = TestData()
t = threading.Thread(
target=_do,
args=(test_data,)
)
t.daemon = True
test_data.watchdog_timer = time.time()
t.start()
while (
(time.time() - test_data.watchdog_timer) * 1000 <= 20000 and
not test_data.event.is_set()
):
test_data.event.wait(0.05)
if not test_data.event.is_set():
self.__class__.exit_event.set()
# self.read_until(REPL_PROMPT)
self.send(CTRL_C)
self.send(CTRL_C)
width = 800
height = 480
if test_data.error_data:
self.fail(test_data.error_data)
try:
frame = bytearray(
b''.join(binascii.unhexlify(lne) for lne in test_data.result)
)
# I don't exactly know why the byte order is backwards but it is
frame = bytes(bytearray([
item for j in range(0, len(frame), 3)
for item in [frame[j + 2], frame[j + 1], frame[j]]
]))
image = Image.new('RGB', (width, height))
image.frombytes(frame)
img = image.convert('RGB')
image.close()
byte_data = list(img.getdata())
img.save(os.path.join(ARTIFACT_PATH, f'frame.png'), 'PNG')
img.close()
with open(os.path.join(ARTIFACT_PATH, f'frame.bin'), 'wb') as f:
# have to flatten the data and remove the alpha
# from the PIL image it is formatted as
# [(r, g, b), (r, g, b)]
f.write(bytes(bytearray([
item for sublist in byte_data
for item in sublist
])))
except binascii.Error:
error = []
for line, err in saved_test_data:
if err:
try:
error.append(err.decode('utf-8'))
except UnicodeDecodeError:
error.append(str(err))
error = '\n'.join(error)
if error:
self.fail(format_error_data(error))
else:
self.fail(b'\n'.join(test_data.result))
except: # NOQA
import traceback
self.fail(traceback.format_exc())
self.assertEqual(res_data, byte_data, 'Frames do not match')
cwd = os.path.abspath(os.getcwd())
ARTIFACT_PATH = os.path.join(cwd, 'artifacts')
MICROPYTHON_PATH = os.path.join(cwd, 'micropython')
if __name__ == '__main__':
import sys
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
'--artifact-path',
dest='artifact_path',
help='path to save artifacts to',
action='store'
)
arg_parser.add_argument(
'--mpy-path',
dest='mpy_path',
help='path to micropython',
action='store'
)
arg_parser.add_argument(
'--debug',
dest='debug',
help='debug output',
action='store_true'
)
args = arg_parser.parse_args()
ARTIFACT_PATH = os.path.join(cwd, args.artifact_path)
MICROPYTHON_PATH = os.path.join(cwd, args.mpy_path)
DEBUG = args.debug
if not os.path.exists(ARTIFACT_PATH):
raise RuntimeError(f'Artifact path does not exist ({ARTIFACT_PATH})')
if not os.path.exists(MICROPYTHON_PATH):
raise RuntimeError(f'MicroPython binary not found ({MICROPYTHON_PATH})')
debug_log_path = os.path.join(ARTIFACT_PATH, 'debug.log')
debug_log = open(debug_log_path, 'w')
unittest.main(argv=[sys.argv[0], '-v'])
debug_log.close()
print(f'View the debug output in "{debug_log_path}"')
|