Skip to content

Commit 0fca76b

Browse files
(PTFE-2534) Remove sort and modify test assertion (#255)
* (PTFE-2534) Remove sort and modify test assertion * Fix flake8
1 parent 12e203f commit 0fca76b

File tree

3 files changed

+55
-30
lines changed

3 files changed

+55
-30
lines changed

bert_e/tests/test_bert_e.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ def test_branch_cascade_mixed_versions(self):
569569
2: {'name': 'development/4.3', 'ignore': True},
570570
3: {'name': 'development/5.1.8', 'ignore': True},
571571
4: {'name': 'development/5.1', 'ignore': False},
572-
5: {'name': 'development/10', 'ignore': False}
572+
5: {'name': 'development/10.0', 'ignore': False}
573573
})
574574
tags = ['4.3.16', '4.3.18', '5.1.3', '5.1.7']
575575
fixver = ['5.1.9', '10.0.0']

bert_e/tests/unit/test_sorted.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_sorted_versions():
5555

5656
def test_compare_branches_major_minor_vs_major_only():
5757
branch1 = ((4, 3),)
58-
branch2 = ((4, None),)
58+
branch2 = ((4, ),)
5959
assert compare_branches(branch1, branch2) == -1
6060

6161

@@ -66,18 +66,18 @@ def test_compare_branches_major_only_vs_major_only_returns_0():
6666

6767

6868
def test_compare_branches_major_only_vs_major_minor():
69-
branch1 = ((4, None),)
69+
branch1 = ((4, ),)
7070
branch2 = ((4, 3),)
7171
assert compare_branches(branch1, branch2) == 1
7272

7373

7474
def test_compare_branches_major_minor_micro_vs_major_minor():
7575
branch1 = ((4, 3, 2),)
7676
branch2 = ((4, 3),)
77-
assert compare_branches(branch1, branch2) == -997
77+
assert compare_branches(branch1, branch2) == -1
7878

7979

8080
def test_compare_branches_major_minor_vs_major_minor_micro():
8181
branch1 = ((4, 3),)
8282
branch2 = ((4, 3, 2),)
83-
assert compare_branches(branch1, branch2) == 997
83+
assert compare_branches(branch1, branch2) == 1

bert_e/workflow/gitwaterflow/branches.py

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,45 @@
2929
LOG = logging.getLogger(__name__)
3030

3131

32+
def _compare_version_component(version1, version2, component_index):
33+
"""
34+
Helper method to compare version components at a specific level.
35+
36+
Args:
37+
version1, version2: Version tuples to compare
38+
component_index: Index of the component to compare
39+
(0 for major, 1 for minor, 2 for micro)
40+
41+
Returns:
42+
int: -1 if version1 < version2, 1 if version1 > version2,
43+
0 if equal or both missing
44+
None: if comparison should continue to next level
45+
"""
46+
len1 = len(version1)
47+
len2 = len(version2)
48+
49+
# Both have the component - compare them
50+
if len1 > component_index and len2 > component_index:
51+
comp1 = version1[component_index]
52+
comp2 = version2[component_index]
53+
if comp1 != comp2:
54+
return comp1 - comp2
55+
return None # Equal, continue to next level
56+
57+
# Only one has the component
58+
elif len1 > component_index and len2 <= component_index:
59+
# version1 has component, version2 doesn't
60+
# -> version1 comes first in the cascade
61+
return -1
62+
elif len1 <= component_index and len2 > component_index:
63+
# version2 has component, version1 doesn't
64+
# -> version2 comes first in the cascade
65+
return 1
66+
67+
# Neither has the component at this level
68+
return None
69+
70+
3271
def compare_branches(branch1, branch2):
3372
"""
3473
Compare GitWaterflow branches for sorting.
@@ -39,13 +78,11 @@ def compare_branches(branch1, branch2):
3978
# Safely extract version components
4079
version1 = branch1[0]
4180
version2 = branch2[0]
42-
# Extract major versions (always present)
43-
major1 = version1[0] if len(version1) > 0 else 0
44-
major2 = version2[0] if len(version2) > 0 else 0
4581

46-
# Compare major versions first
47-
if major1 != major2:
48-
return major1 - major2
82+
# Compare major versions first using helper
83+
major_result = _compare_version_component(version1, version2, 0)
84+
if major_result is not None:
85+
return major_result
4986

5087
# Same major version - check if one is major-only vs major.minor
5188
# Major-only branches have None as minor version, e.g., (4, None)
@@ -62,25 +99,13 @@ def compare_branches(branch1, branch2):
6299
# major.minor comes before
63100
return -1
64101

65-
# Both are major.minor or longer - compare normally
66-
minor1 = version1[1] if len(version1) > 1 else 999
67-
minor2 = version2[1] if len(version2) > 1 else 999
68-
69-
# Compare minor versions
70-
if minor1 != minor2:
71-
return minor1 - minor2
72-
73-
# Same major.minor - extract micro versions
74-
# Default to 0 if no micro
75-
micro1 = version1[2] if len(version1) > 2 else 999
76-
# Default to 0 if no micro
77-
micro2 = version2[2] if len(version2) > 2 else 999
78-
79-
# Compare micro versions
80-
if micro1 != micro2:
81-
return micro1 - micro2
82-
else:
83-
return 0
102+
# Compare remaining version components (minor, micro) using helper
103+
for level in range(1, 3): # 1=minor, 2=micro
104+
result = _compare_version_component(version1, version2, level)
105+
if result is not None:
106+
return result
107+
108+
return 0
84109

85110

86111
def compare_queues(version1, version2):

0 commit comments

Comments
 (0)