marmoset-test-cases/run-tests.py

45 lines
2.0 KiB
Python
Raw Normal View History

2023-09-11 20:51:15 +01:00
#!/bin/python3
import glob
import subprocess
import os
2023-09-12 13:24:27 +01:00
import sys
2023-09-25 18:46:21 +01:00
import difflib
2023-09-11 20:51:15 +01:00
# will break if there are namespace collisions
2023-09-12 14:13:44 +01:00
if os.path.isfile("a.out"):
raise FileExistsError # TODO: generate uuid .out
2023-09-12 13:49:39 +01:00
file_list = glob.glob(f"{os.path.dirname(__file__)}/**/*.cases", recursive=True)
file_only_list = [os.path.basename(os.path.splitext(x)[0]) for x in file_list]
2023-09-11 20:51:15 +01:00
2023-09-12 13:24:27 +01:00
if sys.platform == "win32":
2023-09-12 13:30:10 +01:00
os.system("color") # enables ANSI escape characters, bug in Python
2023-09-12 13:24:27 +01:00
2023-09-13 16:11:41 +01:00
exit_code = 0
2023-09-11 20:51:15 +01:00
for file_name in glob.glob("*.c", recursive=False):
if file_name.rstrip(".c") in file_only_list:
2023-09-14 18:58:12 +01:00
should_continue = "y" if os.getenv("DEBIAN_FRONTEND") == "noninteractive" else input(f"{file_name}; run (y/N)? ").lower()
2023-09-12 13:31:53 +01:00
if should_continue != "y":
2023-09-11 20:51:15 +01:00
continue
subprocess.run(["gcc", "-o", "a.out", "-std=c11", "-Wall", "-g", file_name], check=True)
with open(next(x for x in file_list if os.path.splitext(file_name)[0] in x)) as f:
to_run = f.read().strip().split("\n\n")
total_tests, passed_tests = 0, 0
2023-09-12 13:59:47 +01:00
for x in to_run:
2023-09-25 18:45:16 +01:00
x_split = x.split("\n", 1)
2023-09-12 14:04:53 +01:00
return_data = subprocess.run(["./a.out"], input=x_split[0].rstrip().encode(), capture_output=True).stdout.decode().rstrip()
test_passed_int = int(return_data == x_split[1].rstrip())
total_tests += 1
passed_tests += test_passed_int
2023-09-25 18:45:16 +01:00
print(f"\u001b[{31 + test_passed_int}m({x_split[0]})\u001b[0m")
2023-09-25 19:58:01 +01:00
if not passed_tests:
[print(x) for x in difflib.Differ().compare(x_split[1].splitlines(), return_data.splitlines())]
else:
[print(x) for x in return_data.splitlines()]
print(f"\u001b[32m{passed_tests} tests passed\u001b[0m, \u001b[{31 - 31*int(total_tests == passed_tests)}m{total_tests - passed_tests} tests failed\u001b[0m")
2023-09-13 16:11:41 +01:00
exit_code += total_tests - passed_tests
2023-09-12 14:10:27 +01:00
os.remove("a.out")
2023-09-13 16:11:41 +01:00
sys.exit(exit_code)