Skip to content

Commit ac0ba7f

Browse files
authored
feat: improve handling of uv_python_preference (#264)
When uv_python_reference is used as variable with values for test environment variants, if the variant does not include it "None" is passed but not handled. "none" being a valid input, simply return the lowered version of what has been passed.
1 parent 7556646 commit ac0ba7f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/tox_uv/_venv.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ def uv_python_preference_default(conf: object, name: object) -> str: # noqa: AR
7373
else os.environ.get("UV_PYTHON_PREFERENCE", "system")
7474
)
7575

76+
def uv_python_preference_post_process(value: str | None) -> str:
77+
if value is not None:
78+
return value.lower()
79+
return "system"
80+
7681
# The cast(...) might seems superfluous but removing it makes mypy crash. The problem isy on tox typing side.
7782
self.conf.add_config(
7883
keys=["uv_python_preference"],
@@ -90,6 +95,7 @@ def uv_python_preference_default(conf: object, name: object) -> str: # noqa: AR
9095
" interpreters with all tox environments and avoid accidental"
9196
" downloading of other interpreters."
9297
),
98+
post_process=uv_python_preference_post_process,
9399
)
94100

95101
def python_cache(self) -> dict[str, Any]:

tests/test_tox_uv_venv.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,35 @@ def test_uv_env_python_preference(
369369
assert env_bin_dir in result.out
370370

371371

372+
@pytest.mark.parametrize(
373+
"env",
374+
["3.10", "3.10-onlymanaged"],
375+
)
376+
def test_uv_env_python_preference_complex(
377+
tox_project: ToxProjectCreator,
378+
*,
379+
env: str,
380+
) -> None:
381+
project = tox_project({
382+
"tox.ini": (
383+
"[tox]\n"
384+
"env_list =\n"
385+
" 3.10\n"
386+
"[testenv]\n"
387+
"package=skip\n"
388+
"uv_python_preference=\n"
389+
" onlymanaged: only-managed\n"
390+
"commands=python -c 'print(\"{env_python}\")'"
391+
)
392+
})
393+
result = project.run("-vv", "-e", env)
394+
result.assert_success()
395+
396+
exe = "python.exe" if sys.platform == "win32" else "python"
397+
env_bin_dir = str(project.path / ".tox" / env / ("Scripts" if sys.platform == "win32" else "bin") / exe)
398+
assert env_bin_dir in result.out
399+
400+
372401
def test_uv_env_site_package_dir_run(tox_project: ToxProjectCreator) -> None:
373402
project = tox_project({"tox.ini": "[testenv]\npackage=skip\ncommands=python -c 'print(\"{envsitepackagesdir}\")'"})
374403
result = project.run("-vv")

0 commit comments

Comments
 (0)