Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tox_env.python): do not process absolute paths to interpreter as PythonSpec #3311

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog/3191.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``base_python`` now accepts absolute paths to interpreter executable - by :user:`paveldikov`.
4 changes: 3 additions & 1 deletion src/tox/tox_env/python/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import sys
from abc import ABC, abstractmethod
from pathlib import Path
from typing import TYPE_CHECKING, Any, List, NamedTuple, cast

from virtualenv.discovery.py_spec import PythonSpec
Expand All @@ -14,7 +15,6 @@
from tox.tox_env.errors import Fail, Recreate, Skip

if TYPE_CHECKING:
from pathlib import Path

from tox.config.main import Config

Expand Down Expand Up @@ -169,6 +169,8 @@ def _validate_base_python(
if env_base_python is not None:
spec_name = PythonSpec.from_string_spec(env_base_python)
for base_python in base_pythons:
if Path(base_python).is_absolute():
return [base_python]
spec_base = PythonSpec.from_string_spec(base_python)
if any(
getattr(spec_base, key) != getattr(spec_name, key)
Expand Down
19 changes: 19 additions & 0 deletions tests/tox_env/python/test_python_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import sys
from typing import TYPE_CHECKING, Callable
from unittest.mock import patch
Expand Down Expand Up @@ -125,6 +126,24 @@ def test_base_python_env_no_conflict(env: str, base_python: list[str], ignore_co
assert result is base_python


@pytest.mark.parametrize(
("env", "base_python", "platform"),
[
("py312-unix", ["/opt/python312/bin/python"], "posix"),
("py312-win", [r"C:\Program Files\Python312\python.exe"], "nt"),
("py311-win", [r"\\a\python311\python.exe"], "nt"),
("py310-win", [r"\\?\UNC\a\python310\python.exe"], "nt"),
("py310", ["//a/python310/bin/python"], None),
],
ids=lambda a: "|".join(a) if isinstance(a, list) else str(a),
)
def test_base_python_absolute(env: str, base_python: list[str], platform: str | None) -> None:
if platform and platform != os.name:
pytest.skip(f"Not applicable to this platform. ({platform} != {os.name})")
result = Python._validate_base_python(env, base_python, False) # noqa: SLF001
assert result == base_python


@pytest.mark.parametrize("ignore_conflict", [True, False])
@pytest.mark.parametrize(
("env", "base_python", "expected", "conflict"),
Expand Down
Loading