Skip to content

Commit

Permalink
add version_range marker, adjust stubs loc search and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
klmcadams committed Oct 22, 2024
1 parent e1687ad commit 363d3b7
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 13 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ markers = [
"remote_session_launch: tests that launch Mechanical and work with gRPC server inside it",
"remote_session_connect: tests that connect to Mechanical and work with gRPC server inside it",
"minimum_version(num): tests that run if ansys-version is greater than or equal to the minimum version provided",
"version_range(min_revn, max_revn): tests that run if ansys-version is in the range of the minimum and maximum revision numbers inclusive.",
"windows_only: tests that run if the testing platform is on Windows",
"linux_only: tests that run if the testing platform is on Linux",
"cli: tests for the Command Line Interface",
Expand Down
13 changes: 6 additions & 7 deletions src/ansys/mechanical/core/ide_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import json
import os
from pathlib import Path
import re
import site
import sys

Expand All @@ -40,16 +41,14 @@ def get_stubs_location():
pathlib.Path
The path to the ansys-mechanical-stubs installation in site-packages.
"""
site_packages_path = ""
site_packages = site.getsitepackages()
prefix_path = sys.prefix.replace("\\", "\\\\")
site_packages_regex = re.compile(f"{prefix_path}.*site-packages$")
site_packages_paths = list(filter(site_packages_regex.match, site_packages))

for loc in site_packages:
if ("site-packages" in loc) and (sys.prefix in loc):
site_packages_path = loc

if site_packages_path:
if len(site_packages_paths) == 1:
# Get the stubs location
stubs_location = Path(site_packages_path) / "ansys" / "mechanical" / "stubs"
stubs_location = Path(site_packages_paths[0]) / "ansys" / "mechanical" / "stubs"
return stubs_location

raise Exception("Could not retrieve the location of the ansys-mechanical-stubs package.")

Check warning on line 54 in src/ansys/mechanical/core/ide_config.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/ide_config.py#L54

Added line #L54 was not covered by tests
Expand Down
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def pytest_addoption(parser):
def pytest_collection_modifyitems(config, items):
"""Skips tests marked minimum_version if ansys-version is less than mark argument."""
for item in items:
# Skip tests that are less than the minimum version
if "minimum_version" in item.keywords:
revn = [mark.args[0] for mark in item.iter_markers(name="minimum_version")]
if int(config.getoption("--ansys-version")) < revn[0]:
Expand All @@ -393,6 +394,18 @@ def pytest_collection_modifyitems(config, items):
)
item.add_marker(skip_versions)

# Skip tests that are outside of the provided version range. For example,
# @pytest.mark.version_range(241,242)
if "version_range" in item.keywords:
revns = [mark.args for mark in item.iter_markers(name="version_range")][0]
ansys_version = int(config.getoption("--ansys-version"))

if (ansys_version < revns[0]) or (ansys_version > revns[1]):
skip_versions = pytest.mark.skip(
reason=f"Requires ansys-version in the range {revns[0]} to {revns[1]}."
)
item.add_marker(skip_versions)

# Skip on platforms other than Windows
if "windows_only" in item.keywords and sys.platform != "win32":
skip_except_windows = pytest.mark.skip(reason="Test requires Windows platform.")
Expand Down
153 changes: 147 additions & 6 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import os
from pathlib import Path
import subprocess
import sys

import pytest
Expand All @@ -30,6 +31,11 @@
from ansys.mechanical.core.ide_config import get_stubs_location, get_stubs_versions
from ansys.mechanical.core.run import _cli_impl

STUBS_LOC = get_stubs_location()
STUBS_REVNS = get_stubs_versions(STUBS_LOC)
MIN_STUBS_REVN = min(STUBS_REVNS)
MAX_STUBS_REVN = max(STUBS_REVNS)


@pytest.mark.cli
def test_cli_default(disable_cli):
Expand Down Expand Up @@ -263,12 +269,18 @@ def test_ideconfig_cli_ide_exception(capfd, pytestconfig):
)


def test_ideconfig_cli_version_exception(pytestconfig):
"""Test IDE configuration raises an exception when the version is out of bounds."""
def get_ideconfig_vars(pytestconfig):
revision = int(pytestconfig.getoption("ansys_version"))
stubs_location = get_stubs_location()
stubs_revns = get_stubs_versions(stubs_location)

return revision, stubs_location, stubs_revns


def test_ideconfig_cli_version_exception(pytestconfig):
"""Test the IDE configuration raises an exception when the version is out of bounds."""
revision, stubs_location, stubs_revns = get_ideconfig_vars(pytestconfig)

if revision > max(stubs_revns):
with pytest.raises(Exception):
ideconfig_cli_impl(
Expand All @@ -278,7 +290,136 @@ def test_ideconfig_cli_version_exception(pytestconfig):
)


# user settings
# workspace settings
# venv
# default - just ansys-mechanical-ideconfig
@pytest.mark.cli
@pytest.mark.version_range(MIN_STUBS_REVN, MAX_STUBS_REVN)
def test_ideconfig_cli_user_settings(capfd, pytestconfig):
"""Test the IDE configuration prints correct information for user settings."""
# Set the revision number
revision, stubs_location, stubs_revns = get_ideconfig_vars(pytestconfig)

# Run the IDE configuration command for the user settings type
ideconfig_cli_impl(
ide="vscode",
target="user",
revision=revision,
)

# Get output of the IDE configuration command
out, err = capfd.readouterr()
out = out.replace("\\\\", "\\")

# Get the path to the settings.json file based on operating system env vars
settings_json = get_settings_location()

assert f"Update {settings_json} with the following information" in out
assert str(stubs_location) in out


@pytest.mark.cli
@pytest.mark.version_range(MIN_STUBS_REVN, MAX_STUBS_REVN)
def test_ideconfig_cli_workspace_settings(capfd, pytestconfig):
"""Test the IDE configuration prints correct information for workplace settings."""
# Set the revision number
revision, stubs_location, stubs_revns = get_ideconfig_vars(pytestconfig)

# Run the IDE configuration command
ideconfig_cli_impl(
ide="vscode",
target="workspace",
revision=revision,
)

# Get output of the IDE configuration command
out, err = capfd.readouterr()
out = out.replace("\\\\", "\\")

# Get the path to the settings.json file based on the current directory & .vscode folder
settings_json = Path.cwd() / ".vscode" / "settings.json"

# Assert the correct settings.json file and stubs location is in the output
assert f"Update {settings_json} with the following information" in out
assert str(stubs_location) in out
assert "Please ensure the .vscode folder is in the root of your project or repository" in out


@pytest.mark.cli
@pytest.mark.python_env
@pytest.mark.version_range(MIN_STUBS_REVN, MAX_STUBS_REVN)
def test_ideconfig_cli_venv(test_env, run_subprocess, rootdir, pytestconfig):
"""Test the IDE configuration location when a virtual environment is active."""
# Set the revision number
revision = pytestconfig.getoption("ansys_version")

# Install pymechanical
subprocess.check_call(
[test_env.python, "-m", "pip", "install", "-e", "."],
cwd=rootdir,
env=test_env.env,
)

# Get the virtual environment location
process, venv_loc, stderr = run_subprocess(
[test_env.python, "-c", "'import sys; print(sys.prefix)'"],
env=test_env.env,
)
# Decode stdout and fix extra backslashes in paths
venv_loc = venv_loc.decode().replace("\\\\", "\\")

# Run ansys-mechanical-ideconfig in the test virtual environment
process, stdout, stderr = run_subprocess(
[
"ansys-mechanical-ideconfig",
"--ide",
"vscode",
"--target",
"user",
"--revision",
str(revision),
],
env=test_env.env,
)
# Decode stdout and fix extra backslashes in paths
stdout = stdout.decode().replace("\\\\", "\\")

# Assert virtual environment is in the stdout
assert venv_loc in stdout


@pytest.mark.cli
@pytest.mark.python_env
@pytest.mark.version_range(MIN_STUBS_REVN, MAX_STUBS_REVN)
def test_ideconfig_cli_default(test_env, run_subprocess, rootdir, pytestconfig):
"""Test the IDE configuration location when no arguments are supplied."""
# Get the revision number
revision = pytestconfig.getoption("ansys_version")
# Set part of the settings.json path
settings_json_fragment = Path("Code") / "User" / "settings.json"

# Install pymechanical
subprocess.check_call(
[test_env.python, "-m", "pip", "install", "-e", "."],
cwd=rootdir,
env=test_env.env,
)

# Get the virtual environment location
process, venv_loc, stderr = run_subprocess(
[test_env.python, "-c", "'import sys; print(sys.prefix)'"],
env=test_env.env,
)
# Decode stdout and fix extra backslashes in paths
venv_loc = venv_loc.decode().replace("\\\\", "\\")

# Run ansys-mechanical-ideconfig in the test virtual environment
process, stdout, stderr = run_subprocess(
[
"ansys-mechanical-ideconfig",
],
env=test_env.env,
)
# Decode stdout and fix extra backslashes in paths
stdout = stdout.decode().replace("\\\\", "\\")

assert revision in stdout
assert str(settings_json_fragment) in stdout
assert venv_loc in stdout

0 comments on commit 363d3b7

Please sign in to comment.