Skip to content

Commit e219ef1

Browse files
authored
Merge branch 'main' into add-workflow-to-test-cpython
2 parents d9939be + d3169df commit e219ef1

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

cpp_linter_hooks/util.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ def get_version_from_dependency(tool: str) -> Optional[str]:
2020
return None
2121
with open(pyproject_path, "rb") as f:
2222
data = tomllib.load(f)
23+
# First try project.optional-dependencies.tools
24+
optional_deps = data.get("project", {}).get("optional-dependencies", {})
25+
tools_deps = optional_deps.get("tools", [])
26+
for dep in tools_deps:
27+
if dep.startswith(f"{tool}=="):
28+
return dep.split("==")[1]
29+
30+
# Fallback to project.dependencies for backward compatibility
2331
dependencies = data.get("project", {}).get("dependencies", [])
2432
for dep in dependencies:
2533
if dep.startswith(f"{tool}=="):
@@ -176,6 +184,14 @@ def _resolve_install(tool: str, version: Optional[str]) -> Optional[Path]:
176184
else DEFAULT_CLANG_TIDY_VERSION
177185
)
178186

187+
# Additional safety check in case DEFAULT versions are None
188+
if user_version is None:
189+
user_version = (
190+
DEFAULT_CLANG_FORMAT_VERSION
191+
if tool == "clang-format"
192+
else DEFAULT_CLANG_TIDY_VERSION
193+
)
194+
179195
path = shutil.which(tool)
180196
if path:
181197
runtime_version = _get_runtime_version(tool)

pyproject.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ classifiers = [
3232
"Topic :: Software Development :: Build Tools",
3333
]
3434
dependencies = [
35-
"clang-format==20.1.7",
36-
"clang-tidy==20.1.0",
3735
"tomli>=1.1.0; python_version < '3.11'",
36+
"setuptools>=45.0.0", # Required for pkg_resources in clang-tidy
3837
]
3938
dynamic = ["version"]
4039

@@ -47,6 +46,12 @@ source = "https://github.com/cpp-linter/cpp-linter-hooks"
4746
tracker = "https://github.com/cpp-linter/cpp-linter-hooks/issues"
4847

4948
[project.optional-dependencies]
49+
# only clang tools can added to this section to make hooks work
50+
tools = [
51+
"clang-format==20.1.7",
52+
"clang-tidy==20.1.0",
53+
]
54+
5055
dev = [
5156
"coverage",
5257
"pre-commit",

tests/test_util.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,13 @@ def test_get_version_from_dependency_success():
102102
"""Test get_version_from_dependency with valid pyproject.toml."""
103103
mock_toml_content = {
104104
"project": {
105-
"dependencies": [
106-
"clang-format==20.1.7",
107-
"clang-tidy==20.1.0",
108-
"other-package==1.0.0",
109-
]
105+
"optional-dependencies": {
106+
"tools": [
107+
"clang-format==20.1.7",
108+
"clang-tidy==20.1.0",
109+
"other-package==1.0.0",
110+
]
111+
}
110112
}
111113
}
112114

@@ -132,7 +134,9 @@ def test_get_version_from_dependency_missing_file():
132134
@pytest.mark.benchmark
133135
def test_get_version_from_dependency_missing_dependency():
134136
"""Test get_version_from_dependency with missing dependency."""
135-
mock_toml_content = {"project": {"dependencies": ["other-package==1.0.0"]}}
137+
mock_toml_content = {
138+
"project": {"optional-dependencies": {"tools": ["other-package==1.0.0"]}}
139+
}
136140

137141
with (
138142
patch("pathlib.Path.exists", return_value=True),
@@ -423,3 +427,22 @@ def test_version_lists_not_empty():
423427
assert len(CLANG_TIDY_VERSIONS) > 0
424428
assert all(isinstance(v, str) for v in CLANG_FORMAT_VERSIONS)
425429
assert all(isinstance(v, str) for v in CLANG_TIDY_VERSIONS)
430+
431+
432+
@pytest.mark.benchmark
433+
def test_resolve_install_with_none_default_version():
434+
"""Test _resolve_install when DEFAULT versions are None."""
435+
with (
436+
patch("shutil.which", return_value=None),
437+
patch("cpp_linter_hooks.util.DEFAULT_CLANG_FORMAT_VERSION", None),
438+
patch("cpp_linter_hooks.util.DEFAULT_CLANG_TIDY_VERSION", None),
439+
patch(
440+
"cpp_linter_hooks.util._install_tool",
441+
return_value=Path("/usr/bin/clang-format"),
442+
) as mock_install,
443+
):
444+
result = _resolve_install("clang-format", None)
445+
assert result == Path("/usr/bin/clang-format")
446+
447+
# Should fallback to hardcoded version when DEFAULT is None
448+
mock_install.assert_called_once_with("clang-format", None)

0 commit comments

Comments
 (0)