Skip to content

Commit

Permalink
feat: raising error if plot image cannot be obtained (#3559)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
germa89 and pyansys-ci-bot authored Nov 21, 2024
1 parent dff1bab commit 9cdcaaa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3559.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: raising error if plot image cannot be obtained
20 changes: 15 additions & 5 deletions src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2359,12 +2359,15 @@ def _cleanup_loggers(self):
logger.std_out_handler.close()
logger.std_out_handler = None

def is_png_found(self, text: str) -> bool:
# findall returns None if there is no match
return PNG_IS_WRITTEN_TO_FILE.findall(text) is not None

def _get_plot_name(self, text: str) -> str:
"""Obtain the plot filename."""
self._log.debug(text)
png_found = PNG_IS_WRITTEN_TO_FILE.findall(text)
self._log.debug(f"Output from terminal used to find plot name: {text}")

if png_found:
if self.is_png_found(text):
# flush graphics writer
previous_device = self.file_type_for_plots
self.show("CLOSE", mute=True)
Expand All @@ -2377,9 +2380,16 @@ def _get_plot_name(self, text: str) -> str:
if os.path.isfile(filename):
return filename
else: # pragma: no cover
self._log.error("Unable to find screenshot at %s", filename)
raise MapdlRuntimeError("Unable to find screenshot at %s", filename)
else:
self._log.error("Unable to find file in MAPDL command output.")
raise MapdlRuntimeError(
"Unable to find plotted file in MAPDL command output. "
"One possible reason is that the graphics device is not correct. "
"Please check you are using FULL graphics device. "
"For example:\n"
">>> mapdl.graphics('FULL')"
f"\nThe text output from MAPDL is:\n{text}"
)

def _display_plot(self, filename: str) -> None:
"""Display the last generated plot (*.png) from MAPDL"""
Expand Down
28 changes: 27 additions & 1 deletion tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

"""Unit tests regarding plotting."""
import os
from unittest.mock import patch

import numpy as np
import pytest
Expand All @@ -31,7 +32,7 @@
if not has_dependency("pyvista"):
pytest.skip(allow_module_level=True)

from ansys.mapdl.core.errors import ComponentDoesNotExits
from ansys.mapdl.core.errors import ComponentDoesNotExits, MapdlRuntimeError
from ansys.mapdl.core.plotting.visualizer import MapdlPlotter

FORCE_LABELS = [["FX", "FY", "FZ"], ["HEAT"], ["CHRG"]]
Expand Down Expand Up @@ -1254,3 +1255,28 @@ def test_aplot_quality_fail(mapdl, make_block, quality):
match="The argument 'quality' can only be an integer between 1 and 10",
):
mapdl.aplot(quality=quality)


@patch("ansys.mapdl.core.Mapdl.is_png_found", lambda *args, **kwargs: False)
def test_plot_path(mapdl, tmpdir):
mapdl.graphics("POWER")

with pytest.raises(
MapdlRuntimeError,
match="One possible reason is that the graphics device is not correct",
):
mapdl.eplot(vtk=False)


def test_plot_path_screenshoot(mapdl, cleared, tmpdir):
mapdl.graphics("POWER")
# mapdl.screenshot is not affected by the device.
# It should not raise exceptions
scheenshot_path = os.path.join(tmpdir, "screenshot.png")
mapdl.screenshot(scheenshot_path)

assert os.path.exists(scheenshot_path)
assert os.path.getsize(scheenshot_path) > 100 # check if it is not empty

# Returning to previous state.
mapdl.graphics("FULL")

0 comments on commit 9cdcaaa

Please sign in to comment.