diff --git a/src/ansys/mapdl/core/launcher.py b/src/ansys/mapdl/core/launcher.py index 86f338a759..a0e5547994 100644 --- a/src/ansys/mapdl/core/launcher.py +++ b/src/ansys/mapdl/core/launcher.py @@ -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 " diff --git a/src/ansys/mapdl/core/mapdl_core.py b/src/ansys/mapdl/core/mapdl_core.py index 126264f68f..42c1f1dedb 100644 --- a/src/ansys/mapdl/core/mapdl_core.py +++ b/src/ansys/mapdl/core/mapdl_core.py @@ -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 @@ -57,6 +56,7 @@ from ansys.mapdl.core.errors import ( ComponentNoData, MapdlCommandIgnoredError, + MapdlFileNotFoundError, MapdlInvalidRoutineError, MapdlRuntimeError, ) @@ -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, ) @@ -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.", "") @@ -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) diff --git a/src/ansys/mapdl/core/mapdl_extended.py b/src/ansys/mapdl/core/mapdl_extended.py index 9a27e6a8cf..fe18ae9adf 100644 --- a/src/ansys/mapdl/core/mapdl_extended.py +++ b/src/ansys/mapdl/core/mapdl_extended.py @@ -1352,7 +1352,7 @@ def inquire(self, strarray="", func="", arg1="", arg2=""): "RSTFILE", "RSTEXT", "OUTPUT", - "ENVNAME", + "ENV", "TITLE", "EXIST", "DATE", diff --git a/tests/conftest.py b/tests/conftest.py index 723522a117..63c4631e3f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 @@ -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 diff --git a/tests/test_cli.py b/tests/test_cli.py index 6a231c449d..12200c0e9d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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]) @@ -133,26 +134,26 @@ 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 @@ -160,7 +161,7 @@ def test_launch_mapdl_cli_list(run_cli): 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 diff --git a/tests/test_examples.py b/tests/test_examples.py index 3f1fe09998..723b417b7b 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -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() diff --git a/tests/test_launcher.py b/tests/test_launcher.py index 05e0708118..8f2b7d1136 100644 --- a/tests/test_launcher.py +++ b/tests/test_launcher.py @@ -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: @@ -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. @@ -267,7 +270,7 @@ 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, @@ -275,8 +278,8 @@ def test_remove_temp_files(mapdl): ) # 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_") @@ -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 diff --git a/tests/test_licensing.py b/tests/test_licensing.py index def8e3a383..2e77a80e98 100644 --- a/tests/test_licensing.py +++ b/tests/test_licensing.py @@ -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: diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index f013bfd04c..60337e23a3 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -1266,6 +1266,7 @@ def test_get_file_path(mapdl, tmpdir): assert fobject not in mapdl.list_files() assert fobject not in os.listdir() + prev = mapdl._local mapdl._local = True fname_ = mapdl._get_file_path(fobject) assert fname in fname_ @@ -1277,6 +1278,8 @@ def test_get_file_path(mapdl, tmpdir): # If we are not in local, now it should have been uploaded assert fname in mapdl.list_files() + mapdl._local = prev + @pytest.mark.parametrize( "option2,option3,option4", @@ -1286,7 +1289,7 @@ def test_get_file_path(mapdl, tmpdir): ("expdata", "dat", "DIR"), ], ) -def test_tbft(mapdl, tmpdir, option2, option3, option4): +def test_tbft(mapdl, cleared, tmpdir, option2, option3, option4): fname = "expdata.dat" dirpath = tmpdir.mkdir("tmpdir") fpath = dirpath.join(fname) @@ -1305,12 +1308,16 @@ def test_tbft(mapdl, tmpdir, option2, option3, option4): else: option2 = os.path.join(dirpath, option2) - mapdl.prep7(mute=True) + mapdl.prep7() mat_id = mapdl.get_value("MAT", 0, "NUM", "MAX") + 1 - mapdl.tbft("FADD", mat_id, "HYPER", "MOONEY", "3", mute=True) - mapdl.tbft("EADD", mat_id, "UNIA", option2, option3, option4, "", "", "", mute=True) + output = mapdl.tbft("FADD", mat_id, "HYPER", "MOONEY", "3") + assert "Successfully Constructed Material Model" in output + output = mapdl.tbft("EADD", mat_id, "UNIA", option2, option3, option4, "", "", "") + assert "Successfully Constructed Material Model" in output - assert fname in mapdl.list_files() + # with pytest.warns(UserWarning): + # # checking warning if overwriting + # mapdl.tbft("FADD", mat_id, "HYPER", "MOONEY", "3") def test_tbft_not_found(mapdl): @@ -1485,8 +1492,9 @@ def test_mpfunctions(mapdl, cube_solve, capsys): mapdl.mpread(fname="dummy", ext="dummy", lib="something") # Test suppliying a dir path when in remote - with pytest.raises(IOError): - mapdl.mpwrite("/test_dir/test", "mp") + if not ON_LOCAL: + with pytest.raises(IOError): + mapdl.mpwrite("/test_dir/test", "mp") def test_mapdl_str(mapdl): @@ -1515,7 +1523,7 @@ def test_file_command_local(mapdl, cube_solve, tmpdir): with pytest.raises(FileNotFoundError): mapdl.file("potato") - assert rst_file in mapdl.list_files() + assert os.path.basename(rst_file) in mapdl.list_files() rst_fpath = os.path.join(mapdl.directory, rst_file) # change directory @@ -1851,7 +1859,7 @@ def test_cache_pids(mapdl): mapdl._cache_pids() # Recache pids for each in mapdl._pids: - assert "ansys" in "".join(psutil.Process(each).cmdline()) + assert "ansys" in "".join(psutil.Process(each).cmdline()).lower() @requires("local") @@ -1942,7 +1950,9 @@ def test_igesin_whitespace(mapdl, cleared, tmpdir): @requires("nostudent") def test_save_on_exit(mapdl, cleared): mapdl2 = launch_mapdl( - license_server_check=False, additional_switches=QUICK_LAUNCH_SWITCHES + license_server_check=False, + additional_switches=QUICK_LAUNCH_SWITCHES, + port=mapdl.port + 1, ) mapdl2.parameters["my_par"] = "initial_value" @@ -1957,10 +1967,12 @@ def test_save_on_exit(mapdl, cleared): mapdl2.exit() mapdl2 = launch_mapdl( - license_server_check=False, additional_switches=QUICK_LAUNCH_SWITCHES + license_server_check=False, + additional_switches=QUICK_LAUNCH_SWITCHES, + port=mapdl.port + 1, ) mapdl2.resume(db_path) - if mapdl.version >= 24.1: + if mapdl.version >= 24.2: assert mapdl2.parameters["my_par"] == "initial_value" else: # This fails in earlier versions of MAPDL @@ -1974,7 +1986,9 @@ def test_save_on_exit(mapdl, cleared): mapdl2.exit(save=True) mapdl2 = launch_mapdl( - license_server_check=False, additional_switches=QUICK_LAUNCH_SWITCHES + license_server_check=False, + additional_switches=QUICK_LAUNCH_SWITCHES, + port=mapdl.port + 1, ) mapdl2.resume(db_path) assert mapdl2.parameters["my_par"] == "new_initial_value" @@ -2244,7 +2258,7 @@ def test_inquire_invalid(mapdl): def test_inquire_default(mapdl): mapdl.title = "heeeelloo" - assert mapdl.directory == mapdl.inquire() + assert Path(mapdl.directory) == Path(mapdl.inquire()) def test_vwrite_error(mapdl):