Skip to content

Commit 34337bc

Browse files
committed
Change pytest plugin to use the same importer as other components
Fixes #166
1 parent 479ca16 commit 34337bc

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

changelog/166.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix relative imports for the pytest plugin, note that the relative imports can't be at the top level of the repository alongside .infrahub.yml. They have to be located within a subfolder.

infrahub_sdk/_importer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
def import_module(
1717
module_path: Path, import_root: Optional[str] = None, relative_path: Optional[str] = None
1818
) -> ModuleType:
19+
"""Imports a python module.
20+
21+
Args:
22+
module_path (Path): Absolute path of the module to import.
23+
import_root (Optional[str]): Absolute string path to the current repository.
24+
relative_path (Optional[str]): Relative string path between module_path and import_root.
25+
"""
1926
import_root = import_root or str(module_path.parent)
2027

2128
file_on_disk = module_path

infrahub_sdk/pytest_plugin/items/base.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import difflib
4+
from pathlib import Path
45
from typing import TYPE_CHECKING, Any, Optional, Union
56

67
import pytest
@@ -11,11 +12,11 @@
1112
from ..models import InfrahubInputOutputTest
1213

1314
if TYPE_CHECKING:
14-
from pathlib import Path
15-
1615
from ...schema.repository import InfrahubRepositoryConfigElement
1716
from ..models import InfrahubTest
1817

18+
_infrahub_config_path_attribute = "infrahub_config_path"
19+
1920

2021
class InfrahubItem(pytest.Item):
2122
def __init__(
@@ -74,3 +75,16 @@ def repr_failure(self, excinfo: pytest.ExceptionInfo, style: Optional[str] = Non
7475

7576
def reportinfo(self) -> tuple[Union[Path, str], Optional[int], str]:
7677
return self.path, 0, f"resource: {self.name}"
78+
79+
@property
80+
def repository_base(self) -> str:
81+
"""Return the path to the root of the repository
82+
83+
This will be an absolute path if --infrahub-config-path is an absolut path as happens when
84+
tests are started from within Infrahub server.
85+
"""
86+
config_path: Path = getattr(self.session, _infrahub_config_path_attribute)
87+
if config_path.is_absolute():
88+
return str(config_path.parent)
89+
90+
return str(Path.cwd())

infrahub_sdk/pytest_plugin/items/check.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import annotations
22

33
import asyncio
4+
from pathlib import Path
45
from typing import TYPE_CHECKING, Any, Optional
56

67
import ujson
78
from httpx import HTTPStatusError
89

9-
from ...checks import get_check_class_instance
1010
from ..exceptions import CheckDefinitionError, CheckResultError
1111
from ..models import InfrahubTestExpectedResult
1212
from .base import InfrahubItem
@@ -33,9 +33,12 @@ def __init__(
3333
self.check_instance: InfrahubCheck
3434

3535
def instantiate_check(self) -> None:
36-
self.check_instance = get_check_class_instance(
37-
check_config=self.resource_config, # type: ignore[arg-type]
38-
search_path=self.session.infrahub_config_path.parent, # type: ignore[attr-defined]
36+
relative_path = (
37+
str(self.resource_config.file_path.parent) if self.resource_config.file_path.parent != Path() else None # type: ignore[attr-defined]
38+
)
39+
40+
self.check_instance = self.resource_config.load_class( # type: ignore[attr-defined]
41+
import_root=self.repository_base, relative_path=relative_path
3942
)
4043

4144
def run_check(self, variables: dict[str, Any]) -> Any:

infrahub_sdk/pytest_plugin/items/python_transform.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import annotations
22

33
import asyncio
4+
from pathlib import Path
45
from typing import TYPE_CHECKING, Any, Optional
56

67
import ujson
78
from httpx import HTTPStatusError
89

9-
from ...transforms import get_transform_class_instance
1010
from ..exceptions import OutputMatchError, PythonTransformDefinitionError
1111
from ..models import InfrahubTestExpectedResult
1212
from .base import InfrahubItem
@@ -33,9 +33,11 @@ def __init__(
3333
self.transform_instance: InfrahubTransform
3434

3535
def instantiate_transform(self) -> None:
36-
self.transform_instance = get_transform_class_instance(
37-
transform_config=self.resource_config, # type: ignore[arg-type]
38-
search_path=self.session.infrahub_config_path.parent, # type: ignore[attr-defined]
36+
relative_path = (
37+
str(self.resource_config.file_path.parent) if self.resource_config.file_path.parent != Path() else None # type: ignore[attr-defined]
38+
)
39+
self.transform_instance = self.resource_config.load_class( # type: ignore[attr-defined]
40+
import_root=self.repository_base, relative_path=relative_path
3941
)
4042

4143
def run_transform(self, variables: dict[str, Any]) -> Any:

0 commit comments

Comments
 (0)