|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: 2019 Damien P. George |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: MIT |
| 6 | +# |
| 7 | +# MicroPython uasyncio module |
| 8 | +# MIT license; Copyright (c) 2019 Damien P. George |
| 9 | +# |
| 10 | + |
| 11 | +import sys |
| 12 | +import os |
| 13 | + |
| 14 | +try: |
| 15 | + from typing import List, Tuple |
| 16 | +except ImportError: |
| 17 | + pass |
| 18 | + |
| 19 | +AVAILABLE_SUITES = ["asyncio"] |
| 20 | + |
| 21 | + |
| 22 | +def get_interpreter(): |
| 23 | + interpreter = os.getenv("MICROPY_MICROPYTHON") |
| 24 | + |
| 25 | + if interpreter: |
| 26 | + return interpreter |
| 27 | + |
| 28 | + if sys.platform == "win32": |
| 29 | + return "micropython.exe" |
| 30 | + |
| 31 | + return "micropython" |
| 32 | + |
| 33 | + |
| 34 | +def get_testcases(suite: str) -> List[str]: |
| 35 | + if sys.platform == "win32": |
| 36 | + # dir /b prints only contained filenames, one on a line |
| 37 | + # http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/dir.mspx |
| 38 | + result = os.system("dir /b %s/*.py >tests.lst" % suite) |
| 39 | + else: |
| 40 | + result = os.system("ls %s/*.py | xargs -n1 basename >tests.lst" % suite) |
| 41 | + |
| 42 | + assert result == 0 |
| 43 | + |
| 44 | + with open("tests.lst") as test_list_file: |
| 45 | + testcases = test_list_file.readlines() |
| 46 | + testcases = [l[:-1] for l in testcases] |
| 47 | + |
| 48 | + os.system("rm tests.lst") |
| 49 | + assert testcases, "No tests found in dir '%s', which is implausible" % suite |
| 50 | + |
| 51 | + return testcases |
| 52 | + |
| 53 | + |
| 54 | +def run_testcase(suite: str, testcase: str): |
| 55 | + qtest = "%s/%s" % (suite, testcase) |
| 56 | + |
| 57 | + try: |
| 58 | + with open("%s.exp" % qtest) as expected_output_file: |
| 59 | + expected_output = expected_output_file.read() |
| 60 | + except OSError as exc: |
| 61 | + raise RuntimeError("SKIP") from exc |
| 62 | + |
| 63 | + result = os.system("{0} {1} 2> {1}.out > {1}.out".format(get_interpreter(), qtest)) |
| 64 | + |
| 65 | + with open("%s.out" % qtest) as actual_output_file: |
| 66 | + actual_output = actual_output_file.read() |
| 67 | + |
| 68 | + if result != 0: |
| 69 | + actual_output += "\n\nCRASH\n" |
| 70 | + |
| 71 | + if actual_output == "SKIP\n": |
| 72 | + print("skip %s" % qtest) |
| 73 | + raise RuntimeError("SKIP") |
| 74 | + |
| 75 | + if actual_output != expected_output: |
| 76 | + print("FAIL %s" % qtest) |
| 77 | + os.system("diff -u {0}.exp {0}.out".format(qtest)) |
| 78 | + return False |
| 79 | + |
| 80 | + print("pass %s" % qtest) |
| 81 | + return True |
| 82 | + |
| 83 | + |
| 84 | +def run_suite(suite: str) -> Tuple[int, int, int]: |
| 85 | + test_count = 0 |
| 86 | + passed_count = 0 |
| 87 | + skip_count = 0 |
| 88 | + |
| 89 | + testcases = get_testcases(suite) |
| 90 | + |
| 91 | + for testcase in testcases: |
| 92 | + try: |
| 93 | + if run_testcase(suite, testcase): |
| 94 | + passed_count += 1 |
| 95 | + |
| 96 | + test_count += 1 |
| 97 | + except RuntimeError as exc: |
| 98 | + if str(exc) == "SKIP": |
| 99 | + skip_count += 1 |
| 100 | + |
| 101 | + return test_count, passed_count, skip_count |
| 102 | + |
| 103 | + |
| 104 | +def main(): |
| 105 | + test_count = 0 |
| 106 | + passed_count = 0 |
| 107 | + skip_count = 0 |
| 108 | + |
| 109 | + for suite in AVAILABLE_SUITES: |
| 110 | + suite_test_count, suite_passed_count, suite_skip_count = run_suite(suite) |
| 111 | + |
| 112 | + test_count += suite_test_count |
| 113 | + passed_count += suite_passed_count |
| 114 | + skip_count += suite_skip_count |
| 115 | + |
| 116 | + print("-" * 20) |
| 117 | + print("%s tests performed" % test_count) |
| 118 | + print("%s tests passed" % passed_count) |
| 119 | + if test_count != passed_count: |
| 120 | + print("%s tests failed" % (test_count - passed_count)) |
| 121 | + if skip_count: |
| 122 | + print("%s tests skipped" % skip_count) |
| 123 | + |
| 124 | + if test_count - passed_count > 0: |
| 125 | + sys.exit(1) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
0 commit comments