Skip to content

Commit

Permalink
Respect UV_PYTHON_PREFERENCE environment variable (#159)
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <[email protected]>
  • Loading branch information
gaborbernat authored Jan 21, 2025
1 parent d8307c2 commit 9d847c4
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ jobs:
enable-cache: true
cache-dependency-glob: "pyproject.toml"
- name: Install tox
run: uv tool install --python-preference only-managed --python 3.13 tox --with tox-uv
run: uv tool install --python-preference only-managed --python 3.13 tox --with .
- name: Install Python
if: startsWith(matrix.env, '3.') && matrix.env != '3.13'
run: uv python install --python-preference only-managed ${{ matrix.env }}
- name: Setup test suite
run: tox run -vv --notest --skip-missing-interpreters false -e ${{ matrix.env }}
env:
UV_PYTHON_PREFERENCE: "only-managed"
- name: Run test suite
run: tox run --skip-pkg-install -e ${{ matrix.env }}
env:
PYTEST_ADDOPTS: "-vv --durations=20"
DIFF_AGAINST: HEAD
UV_PYTHON_PREFERENCE: "only-managed"
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
rev: v2.3.0
hooks:
- id: codespell
additional_dependencies: ["tomli>=2.0.2"]
additional_dependencies: ["tomli>=2.2.1"]
- repo: https://github.com/tox-dev/tox-ini-fmt
rev: "1.4.1"
hooks:
Expand Down
20 changes: 10 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
build-backend = "hatchling.build"
requires = [
"hatch-vcs>=0.4",
"hatchling>=1.25",
"hatchling>=1.27",
]

[project]
Expand Down Expand Up @@ -40,10 +40,10 @@ dynamic = [
"version",
]
dependencies = [
"packaging>=24.1",
"tox>=4.21.2,<5",
"packaging>=24.2",
"tox>=4.24.1,<5",
"typing-extensions>=4.12.2; python_version<'3.10'",
"uv>=0.4.18,<1",
"uv>=0.5.21,<1",
]
urls.Changelog = "https://github.com/tox-dev/tox-uv/releases"
urls.Documentation = "https://github.com/tox-dev/tox-uv#tox-uv"
Expand All @@ -62,14 +62,14 @@ dev = [
test = [
"covdefaults>=2.3",
"devpi-process>=1.0.2",
"diff-cover>=9.2",
"pytest>=8.3.3",
"pytest-cov>=5",
"diff-cover>=9.2.1",
"pytest>=8.3.4",
"pytest-cov>=6",
"pytest-mock>=3.14",
]
type = [ "mypy==1.11.2", { include-group = "test" } ]
lint = [ "pre-commit-uv>=4.1.3" ]
pkg-meta = [ "check-wheel-contents>=0.6", "twine>=5.1.1", "uv>=0.4.18" ]
type = [ "mypy==1.14.1", { include-group = "test" } ]
lint = [ "pre-commit-uv>=4.1.4" ]
pkg-meta = [ "check-wheel-contents>=0.6.1", "twine>=6.1", "uv>=0.5.21" ]

[tool.hatch]
build.hooks.vcs.version-file = "src/tox_uv/version.py"
Expand Down
2 changes: 1 addition & 1 deletion src/tox_uv/_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def register_config(self) -> None:
self.conf.add_config(
keys=["uv_python_preference"],
of_type=cast("Type[Optional[PythonPreference]]", Optional[PythonPreference]), # noqa: UP006
default="system",
default=lambda conf, name: self.environment_variables.get("UV_PYTHON_PREFERENCE", "system"), # noqa: ARG005
desc=(
"Whether to prefer using Python installations that are already"
" present on the system, or those that are downloaded and"
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def demo_pkg_inline(root: Path) -> Path:
return root / "demo_pkg_inline"


@pytest.fixture
def clear_python_preference_env_var(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("UV_PYTHON_PREFERENCE", raising=False)


pytest_plugins = [
"tox.pytest",
]
7 changes: 7 additions & 0 deletions tests/test_tox_uv_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tox.pytest import ToxProjectCreator


@pytest.mark.usefixtures("clear_python_preference_env_var")
def test_uv_lock_list_dependencies_command(tox_project: ToxProjectCreator) -> None:
project = tox_project({
"tox.ini": """
Expand Down Expand Up @@ -70,6 +71,7 @@ def test_uv_lock_list_dependencies_command(tox_project: ToxProjectCreator) -> No
assert calls[i] == expected[i]


@pytest.mark.usefixtures("clear_python_preference_env_var")
@pytest.mark.parametrize("verbose", ["", "-v", "-vv", "-vvv"])
def test_uv_lock_command(tox_project: ToxProjectCreator, verbose: str) -> None:
project = tox_project({
Expand Down Expand Up @@ -131,6 +133,7 @@ def test_uv_lock_command(tox_project: ToxProjectCreator, verbose: str) -> None:
assert show_uv_output is (bool(verbose))


@pytest.mark.usefixtures("clear_python_preference_env_var")
def test_uv_lock_with_dev(tox_project: ToxProjectCreator) -> None:
project = tox_project({
"tox.ini": """
Expand Down Expand Up @@ -166,6 +169,7 @@ def test_uv_lock_with_dev(tox_project: ToxProjectCreator) -> None:
assert calls == expected


@pytest.mark.usefixtures("clear_python_preference_env_var")
@pytest.mark.parametrize(
"name",
[
Expand Down Expand Up @@ -229,6 +233,7 @@ def test_uv_lock_with_install_pkg(tox_project: ToxProjectCreator, name: str) ->
assert calls == expected


@pytest.mark.usefixtures("clear_python_preference_env_var")
def test_uv_sync_extra_flags(tox_project: ToxProjectCreator) -> None:
project = tox_project({
"tox.ini": """
Expand Down Expand Up @@ -281,6 +286,7 @@ def test_uv_sync_extra_flags(tox_project: ToxProjectCreator) -> None:
assert calls == expected


@pytest.mark.usefixtures("clear_python_preference_env_var")
def test_uv_sync_extra_flags_toml(tox_project: ToxProjectCreator) -> None:
project = tox_project({
"tox.toml": """
Expand Down Expand Up @@ -333,6 +339,7 @@ def test_uv_sync_extra_flags_toml(tox_project: ToxProjectCreator) -> None:
assert calls == expected


@pytest.mark.usefixtures("clear_python_preference_env_var")
def test_uv_sync_dependency_groups(tox_project: ToxProjectCreator) -> None:
project = tox_project({
"tox.toml": """
Expand Down
30 changes: 30 additions & 0 deletions tests/test_tox_uv_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@ def test_uv_venv_pass_env(tox_project: ToxProjectCreator) -> None:
assert "PKG_CONFIG_PATH" in pass_through


@pytest.mark.usefixtures("clear_python_preference_env_var")
def test_uv_venv_preference_system_by_default(tox_project: ToxProjectCreator) -> None:
project = tox_project({"tox.ini": "[testenv]"})

result = project.run("c", "-k", "uv_python_preference")
result.assert_success()

parser = ConfigParser()
parser.read_string(result.out)
got = parser["testenv:py"]["uv_python_preference"]

assert got == "system"


def test_uv_venv_preference_override_via_env_var(
tox_project: ToxProjectCreator, monkeypatch: pytest.MonkeyPatch
) -> None:
project = tox_project({"tox.ini": "[testenv]"})
monkeypatch.setenv("UV_PYTHON_PREFERENCE", "only-managed")

result = project.run("c", "-k", "uv_python_preference")
result.assert_success()

parser = ConfigParser()
parser.read_string(result.out)
got = parser["testenv:py"]["uv_python_preference"]

assert got == "only-managed"


def test_uv_venv_spec(tox_project: ToxProjectCreator) -> None:
ver = sys.version_info
project = tox_project({"tox.ini": f"[testenv]\npackage=skip\nbase_python={ver.major}.{ver.minor}"})
Expand Down
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tox]
requires =
tox>=4.2
tox-uv>=1.11.3
tox>=4.24.1
tox-uv>=1.19.1
env_list =
fix
3.13
Expand Down Expand Up @@ -36,7 +36,7 @@ dependency_groups = test
description = format the code base to adhere to our styles, and complain about what we cannot do automatically
skip_install = true
deps =
pre-commit-uv>=4.1.3
pre-commit-uv>=4.1.4
commands =
pre-commit run --all-files --show-diff-on-failure

Expand Down

0 comments on commit 9d847c4

Please sign in to comment.