diff --git a/doc/changelog.d/787.added.md b/doc/changelog.d/787.added.md new file mode 100644 index 000000000..210d9d7cd --- /dev/null +++ b/doc/changelog.d/787.added.md @@ -0,0 +1 @@ +FEAT: Add test for building gallery \ No newline at end of file diff --git a/tests/embedding/test_app.py b/tests/embedding/test_app.py index 490619b86..e9d138955 100644 --- a/tests/embedding/test_app.py +++ b/tests/embedding/test_app.py @@ -262,6 +262,32 @@ def test_normal_appdata(pytestconfig, run_subprocess, rootdir): assert "ShowTriad value is False" in stdout +@pytest.mark.embedding_scripts +def test_building_gallery(pytestconfig, run_subprocess, rootdir): + """Test for building gallery check. + + When building the gallery, each example file creates another instance of the app. + When the BUILDING_GALLERY flag is enabled, only one instance is kept. + This is to test the bug fixed in https://github.com/ansys/pymechanical/pull/784 + and will fail on PyMechanical version 0.11.0 + """ + version = pytestconfig.getoption("ansys_version") + + embedded_gallery_py = os.path.join(rootdir, "tests", "scripts", "build_gallery_test.py") + + _, stderr = run_subprocess([sys.executable, embedded_gallery_py, version, "False"], None, False) + stderr = stderr.decode() + + # Assert Exception + assert "Cannot have more than one embedded mechanical instance" in stderr + + stdout, _ = run_subprocess([sys.executable, embedded_gallery_py, version, "True"]) + stdout = stdout.decode() + + # Assert stdout after launching multiple instances + assert "Multiple App launched with building gallery flag on" in stdout + + @pytest.mark.embedding def test_rm_lockfile(embedded_app, tmp_path: pytest.TempPathFactory): """Test lock file is removed on close of embedded application.""" diff --git a/tests/scripts/build_gallery_test.py b/tests/scripts/build_gallery_test.py new file mode 100644 index 000000000..ead9afe63 --- /dev/null +++ b/tests/scripts/build_gallery_test.py @@ -0,0 +1,50 @@ +# Copyright (C) 2022 - 2024 ANSYS, Inc. and/or its affiliates. +# SPDX-License-Identifier: MIT +# +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +"""Launch embedded instance with build gallery flag.""" +import sys + +import ansys.mechanical.core as pymechanical + + +def launch_app(version): + """Launch embedded instance of app.""" + # Configuration.configure(level=logging.DEBUG, to_stdout=True, base_directory=None) + app = pymechanical.App(version=version) + app.update_globals(globals()) + return app + + +if __name__ == "__main__": + version = int(sys.argv[1]) + build_gallery_flag = sys.argv[2] + app1 = launch_app(version) + + if build_gallery_flag == "True": + + pymechanical.BUILDING_GALLERY = True + + app2 = launch_app(version) + print("Multiple App launched with building gallery flag on") + + elif build_gallery_flag == "False": + app2 = launch_app(version)