Skip to content
Merged
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
7 changes: 7 additions & 0 deletions infrahub_sdk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ class InvalidResponseError(Error):
"""Raised when an object requires an initialization step before use"""


class RepositoryFileNotFoundError(Error):
def __init__(self, file_path: str, message: str | None = None) -> None:
self.file_path = file_path
self.message = message or f"File '{file_path}' does not exist."
super().__init__(self.message)


class FileNotValidError(Error):
def __init__(self, name: str, message: str = "") -> None:
self.message = message or f"Cannot parse '{name}' content."
Expand Down
6 changes: 5 additions & 1 deletion infrahub_sdk/schema/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ..exceptions import (
FragmentFileNotFoundError,
ModuleImportError,
RepositoryFileNotFoundError,
ResourceNotDefinedError,
)
from ..generator import InfrahubGenerator
Expand Down Expand Up @@ -157,7 +158,10 @@ class InfrahubRepositoryGraphQLConfig(InfrahubRepositoryConfigElement):

def load_query(self, relative_path: str = ".") -> str:
file_name = Path(f"{relative_path}/{self.file_path}")
return file_name.read_text(encoding="UTF-8")
try:
return file_name.read_text(encoding="UTF-8")
except FileNotFoundError as exc:
raise RepositoryFileNotFoundError(file_path=str(self.file_path)) from exc


class InfrahubRepositoryFragmentConfig(InfrahubRepositoryConfigElement):
Expand Down
24 changes: 22 additions & 2 deletions tests/unit/sdk/test_schema_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

import pytest

from infrahub_sdk.exceptions import FragmentFileNotFoundError, ResourceNotDefinedError
from infrahub_sdk.schema.repository import InfrahubRepositoryConfig, InfrahubRepositoryFragmentConfig
from infrahub_sdk.exceptions import FragmentFileNotFoundError, RepositoryFileNotFoundError, ResourceNotDefinedError
from infrahub_sdk.schema.repository import (
InfrahubRepositoryConfig,
InfrahubRepositoryFragmentConfig,
InfrahubRepositoryGraphQLConfig,
)


@pytest.fixture
Expand Down Expand Up @@ -316,3 +320,19 @@ def test_load_fragments_missing_file_raises() -> None:
with tempfile.TemporaryDirectory() as tmp, pytest.raises(FragmentFileNotFoundError) as exc_info:
cfg.load_fragments(relative_path=tmp)
assert "does_not_exist.gql" in exc_info.value.file_path


def test_load_query_returns_content() -> None:
with tempfile.TemporaryDirectory() as tmp:
query_file = Path(tmp) / "my_query.gql"
query_file.write_text("{ devices { id } }", encoding="UTF-8")
cfg = InfrahubRepositoryGraphQLConfig(name="my_query", file_path=Path("my_query.gql"))
result = cfg.load_query(relative_path=tmp)
assert result == "{ devices { id } }"


def test_load_query_missing_file_raises() -> None:
cfg = InfrahubRepositoryGraphQLConfig(name="my_query", file_path=Path("does_not_exist.gql"))
with tempfile.TemporaryDirectory() as tmp, pytest.raises(RepositoryFileNotFoundError) as exc_info:
cfg.load_query(relative_path=tmp)
assert "does_not_exist.gql" in exc_info.value.file_path