Skip to content

Commit 46fbb13

Browse files
committed
fixup #89 to get latest version
1 parent ddd7de8 commit 46fbb13

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

cpp_linter_hooks/util.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,26 @@ def get_version_from_dependency(tool: str) -> Optional[str]:
113113

114114

115115
def _resolve_version(versions: List[str], user_input: Optional[str]) -> Optional[str]:
116-
"""Resolve the version based on user input and available versions."""
116+
"""Resolve the latest matching version based on user input and available versions."""
117117
if user_input is None:
118118
return None
119119
if user_input in versions:
120120
return user_input
121+
121122
try:
122-
# Check if the user input is a valid version
123-
return next(v for v in versions if v.startswith(user_input) or v == user_input)
124-
except StopIteration:
123+
# filter versions that start with the user input
124+
matched_versions = [v for v in versions if v.startswith(user_input)]
125+
if not matched_versions:
126+
raise ValueError
127+
128+
# define a function to parse version strings into tuples for comparison
129+
def parse_version(v: str):
130+
return tuple(map(int, v.split(".")))
131+
132+
# return the latest version
133+
return max(matched_versions, key=parse_version)
134+
135+
except Exception:
125136
LOG.warning("Version %s not found in available versions", user_input)
126137
return None
127138

tests/test_util.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ def test_get_version_from_dependency_malformed_toml():
161161
"user_input,expected",
162162
[
163163
(None, None),
164-
("20", "20.1.0"), # Should find first 20.x
165-
("20.1", "20.1.0"), # Should find first 20.1.x
164+
("20", "20.1.7"), # Should find latest 20.x
165+
("20.1", "20.1.7"), # Should find latest 20.1.x
166166
("20.1.7", "20.1.7"), # Exact match
167-
("18", "18.1.0"), # Should find first 18.x
168-
("18.1", "18.1.0"), # Should find first 18.1.x
167+
("18", "18.1.8"), # Should find latest 18.x
168+
("18.1", "18.1.8"), # Should find latest 18.1.x
169169
("99", None), # Non-existent major version
170170
("20.99", None), # Non-existent minor version
171171
("invalid", None), # Invalid version string
@@ -182,9 +182,9 @@ def test_resolve_version_clang_format(user_input, expected):
182182
"user_input,expected",
183183
[
184184
(None, None),
185-
("20", "20.1.0"), # Should find first 20.x
186-
("18", "18.1.1"), # Should find first 18.x
187-
("19", "19.1.0"), # Should find first 19.x
185+
("20", "20.1.0"), # Should find latest 20.x
186+
("18", "18.1.8"), # Should find latest 18.x
187+
("19", "19.1.0.1"), # Should find latest 19.x
188188
("99", None), # Non-existent major version
189189
],
190190
)

0 commit comments

Comments
 (0)