diff options
author | David Lechner <david@lechnology.com> | 2020-03-22 21:26:08 -0500 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2020-03-30 13:21:58 +1100 |
commit | 3dc324d3f1312e40d3a8ed87e7244966bb756f26 (patch) | |
tree | 94ff44f8eabba0039582c245b901173597edd11e /tests/run-natmodtests.py | |
parent | 488613bca6c460340ed2995ae5cafafe22d0bfff (diff) | |
download | micropython-3dc324d3f1312e40d3a8ed87e7244966bb756f26.tar.gz micropython-3dc324d3f1312e40d3a8ed87e7244966bb756f26.zip |
tests: Format all Python code with black, except tests in basics subdir.
This adds the Python files in the tests/ directory to be formatted with
./tools/codeformat.py. The basics/ subdirectory is excluded for now so we
aren't changing too much at once.
In a few places `# fmt: off`/`# fmt: on` was used where the code had
special formatting for readability or where the test was actually testing
the specific formatting.
Diffstat (limited to 'tests/run-natmodtests.py')
-rwxr-xr-x | tests/run-natmodtests.py | 115 |
1 files changed, 65 insertions, 50 deletions
diff --git a/tests/run-natmodtests.py b/tests/run-natmodtests.py index 46f21f81d7..f88ca0b788 100755 --- a/tests/run-natmodtests.py +++ b/tests/run-natmodtests.py @@ -9,23 +9,23 @@ import subprocess import sys import argparse -sys.path.append('../tools') +sys.path.append("../tools") import pyboard # Paths for host executables -CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3') -MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', '../ports/unix/micropython-coverage') +CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3") +MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/micropython-coverage") -NATMOD_EXAMPLE_DIR = '../examples/natmod/' +NATMOD_EXAMPLE_DIR = "../examples/natmod/" # Supported tests and their corresponding mpy module TEST_MAPPINGS = { - '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', + "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", } # Code to allow a target MicroPython to import an .mpy from RAM @@ -57,6 +57,7 @@ uos.chdir('/__remote') sys.modules['{}'] = __import__('__injected') """ + class TargetSubprocess: def __init__(self, cmd): self.cmd = cmd @@ -66,10 +67,13 @@ class TargetSubprocess: def run_script(self, script): try: - p = subprocess.run(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, input=script) + p = subprocess.run( + self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, input=script + ) return p.stdout, None except subprocess.CalledProcessError as er: - return b'', er + return b"", er + class TargetPyboard: def __init__(self, pyb): @@ -84,10 +88,11 @@ class TargetPyboard: try: self.pyb.enter_raw_repl() output = self.pyb.exec_(script) - output = output.replace(b'\r\n', b'\n') + output = output.replace(b"\r\n", b"\n") return output, None except pyboard.PyboardError as er: - return b'', er + return b"", er + def run_tests(target_truth, target, args, stats): for test_file in args.files: @@ -95,70 +100,79 @@ def run_tests(target_truth, target, args, stats): for k, v in TEST_MAPPINGS.items(): if test_file.find(k) != -1: test_module = k - test_mpy = v.replace('$(ARCH)', args.arch) + test_mpy = v.replace("$(ARCH)", args.arch) break else: - print('---- {} - no matching mpy'.format(test_file)) + print("---- {} - no matching mpy".format(test_file)) continue # Read test script - with open(test_file, 'rb') as f: + with open(test_file, "rb") as f: test_file_data = f.read() # Create full test with embedded .mpy try: - with open(NATMOD_EXAMPLE_DIR + test_mpy, 'rb') as f: - test_script = b'__buf=' + bytes(repr(f.read()), 'ascii') + b'\n' + with open(NATMOD_EXAMPLE_DIR + test_mpy, "rb") as f: + test_script = b"__buf=" + bytes(repr(f.read()), "ascii") + b"\n" except OSError: - print('---- {} - mpy file not compiled'.format(test_file)) + print("---- {} - mpy file not compiled".format(test_file)) continue - test_script += bytes(injected_import_hook_code.format(test_module), 'ascii') + test_script += bytes(injected_import_hook_code.format(test_module), "ascii") test_script += test_file_data # Run test under MicroPython result_out, error = target.run_script(test_script) # Work out result of test - extra = '' - if error is None and result_out == b'SKIP\n': - result = 'SKIP' + extra = "" + if error is None and result_out == b"SKIP\n": + result = "SKIP" elif error is not None: - result = 'FAIL' - extra = ' - ' + str(error) + result = "FAIL" + extra = " - " + str(error) else: # Check result against truth try: - with open(test_file + '.exp', 'rb') as f: + with open(test_file + ".exp", "rb") as f: result_exp = f.read() error = None except OSError: result_exp, error = target_truth.run_script(test_file_data) if error is not None: - result = 'TRUTH FAIL' + result = "TRUTH FAIL" elif result_out != result_exp: - result = 'FAIL' + result = "FAIL" print(result_out) else: - result = 'pass' + result = "pass" # Accumulate statistics - stats['total'] += 1 - if result == 'pass': - stats['pass'] += 1 - elif result == 'SKIP': - stats['skip'] += 1 + stats["total"] += 1 + if result == "pass": + stats["pass"] += 1 + elif result == "SKIP": + stats["skip"] += 1 else: - stats['fail'] += 1 + stats["fail"] += 1 # Print result - print('{:4} {}{}'.format(result, test_file, extra)) + print("{:4} {}{}".format(result, test_file, extra)) + def main(): - 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') + 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") args = cmd_parser.parse_args() target_truth = TargetSubprocess([CPYTHON3]) @@ -168,21 +182,22 @@ def main(): else: target = TargetSubprocess([MICROPYTHON]) - stats = {'total': 0, 'pass': 0, 'fail':0, 'skip': 0} + stats = {"total": 0, "pass": 0, "fail": 0, "skip": 0} run_tests(target_truth, target, args, stats) target.close() target_truth.close() - 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'])) + 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"])) - if stats['fail']: + if stats["fail"]: sys.exit(1) + if __name__ == "__main__": main() |