Skip to content

Commit

Permalink
Rework message and case logic, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoMi committed Oct 14, 2022
1 parent a52cf7e commit 4b6a1b4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
23 changes: 12 additions & 11 deletions python/publish/__init__.py
Expand Up @@ -748,6 +748,15 @@ def to_dict(self) -> Mapping[str, Any]:
return dictionary


def message_is_contained_in_content(message: Optional[str], content: Optional[str]) -> bool:
# ignore new lines and any leading or trailing white spaces
if content and message:
content = re.sub(r'\s+', ' ', content.strip())
message = re.sub(r'\s+', ' ', message.strip())
return content.startswith(message)
return False


def get_case_annotation(messages: CaseMessages,
key: Tuple[Optional[str], Optional[str], Optional[str]],
state: str,
Expand Down Expand Up @@ -792,19 +801,11 @@ def get_case_annotation(messages: CaseMessages,
'notice'
)

def message_is_contained_in_content(message: Optional[str], content: Optional[str]) -> bool:
# ignore new lines and any leading or trailing white spaces
if content and message:
content = re.sub(r'\s+', ' ', content.strip())
message = re.sub(r'\s+', ' ', message.strip())
return content.startswith(message)
return False

# pick details from message and content, but try to avoid redundancy (e.g. when content repeats message)
# always add stdout and stderr if they are not empty
maybe_message = [case.message] if not message_is_contained_in_content(case.message, case.content) else []
details = [detail.rstrip()
for detail in ([case.content]
if message_is_contained_in_content(case.message, case.content)
else [case.message, case.content]) + [case.stdout, case.stderr]
for detail in maybe_message + [case.content, case.stdout, case.stderr]
if detail and detail.rstrip()]

return Annotation(
Expand Down
21 changes: 19 additions & 2 deletions python/test/test_publish.py
@@ -1,7 +1,6 @@
import pathlib
import unittest
from collections import defaultdict
from typing import Any

import mock

Expand All @@ -15,7 +14,7 @@
get_long_summary_without_runs_md, get_long_summary_with_digest_md, \
get_test_changes_md, get_test_changes_list_md, get_test_changes_summary_md, \
get_case_annotations, get_case_annotation, get_all_tests_list_annotation, \
get_skipped_tests_list_annotation, get_case_messages, chunk_test_list
get_skipped_tests_list_annotation, get_case_messages, chunk_test_list, message_is_contained_in_content
from publish.junit import parse_junit_xml_files, process_junit_xml_elems
from publish.unittestresults import get_stats, UnitTestCase, ParseError
from publish.unittestresults import get_test_results
Expand Down Expand Up @@ -2072,6 +2071,24 @@ def test_files_without_annotations(self):
f'\n'
f'Results for commit example.\n'))

def test_message_is_contained_in_content(self):
# non-contained test cases
for message, content in [(None, None),
('message', None),
(None, 'content'),
('message', 'content'),
('message', 'the message in the content')]:
with self.subTest(message=message, content=content):
self.assertFalse(message_is_contained_in_content(message, content))

# contained test cases
for message, content in [('message', 'message'),
('message', 'message in content'),
('the message', ' the message in content'),
('the message', '\tthe message in the content')]:
with self.subTest(message=message, content=content):
self.assertTrue(message_is_contained_in_content(message, content))


if __name__ == '__main__':
unittest.main()

0 comments on commit 4b6a1b4

Please sign in to comment.