Skip to content

Commit

Permalink
fix(tox_env.python): do not process absolute paths to interpreter as …
Browse files Browse the repository at this point in the history
…PythonSpec (#3311)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Bernát Gábor <gaborjbernat@gmail.com>
Co-authored-by: Bernát Gábor <bgabor8@bloomberg.net>
  • Loading branch information
4 people authored Aug 7, 2024
1 parent fdc9eb0 commit fafce99
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
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

0 comments on commit fafce99

Please sign in to comment.