Tasty/run.py
2024-01-09 00:35:11 +01:00

74 lines
2.1 KiB
Python

import serial
import subprocess
import os
import re
import sys
from termcolor import colored
# Just the content we want to paste into the config.hpp file.
cfg_template = """
#ifndef STA_TASTY_CONFIG_HPP
#define STA_TASTY_CONFIG_HPP
#define TASTY_CASE_{}
#endif // STA_TASTY_CONFIG_HPP
"""
path = 'Tasty/src/cases/'
for case in os.listdir(path):
# Find the number of the test case.
match = re.match('case([0-9])+\.cpp', case)
number = match.group(1)
with open('Tasty/include/sta/tasty/config.hpp', 'w') as f:
f.write(cfg_template.format(number))
print(colored(f'Building case { number }...', 'blue'))
# Build the new project and upload it to the STM32.
out = subprocess.run([r'Tasty\flash.bat', case])
# Check if the bash script was successfully executed.
if out.returncode != 0:
print('Building\t' + colored('[FAILED]', 'red'))
continue
else:
print('Building\t' + colored('[PASSED]', 'red'))
print(colored(f'Running case { number }...', 'blue'))
# Store the results for each file and line combination in a dict.
results = dict()
with serial.Serial('COM4', baudrate=115200) as ser:
while True:
try:
output = ser.readline().decode()
except:
continue
# '[T]' is used to decode the end of a test case.
if '[T]' in output:
break
# Try to extract the test results from the serial output.
match = re.match('\[(.*?)\|([0-9]+)\|(0|1)\]', output)
if match is not None:
file, line, result = match.group(1), match.group(2), match.group(3)
# Update the result for the file and line by logical ANDing it with the new result.
results[(file, line)] = results.get((file, line), True) and int(result) == 1
# Print the test results.
for (file, line), result in results.items():
if result:
print(file + ', line ' + line + '\t' + colored('[PASSED]' , 'green'))
else:
print(file + ', line ' + line + '\t' + colored('[FAILED]' , 'red'))