diff --git a/.gitignore b/.gitignore index 39aa9026..f9bf9c80 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ temp_testing/* build dist Issues/rule_keywords/test_DeleteMolecules_changed.bngl +.jules/ +__pycache__/ diff --git a/bionetgen/core/utils/utils.py b/bionetgen/core/utils/utils.py index 7d19fd23..11c4ad0b 100644 --- a/bionetgen/core/utils/utils.py +++ b/bionetgen/core/utils/utils.py @@ -1,6 +1,6 @@ import os, subprocess from bionetgen.core.exc import BNGPerlError -from distutils import spawn +import shutil as spawn from bionetgen.core.utils.logging import BNGLogger @@ -589,7 +589,7 @@ def _try_path(candidate_path): return hit # 3) On PATH - bng_on_path = spawn.find_executable("BNG2.pl") + bng_on_path = spawn.which("BNG2.pl") if bng_on_path: tried.append(bng_on_path) hit = _try_path(bng_on_path) @@ -616,7 +616,7 @@ def test_perl(app=None, perl_path=None): logger.debug("Checking if perl is installed.", loc=f"{__file__} : test_perl()") # find path to perl binary if perl_path is None: - perl_path = spawn.find_executable("perl") + perl_path = spawn.which("perl") if perl_path is None: raise BNGPerlError # check if perl is actually working diff --git a/bionetgen/simulator/csimulator.py b/bionetgen/simulator/csimulator.py index 34a6bdcd..fda2f39f 100644 --- a/bionetgen/simulator/csimulator.py +++ b/bionetgen/simulator/csimulator.py @@ -1,7 +1,10 @@ import ctypes, os, tempfile, bionetgen import numpy as np -from distutils import ccompiler +try: + from distutils import ccompiler +except ImportError: + pass from .bngsimulator import BNGSimulator from bionetgen.main import BioNetGen from bionetgen.core.exc import BNGCompileError diff --git a/tests/test_runner.py b/tests/test_runner.py new file mode 100644 index 00000000..43411e48 --- /dev/null +++ b/tests/test_runner.py @@ -0,0 +1,59 @@ +import os +import pytest +from unittest.mock import patch, MagicMock, ANY +from bionetgen.modelapi.runner import run + + +@patch("bionetgen.modelapi.runner.BNGCLI") +def test_runner_with_out(mock_bngcli): + mock_cli_instance = MagicMock() + mock_bngcli.return_value = mock_cli_instance + mock_cli_instance.result = "mock_result" + + inp = "test.bngl" + out = "test_out" + + result = run(inp, out=out, suppress=True, timeout=10) + + mock_bngcli.assert_called_once_with(inp, out, ANY, suppress=True, timeout=10) + mock_cli_instance.run.assert_called_once() + assert result == "mock_result" + + +@patch("bionetgen.modelapi.runner.BNGCLI") +@patch("bionetgen.modelapi.runner.TemporaryDirectory") +def test_runner_without_out(mock_tempdir, mock_bngcli): + mock_cli_instance = MagicMock() + mock_bngcli.return_value = mock_cli_instance + mock_cli_instance.result = "mock_result" + + mock_tempdir_instance = MagicMock() + mock_tempdir.return_value.__enter__.return_value = "temp_out" + + inp = "test.bngl" + + result = run(inp, suppress=False, timeout=None) + + mock_tempdir.assert_called_once() + mock_bngcli.assert_called_once_with( + inp, "temp_out", ANY, suppress=False, timeout=None + ) + mock_cli_instance.run.assert_called_once() + assert result == "mock_result" + + +@patch("bionetgen.modelapi.runner.BNGCLI") +def test_runner_exception(mock_bngcli): + mock_cli_instance = MagicMock() + mock_bngcli.return_value = mock_cli_instance + mock_cli_instance.run.side_effect = Exception("Test Exception") + + inp = "test.bngl" + out = "test_out" + + cur_dir = os.getcwd() + + with pytest.raises(Exception, match="Test Exception"): + run(inp, out=out) + + assert os.getcwd() == cur_dir