Skip to content
1 change: 1 addition & 0 deletions doc/changelog.d/787.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FEAT: Add test for building gallery
26 changes: 26 additions & 0 deletions tests/embedding/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
50 changes: 50 additions & 0 deletions tests/scripts/build_gallery_test.py
Original file line number Diff line number Diff line change
@@ -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)