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
1 change: 1 addition & 0 deletions changelog/462.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes schema loading to ignore non-YAML files in folders.
21 changes: 14 additions & 7 deletions infrahub_sdk/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,23 @@
@classmethod
def load_from_disk(cls, paths: list[Path]) -> list[Self]:
yaml_files: list[Self] = []
file_extensions = {".yaml", ".yml", ".json"} # FIXME: .json is not a YAML file, should be removed
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LucasG0 do you know why we have .json here? too afraid about side effect of removing it from here


paths = sorted(paths, key=lambda p: p.name) # Ensure order when processing paths

for file_path in paths:
if file_path.is_file() and file_path.suffix in [".yaml", ".yml", ".json"]:
yaml_files.extend(cls.load_file_from_disk(path=file_path))
if not file_path.exists():
# Check if the provided path exists, relevant for the first call coming from the user
raise FileNotValidError(name=str(file_path), message=f"{file_path} does not exist!")
if file_path.is_file():
if file_path.suffix in file_extensions:
yaml_files.extend(cls.load_file_from_disk(path=file_path))
# else: silently skip files with unrelevant extensions (e.g. .md, .py...)
elif file_path.is_dir():
# Introduce recursion to handle sub-folders
sub_paths = [Path(sub_file_path) for sub_file_path in file_path.glob("*")]
sub_files = cls.load_from_disk(paths=sub_paths)
sorted_sub_files = sorted(sub_files, key=lambda x: x.location)
yaml_files.extend(sorted_sub_files)
else:
raise FileNotValidError(name=str(file_path), message=f"{file_path} does not exist!")
yaml_files.extend(cls.load_from_disk(paths=sub_paths))

Check warning on line 138 in infrahub_sdk/yaml.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/yaml.py#L138

Added line #L138 was not covered by tests
# else: skip non-file, non-dir (e.g., symlink...)

return yaml_files

Expand Down
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions tests/unit/sdk/test_yaml.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from pathlib import Path

import pytest

from infrahub_sdk.exceptions import FileNotValidError
from infrahub_sdk.utils import get_fixtures_dir
from infrahub_sdk.yaml import YamlFile

here = Path(__file__).parent.resolve()
Expand Down Expand Up @@ -42,3 +46,9 @@ def test_read_multiple_files_invalid() -> None:
assert yaml_files[0].valid is True
assert yaml_files[1].document_position == 2
assert yaml_files[1].valid is False


def test_load_non_existing_folder():
with pytest.raises(FileNotValidError) as exc:
YamlFile.load_from_disk(paths=[get_fixtures_dir() / "does_not_exist"])
assert "does not exist" in str(exc.value)