Skip to content

Commit d904db8

Browse files
merge upstream with uv usage
1 parent 3552d66 commit d904db8

1 file changed

Lines changed: 60 additions & 95 deletions

File tree

‎tests/test_cookbook_helpers.py‎

Lines changed: 60 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import shlex
34
import subprocess
45
import sys
56
from pathlib import Path
@@ -67,7 +68,7 @@ async def _fake_exec(*args, **kwargs):
6768
timeout=5,
6869
connect_timeout=4,
6970
strict_host_key_checking=False,
70-
stdin_data=b"python -m pip install vllm",
71+
stdin_data=b"uv pip install --python python3 vllm",
7172
)
7273

7374
assert rc == 0
@@ -87,7 +88,7 @@ async def _fake_exec(*args, **kwargs):
8788
assert captured["stdin"] is not None
8889
assert captured["stdout"] is not None
8990
assert captured["stderr"] is not None
90-
assert captured["input"] == b"python -m pip install vllm"
91+
assert captured["input"] == b"uv pip install --python python3 vllm"
9192

9293

9394
def test_safe_env_prefix_leaves_compound_conda_prefix_unchanged():
@@ -224,88 +225,43 @@ def test_local_tooling_path_export_preserves_spaces_and_expands_path():
224225
assert line.endswith(':$PATH"') # $PATH stays expandable in double quotes
225226

226227

227-
def test_pip_install_fallback_chain_prefers_venv_safe_install():
228+
def test_pip_install_fallback_chain_uses_one_explicit_uv_target():
228229
chain = _pip_install_fallback_chain("huggingface_hub", upgrade=True)
229-
# First attempt: plain install, wrapped in status-preserving subshell
230+
230231
assert chain.startswith("bash -c '")
231-
assert "python3 -m pip install -q -U huggingface_hub" in chain
232-
# Fallback: --user first, then guarded --break-system-packages for PEP-668 pip.
233-
assert "python3 -m pip install --user -q -U huggingface_hub" in chain
234-
assert "python3 -m pip install --help 2>/dev/null | grep -q -- --break-system-packages" in chain
235-
assert "--user --break-system-packages" in chain
236-
assert "python3 -m pip install --user --break-system-packages -q -U huggingface_hub" in chain
237-
# No bare `| tail` (which would mask pip's exit code)
232+
assert "uv pip install --python python3 --no-cache -q -U huggingface_hub" in chain
233+
assert chain.count("uv pip install") == 1
234+
assert "--user" not in chain
235+
assert "--break-system-packages" not in chain
238236
assert "| tail" not in chain
239-
# Negated venv check with && — so failure in a venv propagates instead of
240-
# being masked as success by the venv_check's exit-0.
241-
assert "! python3 -c" in chain
242-
# The group uses && (not ||) between venv check and user attempt
243-
assert "&&" in chain
237+
assert "_rc=$?" in chain
238+
assert "exit $_rc" in chain
244239

245240

246-
def test_pip_install_fallback_chain_allows_custom_python_command():
241+
def test_pip_install_fallback_chain_maps_legacy_pip_to_python3():
247242
chain = _pip_install_fallback_chain("hf_xet", python_cmd="pip", upgrade=False)
248-
assert "pip install -q hf_xet" in chain
249-
assert "pip install --user -q hf_xet" in chain
250-
assert "pip install --help 2>/dev/null | grep -q -- --break-system-packages" in chain
251-
assert "pip install --user --break-system-packages -q hf_xet" in chain
252-
# venv check uses the python executable derived from the pip command
253-
assert 'python -c "import sys; sys.exit(0 if sys.prefix != sys.base_prefix else 1)"' in chain
254-
# All install attempts are wrapped in bash -c subshells
255-
assert chain.count("bash -c '") == 3
243+
244+
assert "uv pip install --python python3 --no-cache -q hf_xet" in chain
245+
assert "--user" not in chain
246+
assert "--break-system-packages" not in chain
247+
assert chain.count("bash -c '") == 1
256248

257249

258250
def test_pip_install_fallback_chain_accepts_python_executable():
259251
chain = _pip_install_fallback_chain("llama-cpp-python[server]", python_cmd="python")
260252

261-
assert "python -m pip install -q 'llama-cpp-python[server]'" in chain
262-
assert "python -m pip install --user -q 'llama-cpp-python[server]'" in chain
263-
assert "python -m pip install --help 2>/dev/null | grep -q -- --break-system-packages" in chain
264-
assert "python install " not in chain
265-
assert 'python -c "import sys; sys.exit(0 if sys.prefix != sys.base_prefix else 1)"' in chain
266-
267-
268-
def test_pip_install_fallback_chain_propagates_failure_in_venv():
269-
"""When base install fails inside a venv, the chain must exit non-zero.
253+
assert "uv pip install --python python --no-cache -q 'llama-cpp-python[server]'" in chain
254+
assert "--extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu" in chain
255+
assert "python -m pip" not in chain
270256

271-
The old `{ venv_check || user }` shape from #903 masked the failure:
272-
venv_check exited 0 (in venv), || short-circuited, and the group
273-
reported success even though nothing was installed. The negated
274-
`{ ! venv_check && user }` shape propagates the failure correctly.
275-
"""
276-
# Simulate "inside a venv" deterministically: the venv check exits 0.
277-
# Base install fails, venv_check exits 0, negated to 1,
278-
# && skips user, group exits 1. This avoids depending on whether the
279-
# test runner's own interpreter happens to be inside a venv (which
280-
# differs between local and CI environments).
281-
script = (
282-
"false || "
283-
"{ ! true " # venv_check=0 (in venv) → negated to 1 → user skipped
284-
"&& echo user_attempt; }"
285-
)
286-
result = subprocess.run(
287-
["bash", "-c", script],
288-
capture_output=True, text=True, timeout=10,
289-
)
290-
assert "user_attempt" not in result.stdout
291-
assert result.returncode != 0, "Chain should propagate failure when base fails in venv"
292257

258+
def test_pip_install_fallback_chain_has_no_system_mutation_fallback():
259+
chain = _pip_install_fallback_chain("hf_xet")
293260

294-
def test_pip_install_fallback_chain_tries_user_outside_venv():
295-
"""When base install fails outside a venv, the chain should try --user."""
296-
# Force "not in venv" by making venv_check return 1 directly.
297-
script = (
298-
"bash -c '"
299-
"python3 -c \"import sys; sys.exit(1)\" || "
300-
"{ ! python3 -c \"import sys; sys.exit(1)\" " # venv_check=1 → negated to 0 → user runs
301-
"&& echo user_attempt; }"
302-
"'"
303-
)
304-
result = subprocess.run(
305-
["bash", "-c", script],
306-
capture_output=True, text=True, timeout=10,
307-
)
308-
assert "user_attempt" in result.stdout, "Chain should try --user when not in venv and base fails"
261+
assert "uv pip install --python python3" in chain
262+
assert "--user" not in chain
263+
assert "--break-system-packages" not in chain
264+
assert " -m pip " not in chain
309265

310266

311267
def test_pip_install_fallback_chain_quotes_extras_spec():
@@ -314,15 +270,15 @@ def test_pip_install_fallback_chain_quotes_extras_spec():
314270
(which pulls in starlette_context for ``python -m llama_cpp.server``) is
315271
actually installed instead of a bare ``llama-cpp-python`` (issue #730)."""
316272
chain = _pip_install_fallback_chain("llama-cpp-python[server]", python_cmd="pip")
317-
# Quoted in the plain, --user, and guarded --break-system-packages attempts.
318-
assert chain.count("'llama-cpp-python[server]'") == 3
273+
# The one uv invocation preserves the quoted extra.
274+
assert chain.count("'llama-cpp-python[server]'") == 1
319275
# llama-cpp installs must prefer prebuilt wheels to avoid fragile source builds.
320276
assert "--extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu" in chain
321277
# Never the unquoted form (bracket-glob risk).
322278
assert "install -q llama-cpp-python[server]" not in chain
323279
# A plain package name is still passed through unquoted (no regression).
324280
plain = _pip_install_fallback_chain("hf_xet", python_cmd="pip")
325-
assert "install -q hf_xet" in plain
281+
assert "--no-cache -q hf_xet" in plain
326282

327283

328284
def test_serve_runner_installs_llama_cpp_server_extra():
@@ -376,31 +332,31 @@ def test_venv_safe_local_pip_install_strips_user_flags_only_for_local_venv():
376332
assert _venv_safe_local_pip_install_cmd(cmd, local=True, in_venv=False) == cmd
377333

378334

379-
def test_pip_install_runner_guards_break_system_packages():
335+
def test_pip_install_runner_rejects_raw_pip_mutations():
380336
lines = []
381337
_append_pip_install_runner_lines(
382338
lines,
383339
'python3 -m pip install --no-cache-dir --user --break-system-packages "llama-cpp-python[server]"',
384340
)
385-
script = "\n".join(lines)
386341

387-
assert "python3 -m pip install --help 2>/dev/null | grep -q -- --break-system-packages" in script
388-
assert 'python3 -m pip install --no-cache-dir --user --break-system-packages "llama-cpp-python[server]"' in script
389-
assert "python3 -m pip install --no-cache-dir --user 'llama-cpp-python[server]'" in script
390-
assert "pip does not support --break-system-packages" in script
342+
assert lines == [
343+
'echo "ERROR: Diogenes rejected a non-uv Python mutation."; exit 127'
344+
]
391345

392346

393-
def test_pip_install_runner_leaves_plain_commands_unchanged():
347+
def test_pip_install_runner_accepts_only_normalized_uv_commands():
394348
lines = []
395-
_append_pip_install_runner_lines(lines, "python3 -m pip install --no-cache-dir vllm")
349+
command = "uv pip install --python /srv/Diogenes/.venv/bin/python --no-cache vllm"
350+
_append_pip_install_runner_lines(lines, command)
396351

397-
assert lines == ["python3 -m pip install --no-cache-dir vllm"]
352+
assert lines == [command]
398353

399354

400355
def test_pip_install_attempt_wraps_in_status_preserving_subshell():
401-
"""Each pip attempt must be a bash -c subshell that captures output,
402-
prints tail, cleans up, and exits with pip's real status — not tail's."""
403-
snippet = _pip_install_attempt("pip install -q huggingface_hub")
356+
"""Each uv attempt captures output and preserves uv's real exit status."""
357+
snippet = _pip_install_attempt(
358+
"uv pip install --python python3 --no-cache -q huggingface_hub"
359+
)
404360
assert snippet.startswith("bash -c '")
405361
assert "$(mktemp)" in snippet
406362
assert "_rc=$?" in snippet
@@ -410,22 +366,26 @@ def test_pip_install_attempt_wraps_in_status_preserving_subshell():
410366

411367

412368
def test_pip_install_attempt_no_bare_pipe_tail():
413-
"""A bare `| tail` pipeline would mask pip's exit code — must not appear."""
414-
snippet = _pip_install_attempt("pip install -q huggingface_hub")
369+
"""A bare `| tail` pipeline would mask uv's exit code — must not appear."""
370+
snippet = _pip_install_attempt(
371+
"uv pip install --python python3 --no-cache -q huggingface_hub"
372+
)
415373
assert "| tail" not in snippet
416374

417375

418376
def test_pip_install_attempt_failure_propagates_real_exit_code():
419-
"""Run the generated snippet against a deliberately broken pip install
420-
to confirm the subshell exits with pip's non-zero status."""
421-
snippet = _pip_install_attempt("python3 -m pip install __nonexistent_package_12345__")
377+
"""A deliberately broken uv install must retain uv's non-zero status."""
378+
snippet = _pip_install_attempt(
379+
"uv pip install --python "
380+
f"{shlex.quote(sys.executable)} --no-cache __nonexistent_package_12345__"
381+
)
422382
result = subprocess.run(
423383
["bash", "-c", snippet],
424384
capture_output=True,
425385
text=True,
426386
timeout=60,
427387
)
428-
assert result.returncode != 0, "pip install of a nonexistent package should fail"
388+
assert result.returncode != 0, "uv install of a nonexistent package should fail"
429389

430390

431391
def test_pip_install_attempt_success_exits_zero():
@@ -441,8 +401,11 @@ def test_pip_install_attempt_success_exits_zero():
441401

442402

443403
def test_pip_install_attempt_surfaces_stderr_on_failure():
444-
"""On failure, the last 5 lines of pip output should appear in stdout."""
445-
snippet = _pip_install_attempt("python3 -m pip install __nonexistent_package_12345__")
404+
"""On failure, the last five lines of uv output should appear in stdout."""
405+
snippet = _pip_install_attempt(
406+
"uv pip install --python "
407+
f"{shlex.quote(sys.executable)} --no-cache __nonexistent_package_12345__"
408+
)
446409
result = subprocess.run(
447410
["bash", "-c", snippet],
448411
capture_output=True,
@@ -876,10 +839,12 @@ def test_cached_model_scan_uses_huggingface_cache_env(tmp_path):
876839

877840
def test_pip_install_no_cache_injects_flag():
878841
from routes.cookbook_helpers import _pip_install_no_cache
842+
# Raw pip payloads are left untouched for the route's uv normalizer; this
843+
# helper never creates a second system-pip execution path.
879844
assert _pip_install_no_cache("python -m pip install vllm") == \
880-
"python -m pip install --no-cache-dir vllm"
845+
"python -m pip install vllm"
881846
assert _pip_install_no_cache("pip install -q huggingface-hub") == \
882-
"pip install --no-cache-dir -q huggingface-hub"
847+
"pip install -q huggingface-hub"
883848
assert _pip_install_no_cache(
884849
"uv pip install --python /srv/Diogenes/.venv/bin/python -r /srv/vllm.lock"
885850
) == (

0 commit comments

Comments
 (0)