Skip to content

Loading objects keeps subfolders order #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
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
15 changes: 0 additions & 15 deletions infrahub_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,6 @@ def is_valid_url(url: str) -> bool:
return False


def find_files(extension: str | list[str], directory: str | Path = ".") -> list[Path]:
files: list[Path] = []

if isinstance(extension, str):
extension = [extension]
if isinstance(directory, str):
directory = Path(directory)

for ext in extension:
files.extend(list(directory.glob(f"**/*.{ext}")))
files.extend(list(directory.glob(f"**/.*.{ext}")))

return files


def get_branch(branch: str | None = None, directory: str | Path = ".") -> str:
"""If branch isn't provide, return the name of the local Git branch."""
if branch:
Expand Down
11 changes: 6 additions & 5 deletions infrahub_sdk/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from yaml.parser import ParserError

from .exceptions import FileNotValidError
from .utils import find_files, read_file
from .utils import read_file


class InfrahubFileApiVersion(str, Enum):
Expand Down Expand Up @@ -121,12 +121,13 @@
def load_from_disk(cls, paths: list[Path]) -> list[Self]:
yaml_files: list[Self] = []
for file_path in paths:
if file_path.is_file():
if file_path.is_file() and file_path.suffix in [".yaml", ".yml", ".json"]:
yaml_files.extend(cls.load_file_from_disk(path=file_path))
elif file_path.is_dir():
files = find_files(extension=["yaml", "yml", "json"], directory=file_path)
for item in files:
yaml_files.extend(cls.load_file_from_disk(path=item))
sub_paths = [Path(sub_file_path) for sub_file_path in file_path.glob("*")]
sub_files = cls.load_from_disk(paths=sub_paths)

Check warning on line 128 in infrahub_sdk/yaml.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/yaml.py#L128

Added line #L128 was not covered by tests
sorted_sub_files = sorted(sub_files, key=lambda x: x.location)
yaml_files.extend(sorted_sub_files)

Check warning on line 130 in infrahub_sdk/yaml.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/yaml.py#L130

Added line #L130 was not covered by tests
else:
raise FileNotValidError(name=str(file_path), message=f"{file_path} does not exist!")

Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions tests/integration/test_spec_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from infrahub_sdk.testing.docker import TestInfrahubDockerClient
from infrahub_sdk.testing.schemas.animal import SchemaAnimal
from infrahub_sdk.utils import get_fixtures_dir
from infrahub_sdk.yaml import YamlFile


def load_object_file(name: str) -> ObjectFile:
Expand All @@ -23,6 +24,17 @@ def load_menu_file(name: str) -> MenuFile:
return files[0]


def test_load_nested_folders_order():
files = YamlFile.load_from_disk(paths=[get_fixtures_dir() / "nested_spec_objects"])
assert len(files) == 6
assert Path(files[0].location).name == "3_file.yml"
assert Path(files[1].location).name == "5_file.yml"
assert Path(files[2].location).name == "6_file.yml"
assert Path(files[3].location).name == "0_file.yml"
assert Path(files[4].location).name == "1_file.yml"
assert Path(files[5].location).name == "4_file.yml"


class TestSpecObject(TestInfrahubDockerClient, SchemaAnimal):
@pytest.fixture(scope="class")
def branch_name(self) -> str:
Expand Down