Skip to content

Commit

Permalink
Mock conda prefix eval util method
Browse files Browse the repository at this point in the history
  • Loading branch information
DhanshreeA committed Dec 17, 2024
1 parent 67519af commit 7e69285
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
10 changes: 3 additions & 7 deletions src/ersilia_pack/parsers/install_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 6 additions & 1 deletion src/ersilia_pack/utils.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions tests/test_dockerfile_install_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"

Expand All @@ -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()
7 changes: 3 additions & 4 deletions tests/test_yaml_install_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"

Expand All @@ -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()

0 comments on commit 7e69285

Please sign in to comment.