From 52400feacf4174c85fcff0aec1eba4d31d967e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Tue, 24 Sep 2024 17:26:01 +0200 Subject: [PATCH] Improve error handling for auto tagging (#855) --- bookmarks/services/auto_tagging.py | 3 +++ bookmarks/tests/test_auto_tagging.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/bookmarks/services/auto_tagging.py b/bookmarks/services/auto_tagging.py index 2608761..840745a 100644 --- a/bookmarks/services/auto_tagging.py +++ b/bookmarks/services/auto_tagging.py @@ -7,6 +7,9 @@ def get_tags(script: str, url: str): parsed_url = urlparse(url.lower()) result = set() + if not parsed_url.hostname: + return result + for line in script.lower().split("\n"): if "#" in line: i = line.index("#") diff --git a/bookmarks/tests/test_auto_tagging.py b/bookmarks/tests/test_auto_tagging.py index 320003f..b4213a1 100644 --- a/bookmarks/tests/test_auto_tagging.py +++ b/bookmarks/tests/test_auto_tagging.py @@ -14,6 +14,20 @@ class AutoTaggingTestCase(TestCase): self.assertEqual(tags, {"example"}) + def test_auto_tag_by_domain_handles_invalid_urls(self): + script = """ + example.com example + test.com test + """ + + url = "https://" + tags = auto_tagging.get_tags(script, url) + self.assertEqual(tags, set([])) + + url = "example.com" + tags = auto_tagging.get_tags(script, url) + self.assertEqual(tags, set([])) + def test_auto_tag_by_domain_works_with_port(self): script = """ example.com example