From 966b962ccfbdf76f5a42850ec81361870ad0bc9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Wed, 1 Sep 2021 12:50:44 +0200 Subject: [PATCH] Ignore all files and directories that starts with . (#3094) * Ignore all files and directories that starts with . * pylint * Check all parts --- supervisor/store/data.py | 15 ++++++++++----- tests/store/test_reading_addons.py | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 tests/store/test_reading_addons.py diff --git a/supervisor/store/data.py b/supervisor/store/data.py index 34d07f8c2..fd44dbbfb 100644 --- a/supervisor/store/data.py +++ b/supervisor/store/data.py @@ -1,7 +1,7 @@ """Init file for Supervisor add-on data.""" import logging from pathlib import Path -from typing import Any, Dict +from typing import Any, Dict, Optional import voluptuous as vol from voluptuous.humanize import humanize_error @@ -86,15 +86,14 @@ class StoreData(CoreSysAttributes): self.repositories[slug] = repository_info self._read_addons_folder(path, slug) - def _read_addons_folder(self, path: Path, repository: Dict) -> None: - """Read data from add-ons folder.""" + def _find_addons(self, path: Path, repository: Dict) -> Optional[list[Path]]: + """Find add-ons in the path.""" try: # Generate a list without artefact, safe for corruptions addon_list = [ addon for addon in path.glob("**/config.*") - if ".git" not in addon.parts - and ".github" not in addon.parts + if not [part for part in addon.parts if part.startswith(".")] and addon.suffix in FILE_SUFFIX_CONFIGURATION ] except OSError as err: @@ -110,6 +109,12 @@ class StoreData(CoreSysAttributes): _LOGGER.critical( "Can't process %s because of Filesystem issues: %s", repository, err ) + return None + return addon_list + + def _read_addons_folder(self, path: Path, repository: Dict) -> None: + """Read data from add-ons folder.""" + if not (addon_list := self._find_addons(path, repository)): return for addon in addon_list: diff --git a/tests/store/test_reading_addons.py b/tests/store/test_reading_addons.py new file mode 100644 index 000000000..1c2fffd17 --- /dev/null +++ b/tests/store/test_reading_addons.py @@ -0,0 +1,24 @@ +"""Test that we are reading add-on files correctly.""" +from pathlib import Path +from unittest.mock import patch + +from supervisor.coresys import CoreSys + + +def test_read_addon_files(coresys: CoreSys): + """Test that we are reading add-on files correctly.""" + with patch( + "pathlib.Path.glob", + return_value=[ + Path("addon/config.yml"), + Path(".git/config.yml"), + Path("somepath/.git/config.yml"), + Path("somepath/deeper_in_the_structure/.github/config.yml"), + Path(".github/config.yml"), + Path(".circleci/config.yml"), + ], + ): + addon_list = coresys.store.data._find_addons(Path("test"), {}) + + assert len(addon_list) == 1 + assert str(addon_list[0]) == "addon/config.yml"