| 1 |
from os.path import isabs, basename, dirname |
| 2 |
import os |
| 3 |
import re |
| 4 |
import sys |
| 5 |
import venv |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from flit import PythonNotFoundError, find_python_executable |
| 10 |
|
| 11 |
|
| 12 |
def test_default(): |
| 13 |
assert find_python_executable(None) == sys.executable |
| 14 |
|
| 15 |
|
| 16 |
def test_self(): |
| 17 |
assert find_python_executable(sys.executable) == sys.executable |
| 18 |
|
| 19 |
|
| 20 |
def test_abs(): |
| 21 |
abs_path = "C:\\PythonXY\\python.exe" if os.name == 'nt' else '/usr/bin/python' |
| 22 |
assert find_python_executable(abs_path) == abs_path |
| 23 |
|
| 24 |
|
| 25 |
def test_find_in_path(): |
| 26 |
assert isabs(find_python_executable("python")) |
| 27 |
|
| 28 |
|
| 29 |
def test_env(tmp_path): |
| 30 |
path = tmp_path / "venv" |
| 31 |
venv.create(path) |
| 32 |
|
| 33 |
executable = find_python_executable(path) |
| 34 |
assert basename(dirname(dirname(executable))) == "venv" |
| 35 |
|
| 36 |
|
| 37 |
def test_env_abs(tmp_path, monkeypatch): |
| 38 |
path = tmp_path / "venv" |
| 39 |
venv.create(path) |
| 40 |
|
| 41 |
monkeypatch.chdir(tmp_path) |
| 42 |
assert isabs(find_python_executable("venv")) |
| 43 |
|
| 44 |
|
| 45 |
@pytest.mark.parametrize("bad_python_name", ["pyhton", "ls", "."]) |
| 46 |
def test_exception(bad_python_name: str): |
| 47 |
"""Test that an appropriate exception (that contains the error string) is raised.""" |
| 48 |
with pytest.raises(PythonNotFoundError, match=re.escape(bad_python_name)): |
| 49 |
find_python_executable(bad_python_name) |