From 516bc06fe28cdf651f4c0ddba8320c20d9f032e2 Mon Sep 17 00:00:00 2001 From: Cielquan Date: Mon, 7 Dec 2020 23:08:06 +0100 Subject: [PATCH] get_venv_ functions not always raise on no found; update tests --- src/formelsammlung/venv_utils.py | 69 ++++++++++++-------------------- tests/test_venv_utils.py | 56 ++++---------------------- 2 files changed, 33 insertions(+), 92 deletions(-) diff --git a/src/formelsammlung/venv_utils.py b/src/formelsammlung/venv_utils.py index 093a35d..d38692d 100644 --- a/src/formelsammlung/venv_utils.py +++ b/src/formelsammlung/venv_utils.py @@ -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]]: diff --git a/tests/test_venv_utils.py b/tests/test_venv_utils.py index 309e16e..dd90ef0 100644 --- a/tests/test_venv_utils.py +++ b/tests/test_venv_utils.py @@ -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) @@ -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) @@ -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) @@ -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)