Skip to content
33 changes: 33 additions & 0 deletions custom_components/hacs/validate/license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from .base import ActionValidationBase, ValidationException

if TYPE_CHECKING:
from ..repositories.base import HacsRepository


async def async_setup_validator(repository: HacsRepository) -> Validator:
"""Set up this validator."""
return Validator(repository=repository)


class Validator(ActionValidationBase):
"""Validate the repository."""

more_info = "https://hacs.xyz/docs/publish/include#check-repository"
allow_fork = False

async def async_validate(self) -> None:
"""Validate the repository."""
if (license_info := self.repository.repository_object.attributes.get("license")) is None:
raise ValidationException("The repository has no license")
if license_info.get("key") == "other":
raise ValidationException(
"The repository has no recognized license "
f"(license name is '{license_info.get('name', 'unknown')}')"
)
Comment thread
ludeeus marked this conversation as resolved.
Outdated
self.repository.logger.debug(
"The repository has a valid license: %s", license_info.get("name")
)
31 changes: 31 additions & 0 deletions tests/validate/test_repository_license_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from unittest.mock import MagicMock

from custom_components.hacs.validate.license import Validator


async def test_repository_no_license(repository):
repository.repository_object = MagicMock()
repository.repository_object.attributes = {"license": None}
check = Validator(repository)
await check.execute_validation()
assert check.failed


async def test_repository_unrecognized_license(repository):
repository.repository_object = MagicMock()
repository.repository_object.attributes = {
"license": {"key": "other", "name": "Other", "spdx_id": "NOASSERTION"},
}
check = Validator(repository)
await check.execute_validation()
assert check.failed


async def test_repository_valid_license(repository):
repository.repository_object = MagicMock()
repository.repository_object.attributes = {
"license": {"key": "mit", "name": "MIT License", "spdx_id": "MIT"},
}
check = Validator(repository)
await check.execute_validation()
assert not check.failed
Loading