Add automatic cargo testing script (python) for all exercises

This commit is contained in:
2023-07-02 15:36:14 -05:00
parent 532545e592
commit a94a0827cd

61
rust/test.py Executable file
View File

@@ -0,0 +1,61 @@
#!/usr/bin/env python3
from pathlib import Path
import re
from subprocess import Popen, PIPE
from typing import List, Tuple
CURRENT_DIR = Path(__file__).parent
def main() -> None:
# Find all tests
test_directories: List[Path] = [x for x in CURRENT_DIR.iterdir(
) if x.is_dir() and x.joinpath("Cargo.toml").exists()]
for directory_path in test_directories:
passed, total = test_directory(directory_path)
print(f"{directory_path.name}: {passed}/{total}")
def test_directory(path: Path) -> Tuple[int, int]:
targets = list(
filter(
lambda target: target.suffix == ".rs",
path.joinpath("tests").iterdir()
)
)
passed, total = 0, 0
for target in targets:
results = test_target(path, target)
passed += results[0]
total += results[1]
return passed, total
def test_target(base_directory: Path, target: Path) -> Tuple[int, int]:
process = Popen(["cargo", "test", "--no-fail-fast", "--test", target.stem, "--", "--include-ignored"], cwd=base_directory, stdout=PIPE, stderr=PIPE)
process.wait()
stdout = process.stdout.read().decode("utf-8").strip().split("\n")
total_tests = int(re.match(r"running (\d+) tests?", stdout[0]).group(1))
results_match = re.search(r"test result: (.+). (\d+) passed;", stdout[-1])
passed_tests = int(results_match.group(2))
test_status = results_match.group(1)
# Unstable
if total_tests == passed_tests:
assert test_status == "ok"
else:
assert test_status != "ok"
return passed_tests, total_tests
if __name__ == "__main__":
main()