Skip to content

Commit

Permalink
get_venv_ functions not always raise on no found; update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cielquan committed Dec 7, 2020
1 parent 986beb9 commit 516bc06
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 92 deletions.
69 changes: 25 additions & 44 deletions src/formelsammlung/venv_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,82 +18,63 @@
OS_BIN = "Scripts" if sys.platform == "win32" else "bin"


def get_venv_path(raise_error: bool = False) -> Optional[Path]:
def get_venv_path() -> Path:
"""Get path to the venv from where the python executable runs.
:param raise_error: raise FileNotFoundError if no venv is detected.
Default: ``False``
:raises FileNotFoundError: when no calling venv can be detected.
:return: Return venv path or None if python is not called from a venv.
:return: Return venv path
"""
if hasattr(sys, "real_prefix"):
return Path(sys.real_prefix) # type: ignore[no-any-return,attr-defined] # pylint: disable=E1101
if sys.base_prefix != sys.prefix:
return Path(sys.prefix)
if raise_error:
raise FileNotFoundError("No calling venv could be detected.")
return None
raise FileNotFoundError("No calling venv could be detected.")


def get_venv_bin_dir(
venv_path: Union[str, Path], raise_error: bool = False
) -> Optional[Path]:
def get_venv_bin_dir(venv_path: Union[str, Path]) -> Path:
"""Return path to bin/Scripts dir of given venv.
:param venv_path: Path to venv
:param raise_error: raise FileNotFoundError if no bin/Scripts dir is found.
Default: ``False``
:raises FileNotFoundError: when no bin/Scripts dir can be found for given venv.
:return: Path to bin/Scripts dir or None
:return: Path to bin/Scripts dir
"""
bin_dir = Path(venv_path) / OS_BIN
if bin_dir.is_dir():
return bin_dir

if not bin_dir.is_dir():
if raise_error:
raise FileNotFoundError(f"Given venv has no '{OS_BIN}' directory.")
return None
return bin_dir
raise FileNotFoundError(f"Given venv has no '{OS_BIN}' directory.")


def get_venv_tmp_dir(
venv_path: Union[str, Path], raise_error: bool = False
) -> Optional[Path]:
def get_venv_tmp_dir(venv_path: Union[str, Path]) -> Path:
"""Return path to tmp/temp dir of given venv.
:param venv_path: Path to venv
:param raise_error: raise FileNotFoundError if no tmp/temp dir is found.
Default: ``False``
:raises FileNotFoundError: when no tmp/temp dir can be found for given venv.
:return: Path to tmp/temp dir or None
:return: Path to tmp/temp dir
"""
tmp_dir = Path(venv_path) / "tmp"
if not tmp_dir.is_dir():
tmp_dir = Path(venv_path) / "temp"
if not tmp_dir.is_dir():
if raise_error:
raise FileNotFoundError("Given venv has no 'tmp' or 'temp' directory.")
return None
return tmp_dir


def get_venv_site_packages_dir(
venv_path: Union[str, Path], raise_error: bool = False
) -> Optional[Path]:
if tmp_dir.is_dir():
return tmp_dir

tmp_dir = Path(venv_path) / "temp"
if tmp_dir.is_dir():
return tmp_dir

raise FileNotFoundError("Given venv has no 'tmp' or 'temp' directory.")


def get_venv_site_packages_dir(venv_path: Union[str, Path]) -> Path:
"""Return path to site-packages dir of given venv.
:param venv_path: Path to venv
:param raise_error: raise FileNotFoundError if no site-packages dir is found.
Default: ``False``
:raises FileNotFoundError: when no site-packages dir can be found for given venv.
:return: Path to site-packages dir or None
:return: Path to site-packages dir
"""
paths = list(Path(venv_path).glob("**/site-packages"))
if paths:
return paths[0]

if not paths:
if raise_error:
raise FileNotFoundError("Given venv has no 'site-packages' directory.")
return None
return paths[0]
raise FileNotFoundError("Given venv has no 'site-packages' directory.")


def where_installed(program: str) -> Tuple[int, Optional[str], Optional[str]]:
Expand Down
56 changes: 8 additions & 48 deletions tests/test_venv_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,13 @@ def test_get_venv_path_w_prefix(monkeypatch):
assert result == Path("path-to-venv-via-prefix")


def test_get_venv_path_no_venv(monkeypatch):
"""Test get_venv_path return when no venv is used."""
monkeypatch.delattr(sys, "real_prefix", raising=False)
monkeypatch.setattr(sys, "prefix", sys.base_prefix)

result = vu.get_venv_path()

assert result is None


def test_get_venv_path_raise(monkeypatch):
"""Test get_venv_path raising exception if raise_error is True."""
"""Test get_venv_path raising exception on no found venv."""
monkeypatch.delattr(sys, "real_prefix", raising=False)
monkeypatch.setattr(sys, "prefix", sys.base_prefix)

with pytest.raises(FileNotFoundError) as excinfo:
vu.get_venv_path(raise_error=True)
vu.get_venv_path()
assert "No calling venv could" in str(excinfo.value)


Expand All @@ -71,23 +61,13 @@ def test_get_venv_bin_dir(tmp_path):
assert result == bin_dir


def test_get_venv_bin_dir_no_found(tmp_path):
"""Test get_venv_bin_dir return when no bin/Scripts dir is found."""
fake_venv = tmp_path / ".venv"
fake_venv.mkdir(parents=True)

result = vu.get_venv_bin_dir(fake_venv)

assert result is None


def test_get_venv_bin_dir_raise(tmp_path):
"""Test get_venv_bin_dir raising exception if raise_error is True."""
"""Test get_venv_bin_dir raising exception on no found dir."""
fake_venv = tmp_path / ".venv"
fake_venv.mkdir(parents=True)

with pytest.raises(FileNotFoundError) as excinfo:
vu.get_venv_bin_dir(fake_venv, raise_error=True)
vu.get_venv_bin_dir(fake_venv)
assert "Given venv has no" in str(excinfo.value)


Expand All @@ -114,23 +94,13 @@ def test_get_venv_temp_dir(tmp_path):
assert result == tmp_dir


def test_get_venv_tmp_dir_no_found(tmp_path):
"""Test get_venv_tmp_dir return when no temp/temp dir is found."""
fake_venv = tmp_path / ".venv"
fake_venv.mkdir(parents=True)

result = vu.get_venv_tmp_dir(fake_venv)

assert result is None


def test_get_venv_tmp_dir_raise(tmp_path):
"""Test get_venv_tmp_dir raising exception if raise_error is True."""
"""Test get_venv_tmp_dir raising exception on no found dir."""
fake_venv = tmp_path / ".venv"
fake_venv.mkdir(parents=True)

with pytest.raises(FileNotFoundError) as excinfo:
vu.get_venv_tmp_dir(fake_venv, raise_error=True)
vu.get_venv_tmp_dir(fake_venv)
assert "Given venv has no" in str(excinfo.value)


Expand All @@ -146,23 +116,13 @@ def test_get_venv_site_packages_dir(tmp_path):
assert result == site_pkg_dir


def test_get_venv_site_packages_dir_no_found(tmp_path):
"""Test get_venv_site_packages_dir return when no site-packages dir is found."""
fake_venv = tmp_path / ".venv"
fake_venv.mkdir(parents=True)

result = vu.get_venv_site_packages_dir(fake_venv)

assert result is None


def test_get_venv_site_packages_dir_raise(tmp_path):
"""Test get_venv_site_packages_dir raising exception if raise_error is True."""
"""Test get_venv_site_packages_dir raising exception on no found dir."""
fake_venv = tmp_path / ".venv"
fake_venv.mkdir(parents=True)

with pytest.raises(FileNotFoundError) as excinfo:
vu.get_venv_site_packages_dir(fake_venv, raise_error=True)
vu.get_venv_site_packages_dir(fake_venv)
assert "Given venv has no" in str(excinfo.value)


Expand Down

0 comments on commit 516bc06

Please sign in to comment.