fix(git proxy): start/stop 只认领匹配进程(防误杀同端口其它进程) + status/start 自动恢复指向死端口的 stale git proxy

This commit is contained in:
Zhengshou Lai
2026-08-16 17:59:06 +08:00
parent b150bafa4e
commit 52acb197a4
2 changed files with 144 additions and 9 deletions
+62
View File
@@ -11,3 +11,65 @@ def test_proxy_loopback_port_http():
def test_proxy_loopback_port_rejects_remote():
assert _proxy_loopback_port("http://10.211.55.2:38457") is None
assert _proxy_loopback_port("socks5://127.0.0.1:1080") is None
def test_procs_matching_filters_by_command():
from mytoolkit.commands.git import _procs_matching
import os
pid = os.getpid()
# Current test process command line contains "python".
assert pid in _procs_matching([pid], "python")
assert _procs_matching([pid], "definitely-not-a-real-proc-xyz") == []
def test_restore_stale_git_proxy_restores_when_port_dead(monkeypatch):
from mytoolkit.commands.git import _restore_stale_git_proxy
restored: dict[str, object] = {}
monkeypatch.setattr(
"mytoolkit.commands.git._read_state",
lambda: {"old_http": "http://old.example:1", "old_https": None},
)
monkeypatch.setattr(
"mytoolkit.commands.git._get_git_proxy",
lambda key: "http://127.0.0.1:59999",
)
monkeypatch.setattr(
"mytoolkit.commands.git._proxy_loopback_port", lambda url: 59999
)
monkeypatch.setattr(
"mytoolkit.commands.git._port_listening_on", lambda host, port: False
)
monkeypatch.setattr(
"mytoolkit.commands.git._restore_proxy_key",
lambda key, old: restored.__setitem__(key, old),
)
assert _restore_stale_git_proxy() is True
assert restored == {"http.proxy": "http://old.example:1", "https.proxy": None}
def test_restore_stale_git_proxy_noop_when_alive(monkeypatch):
from mytoolkit.commands.git import _restore_stale_git_proxy
restored: dict[str, object] = {}
monkeypatch.setattr(
"mytoolkit.commands.git._read_state",
lambda: {"old_http": "http://old.example:1"},
)
monkeypatch.setattr(
"mytoolkit.commands.git._get_git_proxy",
lambda key: "http://127.0.0.1:59999",
)
monkeypatch.setattr(
"mytoolkit.commands.git._proxy_loopback_port", lambda url: 59999
)
monkeypatch.setattr(
"mytoolkit.commands.git._port_listening_on", lambda host, port: True
)
monkeypatch.setattr(
"mytoolkit.commands.git._restore_proxy_key",
lambda key, old: restored.__setitem__(key, old),
)
assert _restore_stale_git_proxy() is False
assert restored == {}