File size: 2,189 Bytes
6172a47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
from unittest.mock import patch


def test_process_registry_register_pid_zero_noop():
    """register_pid(0) is a no-op (early return)."""
    from cli import process_registry as pr

    before = len(pr._pids)
    pr.register_pid(0)
    assert len(pr._pids) == before


def test_process_registry_unregister_pid_zero_noop():
    """unregister_pid(0) is a no-op."""
    from cli import process_registry as pr

    pr.register_pid(99999)
    pr.unregister_pid(0)
    assert 99999 in pr._pids
    pr.unregister_pid(99999)


def test_process_registry_ensure_atexit_idempotent():
    """Second call to ensure_atexit_registered is idempotent."""
    from cli import process_registry as pr

    pr.ensure_atexit_registered()
    pr.ensure_atexit_registered()
    # Should not raise; atexit handler registered once


def test_process_registry_kill_all_exception_logged_no_raise(monkeypatch):
    """Exception in os.kill/taskkill is logged but does not raise."""
    from cli import process_registry as pr

    monkeypatch.setattr(pr, "_pids", {99999})
    monkeypatch.setattr(os, "name", "posix", raising=False)

    def _kill_raises(pid, sig):
        raise ProcessLookupError("no such process")

    with patch("os.kill", _kill_raises):
        pr.kill_all_best_effort()
    # Should not raise


def test_process_registry_register_unregister_does_not_crash():
    from cli import process_registry as pr

    pr.register_pid(12345)
    pr.unregister_pid(12345)


def test_process_registry_kill_all_best_effort_empty_is_noop():
    from cli import process_registry as pr

    # Ensure no exception on empty set
    pr.kill_all_best_effort()


def test_process_registry_kill_all_best_effort_windows_noop_when_taskkill_missing(
    monkeypatch,
):
    from cli import process_registry as pr

    # Simulate windows path in a stable way.
    monkeypatch.setattr(pr, "_pids", {12345})
    monkeypatch.setattr(os, "name", "nt", raising=False)

    # If taskkill isn't callable, we still should not crash.
    import subprocess

    def _boom(*args, **kwargs):
        raise FileNotFoundError("taskkill missing")

    monkeypatch.setattr(subprocess, "run", _boom)
    pr.kill_all_best_effort()