Skip to content

Commit

Permalink
adding some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saucoide committed Sep 15, 2024
1 parent 8300cb3 commit 4ddee7c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
11 changes: 7 additions & 4 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,21 @@ def find_uv() -> tuple[bool, str]:
return uv_on_path is not None, "uv"


def uv_version() -> str:
def uv_version() -> version.Version:
ret = subprocess.run(
[UV, "--version"],
check=False,
text=True,
capture_output=True,
)
if ret.returncode == 0 and ret.stdout:
return ret.stdout.strip().lstrip("uv ")
return version.Version(ret.stdout.strip().lstrip("uv "))
else:
logger.info("Failed to establish uv's version.")
return version.Version("0.0")


def uv_install_python(python_version) -> bool:
def uv_install_python(python_version: str) -> bool:
"""Attempts to install a given python version with uv"""
ret = subprocess.run(
[UV, "python", "install", python_version],
Expand All @@ -89,7 +92,7 @@ def uv_install_python(python_version) -> bool:

HAS_UV, UV = find_uv()
if HAS_UV:
UV_PYTHON_SUPPORT = version.Version(uv_version()) >= version.Version("0.3")
UV_PYTHON_SUPPORT = uv_version() >= version.Version("0.3")


class InterpreterNotFound(OSError):
Expand Down
41 changes: 41 additions & 0 deletions tests/test_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ def test_uv_creation(make_one):
assert venv._check_reused_environment_type()


@has_uv
def test_uv_managed_python(make_one):
make_one(interpreter="cpython3.12", venv_backend="uv")


def test_constructor_defaults(make_one):
venv, _ = make_one()
assert venv.location
Expand Down Expand Up @@ -620,6 +625,42 @@ def find_uv_bin():
assert nox.virtualenv.find_uv() == (found, path)


@pytest.mark.parametrize(
["return_code", "stdout", "expected_result"],
[
(0, "uv 0.2.3", "0.2.3"),
(1, None, "0.0"),
(1, "uv 9.9.9", "0.0"),
],
)
def test_uv_version_error(monkeypatch, return_code, stdout, expected_result):
def mock_run(*args, **kwargs):
return subprocess.CompletedProcess(
args=["uv", "--version"],
stdout=stdout,
returncode=return_code,
)

monkeypatch.setattr(subprocess, "run", mock_run)
assert nox.virtualenv.uv_version() == version.Version(expected_result)


@pytest.mark.parametrize(
["requested_python", "expected_result"],
[
("3.11", True),
("pypy3.8", True),
("cpython3.9", True),
("python3.12", True),
("nonpython9.22", False),
("java11", False),
],
)
@has_uv
def test_uv_install(requested_python, expected_result):
assert nox.virtualenv.uv_install_python(requested_python) == expected_result


def test_create_reuse_venv_environment(make_one, monkeypatch):
# Making the reuse requirement more strict
monkeypatch.setenv("NOX_ENABLE_STALENESS_CHECK", "1")
Expand Down

0 comments on commit 4ddee7c

Please sign in to comment.