Skip to content

Commit ea45dd4

Browse files
committed
refactor(pytest_plugin): migrate session state to pytest.Stash
1 parent 551b629 commit ea45dd4

11 files changed

Lines changed: 45 additions & 22 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
from typing import TYPE_CHECKING
5+
6+
import pytest
7+
8+
if TYPE_CHECKING:
9+
from .. import InfrahubClientSync
10+
from ..schema.repository import InfrahubRepositoryConfig
11+
12+
13+
INFRAHUB_CLIENT_KEY: pytest.StashKey[InfrahubClientSync] = pytest.StashKey()
14+
INFRAHUB_CONFIG_PATH_KEY: pytest.StashKey[Path] = pytest.StashKey()
15+
INFRAHUB_REPO_CONFIG_KEY: pytest.StashKey[InfrahubRepositoryConfig] = pytest.StashKey()

infrahub_sdk/pytest_plugin/items/base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
import pytest
88
import ujson
99

10+
from .._stash import INFRAHUB_CONFIG_PATH_KEY
1011
from ..exceptions import InvalidResourceConfigError
1112
from ..models import InfrahubInputOutputTest
1213

1314
if TYPE_CHECKING:
1415
from ...schema.repository import InfrahubRepositoryConfigElement
1516
from ..models import InfrahubTest
1617

17-
_infrahub_config_path_attribute = "infrahub_config_path"
18-
1918

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

infrahub_sdk/pytest_plugin/items/check.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import ujson
88
from httpx import HTTPStatusError
99

10+
from .._stash import INFRAHUB_CLIENT_KEY
1011
from ..exceptions import CheckDefinitionError, CheckResultError
1112
from ..models import InfrahubTestExpectedResult
1213
from .base import InfrahubItem
@@ -83,7 +84,7 @@ def runtest(self) -> None:
8384

8485
class InfrahubCheckIntegrationItem(InfrahubCheckItem):
8586
def runtest(self) -> None:
86-
input_data = self.session.infrahub_client.query_gql_query( # type: ignore[attr-defined]
87+
input_data = self.session.stash[INFRAHUB_CLIENT_KEY].query_gql_query(
8788
self.check_instance.query,
8889
variables=self.test.spec.get_variables_data(), # type: ignore[union-attr]
8990
)

infrahub_sdk/pytest_plugin/items/graphql_query.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from httpx import HTTPStatusError
77

88
from ...analyzer import GraphQLQueryAnalyzer
9+
from .._stash import INFRAHUB_CLIENT_KEY, INFRAHUB_CONFIG_PATH_KEY
910
from ..exceptions import OutputMatchError
1011
from ..models import InfrahubTestExpectedResult
1112
from .base import InfrahubItem
@@ -20,7 +21,7 @@ def validate_resource_config(self) -> None:
2021
return
2122

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

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

5354

infrahub_sdk/pytest_plugin/items/jinja2_transform.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from ...template import Jinja2Template
1313
from ...template.exceptions import JinjaTemplateError
14+
from .._stash import INFRAHUB_CLIENT_KEY, INFRAHUB_CONFIG_PATH_KEY
1415
from ..exceptions import OutputMatchError
1516
from ..models import InfrahubInputOutputTest, InfrahubTestExpectedResult
1617
from .base import InfrahubItem
@@ -23,7 +24,7 @@ class InfrahubJinja2Item(InfrahubItem):
2324
def _get_jinja2(self) -> Jinja2Template:
2425
return Jinja2Template(
2526
template=Path(self.resource_config.template_path), # type: ignore[attr-defined]
26-
template_directory=Path(self.session.infrahub_config_path.parent), # type: ignore[attr-defined]
27+
template_directory=Path(self.session.stash[INFRAHUB_CONFIG_PATH_KEY].parent),
2728
)
2829

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

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

8889

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

104105
class InfrahubJinja2TransformIntegrationItem(InfrahubJinja2Item):
105106
def runtest(self) -> None:
106-
graphql_result = self.session.infrahub_client.query_gql_query( # type: ignore[attr-defined]
107+
graphql_result = self.session.stash[INFRAHUB_CLIENT_KEY].query_gql_query(
107108
self.resource_config.query, # type: ignore[attr-defined]
108109
variables=self.test.spec.get_variables_data(), # type: ignore[union-attr]
109110
)

infrahub_sdk/pytest_plugin/items/python_transform.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from httpx import HTTPStatusError
99

1010
from ...node import InfrahubNode
11+
from .._stash import INFRAHUB_CLIENT_KEY
1112
from ..exceptions import OutputMatchError, PythonTransformDefinitionError
1213
from ..models import InfrahubTestExpectedResult
1314
from .base import InfrahubItem
@@ -40,7 +41,7 @@ def instantiate_transform(self) -> None:
4041
transform_class = self.resource_config.load_class( # type: ignore[attr-defined]
4142
import_root=self.repository_base, relative_path=relative_path
4243
)
43-
client = self.session.infrahub_client # type: ignore[attr-defined]
44+
client = self.session.stash[INFRAHUB_CLIENT_KEY]
4445
# 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
4546
self.transform_instance = transform_class(branch="", client=client, infrahub_node=InfrahubNode)
4647

@@ -90,7 +91,7 @@ def runtest(self) -> None:
9091
class InfrahubPythonTransformIntegrationItem(InfrahubPythonTransformItem):
9192
def runtest(self) -> None:
9293
self.instantiate_transform()
93-
input_data = self.session.infrahub_client.query_gql_query( # type: ignore[attr-defined]
94+
input_data = self.session.stash[INFRAHUB_CLIENT_KEY].query_gql_query(
9495
self.transform_instance.query,
9596
variables=self.test.spec.get_variables_data(), # type: ignore[union-attr]
9697
)

infrahub_sdk/pytest_plugin/loader.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88
import yaml
99

10+
from ._stash import INFRAHUB_REPO_CONFIG_KEY
1011
from .exceptions import InvalidResourceConfigError
1112
from .items import (
1213
InfrahubCheckIntegrationItem,
@@ -59,7 +60,8 @@ def get_resource_config(self, group: InfrahubTestGroup) -> Any | None:
5960

6061
resource_config = None
6162
if resource_config_function is not None:
62-
func = getattr(self.session.infrahub_repo_config, resource_config_function) # type:ignore[attr-defined]
63+
repo_config = self.session.stash[INFRAHUB_REPO_CONFIG_KEY]
64+
func = getattr(repo_config, resource_config_function)
6365
with contextlib.suppress(KeyError):
6466
resource_config = func(group.resource_name)
6567

infrahub_sdk/pytest_plugin/plugin.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from .. import InfrahubClientSync
99
from ..utils import is_valid_url
10+
from ._stash import INFRAHUB_CLIENT_KEY, INFRAHUB_CONFIG_PATH_KEY, INFRAHUB_REPO_CONFIG_KEY
1011
from .loader import InfrahubYamlFile
1112
from .utils import find_repository_config_file, load_repository_config
1213

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

6364

6465
def pytest_sessionstart(session: pytest.Session) -> None:
65-
if session.config.option.infrahub_repo_config:
66-
session.infrahub_config_path = Path(session.config.option.infrahub_repo_config) # type: ignore[attr-defined]
67-
else:
68-
session.infrahub_config_path = find_repository_config_file() # type: ignore[attr-defined]
66+
config_path = (
67+
Path(session.config.option.infrahub_repo_config)
68+
if session.config.option.infrahub_repo_config
69+
else find_repository_config_file()
70+
)
71+
session.stash[INFRAHUB_CONFIG_PATH_KEY] = config_path
6972

70-
if session.infrahub_config_path.is_file(): # type: ignore[attr-defined]
71-
session.infrahub_repo_config = load_repository_config(repo_config_file=session.infrahub_config_path) # type: ignore[attr-defined]
73+
if config_path.is_file():
74+
session.stash[INFRAHUB_REPO_CONFIG_KEY] = load_repository_config(repo_config_file=config_path)
7275

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

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

9092

9193
def pytest_collect_file(parent: pytest.Collector | pytest.Item, file_path: Path) -> InfrahubYamlFile | None:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ all = [
5959
"numpy>=1.24.2; python_version<'3.12'",
6060
"numpy>=1.26.2; python_version>='3.12'",
6161
"pyarrow>=14",
62-
"pytest",
62+
"pytest>=7.0",
6363
"pyyaml>=6",
6464
"rich>=12,<14",
6565
"typer>=0.12.5",

0 commit comments

Comments
 (0)