Skip to content

Commit 1efe5de

Browse files
authored
Loading objects keeps subfolders order (#426)
1 parent 82b5e1c commit 1efe5de

File tree

9 files changed

+18
-20
lines changed

9 files changed

+18
-20
lines changed

infrahub_sdk/utils.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,21 +240,6 @@ def is_valid_url(url: str) -> bool:
240240
return False
241241

242242

243-
def find_files(extension: str | list[str], directory: str | Path = ".") -> list[Path]:
244-
files: list[Path] = []
245-
246-
if isinstance(extension, str):
247-
extension = [extension]
248-
if isinstance(directory, str):
249-
directory = Path(directory)
250-
251-
for ext in extension:
252-
files.extend(list(directory.glob(f"**/*.{ext}")))
253-
files.extend(list(directory.glob(f"**/.*.{ext}")))
254-
255-
return files
256-
257-
258243
def get_branch(branch: str | None = None, directory: str | Path = ".") -> str:
259244
"""If branch isn't provide, return the name of the local Git branch."""
260245
if branch:

infrahub_sdk/yaml.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from yaml.parser import ParserError
1111

1212
from .exceptions import FileNotValidError
13-
from .utils import find_files, read_file
13+
from .utils import read_file
1414

1515

1616
class InfrahubFileApiVersion(str, Enum):
@@ -121,12 +121,13 @@ def load_file_from_disk(cls, path: Path) -> list[Self]:
121121
def load_from_disk(cls, paths: list[Path]) -> list[Self]:
122122
yaml_files: list[Self] = []
123123
for file_path in paths:
124-
if file_path.is_file():
124+
if file_path.is_file() and file_path.suffix in [".yaml", ".yml", ".json"]:
125125
yaml_files.extend(cls.load_file_from_disk(path=file_path))
126126
elif file_path.is_dir():
127-
files = find_files(extension=["yaml", "yml", "json"], directory=file_path)
128-
for item in files:
129-
yaml_files.extend(cls.load_file_from_disk(path=item))
127+
sub_paths = [Path(sub_file_path) for sub_file_path in file_path.glob("*")]
128+
sub_files = cls.load_from_disk(paths=sub_paths)
129+
sorted_sub_files = sorted(sub_files, key=lambda x: x.location)
130+
yaml_files.extend(sorted_sub_files)
130131
else:
131132
raise FileNotValidError(name=str(file_path), message=f"{file_path} does not exist!")
132133

tests/fixtures/nested_spec_objects/0_folder/3_file.yml

Whitespace-only changes.

tests/fixtures/nested_spec_objects/0_folder/4_subfolder/5_file.yml

Whitespace-only changes.

tests/fixtures/nested_spec_objects/0_folder/4_subfolder/6_file.yml

Whitespace-only changes.

tests/fixtures/nested_spec_objects/2_folder/0_file.yml

Whitespace-only changes.

tests/fixtures/nested_spec_objects/2_folder/1_file.yml

Whitespace-only changes.

tests/fixtures/nested_spec_objects/4_file.yml

Whitespace-only changes.

tests/integration/test_spec_object.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from infrahub_sdk.testing.docker import TestInfrahubDockerClient
1010
from infrahub_sdk.testing.schemas.animal import SchemaAnimal
1111
from infrahub_sdk.utils import get_fixtures_dir
12+
from infrahub_sdk.yaml import YamlFile
1213

1314

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

2526

27+
def test_load_nested_folders_order():
28+
files = YamlFile.load_from_disk(paths=[get_fixtures_dir() / "nested_spec_objects"])
29+
assert len(files) == 6
30+
assert Path(files[0].location).name == "3_file.yml"
31+
assert Path(files[1].location).name == "5_file.yml"
32+
assert Path(files[2].location).name == "6_file.yml"
33+
assert Path(files[3].location).name == "0_file.yml"
34+
assert Path(files[4].location).name == "1_file.yml"
35+
assert Path(files[5].location).name == "4_file.yml"
36+
37+
2638
class TestSpecObject(TestInfrahubDockerClient, SchemaAnimal):
2739
@pytest.fixture(scope="class")
2840
def branch_name(self) -> str:

0 commit comments

Comments
 (0)