Skip to content

Commit

Permalink
Adding a warning if there is a conflict between env vars and arguments (
Browse files Browse the repository at this point in the history
#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>
  • Loading branch information
germa89 and clatapie authored Mar 20, 2024
1 parent 1bdd4ee commit c97a275
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.")

Expand Down
14 changes: 13 additions & 1 deletion tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import os
import tempfile
from time import sleep
import warnings

import psutil
import pytest
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c97a275

Please sign in to comment.