Skip to content

Commit

Permalink
Add script to validate output of generated data (#3589)
Browse files Browse the repository at this point in the history
* Add script to validate output of generated data

* Expand and humanize error

* lint

* linit

* Simplify check

* Add usage if unexpected number of arguments
  • Loading branch information
ludeeus authored Apr 9, 2024
1 parent 0e12c88 commit ebf0f4c
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions scripts/data/validate_category_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Validate HACS V2 data."""
from __future__ import annotations

import asyncio
import json
import os
import sys
from typing import Any

import voluptuous as vol

from custom_components.hacs.utils.validate import V2_REPOS_SCHEMA


def expand_and_humanize_error(content: dict[str, Any], error: vol.Error) -> list[str] | str:
"""Expand and humanize error."""
if isinstance(error, vol.MultipleInvalid):
return sorted(expand_and_humanize_error(content, sub_error) for sub_error in error.errors)

repoid = error.path[0]
return f"[{content[repoid].get('full_name', repoid)}] {vol.humanize.humanize_error(content, error)}"


async def validate_category_data(category: str, file_path: str) -> None:
"""Validate category data."""
target_path = os.path.join(os.getcwd(), file_path)

def print_error_and_exit(err: str):
print(f"::error::{err} in {target_path} for the {category} category")
sys.exit(1)

if not os.path.isfile(target_path):
print_error_and_exit(f"File {target_path} does not exist")
if category not in V2_REPOS_SCHEMA:
print_error_and_exit(f"Category {category} is not supported")

with open(
target_path,
encoding="utf-8",
) as data_file:
contents: dict[str, dict[str, Any]] = json.loads(data_file.read())
did_raise = False

if not data_file or len(contents) == 0 or not isinstance(contents, dict):
print_error_and_exit(f"File {target_path} is empty")

try:
V2_REPOS_SCHEMA[category](contents)
except vol.Error as error:
did_raise = True
errors = expand_and_humanize_error(contents, error)
if isinstance(errors, list):
for err in errors:
print(f"::error::{err}")
sys.exit(1)

print_error_and_exit(f"Invalid data: {expand_and_humanize_error(contents, error)}")

if did_raise:
print_error_and_exit("Validation did raise but did not exit!")
sys.exit(1) # Fallback, should not be reached

print(
f"All {len(contents)} entries for the "
f"{category} category in {target_path} are valid."
)


if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python3 -m scripts.data.validate_category_data <category> <file>")
sys.exit(1)
asyncio.run(validate_category_data(sys.argv[1], sys.argv[2]))

0 comments on commit ebf0f4c

Please sign in to comment.