Jack1808/Claude_Code
0
1import os2from unittest.mock import patch3 4 5def test_process_registry_register_pid_zero_noop():6 """register_pid(0) is a no-op (early return)."""7 from cli import process_registry as pr8 9 before = len(pr._pids)10 pr.register_pid(0)11 assert len(pr._pids) == before12 13 14def test_process_registry_unregister_pid_zero_noop():15 """unregister_pid(0) is a no-op."""16 from cli import process_registry as pr17 18 pr.register_pid(99999)19 pr.unregister_pid(0)20 assert 99999 in pr._pids21 pr.unregister_pid(99999)22 23 24def test_process_registry_ensure_atexit_idempotent():25 """Second call to ensure_atexit_registered is idempotent."""26 from cli import process_registry as pr27 28 pr.ensure_atexit_registered()29 pr.ensure_atexit_registered()30 # Should not raise; atexit handler registered once31 32 33def test_process_registry_kill_all_exception_logged_no_raise(monkeypatch):34 """Exception in os.kill/taskkill is logged but does not raise."""35 from cli import process_registry as pr36 37 monkeypatch.setattr(pr, "_pids", {99999})38 monkeypatch.setattr(os, "name", "posix", raising=False)39 40 def _kill_raises(pid, sig):41 raise ProcessLookupError("no such process")42 43 with patch("os.kill", _kill_raises):44 pr.kill_all_best_effort()45 # Should not raise46 47 48def test_process_registry_register_unregister_does_not_crash():49 from cli import process_registry as pr50 51 pr.register_pid(12345)52 pr.unregister_pid(12345)53 54 55def test_process_registry_kill_all_best_effort_empty_is_noop():56 from cli import process_registry as pr57 58 # Ensure no exception on empty set59 pr.kill_all_best_effort()60 61 62def test_process_registry_kill_all_best_effort_windows_noop_when_taskkill_missing(63 monkeypatch,64):65 from cli import process_registry as pr66 67 # Simulate windows path in a stable way.68 monkeypatch.setattr(pr, "_pids", {12345})69 monkeypatch.setattr(os, "name", "nt", raising=False)70 71 # If taskkill isn't callable, we still should not crash.72 import subprocess73 74 def _boom(*args, **kwargs):75 raise FileNotFoundError("taskkill missing")76 77 monkeypatch.setattr(subprocess, "run", _boom)78 pr.kill_all_best_effort()79 