Skip to content

Commit

Permalink
FIX: Set DOTNET_ROOT with str and not Path (#933)
Browse files Browse the repository at this point in the history
* FIX: Set DOTNET_ROOT with str and not Path

* TESTS: Load clr_module in Linux

* TESTS: Update fixture to restaure sys and os
  • Loading branch information
SMoraisAnsys authored Dec 7, 2024
1 parent ec5b277 commit 42e71aa
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/pyedb/dotnet/clr_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def custom_show_warning(message, category, filename, lineno, file=None, line=Non

runtime = get_coreclr()
load(runtime)
os.environ["DOTNET_ROOT"] = str(runtime.dotnet_root)
os.environ["DOTNET_ROOT"] = runtime.dotnet_root.as_posix()
is_clr = True
# TODO: Fall backing to dotnetcore2 should be removed in a near future.
except Exception:
Expand Down Expand Up @@ -83,7 +83,7 @@ def custom_show_warning(message, category, filename, lineno, file=None, line=Non
if dotnet_root is not None and runtime_config is not None:
try:
load("coreclr", runtime_config=str(runtime_config), dotnet_root=str(dotnet_root))
os.environ["DOTNET_ROOT"] = dotnet_root
os.environ["DOTNET_ROOT"] = dotnet_root.as_posix()
if "mono" not in os.getenv("LD_LIBRARY_PATH", ""):
warnings.warn("LD_LIBRARY_PATH needs to be setup to use pyedb.")
warnings.warn("export ANSYSEM_ROOT242=/path/to/AnsysEM/v242/Linux64")
Expand Down
102 changes: 102 additions & 0 deletions tests/legacy/unit/test_clr_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import os
from pathlib import Path
import sys
from unittest.mock import MagicMock, patch

import pytest

DOTNET_ROOT = "dummy/root/path"
DOTNET_ROOT_PATH = Path(DOTNET_ROOT)
DOTNETCORE2_FILE = "dummy/dotnetcore2/file"
DOTNETCORE2_BIN = "dummy/dotnetcore2/bin"
PYEDB_FILE = "dummy/pyedb/file"


@pytest.fixture
def clean_environment():
initial_sys_modules = sys.modules.copy()
initial_os_environ = os.environ.copy()

if "pyedb.dotnet.clr_module" in sys.modules:
del sys.modules["pyedb.dotnet.clr_module"]
if "DOTNET_ROOT" in os.environ:
del os.environ["DOTNET_ROOT"]

yield

sys.modules.clear()
sys.modules.update(initial_sys_modules)
os.environ.clear()
os.environ.update(initial_os_environ)


@pytest.mark.skipif(os.name != "posix", reason="test for linux behavior")
@patch("pythonnet.load")
@patch("clr_loader.get_coreclr")
def test_use_system_dotnet(mock_get_coreclr, mock_load, clean_environment):
mock_runtime = MagicMock()
mock_runtime.dotnet_root = DOTNET_ROOT_PATH
mock_get_coreclr.return_value = mock_runtime

import pyedb.dotnet.clr_module as cm

assert cm.is_clr
assert DOTNET_ROOT_PATH.as_posix() == os.environ["DOTNET_ROOT"]
del os.environ["DOTNET_ROOT"]


@pytest.mark.skipif(os.name != "posix", reason="test for linux behavior")
@patch("dotnetcore2.__file__", new=DOTNETCORE2_FILE)
@patch("pythonnet.load")
@patch("clr_loader.get_coreclr", side_effect=Exception("Dummy exception"))
def test_use_dotnetcore2(mock_get_coreclr, mock_load, clean_environment, capsys):
import pyedb.dotnet.clr_module as cm

captured = capsys.readouterr()
from pyedb.dotnet.clr_module import LINUX_WARNING

assert cm.is_clr
assert DOTNETCORE2_BIN == os.environ["DOTNET_ROOT"]
assert LINUX_WARNING in captured.out


@pytest.mark.skipif(os.name != "posix", reason="test for linux behavior")
@patch("dotnetcore2.__file__", new=DOTNETCORE2_FILE)
@patch("pythonnet.load")
@patch("clr_loader.find_runtimes", return_value=[])
def test_use_dotnet_root_env_variable_failure(mock_find_runtimes, mock_load, clean_environment, capsys):
os.environ["DOTNET_ROOT"] = DOTNET_ROOT

with pytest.raises(RuntimeError):
import pyedb.dotnet.clr_module # noqa: F401


@pytest.mark.skipif(os.name != "posix", reason="test for linux behavior")
@patch("dotnetcore2.__file__", new=DOTNETCORE2_FILE)
@patch("pythonnet.load")
def test_use_dotnet_root_env_variable_success_dotnetcore2(mock_load, clean_environment, capsys):
os.environ["DOTNET_ROOT"] = DOTNETCORE2_BIN

import pyedb.dotnet.clr_module as cm

captured = capsys.readouterr()
from pyedb.dotnet.clr_module import LINUX_WARNING

assert cm.is_clr
assert DOTNETCORE2_BIN == os.environ["DOTNET_ROOT"]
assert LINUX_WARNING not in captured.out


@pytest.mark.skipif(os.name != "posix", reason="test for linux behavior")
@patch("dotnetcore2.__file__", new=DOTNETCORE2_FILE)
@patch("pythonnet.load")
@patch("clr_loader.find_runtimes")
def test_use_dotnet_root_env_variable_success(mock_find_runtimes, mock_load, clean_environment, capsys):
os.environ["DOTNET_ROOT"] = DOTNET_ROOT
mock_runtime = MagicMock()
mock_runtime.name = "Microsoft.NETCore.App"
mock_find_runtimes.return_value = [mock_runtime]

import pyedb.dotnet.clr_module # noqa: F401

assert os.environ["DOTNET_ROOT"]

0 comments on commit 42e71aa

Please sign in to comment.