Skip to content

fix: move clang tools dependencies to optional #92

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 8 commits into from
Jul 16, 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
16 changes: 16 additions & 0 deletions cpp_linter_hooks/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def get_version_from_dependency(tool: str) -> Optional[str]:
return None
with open(pyproject_path, "rb") as f:
data = tomllib.load(f)
# First try project.optional-dependencies.tools
optional_deps = data.get("project", {}).get("optional-dependencies", {})
tools_deps = optional_deps.get("tools", [])
for dep in tools_deps:
if dep.startswith(f"{tool}=="):
return dep.split("==")[1]

# Fallback to project.dependencies for backward compatibility
dependencies = data.get("project", {}).get("dependencies", [])
for dep in dependencies:
if dep.startswith(f"{tool}=="):
Expand Down Expand Up @@ -176,6 +184,14 @@ def _resolve_install(tool: str, version: Optional[str]) -> Optional[Path]:
else DEFAULT_CLANG_TIDY_VERSION
)

# Additional safety check in case DEFAULT versions are None
if user_version is None:
user_version = (
DEFAULT_CLANG_FORMAT_VERSION
if tool == "clang-format"
else DEFAULT_CLANG_TIDY_VERSION
)

path = shutil.which(tool)
if path:
runtime_version = _get_runtime_version(tool)
Expand Down
9 changes: 7 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ classifiers = [
"Topic :: Software Development :: Build Tools",
]
dependencies = [
"clang-format==20.1.7",
"clang-tidy==20.1.0",
"tomli>=1.1.0; python_version < '3.11'",
"setuptools>=45.0.0", # Required for pkg_resources in clang-tidy
]
dynamic = ["version"]

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

[project.optional-dependencies]
# only clang tools can added to this section to make hooks work
tools = [
"clang-format==20.1.7",
"clang-tidy==20.1.0",
]

dev = [
"coverage",
"pre-commit",
Expand Down
35 changes: 29 additions & 6 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ def test_get_version_from_dependency_success():
"""Test get_version_from_dependency with valid pyproject.toml."""
mock_toml_content = {
"project": {
"dependencies": [
"clang-format==20.1.7",
"clang-tidy==20.1.0",
"other-package==1.0.0",
]
"optional-dependencies": {
"tools": [
"clang-format==20.1.7",
"clang-tidy==20.1.0",
"other-package==1.0.0",
]
}
}
}

Expand All @@ -132,7 +134,9 @@ def test_get_version_from_dependency_missing_file():
@pytest.mark.benchmark
def test_get_version_from_dependency_missing_dependency():
"""Test get_version_from_dependency with missing dependency."""
mock_toml_content = {"project": {"dependencies": ["other-package==1.0.0"]}}
mock_toml_content = {
"project": {"optional-dependencies": {"tools": ["other-package==1.0.0"]}}
}

with (
patch("pathlib.Path.exists", return_value=True),
Expand Down Expand Up @@ -423,3 +427,22 @@ def test_version_lists_not_empty():
assert len(CLANG_TIDY_VERSIONS) > 0
assert all(isinstance(v, str) for v in CLANG_FORMAT_VERSIONS)
assert all(isinstance(v, str) for v in CLANG_TIDY_VERSIONS)


@pytest.mark.benchmark
def test_resolve_install_with_none_default_version():
"""Test _resolve_install when DEFAULT versions are None."""
with (
patch("shutil.which", return_value=None),
patch("cpp_linter_hooks.util.DEFAULT_CLANG_FORMAT_VERSION", None),
patch("cpp_linter_hooks.util.DEFAULT_CLANG_TIDY_VERSION", None),
patch(
"cpp_linter_hooks.util._install_tool",
return_value=Path("/usr/bin/clang-format"),
) as mock_install,
):
result = _resolve_install("clang-format", None)
assert result == Path("/usr/bin/clang-format")

# Should fallback to hardcoded version when DEFAULT is None
mock_install.assert_called_once_with("clang-format", None)
Loading