-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: Update private app data creation and add tests (#986)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: Kerry McAdams <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
cb0043b
commit af612b6
Showing
7 changed files
with
122 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Update private app data creation and add tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# 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. | ||
|
||
"""Embedding tests related to app data""" | ||
|
||
import os | ||
from pathlib import Path | ||
import sys | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from ansys.mechanical.core.embedding.appdata import UniqueUserProfile | ||
|
||
|
||
@pytest.mark.embedding_scripts | ||
@pytest.mark.python_env | ||
def test_private_appdata(pytestconfig, run_subprocess, rootdir): | ||
"""Test embedded instance does not save ShowTriad using a test-scoped Python environment.""" | ||
|
||
version = pytestconfig.getoption("ansys_version") | ||
embedded_py = os.path.join(rootdir, "tests", "scripts", "run_embedded_app.py") | ||
|
||
run_subprocess([sys.executable, embedded_py, version, "True", "Set"]) | ||
process, stdout, stderr = run_subprocess([sys.executable, embedded_py, version, "True", "Run"]) | ||
stdout = stdout.decode() | ||
assert "ShowTriad value is True" in stdout | ||
|
||
|
||
@pytest.mark.embedding_scripts | ||
@pytest.mark.python_env | ||
def test_normal_appdata(pytestconfig, run_subprocess, rootdir): | ||
"""Test embedded instance saves ShowTriad value using a test-scoped Python environment.""" | ||
version = pytestconfig.getoption("ansys_version") | ||
|
||
embedded_py = os.path.join(rootdir, "tests", "scripts", "run_embedded_app.py") | ||
|
||
run_subprocess([sys.executable, embedded_py, version, "False", "Set"]) | ||
process, stdout, stderr = run_subprocess([sys.executable, embedded_py, version, "False", "Run"]) | ||
run_subprocess([sys.executable, embedded_py, version, "False", "Reset"]) | ||
|
||
stdout = stdout.decode() | ||
# Assert ShowTriad was set to False for regular embedded session | ||
assert "ShowTriad value is False" in stdout | ||
|
||
|
||
@pytest.mark.embedding | ||
def test_uniqueprofile_creation(): | ||
"""Test profile is copied when copy_profile is ``True`` and is not copied when ``False``.""" | ||
folder_to_check = Path("AppData") / "Local" / "Ansys" if os.name == "nt" else Path(".mw") | ||
|
||
# Create private app data without copying profiles | ||
private_data2 = UniqueUserProfile(profile_name="test1", copy_profile=False) | ||
assert not os.path.exists(os.path.join(private_data2.location, folder_to_check)) | ||
|
||
# Check if location is same with same profile name | ||
private_data1 = UniqueUserProfile(profile_name="test1") | ||
assert private_data1.location == private_data2.location | ||
|
||
# Check if folder exists after copying profiles | ||
assert Path(private_data1.location / folder_to_check).exists() | ||
|
||
# Create new profile | ||
private_data3 = UniqueUserProfile(profile_name="test2") | ||
assert private_data2.location != private_data3.location | ||
|
||
|
||
@pytest.mark.embedding | ||
def test_uniqueprofile_env(): | ||
"""Test the environment is correctly updated for the profile based on operating system.""" | ||
profile = UniqueUserProfile("test_env") | ||
env = {} | ||
platforms = ["win32", "linux"] | ||
|
||
for platform in platforms: | ||
with mock.patch.object(sys, "platform", platform): | ||
profile.update_environment(env) | ||
|
||
if platform == "win32": | ||
env["USERPROFILE"] = profile.location | ||
env["APPDATA"] = Path(profile.location) / "AppData" / "Roaming" | ||
env["LOCALAPPDATA"] = Path(profile.location) / "AppData" / "Local" | ||
env["TMP"] = Path(profile.location) / "AppData" / "Local" / "Temp" | ||
env["TEMP"] = Path(profile.location) / "AppData" / "Local " / "Temp" | ||
else: | ||
env["HOME"] = profile.location | ||
|
||
|
||
@pytest.mark.embedding | ||
def test_uniqueprofile_dryrun(): | ||
"""Test the profile is not copied during dry runs.""" | ||
profile = UniqueUserProfile("test_dry_run", dry_run=True) | ||
assert not Path(profile.location).exists() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters