Skip to content

Commit

Permalink
fix: always use simplest form if on PATH
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Apr 12, 2024
1 parent c7f07b5 commit 90c62b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
15 changes: 10 additions & 5 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import subprocess
import sys
from collections.abc import Callable, Mapping
from pathlib import Path
from socket import gethostbyname
from typing import Any, ClassVar

Expand All @@ -45,17 +46,21 @@


def find_uv() -> tuple[bool, str]:
uv_on_path = shutil.which("uv")

# Look for uv in Nox's environment, to handle `pipx install nox[uv]`.
with contextlib.suppress(ImportError, FileNotFoundError):
from uv import find_uv_bin

return True, find_uv_bin()
uv_bin = find_uv_bin()

# Fall back to PATH.
if shutil.which("uv") is not None:
return True, "uv"
if uv_on_path and Path(uv_bin).samefile(uv_on_path):
return True, "uv"

return False, "uv"
return True, uv_bin

# Fall back to PATH.
return uv_on_path is not None, "uv"


HAS_UV, UV = find_uv()
Expand Down
18 changes: 10 additions & 8 deletions tests/test_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import subprocess
import sys
import types
from pathlib import Path
from textwrap import dedent
from typing import NamedTuple
from unittest import mock
Expand Down Expand Up @@ -602,26 +603,27 @@ def test_create_reuse_uv_environment(make_one):


@pytest.mark.parametrize(
["which_result", "find_uv_bin_result", "expected"],
["which_result", "find_uv_bin_result", "found", "path"],
[
("/usr/bin/uv", UV_IN_PIPX_VENV, (True, UV_IN_PIPX_VENV)),
("/usr/bin/uv", FileNotFoundError, (True, "uv")),
(None, UV_IN_PIPX_VENV, (True, UV_IN_PIPX_VENV)),
(None, FileNotFoundError, (False, "uv")),
("/usr/bin/uv", UV_IN_PIPX_VENV, True, UV_IN_PIPX_VENV),
("/usr/bin/uv", FileNotFoundError, True, "uv"),
(None, UV_IN_PIPX_VENV, True, UV_IN_PIPX_VENV),
(None, FileNotFoundError, False, "uv"),
],
) # fmt: skip
def test_find_uv(monkeypatch, which_result, find_uv_bin_result, expected):
)
def test_find_uv(monkeypatch, which_result, find_uv_bin_result, found, path):
def find_uv_bin():
if find_uv_bin_result is FileNotFoundError:
raise FileNotFoundError
return find_uv_bin_result

monkeypatch.setattr(shutil, "which", lambda _: which_result)
monkeypatch.setattr(Path, "samefile", lambda a, b: a == b)
monkeypatch.setitem(
sys.modules, "uv", types.SimpleNamespace(find_uv_bin=find_uv_bin)
)

assert nox.virtualenv.find_uv() == expected
assert nox.virtualenv.find_uv() == (found, path)


def test_create_reuse_venv_environment(make_one, monkeypatch):
Expand Down

0 comments on commit 90c62b0

Please sign in to comment.