Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup validation #2365

Merged
merged 1 commit into from
Dec 25, 2021
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
7 changes: 7 additions & 0 deletions custom_components/hacs/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def __str__(self):
return str(self.value)


class RepositoryFile(str, Enum):
"""Repository file names."""

HACS_JSON = "hacs.json"
MAINIFEST_JSON = "manifest.json"


class ConfigurationType(str, Enum):
YAML = "yaml"
CONFIG_ENTRY = "config_entry"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hacs/helpers/methods/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ async def async_registration(self, ref=None) -> None:

class RepositoryMethodPostRegistration(ABC):
async def async_post_registration(self):
await async_run_repository_checks(self)
await async_run_repository_checks(self.hacs, self)
19 changes: 13 additions & 6 deletions custom_components/hacs/validate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from __future__ import annotations
import asyncio
import glob
import importlib
from os.path import dirname, join, sep
from typing import TYPE_CHECKING

from custom_components.hacs.share import SHARE, get_hacs
from homeassistant.core import HomeAssistant


from ..share import SHARE

if TYPE_CHECKING:
from ..base import HacsBase
from ..helpers.classes.repository import HacsRepository


def _initialize_rules():
Expand All @@ -15,15 +24,13 @@ def _initialize_rules():
importlib.import_module(rule)


async def async_initialize_rules():
hass = get_hacs().hass
async def async_initialize_rules(hass: HomeAssistant) -> None:
await hass.async_add_executor_job(_initialize_rules)


async def async_run_repository_checks(repository):
hacs = get_hacs()
async def async_run_repository_checks(hacs: HacsBase, repository: HacsRepository):
if not SHARE["rules"]:
await async_initialize_rules()
await async_initialize_rules(hacs.hass)
if not hacs.system.running:
return
checks = []
Expand Down
22 changes: 12 additions & 10 deletions custom_components/hacs/validate/base.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
from custom_components.hacs.share import SHARE, get_hacs
from __future__ import annotations
from typing import TYPE_CHECKING
from ..share import SHARE, get_hacs


if TYPE_CHECKING:
from ..helpers.classes.repository import HacsRepository


class ValidationException(Exception):
pass


class ValidationBase:
def __init__(self, repository) -> None:
self.repository = repository
action_only = False

def __init__(self, repository: HacsRepository) -> None:
self.hacs = get_hacs()
self.repository = repository
self.failed = False
self.logger = repository.logger

Expand All @@ -20,10 +28,6 @@ def __init_subclass__(cls, category="common", **kwargs) -> None:
if cls not in SHARE["rules"][category]:
SHARE["rules"][category].append(cls)

@property
def action_only(self):
return False

async def _async_run_check(self):
"""DO NOT OVERRIDE THIS IN SUBCLASSES!"""
if self.hacs.system.action:
Expand All @@ -43,6 +47,4 @@ async def async_check(self):


class ActionValidationBase(ValidationBase):
@property
def action_only(self):
return True
action_only = True
Empty file.
5 changes: 3 additions & 2 deletions custom_components/hacs/validate/common/hacs_manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from custom_components.hacs.enums import RepositoryFile
from custom_components.hacs.validate.base import (
ActionValidationBase,
ValidationException,
Expand All @@ -6,5 +7,5 @@

class HacsManifest(ActionValidationBase):
def check(self):
if "hacs.json" not in [x.filename for x in self.repository.tree]:
raise ValidationException("The repository has no 'hacs.json' file")
if RepositoryFile.HACS_JSON not in [x.filename for x in self.repository.tree]:
raise ValidationException(f"The repository has no '{RepositoryFile.HACS_JSON}' file")
Empty file.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from custom_components.hacs.validate.base import (
ActionValidationBase,
ValidationException,
)
from ..base import ActionValidationBase, ValidationException
from ...enums import RepositoryFile


class IntegrationManifest(ActionValidationBase, category="integration"):
def check(self):
if "manifest.json" not in [x.filename for x in self.repository.tree]:
raise ValidationException("The repository has no 'hacs.json' file")
if RepositoryFile.MAINIFEST_JSON not in [x.filename for x in self.repository.tree]:
raise ValidationException(
f"The repository has no '{RepositoryFile.MAINIFEST_JSON}' file"
)
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from awesomeversion import AwesomeVersion
from homeassistant.const import __version__ as HAVERSION
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceNotFound
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.loader import Integration
Expand Down Expand Up @@ -94,7 +95,7 @@ def exc_handle(loop, context):


@pytest.fixture
def hacs(hass):
def hacs(hass: HomeAssistant):
"""Fixture to provide a HACS object."""
hacs_obj = Hacs()
hacs_obj.hass = hass
Expand Down
13 changes: 7 additions & 6 deletions tests/validate/test_async_run_repository_checks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from custom_components.hacs.base import HacsBase

from custom_components.hacs.share import SHARE
from custom_components.hacs.validate import (
Expand All @@ -8,22 +9,22 @@


@pytest.mark.asyncio
async def test_async_initialize_rules(hacs):
async def test_async_initialize_rules(hacs: HacsBase):

await async_initialize_rules()
await async_initialize_rules(hacs.hass)


@pytest.mark.asyncio
async def test_async_run_repository_checks(hacs, repository_integration):
await async_run_repository_checks(repository_integration)
async def test_async_run_repository_checks(hacs: HacsBase, repository_integration):
await async_run_repository_checks(hacs, repository_integration)

hacs.system.action = True
hacs.system.running = True
repository_integration.tree = []
with pytest.raises(SystemExit):
await async_run_repository_checks(repository_integration)
await async_run_repository_checks(hacs, repository_integration)

hacs.system.action = False
SHARE["rules"] = {}
await async_run_repository_checks(repository_integration)
await async_run_repository_checks(hacs, repository_integration)
hacs.system.running = False