Files
mytoolkit/tests/test_git_proxy.py
T

88 lines
3.1 KiB
Python

"""Unit tests for git proxy helpers."""
from mytoolkit.commands.git import _proxy_loopback_port, _restore_baseline
def test_proxy_loopback_port_http():
assert _proxy_loopback_port("http://127.0.0.1:38457") == 38457
assert _proxy_loopback_port("http://localhost:10080") == 10080
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_restore_baseline_daemon_own_port_becomes_unset():
# Git already pointing at the daemon's loopback port → treat as no proxy.
assert _restore_baseline("http://127.0.0.1:38457", 38457) == "(not set)"
assert _restore_baseline("http://localhost:38457", 38457) == "(not set)"
def test_restore_baseline_foreign_values_pass_through():
assert _restore_baseline("http://127.0.0.1:10080", 38457) == "http://127.0.0.1:10080"
assert _restore_baseline("(not set)", 38457) == "(not set)"
assert _restore_baseline("http://127.0.0.1:38457", None) == "http://127.0.0.1:38457"
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 == {}