diff --git a/supervisor/addons/build.py b/supervisor/addons/build.py index dbf12c02d..6556ed8aa 100644 --- a/supervisor/addons/build.py +++ b/supervisor/addons/build.py @@ -14,6 +14,7 @@ from ..const import ( META_ADDON, ) from ..coresys import CoreSys, CoreSysAttributes +from ..exceptions import ConfigurationFileError from ..utils.common import FileConfiguration, find_one_filetype from .validate import SCHEMA_BUILD_CONFIG @@ -29,12 +30,14 @@ class AddonBuild(FileConfiguration, CoreSysAttributes): self.coresys: CoreSys = coresys self.addon = addon - super().__init__( - find_one_filetype( + try: + build_file = find_one_filetype( self.addon.path_location, "build", FILE_SUFFIX_CONFIGURATION - ), - SCHEMA_BUILD_CONFIG, - ) + ) + except ConfigurationFileError: + build_file = self.addon.path_location / "build.json" + + super().__init__(build_file, SCHEMA_BUILD_CONFIG) def save_data(self): """Ignore save function.""" diff --git a/supervisor/store/data.py b/supervisor/store/data.py index 1a5103231..34d07f8c2 100644 --- a/supervisor/store/data.py +++ b/supervisor/store/data.py @@ -61,11 +61,11 @@ class StoreData(CoreSysAttributes): slug = extract_hash_from_path(path) # exists repository json - repository_file = find_one_filetype( - path, "repository", FILE_SUFFIX_CONFIGURATION - ) - - if repository_file is None: + try: + repository_file = find_one_filetype( + path, "repository", FILE_SUFFIX_CONFIGURATION + ) + except ConfigurationFileError: _LOGGER.warning("No repository information exists at %s", path) return diff --git a/supervisor/utils/common.py b/supervisor/utils/common.py index 362f9a176..495abe64b 100644 --- a/supervisor/utils/common.py +++ b/supervisor/utils/common.py @@ -1,7 +1,7 @@ """Common utils.""" import logging from pathlib import Path -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List import voluptuous as vol from voluptuous.humanize import humanize_error @@ -15,14 +15,12 @@ _LOGGER: logging.Logger = logging.getLogger(__name__) _DEFAULT: Dict[str, Any] = {} -def find_one_filetype( - path: Path, filename: str, filetypes: List[str] -) -> Optional[Path]: +def find_one_filetype(path: Path, filename: str, filetypes: List[str]) -> Path: """Find first file matching filetypes.""" for file in path.glob(f"**/{filename}.*"): if file.suffix in filetypes: return file - return None + raise ConfigurationFileError(f"{path!s}/{filename}.({filetypes}) not exists!") def read_json_or_yaml_file(path: Path) -> dict: diff --git a/tests/utils/test_common.py b/tests/utils/test_common.py new file mode 100644 index 000000000..b64d692c1 --- /dev/null +++ b/tests/utils/test_common.py @@ -0,0 +1,21 @@ +"""Test common.""" +from pathlib import Path + +import pytest + +from supervisor.exceptions import ConfigurationFileError +from supervisor.utils.common import find_one_filetype + + +def test_not_found(tmp_path): + """Test default.""" + with pytest.raises(ConfigurationFileError): + find_one_filetype(tmp_path, "test", [".json"]) + + +def test_with_found(tmp_path): + """Test default.""" + test_file = Path(tmp_path, "test.json") + test_file.write_text("found") + + assert find_one_filetype(tmp_path, "test", [".json"]) == test_file diff --git a/tests/utils/test_yaml.py b/tests/utils/test_yaml.py index 3bdd9d860..742968fc9 100644 --- a/tests/utils/test_yaml.py +++ b/tests/utils/test_yaml.py @@ -1,5 +1,9 @@ """test yaml.""" + +import pytest + from supervisor.const import FILE_SUFFIX_CONFIGURATION +from supervisor.exceptions import ConfigurationFileError from supervisor.utils.common import find_one_filetype, read_json_or_yaml_file from supervisor.utils.json import write_json_file from supervisor.utils.yaml import read_yaml_file, write_yaml_file @@ -32,8 +36,8 @@ def test_get_file_from_type(tmp_path): tempfile = tmp_path / "test.config" write_yaml_file(tempfile, {"test": "test"}) - found = find_one_filetype(tmp_path, "test4", FILE_SUFFIX_CONFIGURATION) - assert not found + with pytest.raises(ConfigurationFileError): + find_one_filetype(tmp_path, "test4", FILE_SUFFIX_CONFIGURATION) def test_read_json_or_yaml_file(tmp_path):