Skip to content

Commit

Permalink
Merge pull request #8 from pyansys/testing/osl_process
Browse files Browse the repository at this point in the history
Unit tests for OslServerProcess have been implemented.
  • Loading branch information
ekrja authored Jul 26, 2022
2 parents decddea + e5a9192 commit 77451c2
Show file tree
Hide file tree
Showing 7 changed files with 533 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies = [
test = [
"pytest>=7.1.0",
"pytest-cov>=3.0.0",
"psutil >=5.9"
]
doc = [
"Sphinx>=4.4",
Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
markers =
local_osl: local optiSLang process is used
1 change: 1 addition & 0 deletions requirements/requirements_tests.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pytest>=7.1.0
pytest-cov>=3.0.0
psutil >=5.9
105 changes: 105 additions & 0 deletions src/ansys/optislang/core/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"""Find optislang module."""
from collections import OrderedDict
from glob import glob
import os
import re


def get_osl_executable(osl_version=None):
"""Return path to optiSLang executable file (by default the latest available version).
Parameters
----------
osl_version : str, optional
Version of optiSLang, e.g. "221".
Returns
-------
path: str
Path of the specified optiSLang version.
"""
# TODO: Must work for standalone optiSLang as well
# windows
if os.name == "nt":
# find all ansys system variables, optiSLang installed in AWP_ROOT on windows
installed_versions = _get_system_vars(pattern="^AWP_ROOT.*")
if not installed_versions:
raise FileNotFoundError(
f"No version of ansys was found, please specify direct path of executable."
)
if osl_version:
version = "AWP_ROOT" + osl_version
if version in installed_versions:
path = os.path.join(installed_versions[version], "optiSlang", "optislang.exe")
if os.path.isfile(path):
return path
else:
raise FileNotFoundError(
f"Optislang not installed in this version: {osl_version}"
)
else:
raise FileNotFoundError(
f"This version of ansys is not defined in system variables: {osl_version}"
)
else:
# sort installed versions from the highest number
installed_versions = OrderedDict(reversed(sorted(installed_versions.items())))
for version in list(installed_versions.keys()):
path = os.path.join(installed_versions[version], "optiSlang", "optislang.exe")
if os.path.isfile(path):
return path

raise FileNotFoundError(
"No version of optiSLang was found, please specify direct path of executable."
)

# linux
elif os.name == "posix":
base_path = None
for path in ["/usr/ansys_inc", "/ansys_inc"]:
if os.path.isdir(path):
base_path = path
if osl_version:
dir_path = os.path.join(base_path, "v" + osl_version)
if not os.path.exists(dir_path):
raise FileNotFoundError(f"Ansys not installed in this version: {osl_version}")

file_path = os.path.join(dir_path, "optiSLang", "optislang")
if not os.path.isfile(file_path):
raise FileNotFoundError(f"Optislang not installed in this version: {osl_version}")

return file_path
else:
paths = sorted(glob(os.path.join(base_path, "v*")), reverse=True)
for path in paths:
file_path = os.path.join(path, "optiSLang", "optislang")
if os.path.isfile(file_path):
return file_path
raise FileNotFoundError(
"No version of optislang was found, please specify direct path of executable."
)

# another os
else:
raise OSError(f"Unsupported OS {os.name}")


def _get_system_vars(pattern=".*"):
"""Return dict of matching system variables.
Parameters
----------
pattern: str, optional
regex template
Returns
-------
installed_versions: dict
Dictionary of matching system variables.
"""
sys_vars = os.environ.copy()
dictionary = {}
for varname, value in sys_vars.items():
varname_match = re.search(pattern, varname)
if varname_match:
dictionary[varname] = value
return dictionary
91 changes: 91 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import os
import pathlib

import pytest

from ansys.optislang.core import utils


def pytest_addoption(parser):
parser.addoption(
"--local_osl",
action="store_true",
default=False,
help="run tests with local optiSLang process",
)


def pytest_collection_modifyitems(config, items):
if not config.getoption("--local_osl"):
# --local_osl given in cli: run local optiSLang process
skip_local_osl = pytest.mark.skip(reason="need --local_osl option to run")
for item in items:
if "local_osl" in item.keywords:
item.add_marker(skip_local_osl)


@pytest.fixture()
def executable():
"""Get path to the optiSLang executable.
Returns
-------
str
Path to the optiSLang executable.
"""
return utils.get_osl_executable()


@pytest.fixture()
def project_file(tmp_path):
"""Get path to the optiSLang project.
Parameters
----------
tmp_path : pathlib.Path
Builtin fixture to temporary directory unique to the test invocation.
Returns
-------
str
Path to the optiSLang project.
"""
return os.path.join(tmp_path, "test.opf")


def get_server_info_file_path(project_file: str, server_info_file_name: str) -> str:
"""Get path to the server information file.
Parameters
----------
project_file : str
Path to the project file.
server_info_file_name : str
Name of the server information file with file extension.
Returns
-------
str
Path to the server information file.
"""
project_dir = os.path.dirname(project_file)
project_name = pathlib.Path(project_file).stem
return os.path.join(project_dir, project_name + ".opd", server_info_file_name)


@pytest.fixture()
def server_info_file(project_file):
"""Get path to the server information file.
Parameters
----------
project_file : str
Path to the project file.
Returns
-------
str
Path to the server information file.
"""
info_file_name = "server_info.ini"
return get_server_info_file_path(project_file, info_file_name)
Loading

0 comments on commit 77451c2

Please sign in to comment.