Skip to content

Commit

Permalink
Binary data escaping in listener module
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Mar 15, 2024
1 parent 3723b6c commit c9301be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]
### Added
- Binary data escaping in `listener` module (enhancing `Get Binary File` keyword logging), by @HardNorth

## [5.5.1]
### Changed
Expand Down
26 changes: 25 additions & 1 deletion robotframework_reportportal/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@
from .variables import Variables

logger = logging.getLogger(__name__)
DATA_SIGN = '${data} = '


def is_binary(iterable: Union[bytes, bytearray, str]) -> bool:
"""Check if given iterable is binary.
:param iterable: iterable to check
:return: True if iterable contains binary bytes, False otherwise
"""
if isinstance(iterable, str):
byte_iterable = iterable.encode('utf-8')
else:
byte_iterable = iterable

if 0x00 in byte_iterable:
return True
return False


def check_rp_enabled(func):
Expand Down Expand Up @@ -88,11 +105,15 @@ def current_item(self) -> Optional[Union[Keyword, Launch, Suite, Test]]:

@check_rp_enabled
def log_message(self, message: Dict) -> None:
"""Send log message to the ReportPortal.
"""Send log message to the Report Portal.
:param message: Message passed by the Robot Framework
"""
msg = self._build_msg_struct(message)
if msg.message.startswith(DATA_SIGN):
msg_content = msg.message[len(DATA_SIGN):]
if is_binary(msg_content):
msg.message = DATA_SIGN + str(msg_content.encode('utf-8')[:-4]) # remove trailing '"...'
logger.debug('ReportPortal - Log Message: {0}'.format(message))
self.service.log(message=msg)

Expand Down Expand Up @@ -178,6 +199,7 @@ def start_suite(self, name: str, attributes: Dict, ts: Optional[Any] = None) ->
def end_suite(self, _: Optional[str], attributes: Dict, ts: Optional[Any] = None) -> None:
"""Finish started test suite at the ReportPortal.
:param _: Test suite name
:param attributes: Dictionary passed by the Robot Framework
:param ts: Timestamp(used by the ResultVisitor)
"""
Expand Down Expand Up @@ -214,6 +236,7 @@ def start_test(self, name: str, attributes: Dict, ts: Optional[Any] = None) -> N
def end_test(self, _: Optional[str], attributes: Dict, ts: Optional[Any] = None) -> None:
"""Finish started test case at the ReportPortal.
:param _: Test case name
:param attributes: Dictionary passed by the Robot Framework
:param ts: Timestamp(used by the ResultVisitor)
"""
Expand Down Expand Up @@ -247,6 +270,7 @@ def start_keyword(self, name: str, attributes: Dict, ts: Optional[Any] = None) -
def end_keyword(self, _: Optional[str], attributes: Dict, ts: Optional[Any] = None) -> None:
"""Finish started keyword at the ReportPortal.
:param _: Keyword name
:param attributes: Dictionary passed by the Robot Framework
:param ts: Timestamp(used by the ResultVisitor)
"""
Expand Down

0 comments on commit c9301be

Please sign in to comment.