diff --git a/src/ersilia_pack/parsers/install_parser.py b/src/ersilia_pack/parsers/install_parser.py index 7fe374b..11bfcf6 100644 --- a/src/ersilia_pack/parsers/install_parser.py +++ b/src/ersilia_pack/parsers/install_parser.py @@ -2,6 +2,7 @@ import re import textwrap +from ..utils import eval_conda_prefix class InstallParser: def __init__(self, file_name, conda_env_name=None): @@ -15,13 +16,8 @@ def _get_python_version(self): def _get_commands(self): raise NotImplementedError("Implement this in subclass") - @staticmethod - def _eval_conda_prefix(): - # This returns an empty string if conda is not discoverable - return os.popen("conda info --base").read().strip() - def get_python_exe(self): - conda_prefix = self._eval_conda_prefix() + conda_prefix = eval_conda_prefix() if not conda_prefix: return "python" if self.conda_env_name is None: @@ -79,7 +75,7 @@ def _convert_commands_to_bash_script(self): lines = [] commands = self._get_commands() has_conda = self._has_conda(commands) - conda_prefix = self._eval_conda_prefix() + conda_prefix = eval_conda_prefix() python_exe = self.get_python_exe() for command in commands: if isinstance(command, list): diff --git a/src/ersilia_pack/utils.py b/src/ersilia_pack/utils.py index c35e2ff..c4a3da2 100644 --- a/src/ersilia_pack/utils.py +++ b/src/ersilia_pack/utils.py @@ -1,5 +1,6 @@ -import socket import logging +import os +import socket def find_free_port(host='localhost'): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -14,4 +15,8 @@ def get_logger(): logger = logging.getLogger(__name__) return logger +def eval_conda_prefix(): + # This returns an empty string if conda is not discoverable + return os.popen("conda info --base").read().strip() + logger = get_logger() \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..e05def9 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,7 @@ +from unittest.mock import patch +import pytest + +@pytest.fixture(scope="module") +def mock_conda(): + with patch("src.ersilia_pack.parsers.install_parser.eval_conda_prefix", return_value=""): + yield \ No newline at end of file diff --git a/tests/test_dockerfile_install_parser.py b/tests/test_dockerfile_install_parser.py index 862376b..279c5c8 100644 --- a/tests/test_dockerfile_install_parser.py +++ b/tests/test_dockerfile_install_parser.py @@ -6,7 +6,7 @@ class TestDockerfileInstallParser: @patch("src.ersilia_pack.parsers.dockerfile_install_parser.FILE_TYPE", "simple_dockerfile.txt") - def test_simple_dockerfile(self): + def test_simple_dockerfile(self, mock_conda): parser = DockerfileInstallParser(file_dir="tests/data") assert parser._get_python_version() == "3.11" @@ -22,7 +22,7 @@ def test_simple_dockerfile(self): assert install_script == expected_script @patch("src.ersilia_pack.parsers.dockerfile_install_parser.FILE_TYPE", "complex_dockerfile.txt") - def test_complex_dockerfile(self): + def test_complex_dockerfile(self, mock_conda): parser = DockerfileInstallParser(file_dir="tests/data") assert parser._get_python_version() == "3.9" @@ -39,7 +39,7 @@ def test_complex_dockerfile(self): assert install_script == expected_script @patch("src.ersilia_pack.parsers.dockerfile_install_parser.FILE_TYPE", "invalid_dockerfile.txt") - def test_invalid_dockerfile(self): + def test_invalid_dockerfile(self, mock_conda): parser = DockerfileInstallParser(file_dir="tests/data") with pytest.raises(ValueError) as e: parser._convert_commands_to_bash_script() diff --git a/tests/test_yaml_install_parser.py b/tests/test_yaml_install_parser.py index 259bb74..f18eb69 100644 --- a/tests/test_yaml_install_parser.py +++ b/tests/test_yaml_install_parser.py @@ -7,7 +7,7 @@ class TestYamlInstallParser: @patch("src.ersilia_pack.parsers.yaml_install_parser.FILE_TYPE", "simple_install.yml") - def test_simple_install_yaml(self): + def test_simple_install_yaml(self, mock_conda): parser = YAMLInstallParser(file_dir="tests/data") assert parser._get_python_version() == "3.10" @@ -23,7 +23,7 @@ def test_simple_install_yaml(self): assert install_script == expected_script @patch("src.ersilia_pack.parsers.yaml_install_parser.FILE_TYPE", "complex_install.yml") - def test_complex_install_yaml(self): + def test_complex_install_yaml(self, mock_conda): parser = YAMLInstallParser(file_dir="tests/data") assert parser._get_python_version() == "3.10" @@ -39,9 +39,8 @@ def test_complex_install_yaml(self): expected_script = file.read() assert install_script == expected_script - @patch("src.ersilia_pack.parsers.yaml_install_parser.FILE_TYPE", "invalid_install.yml") - def test_invalid_install_yaml(self): + def test_invalid_install_yaml(self, mock_conda): with pytest.raises(ValueError): parser = YAMLInstallParser(file_dir="tests/data") parser._convert_commands_to_bash_script() \ No newline at end of file