-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_clang_tidy.py
More file actions
298 lines (254 loc) · 9.57 KB
/
test_clang_tidy.py
File metadata and controls
298 lines (254 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import pytest
import subprocess
import time
from pathlib import Path
from unittest.mock import patch, MagicMock
from cpp_linter_hooks.clang_tidy import run_clang_tidy
@pytest.fixture(scope="function")
def generate_compilation_database():
subprocess.run(["mkdir", "-p", "build"])
subprocess.run(["cmake", "-Bbuild", "testing/"])
subprocess.run(["cmake", "-Bbuild", "testing/"])
@pytest.mark.benchmark
@pytest.mark.parametrize(
("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=21"], 1),
),
)
def test_run_clang_tidy_valid(args, expected_retval):
# copy test file to tmp_path to prevent modifying repo data
test_file = Path("testing/main.c")
test_file.write_bytes(Path("testing/main.c").read_bytes())
ret, output = run_clang_tidy(args + [str(test_file)])
assert ret == expected_retval
print(output)
@pytest.mark.benchmark
@pytest.mark.parametrize(
("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=21"], 1),
),
)
def test_run_clang_tidy_invalid(args, expected_retval, tmp_path):
# non existent file
test_file = tmp_path / "main.c"
ret, _ = run_clang_tidy(args + [str(test_file)])
assert ret == expected_retval
# --- compile_commands tests (all mock subprocess.run and resolve_install) ---
_MOCK_RUN = MagicMock(returncode=0, stdout="", stderr="")
def _patch():
return (
patch("cpp_linter_hooks.clang_tidy.subprocess.run", return_value=_MOCK_RUN),
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
)
def test_compile_commands_explicit(tmp_path):
db_dir = tmp_path / "build"
db_dir.mkdir()
(db_dir / "compile_commands.json").write_text("[]")
with (
patch(
"cpp_linter_hooks.clang_tidy.subprocess.run", return_value=_MOCK_RUN
) as mock_run,
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
run_clang_tidy([f"--compile-commands={db_dir}", "dummy.cpp"])
cmd = mock_run.call_args[0][0]
assert "-p" in cmd
assert cmd[cmd.index("-p") + 1] == str(db_dir)
def test_compile_commands_auto_detect(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
build_dir = tmp_path / "build"
build_dir.mkdir()
(build_dir / "compile_commands.json").write_text("[]")
with (
patch(
"cpp_linter_hooks.clang_tidy.subprocess.run", return_value=_MOCK_RUN
) as mock_run,
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
run_clang_tidy(["dummy.cpp"])
cmd = mock_run.call_args[0][0]
assert "-p" in cmd
assert cmd[cmd.index("-p") + 1] == "build"
def test_compile_commands_auto_detect_fallback(tmp_path, monkeypatch):
# Only ./out has compile_commands.json, not ./build
monkeypatch.chdir(tmp_path)
out_dir = tmp_path / "out"
out_dir.mkdir()
(out_dir / "compile_commands.json").write_text("[]")
with (
patch(
"cpp_linter_hooks.clang_tidy.subprocess.run", return_value=_MOCK_RUN
) as mock_run,
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
run_clang_tidy(["dummy.cpp"])
cmd = mock_run.call_args[0][0]
assert "-p" in cmd
assert cmd[cmd.index("-p") + 1] == "out"
def test_compile_commands_none(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
with (
patch(
"cpp_linter_hooks.clang_tidy.subprocess.run", return_value=_MOCK_RUN
) as mock_run,
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
run_clang_tidy(["dummy.cpp"])
cmd = mock_run.call_args[0][0]
assert "-p" not in cmd
def test_compile_commands_conflict_guard(tmp_path, monkeypatch):
# -p already in args: auto-detect should NOT fire even if build/ exists
monkeypatch.chdir(tmp_path)
build_dir = tmp_path / "build"
build_dir.mkdir()
(build_dir / "compile_commands.json").write_text("[]")
with (
patch(
"cpp_linter_hooks.clang_tidy.subprocess.run", return_value=_MOCK_RUN
) as mock_run,
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
run_clang_tidy(["-p", "./custom", "dummy.cpp"])
cmd = mock_run.call_args[0][0]
assert cmd.count("-p") == 1
assert "./custom" in cmd
def test_compile_commands_no_flag(tmp_path, monkeypatch):
# --no-compile-commands disables auto-detect even when build/ exists
monkeypatch.chdir(tmp_path)
build_dir = tmp_path / "build"
build_dir.mkdir()
(build_dir / "compile_commands.json").write_text("[]")
with (
patch(
"cpp_linter_hooks.clang_tidy.subprocess.run", return_value=_MOCK_RUN
) as mock_run,
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
run_clang_tidy(["--no-compile-commands", "dummy.cpp"])
cmd = mock_run.call_args[0][0]
assert "-p" not in cmd
def test_compile_commands_invalid_path(tmp_path):
# Case 1: directory does not exist
fake_dir = tmp_path / "nonexistent"
with patch("cpp_linter_hooks.clang_tidy.resolve_install"):
ret, output = run_clang_tidy([f"--compile-commands={fake_dir}", "dummy.cpp"])
assert ret == 1
assert "nonexistent" in output
# Case 2: directory exists but has no compile_commands.json
empty_dir = tmp_path / "empty_build"
empty_dir.mkdir()
with patch("cpp_linter_hooks.clang_tidy.resolve_install"):
ret, output = run_clang_tidy([f"--compile-commands={empty_dir}", "dummy.cpp"])
assert ret == 1
assert "empty_build" in output
def test_compile_commands_explicit_with_p_conflict(tmp_path, capsys):
# --compile-commands + -p in args: warning printed, only the user's -p used
db_dir = tmp_path / "build"
db_dir.mkdir()
(db_dir / "compile_commands.json").write_text("[]")
with (
patch(
"cpp_linter_hooks.clang_tidy.subprocess.run", return_value=_MOCK_RUN
) as mock_run,
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
run_clang_tidy([f"--compile-commands={db_dir}", "-p", "./other", "dummy.cpp"])
captured = capsys.readouterr()
assert "Warning" in captured.err
cmd = mock_run.call_args[0][0]
assert cmd.count("-p") == 1
assert "./other" in cmd
def test_verbose_prints_compile_db_path(tmp_path, monkeypatch, capsys):
monkeypatch.chdir(tmp_path)
build_dir = tmp_path / "build"
build_dir.mkdir()
(build_dir / "compile_commands.json").write_text("[]")
with (
patch("cpp_linter_hooks.clang_tidy.subprocess.run", return_value=_MOCK_RUN),
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
run_clang_tidy(["--verbose", "dummy.cpp"])
assert "build" in capsys.readouterr().err
def test_no_verbose_no_extra_stderr(tmp_path, monkeypatch, capsys):
monkeypatch.chdir(tmp_path)
build_dir = tmp_path / "build"
build_dir.mkdir()
(build_dir / "compile_commands.json").write_text("[]")
with (
patch("cpp_linter_hooks.clang_tidy.subprocess.run", return_value=_MOCK_RUN),
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
run_clang_tidy(["dummy.cpp"])
assert capsys.readouterr().err == ""
def test_jobs_one_keeps_single_invocation():
with (
patch(
"cpp_linter_hooks.clang_tidy._exec_clang_tidy", return_value=(0, "")
) as mock_exec,
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
run_clang_tidy(["--jobs=1", "-p", "./build", "a.cpp", "b.cpp"])
mock_exec.assert_called_once_with(
["clang-tidy", "-p", "./build", "a.cpp", "b.cpp"]
)
def test_jobs_parallelizes_source_files_and_preserves_output_order():
def fake_exec(command):
source_file = command[-1]
if source_file == "a.cpp":
time.sleep(0.05)
return 0, "a.cpp output"
return 1, "b.cpp output"
with (
patch("cpp_linter_hooks.clang_tidy._exec_clang_tidy", side_effect=fake_exec),
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
ret, output = run_clang_tidy(
[
"--jobs=4",
"-p",
"./build",
"--export-fixes",
"fixes.yaml",
"a.cpp",
"b.cpp",
]
)
assert ret == 1
assert output == "a.cpp output\nb.cpp output"
def test_jobs_parallelizes_only_trailing_source_files():
with (
patch(
"cpp_linter_hooks.clang_tidy._exec_clang_tidy", return_value=(0, "")
) as mock_exec,
patch("cpp_linter_hooks.clang_tidy.resolve_install"),
):
run_clang_tidy(
[
"--jobs=2",
"-p",
"./build",
"--export-fixes",
"fixes.yaml",
"a.cpp",
"b.hpp",
]
)
commands = {tuple(call.args[0]) for call in mock_exec.call_args_list}
assert commands == {
("clang-tidy", "-p", "./build", "--export-fixes", "fixes.yaml", "a.cpp"),
("clang-tidy", "-p", "./build", "--export-fixes", "fixes.yaml", "b.hpp"),
}