fix(plugins): use uv when pip is unavailable

This commit is contained in:
KDB
2026-08-03 15:42:26 +08:00
committed by Xubin Ren
parent 5c72fdcd88
commit 52bc79d3a0
2 changed files with 87 additions and 1 deletions
+65
View File
@@ -2479,6 +2479,69 @@ def test_optional_features_payload_preserves_legacy_flat_feishu_config(monkeypat
assert "instances" not in saved
@pytest.mark.parametrize(
"index_url",
[
"",
"https://mirror.example/simple",
],
)
def test_enable_uses_uv_when_tool_environment_has_no_pip(
monkeypatch,
index_url,
):
from nanobot import optional_features
calls: list[list[str]] = []
call_envs: list[dict[str, str] | None] = []
def _run(
argv: list[str],
*,
env: dict[str, str] | None = None,
) -> subprocess.CompletedProcess[str]:
calls.append(argv)
call_envs.append(env)
if len(calls) == 1:
return subprocess.CompletedProcess(argv, 1, stdout="", stderr="No module named pip")
if argv[0] == "uv":
return subprocess.CompletedProcess(argv, 0, stdout="", stderr="")
return subprocess.CompletedProcess(
argv,
1,
stdout="",
stderr="No module named ensurepip",
)
monkeypatch.setattr("shutil.which", lambda name: "uv" if name == "uv" else None)
monkeypatch.setenv("HTTPS_PROXY", "http://proxy.example:8080")
monkeypatch.delenv("UV_INDEX_URL", raising=False)
if index_url:
monkeypatch.setenv("PIP_INDEX_URL", index_url)
else:
monkeypatch.delenv("PIP_INDEX_URL", raising=False)
assert optional_features.install_extra("feishu", ["lark-oapi>=1.5.0"], runner=_run).ok is True
assert calls == [
[sys.executable, "-m", "pip", "install", "lark-oapi>=1.5.0"],
[
"uv",
"pip",
"install",
"--python",
sys.executable,
"lark-oapi>=1.5.0",
],
]
assert call_envs[0] is None
assert call_envs[1] is not None
assert call_envs[1]["HTTPS_PROXY"] == "http://proxy.example:8080"
if index_url:
assert call_envs[1]["UV_INDEX_URL"] == index_url
else:
assert "UV_INDEX_URL" not in call_envs[1]
def test_enable_bootstraps_pip_with_ensurepip(monkeypatch):
from nanobot import optional_features
@@ -2490,6 +2553,8 @@ def test_enable_bootstraps_pip_with_ensurepip(monkeypatch):
return subprocess.CompletedProcess(argv, 1, stdout="", stderr="No module named pip")
return subprocess.CompletedProcess(argv, 0, stdout="", stderr="")
monkeypatch.setattr("shutil.which", lambda _name: None)
assert optional_features.install_extra("bedrock", None, runner=_run).ok is True
assert calls == [
[sys.executable, "-m", "pip", "install", "nanobot-ai[bedrock]"],