Skip to content

Commit

Permalink
Closes #188
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Sep 20, 2024
1 parent 9444ad4 commit 89a043e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
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
- `timezone` command line argument for `post_report.py` script, by @HardNorth

## [5.5.5]
### Added
Expand Down
8 changes: 5 additions & 3 deletions robotframework_reportportal/post_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
[--variable RP_MAX_POOL_SIZE:"50"]
[--variable RP_MODE:"DEBUG"]
[--loglevel CRITICAL|ERROR|WARNING|INFO|DEBUG]
[--timezone "+03:00"|"EST"|"Europe/Warsaw"]
[output.xml]
This script needs to be run within the same directory as the report xml file.
Expand Down Expand Up @@ -66,10 +67,9 @@ def process(infile="output.xml"):
def main():
argument_list = sys.argv[1:]
short_options = "hv:"
long_options = ["help", "variable=", "loglevel="]
long_options = ["help", "variable=", "loglevel=", 'timezone=']
try:
arguments, values = getopt.getopt(argument_list, short_options,
long_options)
arguments, values = getopt.getopt(argument_list, short_options, long_options)
except getopt.error:
sys.exit(1)

Expand All @@ -83,6 +83,8 @@ def main():
elif current_argument == "--loglevel":
numeric_level = getattr(logging, current_value.upper(), None)
logging.basicConfig(level=numeric_level)
elif current_argument == "--timezone":
_variables['RP_TIME_ZONE_OFFSET'] = current_value

try:
process(*values)
Expand Down
29 changes: 24 additions & 5 deletions robotframework_reportportal/result_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

import re
import string
from datetime import datetime
import sys
from datetime import datetime, timedelta, timezone

if sys.version_info >= (3, 9):
from zoneinfo import available_timezones, ZoneInfo
from typing import List, Pattern, Optional
from urllib.parse import unquote

Expand All @@ -26,13 +30,28 @@
from robotframework_reportportal.variables import _variables

listener = listener.listener()
if sys.version_info >= (3, 9):
AVAILABLE_TIMEZONES: set[str] = available_timezones()
else:
AVAILABLE_TIMEZONES = set()


def to_timestamp(time_str: str) -> Optional[str]:
if time_str:
dt = datetime.strptime(time_str, '%Y%m%d %H:%M:%S.%f')
return str(int(dt.timestamp() * 1000))
return None
if not time_str:
return None

timezone_offset_str: Optional[str] = _variables.get('RP_TIME_ZONE_OFFSET', None)
dt = datetime.strptime(time_str, '%Y%m%d %H:%M:%S.%f')

if timezone_offset_str:
if timezone_offset_str in AVAILABLE_TIMEZONES:
tz = ZoneInfo(timezone_offset_str)
dt = dt.replace(tzinfo=tz)
else:
hours, minutes = map(int, timezone_offset_str.split(':'))
offset = timedelta(hours=hours, minutes=minutes)
dt = dt.replace(tzinfo=timezone(offset))
return str(int(dt.timestamp() * 1000))


class RobotResultsVisitor(ResultVisitor):
Expand Down
50 changes: 37 additions & 13 deletions tests/unit/test_result_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,47 @@
limitations under the License
"""

import sys
import pytest

from robotframework_reportportal.result_visitor import to_timestamp
from robotframework_reportportal.variables import _variables

class TestResultVisitorTest:

def test_parse_message_no_img_tag(self, visitor):
with pytest.raises(AttributeError):
visitor.parse_message('usual test comment without image')
def test_parse_message_no_img_tag(visitor):
with pytest.raises(AttributeError):
visitor.parse_message('usual test comment without image')

def test_parse_message_bad_img_tag(self, visitor):
with pytest.raises(AttributeError):
visitor.parse_message('<img src=\'bad.html.img>')

def test_parse_message_contains_image(self, visitor):
assert ['src="any.png"', 'any.png'] == visitor.parse_message(
'<img alt="" src="any.png" />')
def test_parse_message_bad_img_tag(visitor):
with pytest.raises(AttributeError):
visitor.parse_message('<img src=\'bad.html.img>')

def test_parse_message_contains_image_with_space(self, visitor):
assert ['src="any%20image.png"', 'any image.png'] == \
visitor.parse_message('<img alt="" src="any%20image.png" />')

def test_parse_message_contains_image(visitor):
assert ['src="any.png"', 'any.png'] == visitor.parse_message(
'<img alt="" src="any.png" />')


def test_parse_message_contains_image_with_space(visitor):
assert ['src="any%20image.png"', 'any image.png'] == \
visitor.parse_message('<img alt="" src="any%20image.png" />')


TIMESTAMP_TEST_CASES = [
('20240920 00:00:00.000', '+3:00', '1726779600000'),
('20240919 18:00:00.000', '-3:00', '1726779600000')
]

if sys.version_info >= (3, 9):
TIMESTAMP_TEST_CASES += [
('20240919 23:00:00.000', 'Europe/Warsaw', '1726779600000'),
('20240920 00:00:00.000', 'UTC', '1726790400000'),
('20240919 19:00:00.000', 'EST', '1726790400000')
]


@pytest.mark.parametrize('time_str, time_shift, expected', TIMESTAMP_TEST_CASES)
def test_time_stamp_conversion(time_str, time_shift, expected):
_variables['RP_TIME_ZONE_OFFSET'] = time_shift
assert to_timestamp(time_str) == expected

0 comments on commit 89a043e

Please sign in to comment.