Skip to content

Commit d49d301

Browse files
committed
fix: improve cross-platform test robustness for kill_proc_tree
- Use signal number 9 instead of signal.SIGKILL (unavailable on Windows) - Properly save/restore dynamically added os.killpg and signal.SIGKILL with try/finally cleanup to prevent test pollution
1 parent e25c4dc commit d49d301

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

tests/test_coverage_gaps.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,7 @@ def test_kill_proc_tree_linux(self):
16201620
patch("os.killpg") as mock_killpg:
16211621
sess_mod._kill_proc_tree(mock_proc)
16221622

1623-
mock_killpg.assert_called_once_with(1234, signal.SIGKILL)
1623+
mock_killpg.assert_called_once_with(1234, 9)
16241624
mock_proc.kill.assert_not_called()
16251625

16261626

@@ -2464,23 +2464,23 @@ def test_linux_killpg_without_psutil(self):
24642464
mock_proc = MagicMock()
24652465
mock_proc.pid = 1234
24662466

2467-
# Add os.killpg if it doesn't exist (Windows)
2468-
if not hasattr(os, "killpg"):
2467+
orig_killpg = getattr(os, "killpg", None)
2468+
orig_sigkill = getattr(signal, "SIGKILL", None)
2469+
if orig_killpg is None:
24692470
os.killpg = lambda pgid, sig: None
2470-
2471-
if not hasattr(signal, "SIGKILL"):
2472-
signal.SIGKILL = 9
2471+
if orig_sigkill is None:
2472+
signal.SIGKILL = 9 # type: ignore[attr-defined]
24732473

24742474
try:
24752475
with patch.dict("sys.modules", {"psutil": None}):
24762476
with patch("chcode.utils.shell.session.os.killpg") as mock_kill:
24772477
_kill_proc_tree(mock_proc)
2478-
# killpg should be called
24792478
assert mock_kill.called
24802479
finally:
2481-
# Clean up if we added them
2482-
if "killpg" in dir(os):
2483-
pass # Keep it for other tests
2480+
if orig_killpg is None and hasattr(os, "killpg"):
2481+
delattr(os, "killpg")
2482+
if orig_sigkill is None and hasattr(signal, "SIGKILL"):
2483+
delattr(signal, "SIGKILL")
24842484

24852485

24862486
# ────────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)