fixed handling empty tags in TXT file

This commit is contained in:
juk0de 2023-08-27 18:07:38 +02:00
parent c7b99fe9b4
commit 9682463af1
3 changed files with 19 additions and 0 deletions

View File

@ -124,6 +124,8 @@ class TagLine(str):
filtered based on prefix or contained string. filtered based on prefix or contained string.
""" """
tagstr = self[len(self.prefix):].strip() tagstr = self[len(self.prefix):].strip()
if tagstr == '':
return set() # no tags, only prefix
separator = Tag.default_separator separator = Tag.default_separator
# look for alternative separators and use the first one found # look for alternative separators and use the first one found
# -> we don't support different separators in the same TagLine # -> we don't support different separators in the same TagLine

View File

@ -556,6 +556,15 @@ This is an answer.
This is a question. This is a question.
{Answer.txt_header} {Answer.txt_header}
This is an answer. This is an answer.
""")
self.file_txt_tags_empty = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
self.file_path_txt_tags_empty = pathlib.Path(self.file_txt_tags_empty.name)
with open(self.file_path_txt_tags_empty, "w") as fd:
fd.write(f"""TAGS:
{Question.txt_header}
This is a question.
{Answer.txt_header}
This is an answer.
""") """)
self.file_yaml = tempfile.NamedTemporaryFile(delete=False, suffix='.yaml') self.file_yaml = tempfile.NamedTemporaryFile(delete=False, suffix='.yaml')
self.file_path_yaml = pathlib.Path(self.file_yaml.name) self.file_path_yaml = pathlib.Path(self.file_yaml.name)
@ -594,6 +603,10 @@ This is an answer.
tags = Message.tags_from_file(self.file_path_txt_no_tags) tags = Message.tags_from_file(self.file_path_txt_no_tags)
self.assertSetEqual(tags, set()) self.assertSetEqual(tags, set())
def test_tags_from_file_txt_tags_empty(self) -> None:
tags = Message.tags_from_file(self.file_path_txt_tags_empty)
self.assertSetEqual(tags, set())
def test_tags_from_file_yaml(self) -> None: def test_tags_from_file_yaml(self) -> None:
tags = Message.tags_from_file(self.file_path_yaml) tags = Message.tags_from_file(self.file_path_yaml)
self.assertSetEqual(tags, {Tag('tag1'), Tag('tag2'), Tag('ptag3')}) self.assertSetEqual(tags, {Tag('tag1'), Tag('tag2'), Tag('ptag3')})

View File

@ -44,6 +44,10 @@ class TestTagLine(CmmTestCase):
tags = tagline.tags() tags = tagline.tags()
self.assertEqual(tags, {Tag('atag1'), Tag('btag2')}) self.assertEqual(tags, {Tag('atag1'), Tag('btag2')})
def test_tags_empty(self) -> None:
tagline = TagLine('TAGS:')
self.assertSetEqual(tagline.tags(), set())
def test_tags_with_newline(self) -> None: def test_tags_with_newline(self) -> None:
tagline = TagLine('TAGS: tag1\n tag2') tagline = TagLine('TAGS: tag1\n tag2')
tags = tagline.tags() tags = tagline.tags()