Skip to content

Commit 79c64a4

Browse files
authored
Add workaround for bad filenames (#3987)
1 parent 5bf7bd9 commit 79c64a4

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

custom_components/hacs/repositories/plugin.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,12 @@ def generate_dashboard_resource_namespace(self) -> str:
161161

162162
def generate_dashboard_resource_url(self) -> str:
163163
"""Get the dashboard resource namespace."""
164+
filename = self.data.file_name
165+
if "/" in filename:
166+
self.logger.warning("%s have defined an invalid file name %s", self.string, filename)
167+
filename = filename.split("/")[-1]
164168
return (
165-
f"{self.generate_dashboard_resource_namespace()}/{self.data.file_name}"
169+
f"{self.generate_dashboard_resource_namespace()}/{filename}"
166170
f"?hacstag={self.generate_dashboard_resource_hacstag()}"
167171
)
168172

tests/repositories/test_plugin_repository.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ async def downloaded_plugin_repository(
1717
) -> HacsPluginRepository:
1818
"""Return a HacsPluginRepository instance."""
1919
hacs = get_hacs(hass)
20-
repository = hacs.repositories.get_by_full_name("hacs-test-org/plugin-basic")
20+
repository = hacs.repositories.get_by_full_name(
21+
"hacs-test-org/plugin-basic")
2122
await repository.async_install(version="1.0.0")
2223
return repository
2324

@@ -182,7 +183,8 @@ async def test_remove_dashboard_resource(
182183
resource_handler = downloaded_plugin_repository._get_resource_handler()
183184
await resource_handler.async_load()
184185

185-
current_urls = [resource["url"] for resource in resource_handler.async_items()]
186+
current_urls = [resource["url"]
187+
for resource in resource_handler.async_items()]
186188
assert len(current_urls) == 1
187189
assert downloaded_plugin_repository.generate_dashboard_resource_url() in current_urls
188190

@@ -192,7 +194,8 @@ async def test_remove_dashboard_resource(
192194
in caplog.text
193195
)
194196

195-
current_urls = [resource["url"] for resource in resource_handler.async_items()]
197+
current_urls = [resource["url"]
198+
for resource in resource_handler.async_items()]
196199
assert len(current_urls) == 0
197200

198201

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

208-
current_urls = [resource["url"] for resource in resource_handler.async_items()]
211+
current_urls = [resource["url"]
212+
for resource in resource_handler.async_items()]
209213
assert len(current_urls) == 0
210214

211215
await downloaded_plugin_repository.update_dashboard_resources()
@@ -223,7 +227,8 @@ async def test_update_dashboard_resource(
223227
"""Test adding a dashboard resource."""
224228
resource_handler = downloaded_plugin_repository._get_resource_handler()
225229
await resource_handler.async_load()
226-
current_urls = [resource["url"] for resource in resource_handler.async_items()]
230+
current_urls = [resource["url"]
231+
for resource in resource_handler.async_items()]
227232
assert len(current_urls) == 1
228233

229234
prev_url = downloaded_plugin_repository.generate_dashboard_resource_url()
@@ -242,6 +247,30 @@ async def test_update_dashboard_resource(
242247
after_url = downloaded_plugin_repository.generate_dashboard_resource_url()
243248
assert after_url != prev_url
244249

245-
current_urls = [resource["url"] for resource in resource_handler.async_items()]
250+
current_urls = [resource["url"]
251+
for resource in resource_handler.async_items()]
246252
assert len(current_urls) == 1
247253
assert current_urls[0] == after_url
254+
255+
256+
async def test_add_dashboard_resource_with_invalid_file_name(
257+
hass: HomeAssistant,
258+
downloaded_plugin_repository: HacsPluginRepository,
259+
caplog: pytest.LogCaptureFixture,
260+
) -> None:
261+
"""Test adding a dashboard resource."""
262+
resource_handler = downloaded_plugin_repository._get_resource_handler()
263+
resource_handler.data.clear()
264+
265+
current_urls = [resource["url"]
266+
for resource in resource_handler.async_items()]
267+
assert len(current_urls) == 0
268+
269+
downloaded_plugin_repository.data.file_name = "dist/plugin-basic.js"
270+
271+
await downloaded_plugin_repository.update_dashboard_resources()
272+
assert "<Plugin hacs-test-org/plugin-basic> have defined an invalid file name dist/plugin-basic.js" in caplog.text
273+
assert (
274+
"Adding dashboard resource /hacsfiles/plugin-basic/plugin-basic.js?hacstag=1296267100"
275+
in caplog.text
276+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"tests/repositories/test_plugin_repository.py::test_add_dashboard_resource_with_invalid_file_name": {
3+
"https://api.github.com/repos/hacs-test-org/plugin-basic": 1,
4+
"https://api.github.com/repos/hacs-test-org/plugin-basic/branches/main": 1,
5+
"https://api.github.com/repos/hacs-test-org/plugin-basic/contents/hacs.json": 1,
6+
"https://api.github.com/repos/hacs-test-org/plugin-basic/git/trees/1.0.0": 1,
7+
"https://api.github.com/repos/hacs-test-org/plugin-basic/releases": 1,
8+
"https://api.github.com/repos/hacs/integration": 1,
9+
"https://api.github.com/repos/hacs/integration/contents/custom_components/hacs/manifest.json": 1,
10+
"https://api.github.com/repos/hacs/integration/contents/hacs.json": 1,
11+
"https://api.github.com/repos/hacs/integration/git/trees/main": 1,
12+
"https://api.github.com/repos/hacs/integration/releases": 1,
13+
"https://raw.githubusercontent.com/hacs-test-org/plugin-basic/1.0.0/README.md": 1,
14+
"https://raw.githubusercontent.com/hacs-test-org/plugin-basic/1.0.0/plugin-basic.js": 1
15+
}
16+
}

0 commit comments

Comments
 (0)