Skip to content

Commit 656c28a

Browse files
committed
chore: remove bandit from pre-commit, increase coverage to 100%
- Remove Bandit from pre-commit hooks (kept in CI security job only) - Add test for check_install_arch() amd64 branch - Add test for clang_tools_binary_url() non-macOS platform branch - Add test for install_dir_name() Linux default directory - Add test for create_sym_link() mkdir when directory doesn't exist - Add tests for _wheel_install() success and failure paths - Add test for _handle_binary with zeroed-out version (0.0.0) Production code coverage: clang_tools/*.py -> 100%
1 parent 946113c commit 656c28a

4 files changed

Lines changed: 74 additions & 6 deletions

File tree

.pre-commit-config.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ repos:
2121
hooks:
2222
- id: ruff-check
2323
- id: ruff-format
24-
- repo: https://github.com/PyCQA/bandit
25-
rev: 1.8.4
26-
hooks:
27-
- id: bandit
28-
args: ["-c", "pyproject.toml"]
29-
additional_dependencies: ["bandit[toml]"]
3024
# - repo: local
3125
# hooks:
3226
# - id: pytest

tests/test_install.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,38 @@ def test_install_clang_tools_path_not_in_env(
335335

336336
result = capsys.readouterr()
337337
assert "directory is not in your environment variable PATH" in result.out
338+
339+
340+
def test_clang_tools_binary_url_non_macos(monkeypatch: pytest.MonkeyPatch):
341+
"""Test clang_tools_binary_url with a non-macOS platform string."""
342+
monkeypatch.setattr("clang_tools.install.install_os", "linux")
343+
monkeypatch.setattr("clang_tools.install.install_arch", "amd64")
344+
url = clang_tools_binary_url("clang-format", "12")
345+
assert "linux-amd64" in url
346+
347+
348+
def test_install_dir_name_linux_default(monkeypatch: pytest.MonkeyPatch):
349+
"""Test install_dir_name returns ~/.local/bin/ on Linux when no dir given."""
350+
monkeypatch.setattr("clang_tools.install.install_os", "linux")
351+
result = install_dir_name("")
352+
assert result == os.path.expanduser("~/.local/bin/")
353+
354+
355+
def test_create_sym_link_nonexistent_dir(
356+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
357+
):
358+
"""Test create_sym_link creates the install directory if it doesn't exist."""
359+
monkeypatch.chdir(str(tmp_path))
360+
tool_name, version = "clang-tool", "1"
361+
# Create the target binary in tmp_path
362+
target = tmp_path / f"{tool_name}-{version}{suffix}"
363+
target.write_bytes(b"some binary data")
364+
365+
# Use a nested subdir that does not exist yet
366+
new_dir = tmp_path / "new_install_dir"
367+
assert not new_dir.exists()
368+
369+
assert create_sym_link(tool_name, version, str(new_dir), False, target=target)
370+
assert new_dir.exists()
371+
link = new_dir / f"{tool_name}{suffix}"
372+
assert link.is_symlink()

tests/test_main.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,39 @@ def test_main_install_auto_detect_invalid_version(
375375
# ---- ``uninstall`` subcommand -----------------------------------------
376376

377377

378+
def test_wheel_install_success(monkeypatch: pytest.MonkeyPatch, capsys):
379+
"""Test _wheel_install directly with a mocked resolve_install (success)."""
380+
from clang_tools.main import _wheel_install
381+
382+
monkeypatch.setattr(
383+
"cpp_linter_hooks.util.resolve_install", lambda t, v: f"/fake/{t}"
384+
)
385+
assert _wheel_install(["clang-format"], "18") == 0
386+
assert "installed at:" in capsys.readouterr().out
387+
388+
389+
def test_wheel_install_failure(monkeypatch: pytest.MonkeyPatch, capsys):
390+
"""Test _wheel_install directly with a failing resolve_install."""
391+
from clang_tools.main import _wheel_install
392+
393+
monkeypatch.setattr("cpp_linter_hooks.util.resolve_install", lambda t, v: None)
394+
assert _wheel_install(["clang-tidy"], "21") == 1
395+
assert "Failed to install" in capsys.readouterr().err
396+
397+
398+
def test_main_install_binary_bad_semver(monkeypatch: pytest.MonkeyPatch, capsys):
399+
"""``--binary`` with a version-like but zeroed-out version (e.g. 0.0.0)."""
400+
monkeypatch.setattr(
401+
sys,
402+
"argv",
403+
["clang-tools", "install", "0.0.0", "--binary"],
404+
)
405+
exit_code = main()
406+
result = capsys.readouterr()
407+
assert exit_code == 1
408+
assert "not a semantic" in result.err
409+
410+
378411
def test_main_uninstall_subcommand(monkeypatch: pytest.MonkeyPatch, tmp_path, capsys):
379412
"""``clang-tools uninstall 12`` removes installed tools."""
380413
tool_name = "clang-format"

tests/test_util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,9 @@ def test_check_install_os_unsupported(monkeypatch: pytest.MonkeyPatch):
157157
monkeypatch.setattr("platform.system", lambda: "SunOS")
158158
with pytest.raises(SystemExit, match="sunos is not currently supported"):
159159
check_install_os()
160+
161+
162+
def test_check_install_arch_amd64(monkeypatch: pytest.MonkeyPatch):
163+
"""Tests check_install_arch returns 'amd64' for x86_64 machines."""
164+
monkeypatch.setattr("platform.machine", lambda: "x86_64")
165+
assert check_install_arch() == "amd64"

0 commit comments

Comments
 (0)