diff --git a/fades/envbuilder.py b/fades/envbuilder.py index 1df1d3f..362531e 100644 --- a/fades/envbuilder.py +++ b/fades/envbuilder.py @@ -18,6 +18,7 @@ import logging import os +import pathlib import shutil from datetime import datetime @@ -80,7 +81,9 @@ def create_with_external_venv(self, interpreter, options): logger.exception("Error creating virtual environment: %s", error) raise FadesError("General error while running external venv") - self.env_bin_path = os.path.join(self.env_path, 'bin') + # XXX Facundo 2024-06-29: the helper uses pathlib; eventually everything will be + # pathlib (see #435), so these translations will be cleaned up + self.env_bin_path = str(helpers.get_env_bin_path(pathlib.Path(self.env_path))) def create_env(self, interpreter, is_current, options): """Create the virtual environment and return its info.""" diff --git a/fades/helpers.py b/fades/helpers.py index 035b4ba..8296c28 100644 --- a/fades/helpers.py +++ b/fades/helpers.py @@ -358,3 +358,12 @@ def download_remote_script(url): temp_fh.write(content) temp_fh.close() return temp_fh.name + + +def get_env_bin_path(base_env_path): + """Find and return the environment's binary path in a multiplatformy way.""" + for subdir in ("bin", "Scripts"): + binpath = base_env_path / subdir + if binpath.exists(): + return binpath + raise ValueError(f"Binary subdir not found in {base_env_path!r}") diff --git a/tests/test_helpers.py b/tests/test_helpers.py index be54f0a..caf895c 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -28,7 +28,7 @@ from urllib.request import Request import logassert - +import pytest from xdg import BaseDirectory from fades import HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK, helpers @@ -563,3 +563,22 @@ def test_downloader_raw_with_redirection(self): self.assertEqual(content, raw_service_response.decode("utf8")) self.assertLoggedInfo("Download redirect detect, now downloading from", final_url) + + +def test_getbinpath_posix(tmp_path): + realbin = tmp_path / "bin" + realbin.mkdir() + path = helpers.get_env_bin_path(tmp_path) + assert path == realbin + + +def test_getbinpath_windows(tmp_path): + realbin = tmp_path / "Scripts" + realbin.mkdir() + path = helpers.get_env_bin_path(tmp_path) + assert path == realbin + + +def test_getbinpath_missing(tmp_path): + with pytest.raises(ValueError): + helpers.get_env_bin_path(tmp_path)