Skip to content

Commit

Permalink
Inline uv venv into the tox folder and expose uv to created env (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbernat authored May 29, 2024
1 parent f1c5688 commit 86a8c3f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ dependencies = [
"importlib-resources>=6.4; python_version<'3.9'",
"packaging>=24",
"tox<5,>=4.15",
"uv<1,>=0.1.39",
"uv<1,>=0.2.5",
]
optional-dependencies.test = [
"covdefaults>=2.3",
"devpi-process>=1",
"pytest>=8.2",
"pytest>=8.2.1",
"pytest-cov>=5",
"pytest-mock>=3.14",
]
Expand Down Expand Up @@ -118,8 +118,8 @@ html.show_contexts = true
html.skip_covered = false
paths.source = [
"src",
".tox/*/.venv/lib/*/site-packages",
".tox\\*\\.venv\\Lib\\site-packages",
".tox/*/lib/*/site-packages",
".tox\\*\\Lib\\site-packages",
"**/src",
"**\\src",
]
Expand Down
7 changes: 4 additions & 3 deletions src/tox_uv/_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def register_config(self) -> None:
def python_cache(self) -> dict[str, Any]:
result = super().python_cache()
result["seed"] = self.conf["uv_seed"]
result["venv"] = str(self.venv_dir.relative_to(self.env_dir))
return result

@property
Expand Down Expand Up @@ -105,7 +106,7 @@ def uv(self) -> str:

@property
def venv_dir(self) -> Path:
return cast(Path, self.conf["env_dir"]) / ".venv"
return cast(Path, self.conf["env_dir"])

@property
def environment_variables(self) -> dict[str, str]:
Expand All @@ -128,7 +129,7 @@ def create_python_env(self) -> None:
else:
uv_imp = "" if (imp and imp == "cpython") else imp
version_spec = f"{uv_imp or ''}{base.major}.{base.minor}" if base.minor else f"{uv_imp or ''}{base.major}"
cmd: list[str] = [self.uv, "venv", "-p", version_spec]
cmd: list[str] = [self.uv, "venv", "-p", version_spec, "--allow-existing"]
if self.options.verbosity > 2: # noqa: PLR2004
cmd.append("-v")
if self.conf["uv_seed"]:
Expand All @@ -145,7 +146,7 @@ def _allow_externals(self) -> list[str]:
return result

def prepend_env_var_path(self) -> list[Path]:
return [self.env_bin_dir()]
return [self.env_bin_dir(), Path(self.uv).parent]

def env_bin_dir(self) -> Path:
if sys.platform == "win32": # pragma: win32 cover
Expand Down
18 changes: 14 additions & 4 deletions tests/test_tox_uv_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import subprocess # noqa: S404
import sys
from configparser import ConfigParser
from importlib.metadata import version
from typing import TYPE_CHECKING

if TYPE_CHECKING:
Expand Down Expand Up @@ -65,17 +66,26 @@ def test_uv_env_bin_dir(tox_project: ToxProjectCreator) -> None:
result = project.run("-vv")
result.assert_success()

env_bin_dir = str(project.path / ".tox" / "py" / ".venv" / ("Scripts" if sys.platform == "win32" else "bin"))
env_bin_dir = str(project.path / ".tox" / "py" / ("Scripts" if sys.platform == "win32" else "bin"))
assert env_bin_dir in result.out


def test_uv_env_has_access_to_plugin_uv(tox_project: ToxProjectCreator) -> None:
project = tox_project({"tox.ini": "[testenv]\npackage=skip\ncommands=uv --version"})
result = project.run()

result.assert_success()
ver = version("uv")
assert f"uv {ver}" in result.out


def test_uv_env_python(tox_project: ToxProjectCreator) -> None:
project = tox_project({"tox.ini": "[testenv]\npackage=skip\ncommands=python -c 'print(\"{env_python}\")'"})
result = project.run("-vv")
result.assert_success()

exe = "python.exe" if sys.platform == "win32" else "python"
env_bin_dir = str(project.path / ".tox" / "py" / ".venv" / ("Scripts" if sys.platform == "win32" else "bin") / exe)
env_bin_dir = str(project.path / ".tox" / "py" / ("Scripts" if sys.platform == "win32" else "bin") / exe)
assert env_bin_dir in result.out


Expand All @@ -84,7 +94,7 @@ def test_uv_env_site_package_dir_run(tox_project: ToxProjectCreator) -> None:
result = project.run("-vv")
result.assert_success()

env_dir = project.path / ".tox" / "py" / ".venv"
env_dir = project.path / ".tox" / "py"
ver = sys.version_info
if sys.platform == "win32": # pragma: win32 cover
path = str(env_dir / "Lib" / "site-packages")
Expand All @@ -99,7 +109,7 @@ def test_uv_env_site_package_dir_conf(tox_project: ToxProjectCreator) -> None:
result = project.run("c", "-e", "py", "-k", "commands")
result.assert_success()

env_dir = project.path / ".tox" / "py" / ".venv"
env_dir = project.path / ".tox" / "py"
ver = sys.version_info
if sys.platform == "win32": # pragma: win32 cover
path = str(env_dir / "Lib" / "site-packages")
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ commands =
description = run static analysis and style check using flake8
skip_install = true
deps =
pre-commit>=3.7
pre-commit>=3.7.1
pass_env =
HOMEPATH
PROGRAMDATA
Expand All @@ -54,7 +54,7 @@ description = check that the package metadata is correct
skip_install = true
deps =
build[virtualenv]>=1.2.1
twine>=5
twine>=5.1
set_env =
{tty:FORCE_COLOR = 1}
change_dir = {toxinidir}
Expand Down

0 comments on commit 86a8c3f

Please sign in to comment.