From d96bcbc3754f1952dcc7a7ff50fb15a546274270 Mon Sep 17 00:00:00 2001 From: DeadlyChirp Date: Thu, 26 Nov 2026 12:57:27 +0100 Subject: [PATCH] tests: keep the TUI's os._exit(130) SIGINT handler out of the test process (TEST-LEONIDA-TUI-SIGINT-LEAK) run_setup() and LeonidaBoard.__enter__ call install_interrupt_handlers(), which replaces the process-wide SIGINT handler (and on Windows SIGBREAK and the console control handler) with one that calls os._exit(130) and never restores the old one. Under one shared runner (pytest tests) a later test that raises KeyboardInterrupt on purpose, such as test_run_semantic_tests.py's interrupt tests, then ends the whole test process with exit code 130 and no summary. Patch install_interrupt_handlers to a no-op for the module and add a test that the process SIGINT handler is unchanged after run_setup(). --- tests/test_leonida_tui.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_leonida_tui.py b/tests/test_leonida_tui.py index 72e9c2f323..5a35330687 100644 --- a/tests/test_leonida_tui.py +++ b/tests/test_leonida_tui.py @@ -5,6 +5,7 @@ import ctypes import os import io +import signal import sys import tempfile import unittest @@ -19,6 +20,23 @@ import leonida_tui # noqa: E402 import make_game # noqa: E402 +# run_setup() and LeonidaBoard.__enter__ call install_interrupt_handlers(), which replaces the process-wide SIGINT +# (and on Windows SIGBREAK and the console control) handler with one that calls os._exit(130) and never puts the old +# one back. Under a shared runner (`pytest tests`) a later test that raises KeyboardInterrupt on purpose, such as the +# semantic runner's interrupt tests (_thread.interrupt_main), then ends the whole test process with exit code 130 and +# no summary. The handlers are the TUI's, not what these tests check, so keep them out of the test process. +_ORIGINAL_SIGINT = signal.getsignal(signal.SIGINT) +_no_interrupt_handlers = mock.patch.object(leonida_tui, "install_interrupt_handlers", lambda: None) + + +def setUpModule(): + _no_interrupt_handlers.start() + + +def tearDownModule(): + _no_interrupt_handlers.stop() + + class EligibleTest(unittest.TestCase): def test_no_tui_flag_disables(self): @@ -490,5 +508,12 @@ def test_overlap_true_reports_stage_and_exception(self): self.assertEqual(err.count("failed with an unexpected exception"), 1) +class InterruptHandlersStayOutOfTestsTest(unittest.TestCase): + def test_setup_and_board_leave_the_process_sigint_handler_alone(self): + with mock.patch.object(leonida_tui.os, "cpu_count", return_value=8), mock.patch.object(leonida_tui, "_read_key", return_value="Enter"), mock.patch.object(leonida_tui, "enable_ansi", return_value=False), mock.patch.object(leonida_tui, "term_size", return_value=(80, 24)), mock.patch("sys.stdout"): + leonida_tui.run_setup(4, default_overlap=False) + self.assertIs(signal.getsignal(signal.SIGINT), _ORIGINAL_SIGINT) + + if __name__ == "__main__": unittest.main()