|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import subprocess, junit_xml, os, sys |
| 4 | +from pathlib import Path |
| 5 | +from multiprocessing import Pool |
| 6 | +import regex as re # supports multi-threading |
| 7 | + |
| 8 | +test_case_re = re.compile("Running test \"(.*?)\".+?-{2,}.*?(PASSED|FAILED)", flags=re.DOTALL) |
| 9 | +LUACOV_CONFIG = Path(os.path.abspath(__file__)).parent.joinpath("config.luacov") |
| 10 | +DRIVER_DIRS = Path(os.path.abspath(__file__)).parents[1].joinpath("drivers") |
| 11 | +DRIVERS = [driver for driver in DRIVER_DIRS.glob("*/*") if driver.is_dir()] # this gets all the children of the children of the drivers directory |
| 12 | +CHANGED_DRIVERS = [Path(driver).name for driver in sys.argv[1:]] |
| 13 | + |
| 14 | +def per_driver_task(driver_dir): |
| 15 | + os.chdir(driver_dir.joinpath('src')) |
| 16 | + results = map(run_test, driver_dir.glob("src/test/test_*.lua")) |
| 17 | + successes, failures, failure_output, test_suites = 0, 0, "", [] |
| 18 | + for result in results: |
| 19 | + test_suites.append(result[0]) |
| 20 | + successes += result[1] |
| 21 | + failures += result[2] |
| 22 | + if result[3] != "": |
| 23 | + failure_output += result[3] + '\n' |
| 24 | + with open(driver_dir.parent.parent.parent.joinpath("tools/test_output/").joinpath(driver_dir.name+"_test_output.xml"), 'w+') as outfile: |
| 25 | + junit_xml.to_xml_report_file(outfile, test_suites) |
| 26 | + print("{}: passed {} of {} tests".format(driver_dir.name, successes, successes+failures)) |
| 27 | + if failure_output != "": |
| 28 | + failure_output = driver_dir.name + ": \n" + failure_output |
| 29 | + else: |
| 30 | + failure_output = None |
| 31 | + if driver_dir.name in CHANGED_DRIVERS: |
| 32 | + with driver_dir.parent.parent.parent.joinpath("tools/coverage_output").joinpath(driver_dir.name+"_coverage.xml") as outfile: |
| 33 | + subprocess.run("luacov-cobertura -o {} -c {}".format(outfile, LUACOV_CONFIG), shell=True) |
| 34 | + return failure_output |
| 35 | + |
| 36 | +def run_test(test_file): |
| 37 | + if test_file.parent.parent.parent.name in CHANGED_DRIVERS: |
| 38 | + a = subprocess.run("lua -lluacov {}".format(test_file), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 39 | + else: |
| 40 | + a = subprocess.run("lua {}".format(test_file), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 41 | + error = a.stderr.decode() |
| 42 | + if error and error != "": |
| 43 | + print(error) |
| 44 | + parsed_output = a.stdout.decode() |
| 45 | + test_suite_name = str(test_file)[str(test_file).rindex('/')+1:-4].replace('_',' ') |
| 46 | + test_suite = junit_xml.TestSuite(test_suite_name) |
| 47 | + successes, failures, failure_output, test_cases = 0, 0, "", [] |
| 48 | + for match in test_case_re.finditer(parsed_output): |
| 49 | + test_case = junit_xml.TestCase(match[1]) |
| 50 | + test_case.stdout = match[0] |
| 51 | + if match[2] == "FAILED": |
| 52 | + failures += 1 |
| 53 | + if "traceback" in match[0]: |
| 54 | + failure_output += "\t{} ERROR in {}\n".format(test_suite_name, match[1]) |
| 55 | + test_case.add_error_info("ERROR", match[0]) |
| 56 | + else: |
| 57 | + failure_output += "\t{} FAILED on {}\n".format(test_suite_name, match[1]) |
| 58 | + test_case.add_failure_info("FAILED", match[0]) |
| 59 | + else: |
| 60 | + successes += 1 |
| 61 | + test_cases.append(test_case) |
| 62 | + test_suite.test_cases = test_cases |
| 63 | + return (test_suite, successes, failures, failure_output) |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + |
| 67 | + try: |
| 68 | + os.mkdir(Path(os.path.abspath(__file__)).parent.joinpath("test_output")) |
| 69 | + except FileExistsError: |
| 70 | + pass |
| 71 | + |
| 72 | + try: |
| 73 | + os.mkdir(Path(os.path.abspath(__file__)).parent.joinpath("coverage_output")) |
| 74 | + except FileExistsError: |
| 75 | + pass |
| 76 | + |
| 77 | + failure_output = "" |
| 78 | + with Pool() as pool: |
| 79 | + failure_output = pool.map(per_driver_task, DRIVERS) |
| 80 | + |
| 81 | + exit_code = 0 |
| 82 | + |
| 83 | + for test_case in failure_output: |
| 84 | + if test_case: |
| 85 | + print(test_case) |
| 86 | + exit_code = 1 |
| 87 | + sys.exit(exit_code) |
0 commit comments