diff --git a/changelog/462.fixed.md b/changelog/462.fixed.md new file mode 100644 index 00000000..deff4e59 --- /dev/null +++ b/changelog/462.fixed.md @@ -0,0 +1 @@ +Fixes schema loading to ignore non-YAML files in folders. \ No newline at end of file diff --git a/infrahub_sdk/yaml.py b/infrahub_sdk/yaml.py index 8e3f2f73..05c66852 100644 --- a/infrahub_sdk/yaml.py +++ b/infrahub_sdk/yaml.py @@ -120,16 +120,23 @@ def load_file_from_disk(cls, path: Path) -> list[Self]: @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 + + 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)) + # else: skip non-file, non-dir (e.g., symlink...) return yaml_files diff --git a/tests/fixtures/nested_spec_objects/0_folder/4_subfolder/to_be_ignored.py b/tests/fixtures/nested_spec_objects/0_folder/4_subfolder/to_be_ignored.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/nested_spec_objects/2_folder/to_be_ignored.md b/tests/fixtures/nested_spec_objects/2_folder/to_be_ignored.md new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/sdk/test_yaml.py b/tests/unit/sdk/test_yaml.py index 3532265c..af5a8591 100644 --- a/tests/unit/sdk/test_yaml.py +++ b/tests/unit/sdk/test_yaml.py @@ -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() @@ -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)