From 9cdcaaa15f3d11f160e00ca3836353cd5f47e9b1 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Thu, 21 Nov 2024 19:47:25 +0100 Subject: [PATCH] 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 <92810346+pyansys-ci-bot@users.noreply.github.com> --- doc/changelog.d/3559.miscellaneous.md | 1 + src/ansys/mapdl/core/mapdl_core.py | 20 ++++++++++++++----- tests/test_plotting.py | 28 ++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 doc/changelog.d/3559.miscellaneous.md diff --git a/doc/changelog.d/3559.miscellaneous.md b/doc/changelog.d/3559.miscellaneous.md new file mode 100644 index 0000000000..e2048aae00 --- /dev/null +++ b/doc/changelog.d/3559.miscellaneous.md @@ -0,0 +1 @@ +feat: raising error if plot image cannot be obtained \ No newline at end of file diff --git a/src/ansys/mapdl/core/mapdl_core.py b/src/ansys/mapdl/core/mapdl_core.py index 8d6cba88d8..8855a05019 100644 --- a/src/ansys/mapdl/core/mapdl_core.py +++ b/src/ansys/mapdl/core/mapdl_core.py @@ -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) @@ -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""" diff --git a/tests/test_plotting.py b/tests/test_plotting.py index 461a6c3142..4c9ab3f71e 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -22,6 +22,7 @@ """Unit tests regarding plotting.""" import os +from unittest.mock import patch import numpy as np import pytest @@ -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"]] @@ -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")