Debian Codegraph @

tests/test_build.py

raw ◀ ▶
2 Debian patches touch this file
1 # SPDX-FileCopyrightText: 2021-present Ofek Lev <[email protected]>
2 #
3 # SPDX-License-Identifier: MIT
4 import os
5 import zipfile
6
7 from packaging.tags import sys_tags
•8 from packaging.version import Version
9 from mypy.version import __version__ as mypy_version
10
11 from .utils import build_project
12
•13 best_matching_tag = next(iter(
14 t for t in sys_tags() if 'manylinux' not in t.platform and 'musllinux' not in t.platform
15 ))
16
•17 mypy_v1_20_2_plus = Version(mypy_version) >= Version("1.20.2")
18
19
20 def test_target_not_wheel(new_project):
21 project_file = new_project / 'pyproject.toml'
22 contents = project_file.read_text(encoding='utf-8')
23 contents = contents.replace('[tool.hatch.build.targets.wheel.hooks.mypyc]', '[tool.hatch.build.hooks.mypyc]')
24 project_file.write_text(contents, encoding='utf-8')
25
26 build_project('-s')
27
28 build_dir = new_project / 'dist'
29 assert build_dir.is_dir()
30
31 artifacts = list(build_dir.iterdir())
32 assert len(artifacts) == 1
33
34 assert artifacts[0].name.endswith('.tar.gz')
35
36
37 def test_no_exclusion(new_project, compiled_extension):
38 build_project()
39
40 build_dir = new_project / 'dist'
41 assert build_dir.is_dir()
42
43 artifacts = list(build_dir.iterdir())
44 assert len(artifacts) == 1
45 wheel_file = artifacts[0]
46
47 assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'
48
49 extraction_directory = new_project.parent / '_archive'
50 extraction_directory.mkdir()
51
52 with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
53 zip_archive.extractall(str(extraction_directory))
54
55 root_paths = list(extraction_directory.iterdir())
56 assert len(root_paths) == 3
57 assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2
58 assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1
59
60 metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
61 assert metadata_directory.is_dir()
62
63 wheel_metadata_file = metadata_directory / 'WHEEL'
64 assert wheel_metadata_file.is_file()
65 assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')
66
67 extracted_package_dir = extraction_directory / 'my_app'
68 assert extracted_package_dir.is_dir()
69
70 distributed_files = list(extracted_package_dir.iterdir())
71 assert len(distributed_files) == 5
72
73 root_files = 0
74 fibonacci_files = 0
75 for distributed_file in distributed_files:
76 if distributed_file.name.startswith('__init__'):
77 root_files += 1
78 elif distributed_file.name.startswith('fib'):
79 fibonacci_files += 1
80
81 assert root_files == 2
82 assert fibonacci_files == 2
83
84
85 def test_exclusion(new_project, compiled_extension):
86 project_file = new_project / 'pyproject.toml'
87 contents = project_file.read_text(encoding='utf-8')
88 contents += '\nexclude = ["__main__.py"]'
89 project_file.write_text(contents, encoding='utf-8')
90
91 package_main = new_project / 'my_app' / '__main__.py'
92 package_main.write_text(
93 """\
94 if __name__ == '__main__':
95 from .fib import fib
96 print(fib())
97 """,
98 encoding='utf-8',
99 )
100
101 build_project()
102
103 build_dir = new_project / 'dist'
104 assert build_dir.is_dir()
105
106 artifacts = list(build_dir.iterdir())
107 assert len(artifacts) == 1
108 wheel_file = artifacts[0]
109
110 assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'
111
112 extraction_directory = new_project.parent / '_archive'
113 extraction_directory.mkdir()
114
115 with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
116 zip_archive.extractall(str(extraction_directory))
117
118 root_paths = list(extraction_directory.iterdir())
119 assert len(root_paths) == 3
120 assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2
121 assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1
122
123 metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
124 assert metadata_directory.is_dir()
125
126 wheel_metadata_file = metadata_directory / 'WHEEL'
127 assert wheel_metadata_file.is_file()
128 assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')
129
130 extracted_package_dir = extraction_directory / 'my_app'
131 assert extracted_package_dir.is_dir()
132
133 distributed_files = list(extracted_package_dir.iterdir())
134 assert len(distributed_files) == 6
135
136 root_files = 0
137 fibonacci_files = 0
138 for distributed_file in distributed_files:
139 if distributed_file.name.startswith('__init__'):
140 root_files += 1
141 elif distributed_file.name.startswith('fib'):
142 fibonacci_files += 1
143
144 assert root_files == 2
145 assert fibonacci_files == 2
146
147
148 def test_separation(new_project):
149 project_file = new_project / 'pyproject.toml'
150 contents = project_file.read_text(encoding='utf-8')
151 contents += '\noptions = { separate = true }'
152 project_file.write_text(contents, encoding='utf-8')
153
154 build_project()
155
156 build_dir = new_project / 'dist'
157 assert build_dir.is_dir()
158
159 artifacts = list(build_dir.iterdir())
160 assert len(artifacts) == 1
161 wheel_file = artifacts[0]
162
163 assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'
164
165 extraction_directory = new_project.parent / '_archive'
166 extraction_directory.mkdir()
167
168 with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
169 zip_archive.extractall(str(extraction_directory))
170
171 root_paths = list(extraction_directory.iterdir())
172 assert len(root_paths) == 2
173 assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2
174
175 metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
176 assert metadata_directory.is_dir()
177
178 wheel_metadata_file = metadata_directory / 'WHEEL'
179 assert wheel_metadata_file.is_file()
180 assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')
181
182 extracted_package_dir = extraction_directory / 'my_app'
183 assert extracted_package_dir.is_dir()
184
185 distributed_files = list(extracted_package_dir.iterdir())
•186 assert len(distributed_files) == (6 if mypy_v1_20_2_plus else 5), distributed_files
187
•188 root_files = []
189 fibonacci_files = []
190 for distributed_file in distributed_files:
191 if distributed_file.name.startswith('__init__'):
•192 root_files.append(distributed_file)
193 elif distributed_file.name.startswith('fib'):
•194 fibonacci_files.append(distributed_file)
195
•196 assert len(root_files) == (2 if mypy_v1_20_2_plus else 1), root_files
197 assert len(fibonacci_files) == 3, fibonacci_files
198
199
200 def test_src_layout(new_project, compiled_extension):
201 project_file = new_project / 'pyproject.toml'
202 contents = project_file.read_text(encoding='utf-8')
203 contents = contents.replace('my_app', 'src/my_app')
204 project_file.write_text(contents, encoding='utf-8')
205 package_dir = new_project / 'my_app'
206 src_dir = new_project / 'src'
207 src_dir.mkdir()
208 package_dir.replace(src_dir / 'my_app')
209
210 build_project()
211
212 build_dir = new_project / 'dist'
213 assert build_dir.is_dir()
214
215 artifacts = list(build_dir.iterdir())
216 assert len(artifacts) == 1
217 wheel_file = artifacts[0]
218
219 assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'
220
221 extraction_directory = new_project.parent / '_archive'
222 extraction_directory.mkdir()
223
224 with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
225 zip_archive.extractall(str(extraction_directory))
226
227 root_paths = list(extraction_directory.iterdir())
228 assert len(root_paths) == 3
229 assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2
230 assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1
231
232 metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
233 assert metadata_directory.is_dir()
234
235 wheel_metadata_file = metadata_directory / 'WHEEL'
236 assert wheel_metadata_file.is_file()
237 assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')
238
239 extracted_package_dir = extraction_directory / 'my_app'
240 assert extracted_package_dir.is_dir()
241
242 distributed_files = list(extracted_package_dir.iterdir())
243 assert len(distributed_files) == 5
244
245 root_files = 0
246 fibonacci_files = 0
247 for distributed_file in distributed_files:
248 if distributed_file.name.startswith('__init__'):
249 root_files += 1
250 elif distributed_file.name.startswith('fib'):
251 fibonacci_files += 1
252
253 assert root_files == 2
254 assert fibonacci_files == 2
255
256
257 def test_dependency(new_project, compiled_extension):
258 # Has py.typed and no transitive dependencies
259 dependency = 'charset_normalizer'
260
261 project_file = new_project / 'pyproject.toml'
262 contents = project_file.read_text(encoding='utf-8')
263 contents = contents.replace('dependencies = []', f'dependencies = ["{dependency}"]')
264 contents += '\nrequire-runtime-dependencies = true'
265 project_file.write_text(contents, encoding='utf-8')
266
267 core_logic_file = new_project / 'my_app' / 'fib.py'
268 core_logic_file.write_text(
269 f"""\
270 import {dependency}
271 {core_logic_file.read_text()}
272 """,
273 encoding='utf-8',
274 )
275
276 build_project()
277
278 build_dir = new_project / 'dist'
279 assert build_dir.is_dir()
280
281 artifacts = list(build_dir.iterdir())
282 assert len(artifacts) == 1
283 wheel_file = artifacts[0]
284
285 assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'
286
287 extraction_directory = new_project.parent / '_archive'
288 extraction_directory.mkdir()
289
290 with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
291 zip_archive.extractall(str(extraction_directory))
292
293 root_paths = list(extraction_directory.iterdir())
294 assert len(root_paths) == 3
295 assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2
296 assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1
297
298 metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
299 assert metadata_directory.is_dir()
300
301 wheel_metadata_file = metadata_directory / 'WHEEL'
302 assert wheel_metadata_file.is_file()
303 assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')
304
305 extracted_package_dir = extraction_directory / 'my_app'
306 assert extracted_package_dir.is_dir()
307
308 distributed_files = list(extracted_package_dir.iterdir())
309 assert len(distributed_files) == 5
310
311 root_files = 0
312 fibonacci_files = 0
313 for distributed_file in distributed_files:
314 if distributed_file.name.startswith('__init__'):
315 root_files += 1
316 elif distributed_file.name.startswith('fib'):
317 fibonacci_files += 1
318
319 assert root_files == 2
320 assert fibonacci_files == 2
321
322
323 def test_build_dir(new_project, compiled_extension):
324 build_dir = os.urandom(8).hex()
325
326 project_file = new_project / 'pyproject.toml'
327 contents = project_file.read_text(encoding='utf-8')
328 contents += f'\nbuild-dir = "{build_dir}"'
329 project_file.write_text(contents, encoding='utf-8')
330
331 build_project()
332
333 build_dir = new_project / 'dist'
334 assert build_dir.is_dir()
335
336 artifacts = list(build_dir.iterdir())
337 assert len(artifacts) == 1
338 wheel_file = artifacts[0]
339
340 assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'
341
342 extraction_directory = new_project.parent / '_archive'
343 extraction_directory.mkdir()
344
345 with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
346 zip_archive.extractall(str(extraction_directory))
347
348 root_paths = list(extraction_directory.iterdir())
349 assert len(root_paths) == 3
350 assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2
351 assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1
352
353 metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
354 assert metadata_directory.is_dir()
355
356 wheel_metadata_file = metadata_directory / 'WHEEL'
357 assert wheel_metadata_file.is_file()
358 assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')
359
360 extracted_package_dir = extraction_directory / 'my_app'
361 assert extracted_package_dir.is_dir()
362
363 distributed_files = list(extracted_package_dir.iterdir())
364 assert len(distributed_files) == 5
365
366 root_files = 0
367 fibonacci_files = 0
368 for distributed_file in distributed_files:
369 if distributed_file.name.startswith('__init__'):
370 root_files += 1
371 elif distributed_file.name.startswith('fib'):
372 fibonacci_files += 1
373
374 assert root_files == 2
375 assert fibonacci_files == 2
376
377 intermediate_build_dir = new_project / build_dir
378 assert intermediate_build_dir.is_dir()