Skip to content

Commit

Permalink
Add tests for update_downloaded_repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Feb 5, 2022
1 parent f7eeeff commit 998ca45
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
13 changes: 12 additions & 1 deletion manage/update_default_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@

def update():
"""Update the shipped default repositories data file."""
storage, to_store = None, {}
storage, to_store, old = None, {}, {}
updated = 0

with open(f"{os.getcwd()}/config/.storage/hacs.repositories", encoding="utf-8") as storage_file:
storage = json.load(storage_file)

with open(
f"{os.getcwd()}/custom_components/hacs/utils/default.repositories", encoding="utf-8"
) as old_file:
old = json.load(old_file)

if storage is None:
sys.exit("No storage file")

Expand All @@ -21,6 +27,9 @@ def update():
for key in ("installed_commit", "selected_tag", "version_installed"):
storage["data"][repo][key] = None

if old.get(repo, {}).get("etag_repository") != storage["data"][repo].get("etag_repository"):
updated += 1

to_store[repo] = storage["data"][repo]

with open(
Expand All @@ -30,6 +39,8 @@ def update():
) as to_store_file:
to_store_file.write(json.dumps(to_store))

print(f"{updated} was updated")


if __name__ == "__main__":
update()
46 changes: 46 additions & 0 deletions tests/tasks/test_update_downloaded_repositories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# pylint: disable=missing-function-docstring,missing-module-docstring, protected-access
import pytest

from custom_components.hacs.base import HacsBase
from custom_components.hacs.enums import HacsCategory
from custom_components.hacs.repositories.base import HacsRepository


@pytest.mark.asyncio
async def test_update_downloaded_repositories(hacs: HacsBase, repository: HacsRepository):
await hacs.tasks.async_load()
task = hacs.tasks.get("update_downloaded_repositories")

repository.data.category = HacsCategory.INTEGRATION
repository.data.installed = True
hacs.repositories.register(repository)

hacs.enable_hacs_category(HacsCategory.INTEGRATION)

assert task

assert hacs.queue.pending_tasks == 0
await task.execute_task()
assert hacs.queue.pending_tasks == 1


@pytest.mark.asyncio
async def test_update_downloaded_repositories_skip_hacs_on_startup(
hacs: HacsBase,
repository: HacsRepository,
):
await hacs.tasks.async_load()
task = hacs.tasks.get("update_downloaded_repositories")

repository.data.category = HacsCategory.INTEGRATION
repository.data.installed = True
repository.data.full_name = "hacs/integration"
hacs.repositories.register(repository)

hacs.enable_hacs_category(HacsCategory.INTEGRATION)

assert task

assert hacs.queue.pending_tasks == 0
await task.execute_task()
assert hacs.queue.pending_tasks == 0

0 comments on commit 998ca45

Please sign in to comment.