Skip to content

feat: add tests and format code using ruff #75

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 5 commits into from
Jul 1, 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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ venv
result.txt
testing/main.c
*/*compile_commands.json

# Ignore clang-tools binaries
clang-tidy-1*
clang-tidy-2*
clang-format-1*
clang-format-2*
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ repos:
rev: v3.20.0
hooks:
- id: pyupgrade
- repo: https://github.com/pycqa/flake8
rev: '7.2.0'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.1
hooks:
- id: flake8
args: [--max-line-length=120]
- id: ruff
- id: ruff-format
2 changes: 1 addition & 1 deletion cpp_linter_hooks/clang_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def run_clang_format(args=None) -> Tuple[int, str]:
hook_args, other_args = parser.parse_known_args(args)
path = ensure_installed("clang-format", hook_args.version)
command = [str(path), '-i']
command = [str(path), "-i"]
command.extend(other_args)

retval = 0
Expand Down
2 changes: 1 addition & 1 deletion cpp_linter_hooks/clang_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def run_clang_tidy(args=None) -> Tuple[int, str]:
retval = 0
output = ""
try:
sp = subprocess.run(command, stdout=subprocess.PIPE, encoding='utf-8')
sp = subprocess.run(command, stdout=subprocess.PIPE, encoding="utf-8")
retval = sp.returncode
output = sp.stdout
if "warning:" in output or "error:" in output:
Expand Down
2 changes: 1 addition & 1 deletion cpp_linter_hooks/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def is_installed(tool_name: str, version: str) -> Optional[Path]:
"""
# check in current python prefix (usual situation when we installed into pre-commit venv)
directory = Path(sys.executable).parent
path = (directory / f"{tool_name}-{version}")
path = directory / f"{tool_name}-{version}"
if path.is_file():
return path

Expand Down
53 changes: 39 additions & 14 deletions tests/test_clang_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@


@pytest.mark.parametrize(
('args', 'expected_retval'), (
(['--style=Google'], (0, "")),
(['--style=Google', '--version=16'], (0, "")),
(['--style=Google', '--version=17'], (0, "")),
(['--style=Google', '--version=18'], (0, "")),
(['--style=Google', '--version=19'], (0, "")),
(['--style=Google', '--version=20'], (0, "")),
("args", "expected_retval"),
(
(["--style=Google"], (0, "")),
(["--style=Google", "--version=16"], (0, "")),
(["--style=Google", "--version=17"], (0, "")),
(["--style=Google", "--version=18"], (0, "")),
(["--style=Google", "--version=19"], (0, "")),
(["--style=Google", "--version=20"], (0, "")),
),
)
def test_run_clang_format_valid(args, expected_retval, tmp_path):
Expand All @@ -24,13 +25,19 @@ def test_run_clang_format_valid(args, expected_retval, tmp_path):


@pytest.mark.parametrize(
('args', 'expected_retval'), (
(['--style=Google',], 1),
(['--style=Google', '--version=16'], 1),
(['--style=Google', '--version=17'], 1),
(['--style=Google', '--version=18'], 1),
(['--style=Google', '--version=19'], 1),
(['--style=Google', '--version=20'], 1),
("args", "expected_retval"),
(
(
[
"--style=Google",
],
1,
),
(["--style=Google", "--version=16"], 1),
(["--style=Google", "--version=17"], 1),
(["--style=Google", "--version=18"], 1),
(["--style=Google", "--version=19"], 1),
(["--style=Google", "--version=20"], 1),
),
)
def test_run_clang_format_invalid(args, expected_retval, tmp_path):
Expand All @@ -39,3 +46,21 @@ def test_run_clang_format_invalid(args, expected_retval, tmp_path):

ret, _ = run_clang_format(args + [str(test_file)])
assert ret == expected_retval


@pytest.mark.parametrize(
("args", "expected_retval"),
(
(
[
"--style=Google",
],
1,
),
),
)
def test_run_clang_format_dry_run(args, expected_retval, tmp_path):
# copy test file to tmp_path to prevent modifying repo data
test_file = tmp_path / "main.c"
ret, _ = run_clang_format(["--dry-run", str(test_file)])
assert ret == -1 # Dry run should not fail
34 changes: 18 additions & 16 deletions tests/test_clang_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
from cpp_linter_hooks.clang_tidy import run_clang_tidy


@pytest.fixture(scope='function')
@pytest.fixture(scope="function")
def generate_compilation_database():
subprocess.run(['mkdir', '-p', 'build'])
subprocess.run(['cmake', '-Bbuild', 'testing/'])
subprocess.run(['cmake', '-Bbuild', 'testing/'])
subprocess.run(["mkdir", "-p", "build"])
subprocess.run(["cmake", "-Bbuild", "testing/"])
subprocess.run(["cmake", "-Bbuild", "testing/"])


@pytest.mark.parametrize(
('args', 'expected_retval'), (
("args", "expected_retval"),
(
(['--checks="boost-*"'], 1),
(['--checks="boost-*"', '--version=16'], 1),
(['--checks="boost-*"', '--version=17'], 1),
(['--checks="boost-*"', '--version=18'], 1),
(['--checks="boost-*"', '--version=19'], 1),
(['--checks="boost-*"', '--version=20'], 1),
(['--checks="boost-*"', "--version=16"], 1),
(['--checks="boost-*"', "--version=17"], 1),
(['--checks="boost-*"', "--version=18"], 1),
(['--checks="boost-*"', "--version=19"], 1),
(['--checks="boost-*"', "--version=20"], 1),
),
)
def test_run_clang_tidy_valid(args, expected_retval):
Expand All @@ -32,13 +33,14 @@ def test_run_clang_tidy_valid(args, expected_retval):


@pytest.mark.parametrize(
('args', 'expected_retval'), (
("args", "expected_retval"),
(
(['--checks="boost-*"'], 1),
(['--checks="boost-*"', '--version=16'], 1),
(['--checks="boost-*"', '--version=17'], 1),
(['--checks="boost-*"', '--version=18'], 1),
(['--checks="boost-*"', '--version=19'], 1),
(['--checks="boost-*"', '--version=20'], 1),
(['--checks="boost-*"', "--version=16"], 1),
(['--checks="boost-*"', "--version=17"], 1),
(['--checks="boost-*"', "--version=18"], 1),
(['--checks="boost-*"', "--version=19"], 1),
(['--checks="boost-*"', "--version=20"], 1),
),
)
def test_run_clang_tidy_invalid(args, expected_retval, tmp_path):
Expand Down
16 changes: 12 additions & 4 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

@pytest.mark.parametrize(("tool", "version"), list(product(TOOLS, VERSIONS)))
def test_ensure_installed(tool, version, tmp_path, monkeypatch, caplog):

bin_path = tmp_path / "bin"
with monkeypatch.context() as m:
m.setattr(sys, "executable", str(bin_path / "python"))
Expand All @@ -31,11 +30,20 @@ def test_ensure_installed(tool, version, tmp_path, monkeypatch, caplog):
assert (bin_path / f"{tool}-{bin_version}").is_file

# first run should install
assert caplog.record_tuples[0][2] == f"Checking for {tool}, version {bin_version}"
assert (
caplog.record_tuples[0][2]
== f"Checking for {tool}, version {bin_version}"
)
if run == 0:
# FIXME
# assert caplog.record_tuples[1][2] == f"Installing {tool}, version {bin_version}"
assert caplog.record_tuples[1][2] == f"{tool}, version {bin_version} is already installed"
assert (
caplog.record_tuples[1][2]
== f"{tool}, version {bin_version} is already installed"
)
# second run should just confirm it's already installed
else:
assert caplog.record_tuples[1][2] == f"{tool}, version {bin_version} is already installed"
assert (
caplog.record_tuples[1][2]
== f"{tool}, version {bin_version} is already installed"
)
Loading