Skip to content

Commit fb2ccfb

Browse files
committed
refactor: test to avoid launch_mapdl
1 parent aa2ee11 commit fb2ccfb

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

tests/test_launcher.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -295,27 +295,39 @@ def test_license_type_dummy(mapdl, cleared):
295295
)
296296

297297

298-
@requires("local")
299-
@requires("nostudent")
300-
def test_remove_temp_dir_on_exit(mapdl, cleared):
298+
@patch("ansys.mapdl.core.Mapdl._exit_mapdl", lambda *args, **kwargs: None)
299+
def test_remove_temp_dir_on_exit(mapdl, cleared, tmpdir):
301300
"""Ensure the working directory is removed when run_location is not set."""
302-
mapdl_ = launch_mapdl(
303-
port=mapdl.port + 1,
304-
remove_temp_dir_on_exit=True,
305-
start_timeout=start_timeout,
306-
additional_switches=QUICK_LAUNCH_SWITCHES,
307-
)
308301

309-
# possible MAPDL is installed but running in "remote" mode
310-
path = mapdl_.directory
311-
mapdl_.exit()
302+
with (
303+
patch.object(mapdl, "finish_job_on_exit", False),
304+
patch.object(mapdl, "_local", True),
305+
patch.object(mapdl, "remove_temp_dir_on_exit", True),
306+
):
312307

313-
tmp_dir = tempfile.gettempdir()
314-
ans_temp_dir = os.path.join(tmp_dir, "ansys_")
315-
if path.startswith(ans_temp_dir):
316-
assert not os.path.isdir(path)
317-
else:
318-
assert os.path.isdir(path)
308+
# Testing reaching the method
309+
with patch.object(mapdl, "_remove_temp_dir_on_exit") as mock_rm:
310+
mock_rm.side_effect = None
311+
312+
mapdl.exit(force=True)
313+
314+
mock_rm.assert_called()
315+
assert mapdl.directory == mock_rm.call_args.args[0]
316+
317+
# Testing the method
318+
# Directory to be deleted
319+
ans_temp_dir = os.path.join(tempfile.gettempdir(), "ansys_")
320+
321+
os.makedirs(ans_temp_dir, exist_ok=True)
322+
assert os.path.isdir(ans_temp_dir)
323+
mapdl._remove_temp_dir_on_exit(ans_temp_dir)
324+
assert not os.path.isdir(ans_temp_dir)
325+
326+
# Directory to NOT be deleted
327+
tmp_dir = str(tmpdir)
328+
assert os.path.isdir(tmp_dir)
329+
mapdl._remove_temp_dir_on_exit(tmp_dir)
330+
assert os.path.isdir(tmp_dir)
319331

320332

321333
@requires("local")

0 commit comments

Comments
 (0)