Skip to content

Commit

Permalink
Add workaround for bad filenames (#3987)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Aug 20, 2024
1 parent 5bf7bd9 commit 79c64a4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
6 changes: 5 additions & 1 deletion custom_components/hacs/repositories/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ def generate_dashboard_resource_namespace(self) -> str:

def generate_dashboard_resource_url(self) -> str:
"""Get the dashboard resource namespace."""
filename = self.data.file_name
if "/" in filename:
self.logger.warning("%s have defined an invalid file name %s", self.string, filename)
filename = filename.split("/")[-1]
return (
f"{self.generate_dashboard_resource_namespace()}/{self.data.file_name}"
f"{self.generate_dashboard_resource_namespace()}/{filename}"
f"?hacstag={self.generate_dashboard_resource_hacstag()}"
)

Expand Down
41 changes: 35 additions & 6 deletions tests/repositories/test_plugin_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ async def downloaded_plugin_repository(
) -> HacsPluginRepository:
"""Return a HacsPluginRepository instance."""
hacs = get_hacs(hass)
repository = hacs.repositories.get_by_full_name("hacs-test-org/plugin-basic")
repository = hacs.repositories.get_by_full_name(
"hacs-test-org/plugin-basic")
await repository.async_install(version="1.0.0")
return repository

Expand Down Expand Up @@ -182,7 +183,8 @@ async def test_remove_dashboard_resource(
resource_handler = downloaded_plugin_repository._get_resource_handler()
await resource_handler.async_load()

current_urls = [resource["url"] for resource in resource_handler.async_items()]
current_urls = [resource["url"]
for resource in resource_handler.async_items()]
assert len(current_urls) == 1
assert downloaded_plugin_repository.generate_dashboard_resource_url() in current_urls

Expand All @@ -192,7 +194,8 @@ async def test_remove_dashboard_resource(
in caplog.text
)

current_urls = [resource["url"] for resource in resource_handler.async_items()]
current_urls = [resource["url"]
for resource in resource_handler.async_items()]
assert len(current_urls) == 0


Expand All @@ -205,7 +208,8 @@ async def test_add_dashboard_resource(
resource_handler = downloaded_plugin_repository._get_resource_handler()
resource_handler.data.clear()

current_urls = [resource["url"] for resource in resource_handler.async_items()]
current_urls = [resource["url"]
for resource in resource_handler.async_items()]
assert len(current_urls) == 0

await downloaded_plugin_repository.update_dashboard_resources()
Expand All @@ -223,7 +227,8 @@ async def test_update_dashboard_resource(
"""Test adding a dashboard resource."""
resource_handler = downloaded_plugin_repository._get_resource_handler()
await resource_handler.async_load()
current_urls = [resource["url"] for resource in resource_handler.async_items()]
current_urls = [resource["url"]
for resource in resource_handler.async_items()]
assert len(current_urls) == 1

prev_url = downloaded_plugin_repository.generate_dashboard_resource_url()
Expand All @@ -242,6 +247,30 @@ async def test_update_dashboard_resource(
after_url = downloaded_plugin_repository.generate_dashboard_resource_url()
assert after_url != prev_url

current_urls = [resource["url"] for resource in resource_handler.async_items()]
current_urls = [resource["url"]
for resource in resource_handler.async_items()]
assert len(current_urls) == 1
assert current_urls[0] == after_url


async def test_add_dashboard_resource_with_invalid_file_name(
hass: HomeAssistant,
downloaded_plugin_repository: HacsPluginRepository,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test adding a dashboard resource."""
resource_handler = downloaded_plugin_repository._get_resource_handler()
resource_handler.data.clear()

current_urls = [resource["url"]
for resource in resource_handler.async_items()]
assert len(current_urls) == 0

downloaded_plugin_repository.data.file_name = "dist/plugin-basic.js"

await downloaded_plugin_repository.update_dashboard_resources()
assert "<Plugin hacs-test-org/plugin-basic> have defined an invalid file name dist/plugin-basic.js" in caplog.text
assert (
"Adding dashboard resource /hacsfiles/plugin-basic/plugin-basic.js?hacstag=1296267100"
in caplog.text
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"tests/repositories/test_plugin_repository.py::test_add_dashboard_resource_with_invalid_file_name": {
"https://api.github.com/repos/hacs-test-org/plugin-basic": 1,
"https://api.github.com/repos/hacs-test-org/plugin-basic/branches/main": 1,
"https://api.github.com/repos/hacs-test-org/plugin-basic/contents/hacs.json": 1,
"https://api.github.com/repos/hacs-test-org/plugin-basic/git/trees/1.0.0": 1,
"https://api.github.com/repos/hacs-test-org/plugin-basic/releases": 1,
"https://api.github.com/repos/hacs/integration": 1,
"https://api.github.com/repos/hacs/integration/contents/custom_components/hacs/manifest.json": 1,
"https://api.github.com/repos/hacs/integration/contents/hacs.json": 1,
"https://api.github.com/repos/hacs/integration/git/trees/main": 1,
"https://api.github.com/repos/hacs/integration/releases": 1,
"https://raw.githubusercontent.com/hacs-test-org/plugin-basic/1.0.0/README.md": 1,
"https://raw.githubusercontent.com/hacs-test-org/plugin-basic/1.0.0/plugin-basic.js": 1
}
}

0 comments on commit 79c64a4

Please sign in to comment.