Skip to content

Commit

Permalink
Fix Windows tests (#2918)
Browse files Browse the repository at this point in the history
* fix inquire default test

* fixing the test_detach_examples_submodule

* Fixing test on linux

* Fixing test_get_file_path

* adding gopr

* Update tests/conftest.py

* fixing tbft test

* fixing launch cli

* Fixing test_launch_mapdl_cli test

* fixing cli list test on cluster

* fixing licensing tests

* making ports depending on the mapdl fixture

* fixing missing licensing tests

* Avoinding raising exception about exec_file

* Fixing test_cache_pids
Fixing test_save_on_exit

* Fix test_file_command_local

* Fix test_inquire

* fixing recovering MAPDL instance between tests

* attemp to fix reconnect to MAPDL.

* fixing cli test

* fixing not raising wanring

* fix test readin

* trying catching the warn

* Reseting full mapdl object

* avoiding test

* improving test reliability

* skipping check on v241

* return empty string if on debug

* Rising the raise exception

* Fixing error type

---------

Co-authored-by: german <gayuso@ansys.com>
  • Loading branch information
germa89 and german authored Mar 22, 2024
1 parent f5cf6ce commit 877c6d0
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 81 deletions.
6 changes: 5 additions & 1 deletion src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,11 @@ def launch_mapdl(

LOG.debug("Using default executable.")
# Load cached path
exec_file = get_ansys_path(version=version) if not _debug_no_launch else ""
if _debug_no_launch:
exec_file = ""
else:
exec_file = get_ansys_path(version=version)

if exec_file is None:
raise FileNotFoundError(
"Invalid exec_file path or cannot load cached "
Expand Down
19 changes: 15 additions & 4 deletions src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import tempfile
import time
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Tuple, Union
import warnings
from warnings import warn
import weakref

Expand All @@ -57,6 +56,7 @@
from ansys.mapdl.core.errors import (
ComponentNoData,
MapdlCommandIgnoredError,
MapdlFileNotFoundError,
MapdlInvalidRoutineError,
MapdlRuntimeError,
)
Expand Down Expand Up @@ -1977,7 +1977,7 @@ def run_multiline(self, commands) -> str:
"""

warnings.warn(
warn(
"'run_multiline()' is being deprecated in future versions.\n Please use 'input_strings'.",
DeprecationWarning,
)
Expand Down Expand Up @@ -2678,7 +2678,13 @@ def _perform_entity_list_selection(
def _raise_errors(self, text):
# to make sure the following error messages are caught even if a breakline is in between.
flat_text = " ".join([each.strip() for each in text.splitlines()])
base_error_msg = "\n\nIgnore these messages by setting 'ignore_errors'=True"
base_error_msg = "\n\nIgnore these messages by setting 'ignore_errors'=True.\n"

if "unable to open file" in flat_text or (
"unable to open" in flat_text and "file" in flat_text
):
text += base_error_msg
raise MapdlFileNotFoundError(text)

if "is not a recognized" in flat_text:
text = text.replace("This command will be ignored.", "")
Expand Down Expand Up @@ -2706,10 +2712,15 @@ def _raise_errors(self, text):

if "For element type = " in flat_text and "is invalid." in flat_text:
if "is normal behavior when a CDB file is used." in flat_text:
warn(text)
warn(text, UserWarning)
else:
text += base_error_msg
raise MapdlCommandIgnoredError(text)

if "Cannot create another with the same name" in flat_text:
# When overriding constitutive models. See 'test_tbft'
warn(text, UserWarning)

# flag errors
if "*** ERROR ***" in flat_text:
self._raise_output_errors(text)
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/mapdl/core/mapdl_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ def inquire(self, strarray="", func="", arg1="", arg2=""):
"RSTFILE",
"RSTEXT",
"OUTPUT",
"ENVNAME",
"ENV",
"TITLE",
"EXIST",
"DATE",
Expand Down
41 changes: 26 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ def requires_dependency(dependency: str):

pymapdl.RUNNING_TESTS = True

from ansys.mapdl.core.errors import MapdlExitedError, MapdlRuntimeError
from ansys.mapdl.core import Mapdl
from ansys.mapdl.core.errors import (
MapdlConnectionError,
MapdlExitedError,
MapdlRuntimeError,
)
from ansys.mapdl.core.examples import vmfiles
from ansys.mapdl.core.launcher import get_start_instance, launch_mapdl

Expand Down Expand Up @@ -406,26 +411,32 @@ def is_exited(mapdl):
except MapdlExitedError:
return True

if START_INSTANCE and is_exited(mapdl):
if START_INSTANCE and (is_exited(mapdl) or mapdl._exited):
# Backing up the current local configuration
local_ = mapdl._local

# Relaunching MAPDL
mapdl_ = launch_mapdl(
port=mapdl._port,
override=True,
run_location=mapdl._path,
cleanup_on_exit=mapdl._cleanup,
)

# Cloning the new mapdl instance channel into the old one.
mapdl._channel = mapdl_._channel
mapdl._multi_connect(timeout=mapdl._timeout)
channel = mapdl._channel
ip = mapdl.ip
port = mapdl.port
try:
# to connect
mapdl = Mapdl(port=port, ip=ip)

except MapdlConnectionError as err:
# we cannot connect.
# Kill the instance
mapdl.exit()

# Relaunching MAPDL
mapdl = launch_mapdl(
port=mapdl._port,
override=True,
run_location=mapdl._path,
cleanup_on_exit=mapdl._cleanup,
)

# Restoring the local configuration
mapdl._local = local_

mapdl.gopr()
yield # this is where the testing happens

# Teardown : fill with any logic you want
Expand Down
17 changes: 9 additions & 8 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ def test_launch_mapdl_cli(monkeypatch, run_cli, start_instance):
if start_instance is not None:
monkeypatch.setenv("PYMAPDL_START_INSTANCE", str(start_instance))
else:
monkeypatch.unset("PYMAPDL_START_INSTANCE")
monkeypatch.delenv("PYMAPDL_START_INSTANCE", raising=False)

output = run_cli()
# Setting a port so it does not collide with the already running instance for testing
output = run_cli("start --port 50053")

# In local
assert "Success: Launched an MAPDL instance " in output
assert "50053" in output

# grab ips and port
pid = int(re.search(r"\(PID=(\d+)\)", output).groups()[0])
Expand Down Expand Up @@ -133,34 +134,34 @@ def test_launch_mapdl_cli_config(run_cli):
@requires("nostudent")
def test_launch_mapdl_cli_list(run_cli):
output = run_cli("list")
assert "running" in output
assert "running" in output or "sleeping" in output
assert "Is Instance" in output
assert len(output.splitlines()) > 2
assert "ansys" in output.lower() or "mapdl" in output.lower()

output = run_cli("list -i")
assert "running" in output
assert "running" in output or "sleeping" in output
assert "Is Instance" not in output
assert len(output.splitlines()) > 2
assert "ansys" in output.lower() or "mapdl" in output.lower()

output = run_cli("list -c")
assert "running" in output
assert "running" in output or "sleeping" in output
assert "Command line" in output
assert "Is Instance" in output
assert len(output.splitlines()) > 2
assert "ansys" in output.lower() or "mapdl" in output.lower()

output = run_cli("list -cwd")
assert "running" in output
assert "running" in output or "sleeping" in output
assert "Command line" not in output
assert "Working directory" in output
assert "Is Instance" in output
assert len(output.splitlines()) > 2
assert "ansys" in output.lower() or "mapdl" in output.lower()

output = run_cli("list -l")
assert "running" in output
assert "running" in output or "sleeping" in output
assert "Is Instance" in output
assert "Command line" in output
assert len(output.splitlines()) > 2
Expand Down
29 changes: 17 additions & 12 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,28 +164,33 @@ def test_download_tech_demo_data(running_test):

@requires("requests")
def test_detach_examples_submodule():
cmd = """
cmd = (
"""
import sys
assert "ansys.mapdl.core" not in sys.modules
assert "requests" not in sys.modules
assert "ansys.mapdl.core.examples" not in sys.modules
assert 'ansys.mapdl.core' not in sys.modules
assert 'requests' not in sys.modules
assert 'ansys.mapdl.core.examples' not in sys.modules
from ansys.mapdl import core as pymapdl
assert "ansys.mapdl.core" in sys.modules
assert "ansys.mapdl.core.examples" not in sys.modules
assert "requests" not in sys.modules
assert 'ansys.mapdl.core' in sys.modules
assert 'ansys.mapdl.core.examples' not in sys.modules
assert 'requests' not in sys.modules
from ansys.mapdl.core.examples import vmfiles
assert "ansys.mapdl.core.examples" in sys.modules
assert "requests" in sys.modules
assert 'ansys.mapdl.core.examples' in sys.modules
assert 'requests' in sys.modules
print("Everything went well")
"""
print('Everything went well')
""".strip()
.replace("\n", ";")
.replace(";;", ";")
)

cmd_line = f"""python -c "{cmd}" """

cmd_line = f"""python -c '{cmd}' """
p = Popen(cmd_line, shell=True, stdout=PIPE, stderr=STDOUT)
out = p.communicate()[0].decode()

Expand Down
47 changes: 25 additions & 22 deletions tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,21 @@ def test_launch_console(version):

@requires("local")
@requires("nostudent")
def test_license_type_keyword():
def test_license_type_keyword(mapdl):
checks = []
for license_name, license_description in LICENSES.items():
try:
mapdl = launch_mapdl(
mapdl_ = launch_mapdl(
license_type=license_name,
start_timeout=start_timeout,
port=mapdl.port + 1,
additional_switches=QUICK_LAUNCH_SWITCHES,
)

# Using first line to ensure not picking up other stuff.
checks.append(license_description in mapdl.__str__().split("\n")[0])
mapdl.exit()
del mapdl
checks.append(license_description in mapdl_.__str__().split("\n")[0])
mapdl_.exit()
del mapdl_
sleep(2)

except MapdlDidNotStart as e:
Expand All @@ -210,43 +211,45 @@ def test_license_type_keyword():

@requires("local")
@requires("nostudent")
def test_license_type_keyword_names():
def test_license_type_keyword_names(mapdl):
# This test might became a way to check available licenses, which is not the purpose.

successful_check = False
for license_name, license_description in LICENSES.items():
mapdl = launch_mapdl(
mapdl_ = launch_mapdl(
license_type=license_name,
start_timeout=start_timeout,
port=mapdl.port + 1,
additional_switches=QUICK_LAUNCH_SWITCHES,
)

# Using first line to ensure not picking up other stuff.
successful_check = (
license_description in mapdl.__str__().split("\n")[0] or successful_check
license_description in mapdl_.__str__().split("\n")[0] or successful_check
)
assert license_description in mapdl.__str__().split("\n")[0]
mapdl.exit()
assert license_description in mapdl_.__str__().split("\n")[0]
mapdl_.exit()

assert successful_check # if at least one license is ok, this should be true.


@requires("local")
@requires("nostudent")
def test_license_type_additional_switch():
def test_license_type_additional_switch(mapdl):
# This test might became a way to check available licenses, which is not the purpose.
successful_check = False
for license_name, license_description in LICENSES.items():
mapdl = launch_mapdl(
mapdl_ = launch_mapdl(
additional_switches=QUICK_LAUNCH_SWITCHES + " -p " + license_name,
start_timeout=start_timeout,
port=mapdl.port + 1,
)

# Using first line to ensure not picking up other stuff.
successful_check = (
license_description in mapdl.__str__().split("\n")[0] or successful_check
license_description in mapdl_.__str__().split("\n")[0] or successful_check
)
mapdl.exit()
mapdl_.exit()

assert successful_check # if at least one license is ok, this should be true.

Expand All @@ -267,16 +270,16 @@ def test_license_type_dummy(mapdl):
@requires("nostudent")
def test_remove_temp_files(mapdl):
"""Ensure the working directory is removed when run_location is not set."""
mapdl = launch_mapdl(
mapdl_ = launch_mapdl(
port=mapdl.port + 1,
remove_temp_files=True,
start_timeout=start_timeout,
additional_switches=QUICK_LAUNCH_SWITCHES,
)

# possible MAPDL is installed but running in "remote" mode
path = mapdl.directory
mapdl.exit()
path = mapdl_.directory
mapdl_.exit()

tmp_dir = tempfile.gettempdir()
ans_temp_dir = os.path.join(tmp_dir, "ansys_")
Expand All @@ -290,17 +293,17 @@ def test_remove_temp_files(mapdl):
@requires("nostudent")
def test_remove_temp_files_fail(tmpdir, mapdl):
"""Ensure the working directory is not removed when the cwd is changed."""
mapdl = launch_mapdl(
mapdl_ = launch_mapdl(
port=mapdl.port + 1,
remove_temp_files=True,
start_timeout=start_timeout,
additional_switches=QUICK_LAUNCH_SWITCHES,
)
old_path = mapdl.directory
old_path = mapdl_.directory
assert os.path.isdir(str(tmpdir))
mapdl.cwd(str(tmpdir))
path = mapdl.directory
mapdl.exit()
mapdl_.cwd(str(tmpdir))
path = mapdl_.directory
mapdl_.exit()
assert os.path.isdir(path)

# Checking no changes in the old path
Expand Down
9 changes: 5 additions & 4 deletions tests/test_licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,21 @@ def test_license_checker(tmpdir, license_checker):

@requires("local")
@skip_no_lic_bin
def test_check_license_file(tmpdir):
def test_check_license_file(mapdl, tmpdir):
timeout = 15
checker = licensing.LicenseChecker(timeout=timeout)
# start the license check in the background
checker.start(checkout_license=False)

try:
mapdl = launch_mapdl(
mapdl_ = launch_mapdl(
license_server_check=False,
start_timeout=timeout,
additional_switches=QUICK_LAUNCH_SWITCHES,
port=mapdl.port + 1,
)
assert mapdl._local
mapdl.exit()
assert mapdl_._local
mapdl_.exit()
except IOError: # MAPDL never started
assert not checker._license_file_success
else:
Expand Down
Loading

0 comments on commit 877c6d0

Please sign in to comment.