Skip to content

Commit 46d7d71

Browse files
authored
feat: add tests and format code using ruff (#75)
* test: test dry-run * fix lint fail * fix test failure * Replace the unused local variable output with _ * use ruff and format code
1 parent 31edb3c commit 46d7d71

File tree

8 files changed

+82
-41
lines changed

8 files changed

+82
-41
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ venv
1212
result.txt
1313
testing/main.c
1414
*/*compile_commands.json
15+
16+
# Ignore clang-tools binaries
17+
clang-tidy-1*
18+
clang-tidy-2*
19+
clang-format-1*
20+
clang-format-2*

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ repos:
1313
rev: v3.20.0
1414
hooks:
1515
- id: pyupgrade
16-
- repo: https://github.com/pycqa/flake8
17-
rev: '7.2.0'
16+
- repo: https://github.com/astral-sh/ruff-pre-commit
17+
rev: v0.12.1
1818
hooks:
19-
- id: flake8
20-
args: [--max-line-length=120]
19+
- id: ruff
20+
- id: ruff-format

cpp_linter_hooks/clang_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
def run_clang_format(args=None) -> Tuple[int, str]:
1313
hook_args, other_args = parser.parse_known_args(args)
1414
path = ensure_installed("clang-format", hook_args.version)
15-
command = [str(path), '-i']
15+
command = [str(path), "-i"]
1616
command.extend(other_args)
1717

1818
retval = 0

cpp_linter_hooks/clang_tidy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def run_clang_tidy(args=None) -> Tuple[int, str]:
1818
retval = 0
1919
output = ""
2020
try:
21-
sp = subprocess.run(command, stdout=subprocess.PIPE, encoding='utf-8')
21+
sp = subprocess.run(command, stdout=subprocess.PIPE, encoding="utf-8")
2222
retval = sp.returncode
2323
output = sp.stdout
2424
if "warning:" in output or "error:" in output:

cpp_linter_hooks/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def is_installed(tool_name: str, version: str) -> Optional[Path]:
2020
"""
2121
# check in current python prefix (usual situation when we installed into pre-commit venv)
2222
directory = Path(sys.executable).parent
23-
path = (directory / f"{tool_name}-{version}")
23+
path = directory / f"{tool_name}-{version}"
2424
if path.is_file():
2525
return path
2626

tests/test_clang_format.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66

77
@pytest.mark.parametrize(
8-
('args', 'expected_retval'), (
9-
(['--style=Google'], (0, "")),
10-
(['--style=Google', '--version=16'], (0, "")),
11-
(['--style=Google', '--version=17'], (0, "")),
12-
(['--style=Google', '--version=18'], (0, "")),
13-
(['--style=Google', '--version=19'], (0, "")),
14-
(['--style=Google', '--version=20'], (0, "")),
8+
("args", "expected_retval"),
9+
(
10+
(["--style=Google"], (0, "")),
11+
(["--style=Google", "--version=16"], (0, "")),
12+
(["--style=Google", "--version=17"], (0, "")),
13+
(["--style=Google", "--version=18"], (0, "")),
14+
(["--style=Google", "--version=19"], (0, "")),
15+
(["--style=Google", "--version=20"], (0, "")),
1516
),
1617
)
1718
def test_run_clang_format_valid(args, expected_retval, tmp_path):
@@ -24,13 +25,19 @@ def test_run_clang_format_valid(args, expected_retval, tmp_path):
2425

2526

2627
@pytest.mark.parametrize(
27-
('args', 'expected_retval'), (
28-
(['--style=Google',], 1),
29-
(['--style=Google', '--version=16'], 1),
30-
(['--style=Google', '--version=17'], 1),
31-
(['--style=Google', '--version=18'], 1),
32-
(['--style=Google', '--version=19'], 1),
33-
(['--style=Google', '--version=20'], 1),
28+
("args", "expected_retval"),
29+
(
30+
(
31+
[
32+
"--style=Google",
33+
],
34+
1,
35+
),
36+
(["--style=Google", "--version=16"], 1),
37+
(["--style=Google", "--version=17"], 1),
38+
(["--style=Google", "--version=18"], 1),
39+
(["--style=Google", "--version=19"], 1),
40+
(["--style=Google", "--version=20"], 1),
3441
),
3542
)
3643
def test_run_clang_format_invalid(args, expected_retval, tmp_path):
@@ -39,3 +46,21 @@ def test_run_clang_format_invalid(args, expected_retval, tmp_path):
3946

4047
ret, _ = run_clang_format(args + [str(test_file)])
4148
assert ret == expected_retval
49+
50+
51+
@pytest.mark.parametrize(
52+
("args", "expected_retval"),
53+
(
54+
(
55+
[
56+
"--style=Google",
57+
],
58+
1,
59+
),
60+
),
61+
)
62+
def test_run_clang_format_dry_run(args, expected_retval, tmp_path):
63+
# copy test file to tmp_path to prevent modifying repo data
64+
test_file = tmp_path / "main.c"
65+
ret, _ = run_clang_format(["--dry-run", str(test_file)])
66+
assert ret == -1 # Dry run should not fail

tests/test_clang_tidy.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55
from cpp_linter_hooks.clang_tidy import run_clang_tidy
66

77

8-
@pytest.fixture(scope='function')
8+
@pytest.fixture(scope="function")
99
def generate_compilation_database():
10-
subprocess.run(['mkdir', '-p', 'build'])
11-
subprocess.run(['cmake', '-Bbuild', 'testing/'])
12-
subprocess.run(['cmake', '-Bbuild', 'testing/'])
10+
subprocess.run(["mkdir", "-p", "build"])
11+
subprocess.run(["cmake", "-Bbuild", "testing/"])
12+
subprocess.run(["cmake", "-Bbuild", "testing/"])
1313

1414

1515
@pytest.mark.parametrize(
16-
('args', 'expected_retval'), (
16+
("args", "expected_retval"),
17+
(
1718
(['--checks="boost-*"'], 1),
18-
(['--checks="boost-*"', '--version=16'], 1),
19-
(['--checks="boost-*"', '--version=17'], 1),
20-
(['--checks="boost-*"', '--version=18'], 1),
21-
(['--checks="boost-*"', '--version=19'], 1),
22-
(['--checks="boost-*"', '--version=20'], 1),
19+
(['--checks="boost-*"', "--version=16"], 1),
20+
(['--checks="boost-*"', "--version=17"], 1),
21+
(['--checks="boost-*"', "--version=18"], 1),
22+
(['--checks="boost-*"', "--version=19"], 1),
23+
(['--checks="boost-*"', "--version=20"], 1),
2324
),
2425
)
2526
def test_run_clang_tidy_valid(args, expected_retval):
@@ -32,13 +33,14 @@ def test_run_clang_tidy_valid(args, expected_retval):
3233

3334

3435
@pytest.mark.parametrize(
35-
('args', 'expected_retval'), (
36+
("args", "expected_retval"),
37+
(
3638
(['--checks="boost-*"'], 1),
37-
(['--checks="boost-*"', '--version=16'], 1),
38-
(['--checks="boost-*"', '--version=17'], 1),
39-
(['--checks="boost-*"', '--version=18'], 1),
40-
(['--checks="boost-*"', '--version=19'], 1),
41-
(['--checks="boost-*"', '--version=20'], 1),
39+
(['--checks="boost-*"', "--version=16"], 1),
40+
(['--checks="boost-*"', "--version=17"], 1),
41+
(['--checks="boost-*"', "--version=18"], 1),
42+
(['--checks="boost-*"', "--version=19"], 1),
43+
(['--checks="boost-*"', "--version=20"], 1),
4244
),
4345
)
4446
def test_run_clang_tidy_invalid(args, expected_retval, tmp_path):

tests/test_util.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
@pytest.mark.parametrize(("tool", "version"), list(product(TOOLS, VERSIONS)))
1414
def test_ensure_installed(tool, version, tmp_path, monkeypatch, caplog):
15-
1615
bin_path = tmp_path / "bin"
1716
with monkeypatch.context() as m:
1817
m.setattr(sys, "executable", str(bin_path / "python"))
@@ -31,11 +30,20 @@ def test_ensure_installed(tool, version, tmp_path, monkeypatch, caplog):
3130
assert (bin_path / f"{tool}-{bin_version}").is_file
3231

3332
# first run should install
34-
assert caplog.record_tuples[0][2] == f"Checking for {tool}, version {bin_version}"
33+
assert (
34+
caplog.record_tuples[0][2]
35+
== f"Checking for {tool}, version {bin_version}"
36+
)
3537
if run == 0:
3638
# FIXME
3739
# assert caplog.record_tuples[1][2] == f"Installing {tool}, version {bin_version}"
38-
assert caplog.record_tuples[1][2] == f"{tool}, version {bin_version} is already installed"
40+
assert (
41+
caplog.record_tuples[1][2]
42+
== f"{tool}, version {bin_version} is already installed"
43+
)
3944
# second run should just confirm it's already installed
4045
else:
41-
assert caplog.record_tuples[1][2] == f"{tool}, version {bin_version} is already installed"
46+
assert (
47+
caplog.record_tuples[1][2]
48+
== f"{tool}, version {bin_version} is already installed"
49+
)

0 commit comments

Comments
 (0)