1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
import platform
import stat
import sys
from pathlib import Path
import pytest
from findpython import register_provider
from findpython.finder import Finder
from findpython.providers.asdf import AsdfProvider
from findpython.providers.pyenv import PyenvProvider
from findpython.providers.rye import RyeProvider
from findpython.providers.uv import UvProvider
if sys.platform == "win32":
pytest.skip("Skip POSIX tests on Windows", allow_module_level=True)
def test_find_python_resolve_symlinks(mocked_python, tmp_path, switch):
link = Path(tmp_path / "python")
link.symlink_to(Path(tmp_path / "python3.7"))
python = mocked_python.add_python(link, "3.7.0")
finder = Finder(resolve_symlinks=switch)
all_pythons = finder.find_all()
assert len(all_pythons) == (3 if switch else 4)
assert (python in all_pythons) is not switch
def test_find_python_from_asdf(mocked_python, tmp_path, monkeypatch):
register_provider(AsdfProvider)
python = mocked_python.add_python(
tmp_path / ".asdf/installs/python/3.8/bin/python", "3.8.0"
)
monkeypatch.setenv("ASDF_DATA_DIR", str(tmp_path / ".asdf"))
pythons = Finder().find_all(3, 8)
assert len(pythons) == 2
assert python in pythons
def test_find_python_exclude_unreadable(mocked_python, tmp_path):
python = Path(tmp_path / "python3.8")
python.chmod(python.stat().st_mode & ~stat.S_IRUSR)
try:
finder = Finder()
all_pythons = finder.find_all()
assert len(all_pythons) == 2, all_pythons
assert python not in [version.executable for version in all_pythons]
finally:
python.chmod(0o744)
def test_find_python_from_provider(mocked_python, tmp_path, monkeypatch):
register_provider(AsdfProvider)
register_provider(PyenvProvider)
python38 = mocked_python.add_python(
tmp_path / ".asdf/installs/python/3.8/bin/python", "3.8.0"
)
python381 = mocked_python.add_python(
tmp_path / ".pyenv/versions/3.8.1/bin/python", "3.8.1"
)
python382 = mocked_python.add_python(
tmp_path / ".asdf/installs/python/3.8.2/bin/python", "3.8.2"
)
monkeypatch.setenv("ASDF_DATA_DIR", str(tmp_path / ".asdf"))
monkeypatch.setenv("PYENV_ROOT", str(tmp_path / ".pyenv"))
pythons = Finder(selected_providers=["pyenv", "asdf"]).find_all(3, 8)
assert len(pythons) == 3
assert python38 in pythons
assert python381 in pythons
assert python382 in pythons
asdf_pythons = Finder(selected_providers=["asdf"]).find_all(3, 8)
assert len(asdf_pythons) == 2
assert python38 in asdf_pythons
assert python382 in asdf_pythons
pyenv_pythons = Finder(selected_providers=["pyenv"]).find_all(3, 8)
assert len(pyenv_pythons) == 1
assert python381 in pyenv_pythons
def test_find_python_from_rye_provider(mocked_python, tmp_path, monkeypatch):
python310 = mocked_python.add_python(
tmp_path / ".rye/py/cpython@3.10.9/install/bin/python3", "3.10.9"
)
python311 = mocked_python.add_python(
tmp_path / ".rye/py/cpython@3.11.8/bin/python3", "3.11.8"
)
monkeypatch.setenv("HOME", str(tmp_path))
register_provider(RyeProvider)
find_310 = Finder(selected_providers=["rye"]).find_all(3, 10)
assert python310 in find_310
find_311 = Finder(selected_providers=["rye"]).find_all(3, 11)
assert python311 in find_311
def test_find_python_from_uv_provider(mocked_python, tmp_path, monkeypatch):
if platform.system() == "Linux":
python_root = tmp_path / ".local/share/uv/python"
else: # macos
python_root = tmp_path / "Library/Application Support/uv/python"
python310 = mocked_python.add_python(
python_root / "cpython@3.10.9/install/bin/python3", "3.10.9"
)
python311 = mocked_python.add_python(
python_root / "cpython@3.11.8/bin/python3", "3.11.8"
)
monkeypatch.setenv("HOME", str(tmp_path))
register_provider(UvProvider)
find_310 = Finder(selected_providers=["uv"]).find_all(3, 10)
assert python310 in find_310
find_311 = Finder(selected_providers=["uv"]).find_all(3, 11)
assert python311 in find_311
monkeypatch.setenv("UV_PYTHON_INSTALL_DIR", str(tmp_path / "uv_dir"))
python311_uv_dir = mocked_python.add_python(
tmp_path / "uv_dir/cpython@3.11.8/bin/python3", "3.11.8"
)
find_311 = Finder(selected_providers=["uv"]).find_all(3, 11)
assert python311_uv_dir in find_311
|