From c97a275d5e97db04e559a60811c2340ecf144876 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 20 Mar 2024 10:41:14 +0100 Subject: [PATCH] Adding a warning if there is a conflict between env vars and arguments (#2913) * Avoiding having both "start_instance" and "ip" set. Adding tests * Adding warnings if there is a conflict between env vars and arguments * Update src/ansys/mapdl/core/launcher.py Co-authored-by: Camille <78221213+clatapie@users.noreply.github.com> --------- Co-authored-by: Camille <78221213+clatapie@users.noreply.github.com> --- src/ansys/mapdl/core/launcher.py | 15 +++++++++++++++ tests/test_launcher.py | 14 +++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/ansys/mapdl/core/launcher.py b/src/ansys/mapdl/core/launcher.py index 65f9495bd6..f890a08570 100644 --- a/src/ansys/mapdl/core/launcher.py +++ b/src/ansys/mapdl/core/launcher.py @@ -799,9 +799,19 @@ def get_start_instance(start_instance: bool = True): Raised when ``PYMAPDL_START_INSTANCE`` is not either true or false (case independent). + Notes + ----- + If the environment variable ``PYMAPDL_START_INSTANCE`` is set, + hence the argument ``start_instance`` is overwritten. + """ if "PYMAPDL_START_INSTANCE" in os.environ and os.environ["PYMAPDL_START_INSTANCE"]: # It should not be empty + if isinstance(start_instance, bool): + warnings.warn( + "The environment variable 'PYMAPDL_START_INSTANCE' is set, " + "hence the argument 'start_instance' is overwritten." + ) start_instance = os.environ["PYMAPDL_START_INSTANCE"] else: LOG.debug( @@ -1475,6 +1485,11 @@ def launch_mapdl( # Getting IP from env var ip_env_var = os.environ.get("PYMAPDL_IP", "") if ip_env_var != "": + if ip: + warnings.warn( + "The env var 'PYMAPDL_IP' is set, hence the 'ip' argument is overwritten." + ) + ip = ip_env_var LOG.debug(f"An IP ({ip}) has been set using 'PYMAPDL_IP' env var.") diff --git a/tests/test_launcher.py b/tests/test_launcher.py index 5bcd542361..05e0708118 100644 --- a/tests/test_launcher.py +++ b/tests/test_launcher.py @@ -25,6 +25,7 @@ import os import tempfile from time import sleep +import warnings import psutil import pytest @@ -648,7 +649,18 @@ def test_ip_and_start_instance( return # Exit - options = launch_mapdl(start_instance=start_instance, ip=ip, _debug_no_launch=True) + if ( + isinstance(start_instance_envvar, bool) and isinstance(start_instance, bool) + ) or (ip_envvar and ip): + with pytest.warns(UserWarning): + options = launch_mapdl( + start_instance=start_instance, ip=ip, _debug_no_launch=True + ) + else: + with warnings.catch_warnings(): + options = launch_mapdl( + start_instance=start_instance, ip=ip, _debug_no_launch=True + ) if start_instance_envvar is True: assert options["start_instance"] is True