-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutest.py
More file actions
101 lines (86 loc) · 3.27 KB
/
utest.py
File metadata and controls
101 lines (86 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
"""
UTEST SCRIPT - Tests unitarios de todos los módulos (common, AOS y SOA)
"""
import subprocess
import sys
import pathlib
import argparse
from datetime import datetime
# === CONFIGURACIÓN DE RUTAS ===
ROOT = pathlib.Path(__file__).resolve().parent
OUT_DIR = ROOT / "out"
BUILD_DIR = OUT_DIR / "build" / "clang-tidy"
LOG_DIR = OUT_DIR / "unit_results"
LOG_DIR.mkdir(parents=True, exist_ok=True)
LOG_FILE = LOG_DIR / "utest_log.txt"
# === FUNCIONES AUXILIARES ===
def log(msg, file=None):
"""Imprime y opcionalmente escribe en un archivo"""
print(msg)
if file:
file.write(msg + "\n")
def run_cmd(cmd, desc, file=None):
"""Ejecuta un comando y devuelve True/False según el resultado"""
log(f"\n=== {desc} ===", file)
log(f"$ {' '.join(cmd)}", file)
result = subprocess.run(cmd, capture_output=True, text=True)
if result.stdout:
log(result.stdout, file)
if result.stderr:
log(result.stderr, file)
return result.returncode == 0
def build_targets(targets, file):
"""Compila los targets especificados"""
if not run_cmd(["cmake", "--preset", "clang-tidy"], "Configurando proyecto (CMake)", file):
return False
for target in targets:
if not run_cmd(
["cmake", "--build", "--preset", "clang-tidy-debug", "--target", target],
f"Compilando target {target}",
file,
):
return False
return True
def run_gtest_binary(name, file):
"""Ejecuta el binario de test unitario correspondiente"""
binary_path = BUILD_DIR / name / "Debug" / name
if not binary_path.exists():
log(f"No se encontró el binario {binary_path}", file)
return False
return run_cmd([str(binary_path), "--gtest_color=yes"], f"Ejecutando tests de {name}", file)
# === MAIN ===
def main():
parser = argparse.ArgumentParser(description="Ejecuta todos los tests unitarios (common, AOS y SOA)")
parser.add_argument("--no-build", action="store_true", help="No recompilar, solo ejecutar tests")
args = parser.parse_args()
# === TARGETS ===
targets = ["utcommon", "utaos", "utsoa"]
start_time = datetime.now()
with open(LOG_FILE, "w") as f:
f.write(f"=== LOG DE TESTS UNITARIOS ({start_time}) ===\n")
log("Iniciando ejecución de tests unitarios...\n", f)
# === COMPILACIÓN (opcional) ===
if not args.no_build:
if not build_targets(targets, f):
log("Error durante la compilación. Abortando.", f)
sys.exit(1)
# === EJECUCIÓN DE TESTS ===
all_passed = True
for target in targets:
ok = run_gtest_binary(target, f)
if not ok:
all_passed = False
# === DURACIÓN TOTAL ===
duration = (datetime.now() - start_time).total_seconds()
log(f"\nDuración total: {duration:.2f} s", f)
log(f"Log completo guardado en: {LOG_FILE}", f)
# === RESULTADO FINAL ===
if all_passed:
print("\nTodos los tests unitarios pasaron correctamente.")
sys.exit(0)
else:
print("\nAlgunos tests unitarios fallaron.")
sys.exit(1)
if __name__ == "__main__":
main()