-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_util.py
More file actions
310 lines (253 loc) · 10.1 KB
/
test_util.py
File metadata and controls
310 lines (253 loc) · 10.1 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
import pytest
from unittest.mock import patch
from pathlib import Path
import subprocess
import sys
from cpp_linter_hooks.util import (
get_version_from_dependency,
_resolve_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"]
# 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": {
"dependencies": [
"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": {"dependencies": ["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 _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"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
@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"),
):
result = _install_tool("clang-format", "20.1.7")
assert result is None
@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),
):
result = _resolve_install("clang-format", "20.1.7")
assert Path(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._install_tool", return_value=Path(mock_path)
) as mock_install,
):
result = _resolve_install("clang-format", "20.1.7")
assert result == Path(mock_path)
mock_install.assert_called_once_with("clang-format", "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 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_get_default_versions_script():
"""Test that the get_default_versions script works correctly."""
import subprocess
import sys
from pathlib import Path
script_path = Path(__file__).parent.parent / "scripts" / "get_default_versions.py"
assert script_path.exists(), "get_default_versions.py script should exist"
result = subprocess.run(
[sys.executable, str(script_path)],
capture_output=True,
text=True
)
assert result.returncode == 0, f"Script failed with: {result.stderr}"
output_lines = result.stdout.strip().split('\n')
# Should have 4 lines of output
assert len(output_lines) >= 4
# Check that it contains expected format
assert any(line.startswith("Default clang-format version:") for line in output_lines)
assert any(line.startswith("Default clang-tidy version:") for line in output_lines)
assert any(line.startswith("CLANG_FORMAT_VERSION=") for line in output_lines)
assert any(line.startswith("CLANG_TIDY_VERSION=") for line in output_lines)
@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)