From 7cf7d2f7849936e3c84ee431433c2e8bcb6706ac Mon Sep 17 00:00:00 2001 From: historia Date: Wed, 26 Aug 2026 00:21:28 -0400 Subject: fix: harden server start/stop guards --- app/tests/test_backends.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'app/tests/test_backends.py') diff --git a/app/tests/test_backends.py b/app/tests/test_backends.py index 8ea6a3f..1f146e5 100644 --- a/app/tests/test_backends.py +++ b/app/tests/test_backends.py @@ -332,7 +332,9 @@ class QwenUninstallTests(unittest.TestCase): def test_stops_servers_and_pips(self): from backends import qwen - with patch.object(qwen.servers, "stop") as mk_stop, \ + # Pid files exist for both managed servers, so stop runs. + with patch.object(qwen.servers, "pid_for", return_value=1234), \ + patch.object(qwen.servers, "stop") as mk_stop, \ patch.object(qwen.common, "pip_uninstall", return_value=0) as mk_pip: rc = qwen.uninstall(emit="EMIT") @@ -342,23 +344,36 @@ class QwenUninstallTests(unittest.TestCase): # The task view's emit is forwarded so pip never touches the terminal. mk_pip.assert_called_once_with([qwen.QWEN_PIP_PKG], emit="EMIT") + def test_skips_stop_when_no_server_was_started(self): + # No pid files: stop() is not called (no "not started by this + # tool" noise during an uninstall). + from backends import qwen + with patch.object(qwen.servers, "pid_for", return_value=None), \ + patch.object(qwen.servers, "stop") as mk_stop, \ + patch.object(qwen.common, "pip_uninstall", return_value=0): + rc = qwen.uninstall() + self.assertEqual(rc, 0) + mk_stop.assert_not_called() + def test_cancel_before_pip_skips_uninstall(self): import threading from backends import qwen cancel = threading.Event() cancel.set() - with patch.object(qwen.servers, "stop") as mk_stop, \ + with patch.object(qwen.servers, "pid_for", return_value=1234), \ + patch.object(qwen.servers, "stop") as mk_stop, \ patch.object(qwen.common, "pip_uninstall") as mk_pip: rc = qwen.uninstall(cancel=cancel) self.assertEqual(rc, 130) self.assertEqual(mk_stop.call_count, 2) mk_pip.assert_not_called() - def test_pip_failure_warns_but_still_succeeds(self): + def test_pip_failure_propagates_the_exit_code(self): from backends import qwen - with patch.object(qwen.servers, "stop"), \ + with patch.object(qwen.servers, "pid_for", return_value=1234), \ + patch.object(qwen.servers, "stop"), \ patch.object(qwen.common, "pip_uninstall", return_value=1): rc = qwen.uninstall() - self.assertEqual(rc, 0) + self.assertEqual(rc, 1) -- cgit v1.2.3