Skip to content

Commit

Permalink
Fix duration TestNode duration parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
priitlatt committed Oct 29, 2024
1 parent 153ca7d commit 919be74
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/codemagic/models/xctests/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import re
from abc import ABC
from abc import abstractmethod
from collections import defaultdict
from datetime import datetime
from typing import Dict
from typing import Iterator
from typing import List
from typing import Optional
Expand Down Expand Up @@ -240,15 +242,30 @@ def _get_test_case_skipped(cls, xc_test_case: XcTestNode) -> Optional[Skipped]:

return Skipped(message="\n".join(skipped_messages))

@classmethod
def parse_xcresult_test_node_duration_value(cls, xc_duration: str) -> float:
counters: Dict[str, float] = defaultdict(float)

try:
for part in xc_duration.split():
if part.endswith("s"):
counter = "seconds"
elif part.endswith("m"):
counter = "minutes"
else:
raise ValueError("Unknown duration unit")
counters[counter] = float(part[:-1].replace(",", "."))
except ValueError as ve:
raise ValueError("Invalid duration", xc_duration) from ve

return counters["minutes"] * 60.0 + counters["seconds"]

@classmethod
def _get_test_node_duration(cls, xc_test_case: XcTestNode) -> float:
if not xc_test_case.duration:
return 0.0

duration = xc_test_case.duration.replace(",", ".")
if duration.endswith("s"):
duration = duration[:-1]
return float(duration)
return cls.parse_xcresult_test_node_duration_value(xc_test_case.duration)

@classmethod
def _get_test_case(cls, xc_test_case: XcTestNode, xc_test_suite: XcTestNode) -> TestCase:
Expand Down
2 changes: 1 addition & 1 deletion src/codemagic/models/xctests/xcresult/xcode_16_xcresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def from_dict(cls, d: Dict[str, Any]) -> XcTestInsight:
class XcTests(XcModel):
"""
Model definitions for `xcresulttool get test-results tests` output.
Check schema with `xcrun xcresulttool help get test-results tests.
Check schema with `xcrun xcresulttool help get test-results tests`.
"""

devices: List[XcDevice]
Expand Down
21 changes: 21 additions & 0 deletions tests/models/xctests/converter/test_xcode_16_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,24 @@ def test_converter(mock_datetime, expected_properties):
time=3.0,
error=Error(message="banaanUITests.swift:40: failed - Bad UI", type="Failure"),
)


@pytest.mark.parametrize(
("duration", "expected_value"),
(
("0,00045s", 0.00045),
("0,002s", 0.002),
("0,0052s", 0.0052),
("0,26s", 0.26),
("0,2s", 0.2),
("2s", 2.0),
("3s", 3.0),
("1m 4s", 64.0),
("2m 26s", 146.0),
("5m 3s", 303.0),
("6m", 360.0),
),
)
def test_parse_xcresult_test_node_duration_value(duration, expected_value):
value = Xcode16XcResultConverter.parse_xcresult_test_node_duration_value(duration)
assert value == pytest.approx(expected_value)

0 comments on commit 919be74

Please sign in to comment.