Skip to content

Commit 9cdcaaa

Browse files
feat: raising error if plot image cannot be obtained (#3559)
* feat: raise exception if file is not found. * feat: improving error message * chore: adding changelog file 3559.miscellaneous.md [dependabot-skip] * tests: adding screenshot testing * fix: tests --------- Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent dff1bab commit 9cdcaaa

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feat: raising error if plot image cannot be obtained

src/ansys/mapdl/core/mapdl_core.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,12 +2359,15 @@ def _cleanup_loggers(self):
23592359
logger.std_out_handler.close()
23602360
logger.std_out_handler = None
23612361

2362+
def is_png_found(self, text: str) -> bool:
2363+
# findall returns None if there is no match
2364+
return PNG_IS_WRITTEN_TO_FILE.findall(text) is not None
2365+
23622366
def _get_plot_name(self, text: str) -> str:
23632367
"""Obtain the plot filename."""
2364-
self._log.debug(text)
2365-
png_found = PNG_IS_WRITTEN_TO_FILE.findall(text)
2368+
self._log.debug(f"Output from terminal used to find plot name: {text}")
23662369

2367-
if png_found:
2370+
if self.is_png_found(text):
23682371
# flush graphics writer
23692372
previous_device = self.file_type_for_plots
23702373
self.show("CLOSE", mute=True)
@@ -2377,9 +2380,16 @@ def _get_plot_name(self, text: str) -> str:
23772380
if os.path.isfile(filename):
23782381
return filename
23792382
else: # pragma: no cover
2380-
self._log.error("Unable to find screenshot at %s", filename)
2383+
raise MapdlRuntimeError("Unable to find screenshot at %s", filename)
23812384
else:
2382-
self._log.error("Unable to find file in MAPDL command output.")
2385+
raise MapdlRuntimeError(
2386+
"Unable to find plotted file in MAPDL command output. "
2387+
"One possible reason is that the graphics device is not correct. "
2388+
"Please check you are using FULL graphics device. "
2389+
"For example:\n"
2390+
">>> mapdl.graphics('FULL')"
2391+
f"\nThe text output from MAPDL is:\n{text}"
2392+
)
23832393

23842394
def _display_plot(self, filename: str) -> None:
23852395
"""Display the last generated plot (*.png) from MAPDL"""

tests/test_plotting.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
"""Unit tests regarding plotting."""
2424
import os
25+
from unittest.mock import patch
2526

2627
import numpy as np
2728
import pytest
@@ -31,7 +32,7 @@
3132
if not has_dependency("pyvista"):
3233
pytest.skip(allow_module_level=True)
3334

34-
from ansys.mapdl.core.errors import ComponentDoesNotExits
35+
from ansys.mapdl.core.errors import ComponentDoesNotExits, MapdlRuntimeError
3536
from ansys.mapdl.core.plotting.visualizer import MapdlPlotter
3637

3738
FORCE_LABELS = [["FX", "FY", "FZ"], ["HEAT"], ["CHRG"]]
@@ -1254,3 +1255,28 @@ def test_aplot_quality_fail(mapdl, make_block, quality):
12541255
match="The argument 'quality' can only be an integer between 1 and 10",
12551256
):
12561257
mapdl.aplot(quality=quality)
1258+
1259+
1260+
@patch("ansys.mapdl.core.Mapdl.is_png_found", lambda *args, **kwargs: False)
1261+
def test_plot_path(mapdl, tmpdir):
1262+
mapdl.graphics("POWER")
1263+
1264+
with pytest.raises(
1265+
MapdlRuntimeError,
1266+
match="One possible reason is that the graphics device is not correct",
1267+
):
1268+
mapdl.eplot(vtk=False)
1269+
1270+
1271+
def test_plot_path_screenshoot(mapdl, cleared, tmpdir):
1272+
mapdl.graphics("POWER")
1273+
# mapdl.screenshot is not affected by the device.
1274+
# It should not raise exceptions
1275+
scheenshot_path = os.path.join(tmpdir, "screenshot.png")
1276+
mapdl.screenshot(scheenshot_path)
1277+
1278+
assert os.path.exists(scheenshot_path)
1279+
assert os.path.getsize(scheenshot_path) > 100 # check if it is not empty
1280+
1281+
# Returning to previous state.
1282+
mapdl.graphics("FULL")

0 commit comments

Comments
 (0)