From 6b792b1d1a659de20028d929d4b697576835e7c5 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Thu, 5 Dec 2024 08:33:18 +0100 Subject: [PATCH 1/3] FIX: Set DOTNET_ROOT with str and not Path --- src/pyedb/dotnet/clr_module.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pyedb/dotnet/clr_module.py b/src/pyedb/dotnet/clr_module.py index e901643760..be541c5dd3 100644 --- a/src/pyedb/dotnet/clr_module.py +++ b/src/pyedb/dotnet/clr_module.py @@ -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: @@ -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") From 3b164149d0e54588a60995a6342a356811bea98d Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 6 Dec 2024 17:21:54 +0100 Subject: [PATCH 2/3] TESTS: Load clr_module in Linux --- tests/legacy/unit/test_clr_module.py | 92 ++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tests/legacy/unit/test_clr_module.py diff --git a/tests/legacy/unit/test_clr_module.py b/tests/legacy/unit/test_clr_module.py new file mode 100644 index 0000000000..98d684c5aa --- /dev/null +++ b/tests/legacy/unit/test_clr_module.py @@ -0,0 +1,92 @@ +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(): + 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"] + + +@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"] From 3a3445b1bfbf8220e9425e5c42943b40781d3766 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 6 Dec 2024 17:51:57 +0100 Subject: [PATCH 3/3] TESTS: Update fixture to restaure sys and os --- tests/legacy/unit/test_clr_module.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/legacy/unit/test_clr_module.py b/tests/legacy/unit/test_clr_module.py index 98d684c5aa..cbb300d69c 100644 --- a/tests/legacy/unit/test_clr_module.py +++ b/tests/legacy/unit/test_clr_module.py @@ -14,11 +14,21 @@ @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")