forked from synodic/cppython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_builder.py
More file actions
225 lines (185 loc) · 8.19 KB
/
test_builder.py
File metadata and controls
225 lines (185 loc) · 8.19 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
"""Unit tests for Conan builder functionality."""
from pathlib import Path
from textwrap import dedent
import pytest
from cppython.plugins.conan.builder import Builder
from cppython.plugins.conan.schema import ConanDependency, ConanfileGenerationData, ConanVersion
class TestBuilder:
"""Test the Conan Builder class."""
@pytest.fixture
def builder(self) -> Builder:
"""Create a Builder instance for testing."""
return Builder()
def test_mixed_dependencies(self, builder: Builder, tmp_path: Path) -> None:
"""Test base conanfile with both regular and test dependencies."""
base_file = tmp_path / 'conanfile_base.py'
dependencies = [
ConanDependency(name='boost', version=ConanVersion.from_string('1.80.0')),
]
dependency_groups = {
'test': [
ConanDependency(name='gtest', version=ConanVersion.from_string('1.14.0')),
]
}
builder._create_base_conanfile(base_file, dependencies, dependency_groups)
assert base_file.exists()
content = base_file.read_text(encoding='utf-8')
assert 'self.requires("boost/1.80.0")' in content
assert 'self.test_requires("gtest/1.14.0")' in content
def test_creates_both_files(self, builder: Builder, tmp_path: Path) -> None:
"""Test generate_conanfile creates both base and user files."""
dependencies = [
ConanDependency(name='boost', version=ConanVersion.from_string('1.80.0')),
]
dependency_groups = {}
data = ConanfileGenerationData(
dependencies=dependencies,
dependency_groups=dependency_groups,
name='test-project',
version='1.0.0',
)
builder.generate_conanfile(
directory=tmp_path,
data=data,
)
base_file = tmp_path / 'conanfile_base.py'
conan_file = tmp_path / 'conanfile.py'
assert base_file.exists()
assert conan_file.exists()
def test_regenerates_base_file(self, builder: Builder, tmp_path: Path) -> None:
"""Test base file is always regenerated with new dependencies."""
initial_dependencies = [
ConanDependency(name='boost', version=ConanVersion.from_string('1.80.0')),
]
initial_data = ConanfileGenerationData(
dependencies=initial_dependencies,
dependency_groups={},
name='test-project',
version='1.0.0',
)
builder.generate_conanfile(
directory=tmp_path,
data=initial_data,
)
base_file = tmp_path / 'conanfile_base.py'
initial_content = base_file.read_text(encoding='utf-8')
assert 'boost/1.80.0' in initial_content
updated_dependencies = [
ConanDependency(name='zlib', version=ConanVersion.from_string('1.2.13')),
]
updated_data = ConanfileGenerationData(
dependencies=updated_dependencies,
dependency_groups={},
name='test-project',
version='1.0.0',
)
builder.generate_conanfile(
directory=tmp_path,
data=updated_data,
)
updated_content = base_file.read_text(encoding='utf-8')
assert 'zlib/1.2.13' in updated_content
assert 'boost/1.80.0' not in updated_content
def test_preserves_user_file(self, builder: Builder, tmp_path: Path) -> None:
"""Test user conanfile is never modified once created."""
conan_file = tmp_path / 'conanfile.py'
custom_content = dedent("""
from conanfile_base import CPPythonBase
class CustomPackage(CPPythonBase):
name = "custom"
version = "1.0.0"
def requirements(self):
super().requirements()
self.requires("custom-lib/1.0.0")
""")
conan_file.write_text(custom_content)
dependencies = [
ConanDependency(name='boost', version=ConanVersion.from_string('1.80.0')),
]
data = ConanfileGenerationData(
dependencies=dependencies,
dependency_groups={},
name='new-name',
version='2.0.0',
)
builder.generate_conanfile(
directory=tmp_path,
data=data,
)
final_content = conan_file.read_text()
assert final_content == custom_content
assert 'CustomPackage' in final_content
assert 'custom-lib/1.0.0' in final_content
def test_inheritance_chain(self, builder: Builder, tmp_path: Path) -> None:
"""Test complete inheritance chain from base to user file."""
dependencies = [
ConanDependency(name='boost', version=ConanVersion.from_string('1.80.0')),
ConanDependency(name='zlib', version=ConanVersion.from_string('1.2.13')),
]
dependency_groups = {
'test': [
ConanDependency(name='gtest', version=ConanVersion.from_string('1.14.0')),
]
}
data = ConanfileGenerationData(
dependencies=dependencies,
dependency_groups=dependency_groups,
name='test-project',
version='1.0.0',
)
builder.generate_conanfile(
directory=tmp_path,
data=data,
)
base_content = (tmp_path / 'conanfile_base.py').read_text(encoding='utf-8')
user_content = (tmp_path / 'conanfile.py').read_text(encoding='utf-8')
assert 'self.requires("boost/1.80.0")' in base_content
assert 'self.requires("zlib/1.2.13")' in base_content
assert 'self.test_requires("gtest/1.14.0")' in base_content
assert 'from conanfile_base import CPPythonBase' in user_content
assert 'class TestProjectPackage(CPPythonBase):' in user_content
assert 'super().requirements()' in user_content
assert 'super().build_requirements()' in user_content
def test_cmake_binary_configure(self, builder: Builder, tmp_path: Path) -> None:
"""Test that cmake_binary generates configure() with forward slashes."""
base_file = tmp_path / 'conanfile_base.py'
cmake_path = Path('C:/Program Files/CMake/bin/cmake.exe')
builder._create_base_conanfile(base_file, [], {}, cmake_binary=cmake_path)
content = base_file.read_text(encoding='utf-8')
assert 'def configure(self):' in content
assert 'self.conf.define("tools.cmake:cmake_program"' in content
assert 'C:/Program Files/CMake/bin/cmake.exe' in content
assert '\\' not in content.split('tools.cmake:cmake_program')[1].split('"')[1]
def test_no_cmake_binary(self, builder: Builder, tmp_path: Path) -> None:
"""Test that no cmake_binary means no configure() method."""
base_file = tmp_path / 'conanfile_base.py'
builder._create_base_conanfile(base_file, [], {}, cmake_binary=None)
content = base_file.read_text(encoding='utf-8')
assert 'def configure(self):' not in content
@pytest.mark.parametrize(
('venv_cmake', 'expect_configure'),
[
('/path/to/venv/bin/cmake', True),
(None, False),
],
)
def test_cmake_binary_venv_fallback(
self,
builder: Builder,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
venv_cmake: str | None,
expect_configure: bool,
) -> None:
"""Test venv cmake fallback when cmake_binary is default."""
monkeypatch.setattr('cppython.plugins.conan.builder.shutil.which', lambda _: venv_cmake)
data = ConanfileGenerationData(
dependencies=[],
dependency_groups={},
name='test-project',
version='1.0.0',
cmake_binary='cmake',
)
builder.generate_conanfile(directory=tmp_path, data=data)
content = (tmp_path / 'conanfile_base.py').read_text(encoding='utf-8')
assert ('def configure(self):' in content) == expect_configure