2019-12-11 03:39:27 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
# The MIT License (MIT)
|
|
|
|
# Copyright (c) 2019 Damien P. George
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
sys.path.append("../tools")
|
2019-12-11 03:39:27 +00:00
|
|
|
import pyboard
|
|
|
|
|
|
|
|
# Paths for host executables
|
2020-03-23 02:26:08 +00:00
|
|
|
CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3")
|
|
|
|
MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/micropython-coverage")
|
2019-12-11 03:39:27 +00:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
NATMOD_EXAMPLE_DIR = "../examples/natmod/"
|
2019-12-11 03:39:27 +00:00
|
|
|
|
|
|
|
# Supported tests and their corresponding mpy module
|
|
|
|
TEST_MAPPINGS = {
|
2020-03-23 02:26:08 +00:00
|
|
|
"btree": "btree/btree_$(ARCH).mpy",
|
|
|
|
"framebuf": "framebuf/framebuf_$(ARCH).mpy",
|
|
|
|
"uheapq": "uheapq/uheapq_$(ARCH).mpy",
|
|
|
|
"urandom": "urandom/urandom_$(ARCH).mpy",
|
|
|
|
"ure": "ure/ure_$(ARCH).mpy",
|
|
|
|
"uzlib": "uzlib/uzlib_$(ARCH).mpy",
|
2019-12-11 03:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Code to allow a target MicroPython to import an .mpy from RAM
|
|
|
|
injected_import_hook_code = """\
|
2020-06-18 09:19:14 +00:00
|
|
|
import usys, uos, uio
|
2019-12-11 03:39:27 +00:00
|
|
|
class __File(uio.IOBase):
|
|
|
|
def __init__(self):
|
|
|
|
self.off = 0
|
|
|
|
def ioctl(self, request, arg):
|
|
|
|
return 0
|
|
|
|
def readinto(self, buf):
|
|
|
|
buf[:] = memoryview(__buf)[self.off:self.off + len(buf)]
|
|
|
|
self.off += len(buf)
|
|
|
|
return len(buf)
|
|
|
|
class __FS:
|
|
|
|
def mount(self, readonly, mkfs):
|
|
|
|
pass
|
|
|
|
def chdir(self, path):
|
|
|
|
pass
|
|
|
|
def stat(self, path):
|
|
|
|
if path == '__injected.mpy':
|
|
|
|
return tuple(0 for _ in range(10))
|
|
|
|
else:
|
|
|
|
raise OSError(-2) # ENOENT
|
|
|
|
def open(self, path, mode):
|
|
|
|
return __File()
|
|
|
|
uos.mount(__FS(), '/__remote')
|
|
|
|
uos.chdir('/__remote')
|
2020-06-18 09:19:14 +00:00
|
|
|
usys.modules['{}'] = __import__('__injected')
|
2019-12-11 03:39:27 +00:00
|
|
|
"""
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-12-11 03:39:27 +00:00
|
|
|
class TargetSubprocess:
|
|
|
|
def __init__(self, cmd):
|
|
|
|
self.cmd = cmd
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def run_script(self, script):
|
|
|
|
try:
|
2020-03-23 02:26:08 +00:00
|
|
|
p = subprocess.run(
|
|
|
|
self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, input=script
|
|
|
|
)
|
2019-12-11 03:39:27 +00:00
|
|
|
return p.stdout, None
|
|
|
|
except subprocess.CalledProcessError as er:
|
2020-03-23 02:26:08 +00:00
|
|
|
return b"", er
|
|
|
|
|
2019-12-11 03:39:27 +00:00
|
|
|
|
|
|
|
class TargetPyboard:
|
|
|
|
def __init__(self, pyb):
|
|
|
|
self.pyb = pyb
|
|
|
|
self.pyb.enter_raw_repl()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.pyb.exit_raw_repl()
|
|
|
|
self.pyb.close()
|
|
|
|
|
|
|
|
def run_script(self, script):
|
|
|
|
try:
|
|
|
|
self.pyb.enter_raw_repl()
|
|
|
|
output = self.pyb.exec_(script)
|
2020-03-23 02:26:08 +00:00
|
|
|
output = output.replace(b"\r\n", b"\n")
|
2019-12-11 03:39:27 +00:00
|
|
|
return output, None
|
|
|
|
except pyboard.PyboardError as er:
|
2020-03-23 02:26:08 +00:00
|
|
|
return b"", er
|
|
|
|
|
2019-12-11 03:39:27 +00:00
|
|
|
|
|
|
|
def run_tests(target_truth, target, args, stats):
|
|
|
|
for test_file in args.files:
|
|
|
|
# Find supported test
|
|
|
|
for k, v in TEST_MAPPINGS.items():
|
|
|
|
if test_file.find(k) != -1:
|
|
|
|
test_module = k
|
2020-03-23 02:26:08 +00:00
|
|
|
test_mpy = v.replace("$(ARCH)", args.arch)
|
2019-12-11 03:39:27 +00:00
|
|
|
break
|
|
|
|
else:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("---- {} - no matching mpy".format(test_file))
|
2019-12-11 03:39:27 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Read test script
|
2020-03-23 02:26:08 +00:00
|
|
|
with open(test_file, "rb") as f:
|
2019-12-11 03:39:27 +00:00
|
|
|
test_file_data = f.read()
|
|
|
|
|
|
|
|
# Create full test with embedded .mpy
|
|
|
|
try:
|
2020-03-23 02:26:08 +00:00
|
|
|
with open(NATMOD_EXAMPLE_DIR + test_mpy, "rb") as f:
|
|
|
|
test_script = b"__buf=" + bytes(repr(f.read()), "ascii") + b"\n"
|
2019-12-11 03:39:27 +00:00
|
|
|
except OSError:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("---- {} - mpy file not compiled".format(test_file))
|
2019-12-11 03:39:27 +00:00
|
|
|
continue
|
2020-03-23 02:26:08 +00:00
|
|
|
test_script += bytes(injected_import_hook_code.format(test_module), "ascii")
|
2019-12-11 03:39:27 +00:00
|
|
|
test_script += test_file_data
|
|
|
|
|
|
|
|
# Run test under MicroPython
|
|
|
|
result_out, error = target.run_script(test_script)
|
|
|
|
|
|
|
|
# Work out result of test
|
2020-03-23 02:26:08 +00:00
|
|
|
extra = ""
|
|
|
|
if error is None and result_out == b"SKIP\n":
|
|
|
|
result = "SKIP"
|
2019-12-11 03:39:27 +00:00
|
|
|
elif error is not None:
|
2020-03-23 02:26:08 +00:00
|
|
|
result = "FAIL"
|
|
|
|
extra = " - " + str(error)
|
2019-12-11 03:39:27 +00:00
|
|
|
else:
|
|
|
|
# Check result against truth
|
|
|
|
try:
|
2020-03-23 02:26:08 +00:00
|
|
|
with open(test_file + ".exp", "rb") as f:
|
2019-12-11 03:39:27 +00:00
|
|
|
result_exp = f.read()
|
|
|
|
error = None
|
|
|
|
except OSError:
|
|
|
|
result_exp, error = target_truth.run_script(test_file_data)
|
|
|
|
if error is not None:
|
2020-03-23 02:26:08 +00:00
|
|
|
result = "TRUTH FAIL"
|
2019-12-11 03:39:27 +00:00
|
|
|
elif result_out != result_exp:
|
2020-03-23 02:26:08 +00:00
|
|
|
result = "FAIL"
|
2019-12-11 03:39:27 +00:00
|
|
|
print(result_out)
|
|
|
|
else:
|
2020-03-23 02:26:08 +00:00
|
|
|
result = "pass"
|
2019-12-11 03:39:27 +00:00
|
|
|
|
|
|
|
# Accumulate statistics
|
2020-03-23 02:26:08 +00:00
|
|
|
stats["total"] += 1
|
|
|
|
if result == "pass":
|
|
|
|
stats["pass"] += 1
|
|
|
|
elif result == "SKIP":
|
|
|
|
stats["skip"] += 1
|
2019-12-11 03:39:27 +00:00
|
|
|
else:
|
2020-03-23 02:26:08 +00:00
|
|
|
stats["fail"] += 1
|
2019-12-11 03:39:27 +00:00
|
|
|
|
|
|
|
# Print result
|
2020-03-23 02:26:08 +00:00
|
|
|
print("{:4} {}{}".format(result, test_file, extra))
|
|
|
|
|
2019-12-11 03:39:27 +00:00
|
|
|
|
|
|
|
def main():
|
2020-03-23 02:26:08 +00:00
|
|
|
cmd_parser = argparse.ArgumentParser(
|
|
|
|
description="Run dynamic-native-module tests under MicroPython"
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"-p", "--pyboard", action="store_true", help="run tests via pyboard.py"
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"-d", "--device", default="/dev/ttyACM0", help="the device for pyboard.py"
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"-a", "--arch", default="x64", help="native architecture of the target"
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument("files", nargs="*", help="input test files")
|
2019-12-11 03:39:27 +00:00
|
|
|
args = cmd_parser.parse_args()
|
|
|
|
|
|
|
|
target_truth = TargetSubprocess([CPYTHON3])
|
|
|
|
|
|
|
|
if args.pyboard:
|
|
|
|
target = TargetPyboard(pyboard.Pyboard(args.device))
|
|
|
|
else:
|
|
|
|
target = TargetSubprocess([MICROPYTHON])
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
stats = {"total": 0, "pass": 0, "fail": 0, "skip": 0}
|
2019-12-11 03:39:27 +00:00
|
|
|
run_tests(target_truth, target, args, stats)
|
|
|
|
|
|
|
|
target.close()
|
|
|
|
target_truth.close()
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
print("{} tests performed".format(stats["total"]))
|
|
|
|
print("{} tests passed".format(stats["pass"]))
|
|
|
|
if stats["fail"]:
|
|
|
|
print("{} tests failed".format(stats["fail"]))
|
|
|
|
if stats["skip"]:
|
|
|
|
print("{} tests skipped".format(stats["skip"]))
|
2019-12-11 03:39:27 +00:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
if stats["fail"]:
|
2019-12-11 03:39:27 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-12-11 03:39:27 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|