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