From ea71a088435d4f1e51433c0a321f2afdff7814b1 Mon Sep 17 00:00:00 2001 From: Disservin Date: Tue, 7 Jan 2025 21:16:12 +0100 Subject: [PATCH] Improve Instrumented Python Testing Script For betting debugging and earlier stop in case of termination, like in https://github.com/official-stockfish/Stockfish/pull/5754#issuecomment-2576120357 closes https://github.com/official-stockfish/Stockfish/pull/5755 No functional change --- tests/testing.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/testing.py b/tests/testing.py index d51ca89ac92..bc1f6b15bf9 100644 --- a/tests/testing.py +++ b/tests/testing.py @@ -150,6 +150,7 @@ def __init__(self): self.failed_test_suites = 0 self.passed_tests = 0 self.failed_tests = 0 + self.stop_on_failure = True def has_failed(self) -> bool: return self.failed_test_suites > 0 @@ -167,6 +168,9 @@ def run(self, classes: List[type]) -> bool: self.failed_test_suites += 1 else: self.passed_test_suites += 1 + except Exception as e: + self.failed_test_suites += 1 + print(f"\n{RED_COLOR}Error: {e}{RESET_COLOR}") finally: os.chdir(original_cwd) @@ -226,6 +230,10 @@ def __run_test_method(self, test_instance, method: str) -> int: if isinstance(e, AssertionError): self.__handle_assertion_error(t0, method) + if self.stop_on_failure: + self.__print_buffer_output(buffer) + raise e + fails += 1 finally: self.__print_buffer_output(buffer) @@ -286,6 +294,11 @@ def __init__( self.start() + def _check_process_alive(self): + if not self.process or self.process.poll() is not None: + print("\n".join(self.output)) + raise RuntimeError("Stockfish process has terminated") + def start(self): if self.cli: self.process = subprocess.run( @@ -314,6 +327,8 @@ def send_command(self, command: str): if not self.process: raise RuntimeError("Stockfish process is not started") + self._check_process_alive() + self.process.stdin.write(command + "\n") self.process.stdin.flush() @@ -355,6 +370,7 @@ def readline(self): raise RuntimeError("Stockfish process is not started") while True: + self._check_process_alive() line = self.process.stdout.readline().strip() self.output.append(line)