Skip to content

fixup #89 to get latest version #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions cpp_linter_hooks/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,26 @@ def get_version_from_dependency(tool: str) -> Optional[str]:


def _resolve_version(versions: List[str], user_input: Optional[str]) -> Optional[str]:
"""Resolve the version based on user input and available versions."""
"""Resolve the latest matching version based on user input and available versions."""
if user_input is None:
return None
if user_input in versions:
return user_input

try:
# Check if the user input is a valid version
return next(v for v in versions if v.startswith(user_input) or v == user_input)
except StopIteration:
# filter versions that start with the user input
matched_versions = [v for v in versions if v.startswith(user_input)]
Copy link
Preview

Copilot AI Jul 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prefix match can unintentionally include versions like '20.10.0' when user_input is '20.1'. Consider matching on segment boundaries (e.g., v == user_input or v.startswith(user_input + '.')) to avoid partial overlaps.

Suggested change
matched_versions = [v for v in versions if v.startswith(user_input)]
matched_versions = [v for v in versions if v == user_input or v.startswith(user_input + '.')]

Copilot uses AI. Check for mistakes.

if not matched_versions:
raise ValueError

# define a function to parse version strings into tuples for comparison
def parse_version(v: str):
return tuple(map(int, v.split(".")))

# return the latest version
return max(matched_versions, key=parse_version)

except ValueError:
LOG.warning("Version %s not found in available versions", user_input)
return None

Expand Down
2 changes: 1 addition & 1 deletion testing/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ failed_cases=`grep -c "Failed" result.txt`

echo $failed_cases " cases failed."

if [ $failed_cases -eq 10 ]; then
if [ $failed_cases -eq 9 ]; then
echo "=============================="
echo "Test cpp-linter-hooks success."
echo "=============================="
Expand Down
14 changes: 7 additions & 7 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ def test_get_version_from_dependency_malformed_toml():
"user_input,expected",
[
(None, None),
("20", "20.1.0"), # Should find first 20.x
("20.1", "20.1.0"), # Should find first 20.1.x
("20", "20.1.7"), # Should find latest 20.x
("20.1", "20.1.7"), # Should find latest 20.1.x
("20.1.7", "20.1.7"), # Exact match
("18", "18.1.0"), # Should find first 18.x
("18.1", "18.1.0"), # Should find first 18.1.x
("18", "18.1.8"), # Should find latest 18.x
("18.1", "18.1.8"), # Should find latest 18.1.x
("99", None), # Non-existent major version
("20.99", None), # Non-existent minor version
("invalid", None), # Invalid version string
Expand All @@ -182,9 +182,9 @@ def test_resolve_version_clang_format(user_input, expected):
"user_input,expected",
[
(None, None),
("20", "20.1.0"), # Should find first 20.x
("18", "18.1.1"), # Should find first 18.x
("19", "19.1.0"), # Should find first 19.x
("20", "20.1.0"), # Should find latest 20.x
("18", "18.1.8"), # Should find latest 18.x
("19", "19.1.0.1"), # Should find latest 19.x
("99", None), # Non-existent major version
],
)
Expand Down
Loading