-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_util.py
More file actions
448 lines (363 loc) · 14.8 KB
/
test_util.py
File metadata and controls
448 lines (363 loc) · 14.8 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import logging
import pytest
from itertools import product
from unittest.mock import patch
from pathlib import Path
import subprocess
import sys
from cpp_linter_hooks.util import (
ensure_installed,
is_installed,
get_version_from_dependency,
_resolve_version,
_get_runtime_version,
_install_tool,
_resolve_install,
CLANG_FORMAT_VERSIONS,
CLANG_TIDY_VERSIONS,
DEFAULT_CLANG_FORMAT_VERSION,
DEFAULT_CLANG_TIDY_VERSION,
)
VERSIONS = [None, "20"]
TOOLS = ["clang-format", "clang-tidy"]
@pytest.mark.benchmark
@pytest.mark.parametrize(("tool", "version"), list(product(TOOLS, VERSIONS)))
def test_ensure_installed(tool, version, tmp_path, monkeypatch, caplog):
"""Test that ensure_installed returns the tool name for wheel packages."""
with monkeypatch.context():
# Mock shutil.which to simulate the tool being available
with patch("shutil.which", return_value=str(tmp_path / tool)):
# Mock _get_runtime_version to return a matching version
mock_version = "20.1.7" if tool == "clang-format" else "20.1.0"
with patch(
"cpp_linter_hooks.util._get_runtime_version", return_value=mock_version
):
caplog.clear()
caplog.set_level(logging.INFO, logger="cpp_linter_hooks.util")
if version is None:
result = ensure_installed(tool)
else:
result = ensure_installed(tool, version=version)
# Should return the tool name for direct execution
assert result == tool
# Check that we logged ensuring the tool is installed
assert any("Ensuring" in record.message for record in caplog.records)
@pytest.mark.benchmark
def test_is_installed_with_shutil_which(tmp_path):
"""Test is_installed when tool is found via shutil.which."""
tool_path = tmp_path / "clang-format"
tool_path.touch()
with patch("shutil.which", return_value=str(tool_path)):
result = is_installed("clang-format")
assert result == tool_path
@pytest.mark.benchmark
def test_is_installed_not_found():
"""Test is_installed when tool is not found anywhere."""
with (
patch("shutil.which", return_value=None),
patch("sys.executable", "/nonexistent/python"),
):
result = is_installed("clang-format")
assert result is None
@pytest.mark.benchmark
def test_ensure_installed_tool_not_found(caplog):
"""Test ensure_installed when tool is not found."""
with (
patch("shutil.which", return_value=None),
patch("cpp_linter_hooks.util._install_tool", return_value=None),
):
caplog.clear()
caplog.set_level(logging.WARNING, logger="cpp_linter_hooks.util")
result = ensure_installed("clang-format")
# Should still return the tool name
assert result == "clang-format"
# Should log a warning
assert any(
"not found and could not be installed" in record.message
for record in caplog.records
)
# Tests for get_version_from_dependency
@pytest.mark.benchmark
def test_get_version_from_dependency_success():
"""Test get_version_from_dependency with valid pyproject.toml."""
mock_toml_content = {
"project": {
"optional-dependencies": {
"tools": [
"clang-format==20.1.7",
"clang-tidy==20.1.0",
"other-package==1.0.0",
]
}
}
}
with (
patch("pathlib.Path.exists", return_value=True),
patch("cpp_linter_hooks.util.tomllib.load", return_value=mock_toml_content),
):
result = get_version_from_dependency("clang-format")
assert result == "20.1.7"
result = get_version_from_dependency("clang-tidy")
assert result == "20.1.0"
@pytest.mark.benchmark
def test_get_version_from_dependency_missing_file():
"""Test get_version_from_dependency when pyproject.toml doesn't exist."""
with patch("pathlib.Path.exists", return_value=False):
result = get_version_from_dependency("clang-format")
assert result is None
@pytest.mark.benchmark
def test_get_version_from_dependency_missing_dependency():
"""Test get_version_from_dependency with missing dependency."""
mock_toml_content = {
"project": {"optional-dependencies": {"tools": ["other-package==1.0.0"]}}
}
with (
patch("pathlib.Path.exists", return_value=True),
patch("cpp_linter_hooks.util.tomllib.load", return_value=mock_toml_content),
):
result = get_version_from_dependency("clang-format")
assert result is None
@pytest.mark.benchmark
def test_get_version_from_dependency_malformed_toml():
"""Test get_version_from_dependency with malformed toml."""
mock_toml_content = {}
with (
patch("pathlib.Path.exists", return_value=True),
patch("cpp_linter_hooks.util.tomllib.load", return_value=mock_toml_content),
):
result = get_version_from_dependency("clang-format")
assert result is None
# Tests for _resolve_version
@pytest.mark.benchmark
@pytest.mark.parametrize(
"user_input,expected",
[
(None, None),
("20", "20.1.8"), # Should find latest 20.x
("20.1", "20.1.8"), # Should find latest 20.1.x
("20.1.7", "20.1.7"), # Exact match
("18", "18.1.8"), # Should find latest 18.x
("18.1", "18.1.8"), # Should find latest 18.1.x
("99", None), # Non-existent major version
("20.99", None), # Non-existent minor version
("invalid", None), # Invalid version string
],
)
def test_resolve_version_clang_format(user_input, expected):
"""Test _resolve_version with various inputs for clang-format."""
result = _resolve_version(CLANG_FORMAT_VERSIONS, user_input)
assert result == expected
@pytest.mark.benchmark
@pytest.mark.parametrize(
"user_input,expected",
[
(None, None),
("20", "20.1.0"), # Should find latest 20.x
("18", "18.1.8"), # Should find latest 18.x
("19", "19.1.0.1"), # Should find latest 19.x
("99", None), # Non-existent major version
],
)
def test_resolve_version_clang_tidy(user_input, expected):
"""Test _resolve_version with various inputs for clang-tidy."""
result = _resolve_version(CLANG_TIDY_VERSIONS, user_input)
assert result == expected
# Tests for _get_runtime_version
@pytest.mark.benchmark
def test_get_runtime_version_clang_format():
"""Test _get_runtime_version for clang-format."""
mock_output = "Ubuntu clang-format version 20.1.7-1ubuntu1\n"
with patch("subprocess.check_output", return_value=mock_output):
result = _get_runtime_version("clang-format")
assert result == "20.1.7-1ubuntu1"
@pytest.mark.benchmark
def test_get_runtime_version_clang_tidy():
"""Test _get_runtime_version for clang-tidy."""
mock_output = "LLVM (http://llvm.org/):\n LLVM version 20.1.0\n"
with patch("subprocess.check_output", return_value=mock_output):
result = _get_runtime_version("clang-tidy")
assert result == "20.1.0"
@pytest.mark.benchmark
def test_get_runtime_version_exception():
"""Test _get_runtime_version when subprocess fails."""
with patch(
"subprocess.check_output",
side_effect=subprocess.CalledProcessError(1, ["clang-format"]),
):
result = _get_runtime_version("clang-format")
assert result is None
@pytest.mark.benchmark
def test_get_runtime_version_clang_tidy_single_line():
"""Test _get_runtime_version for clang-tidy with single line output."""
mock_output = "LLVM version 20.1.0\n"
with patch("subprocess.check_output", return_value=mock_output):
result = _get_runtime_version("clang-tidy")
assert result is None # Should return None for single line
# Tests for _install_tool
@pytest.mark.benchmark
def test_install_tool_success():
"""Test _install_tool successful installation."""
mock_path = "/usr/bin/clang-format"
with (
patch("subprocess.check_call") as mock_check_call,
patch("shutil.which", return_value=mock_path),
):
result = _install_tool("clang-format", "20.1.7")
assert result == mock_path
mock_check_call.assert_called_once_with(
[sys.executable, "-m", "pip", "install", "clang-format==20.1.7"]
)
@pytest.mark.benchmark
def test_install_tool_failure():
"""Test _install_tool when pip install fails."""
with (
patch(
"subprocess.check_call",
side_effect=subprocess.CalledProcessError(1, ["pip"]),
),
patch("cpp_linter_hooks.util.LOG") as mock_log,
):
result = _install_tool("clang-format", "20.1.7")
assert result is None
mock_log.error.assert_called_once_with(
"Failed to install %s==%s", "clang-format", "20.1.7"
)
@pytest.mark.benchmark
def test_install_tool_success_but_not_found():
"""Test _install_tool when install succeeds but tool not found in PATH."""
with patch("subprocess.check_call"), patch("shutil.which", return_value=None):
result = _install_tool("clang-format", "20.1.7")
assert result is None
# Tests for _resolve_install
@pytest.mark.benchmark
def test_resolve_install_tool_already_installed_correct_version():
"""Test _resolve_install when tool is already installed with correct version."""
mock_path = "/usr/bin/clang-format"
with (
patch("shutil.which", return_value=mock_path),
patch("cpp_linter_hooks.util._get_runtime_version", return_value="20.1.7"),
):
result = _resolve_install("clang-format", "20.1.7")
assert result == Path(mock_path)
@pytest.mark.benchmark
def test_resolve_install_tool_version_mismatch():
"""Test _resolve_install when tool has wrong version."""
mock_path = "/usr/bin/clang-format"
with (
patch("shutil.which", return_value=mock_path),
patch("cpp_linter_hooks.util._get_runtime_version", return_value="18.1.8"),
patch(
"cpp_linter_hooks.util._install_tool", return_value=Path(mock_path)
) as mock_install,
patch("cpp_linter_hooks.util.LOG") as mock_log,
):
result = _resolve_install("clang-format", "20.1.7")
assert result == Path(mock_path)
mock_install.assert_called_once_with("clang-format", "20.1.7")
mock_log.info.assert_called_once_with(
"%s version mismatch (%s != %s), reinstalling...",
"clang-format",
"18.1.8",
"20.1.7",
)
@pytest.mark.benchmark
def test_resolve_install_tool_not_installed():
"""Test _resolve_install when tool is not installed."""
with (
patch("shutil.which", return_value=None),
patch(
"cpp_linter_hooks.util._install_tool",
return_value=Path("/usr/bin/clang-format"),
) as mock_install,
):
result = _resolve_install("clang-format", "20.1.7")
assert result == Path("/usr/bin/clang-format")
mock_install.assert_called_once_with("clang-format", "20.1.7")
@pytest.mark.benchmark
def test_resolve_install_no_version_specified():
"""Test _resolve_install when no version is specified."""
with (
patch("shutil.which", return_value=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")
mock_install.assert_called_once_with(
"clang-format", DEFAULT_CLANG_FORMAT_VERSION
)
@pytest.mark.benchmark
def test_resolve_install_invalid_version():
"""Test _resolve_install with invalid version."""
with (
patch("shutil.which", return_value=None),
patch(
"cpp_linter_hooks.util._install_tool",
return_value=Path("/usr/bin/clang-format"),
) as mock_install,
):
result = _resolve_install("clang-format", "invalid.version")
assert result == Path("/usr/bin/clang-format")
# Should fallback to default version
mock_install.assert_called_once_with(
"clang-format", DEFAULT_CLANG_FORMAT_VERSION
)
# Tests for ensure_installed edge cases
@pytest.mark.benchmark
def test_ensure_installed_version_mismatch(caplog):
"""Test ensure_installed with version mismatch scenario."""
mock_path = "/usr/bin/clang-format"
with (
patch("shutil.which", return_value=mock_path),
patch("cpp_linter_hooks.util._get_runtime_version", return_value="18.1.8"),
patch("cpp_linter_hooks.util._install_tool", return_value=Path(mock_path)),
):
caplog.clear()
caplog.set_level(logging.INFO, logger="cpp_linter_hooks.util")
result = ensure_installed("clang-format", "20.1.7")
assert result == "clang-format"
# Should log version mismatch
assert any("version mismatch" in record.message for record in caplog.records)
@pytest.mark.benchmark
def test_ensure_installed_no_runtime_version():
"""Test ensure_installed when runtime version cannot be determined."""
mock_path = "/usr/bin/clang-format"
with (
patch("shutil.which", return_value=mock_path),
patch("cpp_linter_hooks.util._get_runtime_version", return_value=None),
):
result = ensure_installed("clang-format", "20.1.7")
assert result == "clang-format"
# Tests for constants and defaults
@pytest.mark.benchmark
def test_default_versions():
"""Test that default versions are set correctly."""
assert DEFAULT_CLANG_FORMAT_VERSION is not None
assert DEFAULT_CLANG_TIDY_VERSION is not None
assert isinstance(DEFAULT_CLANG_FORMAT_VERSION, str)
assert isinstance(DEFAULT_CLANG_TIDY_VERSION, str)
@pytest.mark.benchmark
def test_version_lists_not_empty():
"""Test that version lists are not empty."""
assert len(CLANG_FORMAT_VERSIONS) > 0
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)