Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.

Commit 44bd275

Browse files
Gryfenfer97germa89
authored andcommitted
fix is_valid_executable_path() call for mechanical (#55)
* fix error * simplify _is_common_executable_path * add back the version check and simplify _is_common_executable_path without any if * add tests for _is_common_executable * remove useless if statement since it is always True * try displaying v_version and ansys_version * fix _is_common_executable_path tests * add some tests
1 parent 6eb95e1 commit 44bd275

File tree

2 files changed

+112
-26
lines changed

2 files changed

+112
-26
lines changed

src/ansys/tools/path/path.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
CONFIG_FILE = os.path.join(SETTINGS_DIR, CONFIG_FILE_NAME)
6565

6666

67-
def _get_installed_windows_versions(supported_versions=SUPPORTED_ANSYS_VERSIONS):
67+
def _get_installed_windows_versions(
68+
supported_versions: SUPPORTED_VERSIONS_TYPE = SUPPORTED_ANSYS_VERSIONS,
69+
): # pragma: no cover
6870
"""Get the AWP_ROOT environment variable values for supported versions."""
6971

7072
# The student version overwrites the AWP_ROOT env var
@@ -103,7 +105,7 @@ def _get_default_linux_base_path():
103105
return None # pragma: no cover
104106

105107

106-
def _get_default_windows_base_path():
108+
def _get_default_windows_base_path(): # pragma: no cover
107109
"""Get the default base path of the Ansys unified install on windows."""
108110

109111
base_path = os.path.join(os.environ["PROGRAMFILES"], "ANSYS INC")
@@ -265,7 +267,7 @@ def find_mechanical(version=None, supported_versions=SUPPORTED_ANSYS_VERSIONS):
265267
ans_path, version = _get_unified_install_base_for_version(version, supported_versions)
266268
if not ans_path or not version:
267269
return "", ""
268-
if is_windows():
270+
if is_windows(): # pragma: no cover
269271
mechanical_bin = os.path.join(ans_path, "aisol", "bin", "winx64", f"AnsysWBU.exe")
270272
else:
271273
mechanical_bin = os.path.join(ans_path, "aisol", ".workbench")
@@ -345,7 +347,7 @@ def is_valid_executable_path(product: str, exe_loc: str):
345347
and re.search(r"ansys\d\d\d", os.path.basename(os.path.normpath(exe_loc))) is not None
346348
)
347349
elif product == "mechanical":
348-
if is_windows():
350+
if is_windows(): # pragma: no cover
349351
return (
350352
os.path.isfile(exe_loc)
351353
and re.search("AnsysWBU.exe", os.path.basename(os.path.normpath(exe_loc)))
@@ -362,42 +364,38 @@ def _is_common_executable_path(product: str, exe_loc: str) -> bool:
362364
if product == "mapdl":
363365
path = os.path.normpath(exe_loc)
364366
path = path.split(os.sep)
365-
if (
366-
re.search(r"v(\d\d\d)", exe_loc) is not None
367-
and re.search(r"ansys(\d\d\d)", exe_loc) is not None
368-
):
369-
equal_version = (
370-
re.search(r"v(\d\d\d)", exe_loc)[1] == re.search(r"ansys(\d\d\d)", exe_loc)[1]
371-
)
372-
else:
373-
equal_version = False
374-
367+
# Look for all v(\d\d\d) to catch the last one
368+
# in case the user has placed the installation folder inside a folder called for example (/ansys/v211)
369+
v_version = re.findall(r"v(\d\d\d)", exe_loc)
370+
ansys_version = re.findall(r"ansys(\d\d\d)", exe_loc, re.IGNORECASE)
375371
return (
376-
is_valid_executable_path("mapdl", exe_loc)
377-
and re.search(r"v\d\d\d", exe_loc)
372+
len(v_version) != 0
373+
and len(ansys_version) != 0
374+
and v_version[-1] == ansys_version[-1]
375+
and is_valid_executable_path("mapdl", exe_loc)
378376
and "ansys" in path
379377
and "bin" in path
380-
and equal_version
381378
)
379+
382380
elif product == "mechanical":
383381
path = os.path.normpath(exe_loc)
384382
path = path.split(os.sep)
385383

386-
is_valid_path = is_valid_executable_path(exe_loc)
384+
is_valid_path = is_valid_executable_path("mechanical", exe_loc)
387385

388-
if is_windows():
386+
if is_windows(): # pragma: no cover
389387
return (
390388
is_valid_path
391-
and re.search(r"v\d\d\d", exe_loc)
389+
and re.search(r"v\d\d\d", exe_loc) is not None
392390
and "aisol" in path
393-
and "bin" in path
391+
and ("bin" in path or "Bin" in path)
394392
and "winx64" in path
395-
and "AnsysWBU.exe" in path
393+
and ("AnsysWBU.exe" in path or "ANSYSWBU.exe" in path)
396394
)
397395

398396
return (
399397
is_valid_path
400-
and re.search(r"v\d\d\d", exe_loc)
398+
and re.search(r"v\d\d\d", exe_loc) is not None
401399
and "aisol" in path
402400
and ".workbench" in path
403401
)
@@ -489,9 +487,8 @@ def _save_path(product: str, exe_loc: str = None, allow_prompt=True) -> str:
489487
_change_default_path(product, exe_loc)
490488
return exe_loc
491489

492-
if exe_loc is not None:
493-
if is_valid_executable_path(product, exe_loc):
494-
return exe_loc
490+
if is_valid_executable_path(product, exe_loc):
491+
return exe_loc
495492
if allow_prompt:
496493
exe_loc = _prompt_path(product)
497494
return exe_loc

tests/test_path.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23

34
import appdirs
45
import pytest
@@ -7,7 +8,9 @@
78
from ansys.tools.path.path import (
89
_check_uncommon_executable_path,
910
_clear_config_file,
11+
_is_common_executable_path,
1012
change_default_mapdl_path,
13+
find_mechanical,
1114
get_available_ansys_installations,
1215
get_mapdl_path,
1316
is_valid_executable_path,
@@ -18,10 +21,36 @@
1821
paths = [
1922
("/usr/dir_v2019.1/slv/ansys_inc/v211/ansys/bin/ansys211", 211),
2023
("C:/Program Files/ANSYS Inc/v202/ansys/bin/win64/ANSYS202.exe", 202),
24+
("C:\\Program Files\\ANSYS Inc\\v202\\ansys\\bin\\win64\\ANSYS202.exe", 202),
2125
("/usr/ansys_inc/v211/ansys/bin/mapdl", 211),
2226
pytest.param(("/usr/ansys_inc/ansys/bin/mapdl", 211), marks=pytest.mark.xfail),
2327
]
2428

29+
mechanical_paths = [
30+
("/usr/install/ansys_inc/v211/ansys/aisol/.workbench", 211),
31+
("C:\\Program Files\\ANSYS Inc\\v202\\aisol\\Bin\\winx64\\ANSYSWBU.exe", 202),
32+
("C:/Program Files/ANSYS Inc/v202/aisol/Bin/winx64/ANSYSWBU.exe", 202),
33+
]
34+
35+
linux_mapdl_executable_paths = [
36+
("/usr/dir_v2019.1/slv/ansys_inc/v211/ansys/bin/ansys211", True),
37+
("/usr/ansys_inc/v211/ansys/bin/mapdl", False),
38+
]
39+
40+
windows_mapdl_executable_paths = [
41+
("C:/Program Files/ANSYS Inc/v202/ansys/bin/win64/ANSYS202.exe", True),
42+
("C:\\Program Files\\ANSYS Inc\\v202\\ansys\\bin\\win64\\ANSYS202.exe", True),
43+
]
44+
45+
windows_mechanical_executable_paths = [
46+
("C:\\Program Files\\ANSYS Inc\\v221\\aisol\\Bin\\winx64\\ANSYSWBU.exe", True),
47+
("C:/Program Files/ANSYS Inc/v221/aisol/Bin/winx64/ANSYSWBU.exe", True),
48+
]
49+
50+
linux_mechanical_executable_paths = [
51+
("/usr/install/ansys_inc/v211/ansys/aisol/.workbench", True),
52+
]
53+
2554

2655
skip_if_ansys_not_local = pytest.mark.skipif(
2756
os.environ.get("ANSYS_LOCAL", "").upper() != "TRUE", reason="Skipping on CI"
@@ -34,6 +63,11 @@ def test_mapdl_version_from_path(path_data):
3463
assert version_from_path("mapdl", exec_file) == version
3564

3665

66+
@pytest.mark.parametrize("exec_file,version", mechanical_paths)
67+
def test_mechanical_version_from_path(exec_file, version):
68+
assert version_from_path("mechanical", exec_file) == version
69+
70+
3771
@skip_if_ansys_not_local
3872
def test_find_mapdl_linux():
3973
# assuming Ansys MAPDL is installed, should be able to find it on linux
@@ -107,3 +141,58 @@ def test_warn_uncommon_executable_path():
107141
def test_get_mapdl_path():
108142
assert get_mapdl_path()
109143
assert get_mapdl_path(version=222)
144+
145+
146+
@pytest.fixture
147+
def mock_is_valid_executable_path(monkeypatch: pytest.MonkeyPatch):
148+
monkeypatch.setattr("ansys.tools.path.path.is_valid_executable_path", lambda _1, _2: True)
149+
150+
151+
@pytest.mark.skipif(sys.platform != "win32", reason="Test only available on windows")
152+
@pytest.mark.parametrize("path,expected", windows_mapdl_executable_paths)
153+
def test_windows_is_common_executable_path_mapdl(mock_is_valid_executable_path, path, expected):
154+
assert _is_common_executable_path("mapdl", path) == expected
155+
156+
157+
@pytest.mark.skipif(sys.platform != "linux", reason="Test only available on linux")
158+
@pytest.mark.parametrize("path,expected", linux_mapdl_executable_paths)
159+
def test_linux_is_common_executable_path_mapdl(mock_is_valid_executable_path, path, expected):
160+
assert _is_common_executable_path("mapdl", path) == expected
161+
162+
163+
@pytest.mark.skipif(sys.platform != "win32", reason="Test only available on windows")
164+
@pytest.mark.parametrize("path,expected", windows_mechanical_executable_paths)
165+
def test_windows_is_common_executable_path_mechanical(
166+
mock_is_valid_executable_path, path, expected
167+
):
168+
assert _is_common_executable_path("mechanical", path) == expected
169+
170+
171+
@pytest.mark.skipif(sys.platform != "linux", reason="Test only available on linux")
172+
@pytest.mark.parametrize("path,expected", linux_mechanical_executable_paths)
173+
def test_linux_is_common_executable_path_mechanical(mock_is_valid_executable_path, path, expected):
174+
assert _is_common_executable_path("mechanical", path) == expected
175+
176+
177+
@pytest.fixture
178+
def mock_default_linux_base_path(monkeypatch: pytest.MonkeyPatch):
179+
monkeypatch.setattr("os.path.isdir", lambda x: (x == "/usr/ansys_inc"))
180+
monkeypatch.setattr("ansys.tools.path.path.glob", lambda _: ["/usr/ansys_inc/v221"])
181+
182+
183+
@pytest.fixture
184+
def mock_empty_linux_base_path(monkeypatch: pytest.MonkeyPatch):
185+
monkeypatch.setattr("os.path.isdir", lambda x: (x == "/usr/ansys_inc"))
186+
monkeypatch.setattr("ansys.tools.path.path.glob", lambda _: [])
187+
188+
189+
def test_get_available_ansys_installation(mock_default_linux_base_path):
190+
assert get_available_ansys_installations() == {221: "/usr/ansys_inc/v221"}
191+
192+
193+
def test_empty_ansys_inttallation(mock_empty_linux_base_path):
194+
assert get_available_ansys_installations() == {}
195+
196+
197+
def test_find_mechanical(mock_default_linux_base_path):
198+
assert find_mechanical() == ("/usr/ansys_inc/v221/aisol/.workbench", 22.1)

0 commit comments

Comments
 (0)