Files
l2/tests/test.py
2026-02-04 09:55:07 +01:00

88 lines
2.7 KiB
Python

#!/usr/bin/python
import sys
import os
import subprocess
import platform
import re
COLORS = {
"red": "\033[91m",
"green": "\033[92m",
"yellow": "\033[93m",
"blue": "\033[94m",
"reset": "\033[0m"
}
def print_colored(text, color):
print(COLORS.get(color, "") + text + COLORS["reset"], end="")
def _is_arm_host():
machine = platform.machine().lower()
return machine.startswith("arm") or machine.startswith("aarch")
def _wrap_qemu_for_arm(command):
if "qemu-x86_64" in command:
return command
pattern = re.compile(r"(^|\s*(?:&&|;)\s*)(\./\S+)")
def _repl(match):
prefix = match.group(1)
binary = match.group(2)
return f"{prefix}qemu-x86_64 {binary}"
return pattern.sub(_repl, command)
def run_tests():
test_dir = "tests"
any_failed = False
if not os.path.isdir(test_dir):
print("No 'tests' directory found.")
return 1
for file in sorted(os.listdir(test_dir)):
if file.endswith(".test"):
test_path = os.path.join(test_dir, file)
expected_path = test_path.replace(".test", ".expected")
if not os.path.isfile(expected_path):
print(f"Missing expected output file for {file}")
any_failed = True
continue
with open(test_path, "r") as test_file:
command = test_file.read().strip()
with open(expected_path, "r") as expected_file:
expected_output = expected_file.read().strip()
try:
run_command = _wrap_qemu_for_arm(command) if _is_arm_host() else command
result = subprocess.run(run_command, shell=True, text=True, capture_output=True)
actual_output = result.stdout.strip()
stderr_output = result.stderr.strip()
if result.returncode == 0 and actual_output == expected_output:
print_colored("[OK] ", "green")
print(f"{file} passed")
else:
print_colored("[ERR] ", "red")
print(f"{file} failed (exit {result.returncode})")
print(f"Expected:\n{expected_output}")
print(f"Got:\n{actual_output}")
if stderr_output:
print(f"Stderr:\n{stderr_output}")
any_failed = True
except Exception as e:
print_colored(f"Error running {file}: {e}", "red")
any_failed = True
print("All tests passed." if not any_failed else "Some tests failed.")
return 1 if any_failed else 0
if __name__ == "__main__":
sys.exit(run_tests())