Skip to content

Commit

Permalink
Fix legacy check
Browse files Browse the repository at this point in the history
  • Loading branch information
priitlatt committed Oct 15, 2024
1 parent 4cab05b commit 4abcbd4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 44 deletions.
32 changes: 3 additions & 29 deletions src/codemagic/models/xctests/xcresulttool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import json
import pathlib
import re
import subprocess
from functools import lru_cache
from tempfile import NamedTemporaryFile
Expand All @@ -19,7 +18,7 @@
from codemagic.cli import CommandArg
from codemagic.mixins import RunningCliAppMixin
from codemagic.mixins import StringConverterMixin
from codemagic.utilities import log
from codemagic.models import Xcode

if TYPE_CHECKING:
from typing_extensions import Literal
Expand All @@ -36,38 +35,13 @@ def __init__(self, message: str, stderr: str):
class XcResultTool(RunningCliAppMixin, StringConverterMixin):
@classmethod
@lru_cache(1)
def get_tool_version(cls) -> Optional[Version]:
# Cache xcresulttool version to avoid repeated checks.
# Assumes Xcode (and thus xcresulttool) version remains constant during execution.

cmd_args = ["xcrun", "xcresulttool", "version"]
try:
stdout = cls._run_command(cmd_args, "Failed to obtain xcresulttool version")
except XcResultToolError as e:
log.get_file_logger(cls).exception(str(e))
return None

version_output = cls._str(stdout.strip())
# Expected version output of xcresulttool (bundled with Xcode 16.0 beta 3) is as follows:
# xcresulttool version 23024, format version 3.53 (current)
match = re.match(r"^xcresulttool version (?P<version>\d+(\.\d+)?)", version_output)

if not match:
log.get_file_logger(cls).error("Failed to capture xcresulttool version from %r", version_output)
return None

return Version(match.group("version"))

@classmethod
def is_legacy(cls) -> bool:
"""
With Xcode 16.0 `xcresulttool get` API was changed. Check if activated
xcresulttool is from Xcode 16.0+ (not legacy) or otherwise (is legacy).
"""
version = cls.get_tool_version()
if not version:
return False
return version < Version("23021")
xcode = Xcode.get_selected()
return Version("16") > xcode.version

@classmethod
def _get_legacy_method_error_message(cls, method_name: Literal["get_bundle", "get_object"]) -> str:
Expand Down
32 changes: 17 additions & 15 deletions tests/models/xctests/test_xcresulttool.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from typing import Optional
from unittest import mock

import pytest
from codemagic.models import Xcode
from codemagic.models.xctests import XcResultTool
from packaging.version import Version


@pytest.mark.parametrize(
("xcresulttool_version", "expected_result"),
(
(None, False),
(Version("0"), True),
(Version("1"), True),
(Version("23020.9"), True),
(Version("23021"), False),
(Version("23021.1"), False),
(Version("23022"), False),
(Version("33022"), False),
),
"xcode_version",
("1", "10", "14.4", "15.0", "15.1", "15.2", "15.3", "15.4", "15.9.10"),
)
def test_is_legacy(xcresulttool_version: Optional[Version], expected_result: bool):
with mock.patch.object(XcResultTool, "get_tool_version", new=mock.MagicMock(return_value=xcresulttool_version)):
assert expected_result is XcResultTool.is_legacy()
def test_is_legacy(xcode_version: str):
mock_xcode = mock.MagicMock(version=Version(xcode_version))
with mock.patch.object(Xcode, "get_selected", new=mock.MagicMock(return_value=mock_xcode)):
assert XcResultTool.is_legacy()


@pytest.mark.parametrize(
"xcode_version",
("16", "16.0", "16.0.1", "16.0.0", "16.1", "16.2", "17.2", "20.2", "100"),
)
def test_is_not_legacy(xcode_version: str):
mock_xcode = mock.MagicMock(version=Version(xcode_version))
with mock.patch.object(Xcode, "get_selected", new=mock.MagicMock(return_value=mock_xcode)):
assert not XcResultTool.is_legacy()

0 comments on commit 4abcbd4

Please sign in to comment.