Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/+pytest-stash-migration.housekeeping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Migrate the pytest plugin's session state to `pytest.Stash`. Internal change with no impact on external test files; removes inline `# type: ignore[attr-defined]` comments from `infrahub_sdk/pytest_plugin/`. Adds a `pytest>=7.0` floor to the `all` extra to make the requirement explicit.
15 changes: 15 additions & 0 deletions infrahub_sdk/pytest_plugin/_stash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

import pytest

if TYPE_CHECKING:
from .. import InfrahubClientSync
from ..schema.repository import InfrahubRepositoryConfig


INFRAHUB_CLIENT_KEY: pytest.StashKey[InfrahubClientSync] = pytest.StashKey()
INFRAHUB_CONFIG_PATH_KEY: pytest.StashKey[Path] = pytest.StashKey()
INFRAHUB_REPO_CONFIG_KEY: pytest.StashKey[InfrahubRepositoryConfig] = pytest.StashKey()
5 changes: 2 additions & 3 deletions infrahub_sdk/pytest_plugin/items/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
import pytest
import ujson

from .._stash import INFRAHUB_CONFIG_PATH_KEY
from ..exceptions import InvalidResourceConfigError
from ..models import InfrahubInputOutputTest

if TYPE_CHECKING:
from ...schema.repository import InfrahubRepositoryConfigElement
from ..models import InfrahubTest

_infrahub_config_path_attribute = "infrahub_config_path"


class InfrahubItem(pytest.Item):
def __init__(
Expand Down Expand Up @@ -78,7 +77,7 @@ def repository_base(self) -> str:
This will be an absolute path if --infrahub-config-path is an absolute path as happens when
tests are started from within Infrahub server.
"""
config_path: Path = getattr(self.session, _infrahub_config_path_attribute)
config_path = self.session.stash[INFRAHUB_CONFIG_PATH_KEY]
if config_path.is_absolute():
return str(config_path.parent)

Expand Down
3 changes: 2 additions & 1 deletion infrahub_sdk/pytest_plugin/items/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ujson
from httpx import HTTPStatusError

from .._stash import INFRAHUB_CLIENT_KEY
from ..exceptions import CheckDefinitionError, CheckResultError
from ..models import InfrahubTestExpectedResult
from .base import InfrahubItem
Expand Down Expand Up @@ -83,7 +84,7 @@ def runtest(self) -> None:

class InfrahubCheckIntegrationItem(InfrahubCheckItem):
def runtest(self) -> None:
input_data = self.session.infrahub_client.query_gql_query( # type: ignore[attr-defined]
input_data = self.session.stash[INFRAHUB_CLIENT_KEY].query_gql_query(
self.check_instance.query,
variables=self.test.spec.get_variables_data(), # type: ignore[union-attr]
)
Expand Down
5 changes: 3 additions & 2 deletions infrahub_sdk/pytest_plugin/items/graphql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from httpx import HTTPStatusError

from ...analyzer import GraphQLQueryAnalyzer
from .._stash import INFRAHUB_CLIENT_KEY, INFRAHUB_CONFIG_PATH_KEY
from ..exceptions import OutputMatchError
from ..models import InfrahubTestExpectedResult
from .base import InfrahubItem
Expand All @@ -20,7 +21,7 @@ def validate_resource_config(self) -> None:
return

def execute_query(self) -> Any:
return self.session.infrahub_client.query_gql_query( # type: ignore[attr-defined]
return self.session.stash[INFRAHUB_CLIENT_KEY].query_gql_query(
self.test.spec.query, # type: ignore[union-attr]
variables=self.test.spec.get_variables_data(), # type: ignore[union-attr]
)
Expand All @@ -47,7 +48,7 @@ def repr_failure(self, excinfo: pytest.ExceptionInfo, style: str | None = None)

class InfrahubGraphQLQuerySmokeItem(InfrahubGraphQLQueryItem):
def runtest(self) -> None:
query = (self.session.infrahub_config_path.parent / self.test.spec.path).read_text() # type: ignore[attr-defined,union-attr]
query = (self.session.stash[INFRAHUB_CONFIG_PATH_KEY].parent / self.test.spec.path).read_text() # type: ignore[union-attr]
GraphQLQueryAnalyzer(query)


Expand Down
7 changes: 4 additions & 3 deletions infrahub_sdk/pytest_plugin/items/jinja2_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from ...template import Jinja2Template
from ...template.exceptions import JinjaTemplateError
from .._stash import INFRAHUB_CLIENT_KEY, INFRAHUB_CONFIG_PATH_KEY
from ..exceptions import OutputMatchError
from ..models import InfrahubInputOutputTest, InfrahubTestExpectedResult
from .base import InfrahubItem
Expand All @@ -23,7 +24,7 @@ class InfrahubJinja2Item(InfrahubItem):
def _get_jinja2(self) -> Jinja2Template:
return Jinja2Template(
template=Path(self.resource_config.template_path), # type: ignore[attr-defined]
template_directory=Path(self.session.infrahub_config_path.parent), # type: ignore[attr-defined]
template_directory=Path(self.session.stash[INFRAHUB_CONFIG_PATH_KEY].parent),
)

def get_jinja2_environment(self) -> jinja2.Environment:
Expand Down Expand Up @@ -82,7 +83,7 @@ def repr_failure(self, excinfo: pytest.ExceptionInfo, style: str | None = None)

class InfrahubJinja2TransformSmokeItem(InfrahubJinja2Item):
def runtest(self) -> None:
file_path: Path = self.session.infrahub_config_path.parent / self.resource_config.template_path # type: ignore[attr-defined]
file_path: Path = self.session.stash[INFRAHUB_CONFIG_PATH_KEY].parent / self.resource_config.template_path # type: ignore[attr-defined]
self.get_jinja2_environment().parse(file_path.read_text(encoding="utf-8"), filename=file_path.name)


Expand All @@ -103,7 +104,7 @@ def repr_failure(self, excinfo: pytest.ExceptionInfo, style: str | None = None)

class InfrahubJinja2TransformIntegrationItem(InfrahubJinja2Item):
def runtest(self) -> None:
graphql_result = self.session.infrahub_client.query_gql_query( # type: ignore[attr-defined]
graphql_result = self.session.stash[INFRAHUB_CLIENT_KEY].query_gql_query(
self.resource_config.query, # type: ignore[attr-defined]
variables=self.test.spec.get_variables_data(), # type: ignore[union-attr]
)
Expand Down
5 changes: 3 additions & 2 deletions infrahub_sdk/pytest_plugin/items/python_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from httpx import HTTPStatusError

from ...node import InfrahubNode
from .._stash import INFRAHUB_CLIENT_KEY
from ..exceptions import OutputMatchError, PythonTransformDefinitionError
from ..models import InfrahubTestExpectedResult
from .base import InfrahubItem
Expand Down Expand Up @@ -40,7 +41,7 @@ def instantiate_transform(self) -> None:
transform_class = self.resource_config.load_class( # type: ignore[attr-defined]
import_root=self.repository_base, relative_path=relative_path
)
client = self.session.infrahub_client # type: ignore[attr-defined]
client = self.session.stash[INFRAHUB_CLIENT_KEY]
# TODO: Look into seeing how a transform class may use the branch, but set as a empty string for the time being to keep current behaviour
self.transform_instance = transform_class(branch="", client=client, infrahub_node=InfrahubNode)

Expand Down Expand Up @@ -90,7 +91,7 @@ def runtest(self) -> None:
class InfrahubPythonTransformIntegrationItem(InfrahubPythonTransformItem):
def runtest(self) -> None:
self.instantiate_transform()
input_data = self.session.infrahub_client.query_gql_query( # type: ignore[attr-defined]
input_data = self.session.stash[INFRAHUB_CLIENT_KEY].query_gql_query(
self.transform_instance.query,
variables=self.test.spec.get_variables_data(), # type: ignore[union-attr]
)
Expand Down
4 changes: 3 additions & 1 deletion infrahub_sdk/pytest_plugin/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
import yaml

from ._stash import INFRAHUB_REPO_CONFIG_KEY
from .exceptions import InvalidResourceConfigError
from .items import (
InfrahubCheckIntegrationItem,
Expand Down Expand Up @@ -59,7 +60,8 @@ def get_resource_config(self, group: InfrahubTestGroup) -> Any | None:

resource_config = None
if resource_config_function is not None:
func = getattr(self.session.infrahub_repo_config, resource_config_function) # type:ignore[attr-defined]
repo_config = self.session.stash[INFRAHUB_REPO_CONFIG_KEY]
func = getattr(repo_config, resource_config_function)
with contextlib.suppress(KeyError):
resource_config = func(group.resource_name)

Expand Down
18 changes: 10 additions & 8 deletions infrahub_sdk/pytest_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .. import InfrahubClientSync
from ..utils import is_valid_url
from ._stash import INFRAHUB_CLIENT_KEY, INFRAHUB_CONFIG_PATH_KEY, INFRAHUB_REPO_CONFIG_KEY
from .loader import InfrahubYamlFile
from .utils import find_repository_config_file, load_repository_config

Expand Down Expand Up @@ -62,13 +63,15 @@ def pytest_addoption(parser: pytest.Parser) -> None:


def pytest_sessionstart(session: pytest.Session) -> None:
if session.config.option.infrahub_repo_config:
session.infrahub_config_path = Path(session.config.option.infrahub_repo_config) # type: ignore[attr-defined]
else:
session.infrahub_config_path = find_repository_config_file() # type: ignore[attr-defined]
config_path = (
Path(session.config.option.infrahub_repo_config)
if session.config.option.infrahub_repo_config
else find_repository_config_file()
)
session.stash[INFRAHUB_CONFIG_PATH_KEY] = config_path

if session.infrahub_config_path.is_file(): # type: ignore[attr-defined]
session.infrahub_repo_config = load_repository_config(repo_config_file=session.infrahub_config_path) # type: ignore[attr-defined]
if config_path.is_file():
session.stash[INFRAHUB_REPO_CONFIG_KEY] = load_repository_config(repo_config_file=config_path)

if not is_valid_url(session.config.option.infrahub_address):
pytest.exit("Infrahub test instance address is not a valid URL", returncode=1)
Expand All @@ -84,8 +87,7 @@ def pytest_sessionstart(session: pytest.Session) -> None:
client_config["username"] = session.config.option.infrahub_username
client_config["password"] = session.config.option.infrahub_password

infrahub_client = InfrahubClientSync(config=client_config)
session.infrahub_client = infrahub_client # type: ignore[attr-defined]
session.stash[INFRAHUB_CLIENT_KEY] = InfrahubClientSync(config=client_config)


def pytest_collect_file(parent: pytest.Collector | pytest.Item, file_path: Path) -> InfrahubYamlFile | None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ all = [
"numpy>=1.24.2; python_version<'3.12'",
"numpy>=1.26.2; python_version>='3.12'",
"pyarrow>=14",
"pytest",
"pytest>=7.0",
"pyyaml>=6",
"rich>=12,<14",
"typer>=0.12.5",
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading