Skip to content

Commit f966317

Browse files
authored
Merge branch 'main' into add-workflow-to-test-cpython
2 parents a8f64d9 + ddd7de8 commit f966317

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

cpp_linter_hooks/util.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
import sys
22
import shutil
3-
import toml
43
import subprocess
54
from pathlib import Path
65
import logging
76
from typing import Optional, List
8-
from packaging.version import Version, InvalidVersion
7+
8+
try:
9+
import tomllib
10+
except ModuleNotFoundError:
11+
import tomli as tomllib
912

1013
LOG = logging.getLogger(__name__)
1114

1215

1316
def get_version_from_dependency(tool: str) -> Optional[str]:
17+
"""Get the version of a tool from the pyproject.toml dependencies."""
1418
pyproject_path = Path(__file__).parent.parent / "pyproject.toml"
1519
if not pyproject_path.exists():
1620
return None
17-
data = toml.load(pyproject_path)
21+
with open(pyproject_path, "rb") as f:
22+
data = tomllib.load(f)
1823
dependencies = data.get("project", {}).get("dependencies", [])
1924
for dep in dependencies:
2025
if dep.startswith(f"{tool}=="):
2126
return dep.split("==")[1]
2227
return None
2328

2429

25-
DEFAULT_CLANG_FORMAT_VERSION = get_version_from_dependency("clang-format") or "20.1.7"
26-
DEFAULT_CLANG_TIDY_VERSION = get_version_from_dependency("clang-tidy") or "20.1.0"
30+
DEFAULT_CLANG_FORMAT_VERSION = get_version_from_dependency("clang-format")
31+
DEFAULT_CLANG_TIDY_VERSION = get_version_from_dependency("clang-tidy")
2732

2833

2934
CLANG_FORMAT_VERSIONS = [
@@ -108,29 +113,21 @@ def get_version_from_dependency(tool: str) -> Optional[str]:
108113

109114

110115
def _resolve_version(versions: List[str], user_input: Optional[str]) -> Optional[str]:
116+
"""Resolve the version based on user input and available versions."""
111117
if user_input is None:
112118
return None
119+
if user_input in versions:
120+
return user_input
113121
try:
114-
user_ver = Version(user_input)
115-
except InvalidVersion:
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:
125+
LOG.warning("Version %s not found in available versions", user_input)
116126
return None
117127

118-
candidates = [Version(v) for v in versions]
119-
if user_input.count(".") == 0:
120-
matches = [v for v in candidates if v.major == user_ver.major]
121-
elif user_input.count(".") == 1:
122-
matches = [
123-
v
124-
for v in candidates
125-
if f"{v.major}.{v.minor}" == f"{user_ver.major}.{user_ver.minor}"
126-
]
127-
else:
128-
return str(user_ver) if user_ver in candidates else None
129-
130-
return str(max(matches)) if matches else None
131-
132128

133129
def _get_runtime_version(tool: str) -> Optional[str]:
130+
"""Get the runtime version of a tool."""
134131
try:
135132
output = subprocess.check_output([tool, "--version"], text=True)
136133
if tool == "clang-tidy":
@@ -144,6 +141,7 @@ def _get_runtime_version(tool: str) -> Optional[str]:
144141

145142

146143
def _install_tool(tool: str, version: str) -> Optional[Path]:
144+
"""Install a tool using pip."""
147145
try:
148146
subprocess.check_call(
149147
[sys.executable, "-m", "pip", "install", f"{tool}=={version}"]
@@ -155,6 +153,7 @@ def _install_tool(tool: str, version: str) -> Optional[Path]:
155153

156154

157155
def _resolve_install(tool: str, version: Optional[str]) -> Optional[Path]:
156+
"""Resolve the installation of a tool, checking for version and installing if necessary."""
158157
user_version = _resolve_version(
159158
CLANG_FORMAT_VERSIONS if tool == "clang-format" else CLANG_TIDY_VERSIONS,
160159
version,
@@ -191,6 +190,7 @@ def is_installed(tool: str) -> Optional[Path]:
191190

192191

193192
def ensure_installed(tool: str, version: Optional[str] = None) -> str:
193+
"""Ensure a tool is installed, resolving its version if necessary."""
194194
LOG.info("Ensuring %s is installed", tool)
195195
tool_path = _resolve_install(tool, version)
196196
if tool_path:

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ classifiers = [
3434
dependencies = [
3535
"clang-format==20.1.7",
3636
"clang-tidy==20.1.0",
37-
"toml>=0.10.2",
38-
"packaging>=20.0",
37+
"tomli>=1.1.0; python_version < '3.11'",
3938
]
4039
dynamic = ["version"]
4140

testing/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ failed_cases=`grep -c "Failed" result.txt`
2929

3030
echo $failed_cases " cases failed."
3131

32-
if [ $failed_cases -eq 9 ]; then
32+
if [ $failed_cases -eq 10 ]; then
3333
echo "=============================="
3434
echo "Test cpp-linter-hooks success."
3535
echo "=============================="

tests/test_util.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def test_get_version_from_dependency_success():
112112

113113
with (
114114
patch("pathlib.Path.exists", return_value=True),
115-
patch("toml.load", return_value=mock_toml_content),
115+
patch("cpp_linter_hooks.util.tomllib.load", return_value=mock_toml_content),
116116
):
117117
result = get_version_from_dependency("clang-format")
118118
assert result == "20.1.7"
@@ -136,7 +136,7 @@ def test_get_version_from_dependency_missing_dependency():
136136

137137
with (
138138
patch("pathlib.Path.exists", return_value=True),
139-
patch("toml.load", return_value=mock_toml_content),
139+
patch("cpp_linter_hooks.util.tomllib.load", return_value=mock_toml_content),
140140
):
141141
result = get_version_from_dependency("clang-format")
142142
assert result is None
@@ -149,7 +149,7 @@ def test_get_version_from_dependency_malformed_toml():
149149

150150
with (
151151
patch("pathlib.Path.exists", return_value=True),
152-
patch("toml.load", return_value=mock_toml_content),
152+
patch("cpp_linter_hooks.util.tomllib.load", return_value=mock_toml_content),
153153
):
154154
result = get_version_from_dependency("clang-format")
155155
assert result is None
@@ -161,11 +161,11 @@ def test_get_version_from_dependency_malformed_toml():
161161
"user_input,expected",
162162
[
163163
(None, None),
164-
("20", "20.1.7"), # Should find latest 20.x
165-
("20.1", "20.1.7"), # Should find latest 20.1.x
164+
("20", "20.1.0"), # Should find first 20.x
165+
("20.1", "20.1.0"), # Should find first 20.1.x
166166
("20.1.7", "20.1.7"), # Exact match
167-
("18", "18.1.8"), # Should find latest 18.x
168-
("18.1", "18.1.8"), # Should find latest 18.1.x
167+
("18", "18.1.0"), # Should find first 18.x
168+
("18.1", "18.1.0"), # Should find first 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 latest 20.x
186-
("18", "18.1.8"), # Should find latest 18.x
187-
("19", "19.1.0.1"), # Should find latest 19.x
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
188188
("99", None), # Non-existent major version
189189
],
190190
)

0 commit comments

Comments
 (0)