File: test_build.py

package info (click to toggle)
python-hatch-mypyc 0.16.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: python: 1,025; makefile: 5
file content (374 lines) | stat: -rw-r--r-- 12,569 bytes parent folder | download
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
# SPDX-FileCopyrightText: 2021-present Ofek Lev <oss@ofek.dev>
#
# SPDX-License-Identifier: MIT
import os
import zipfile

from packaging.tags import sys_tags

from .utils import build_project

best_matching_tag = next(iter(
    t for t in sys_tags() if 'manylinux' not in t.platform and 'musllinux' not in t.platform
))


def test_target_not_wheel(new_project):
    project_file = new_project / 'pyproject.toml'
    contents = project_file.read_text(encoding='utf-8')
    contents = contents.replace('[tool.hatch.build.targets.wheel.hooks.mypyc]', '[tool.hatch.build.hooks.mypyc]')
    project_file.write_text(contents, encoding='utf-8')

    build_project('-s')

    build_dir = new_project / 'dist'
    assert build_dir.is_dir()

    artifacts = list(build_dir.iterdir())
    assert len(artifacts) == 1

    assert artifacts[0].name.endswith('.tar.gz')


def test_no_exclusion(new_project, compiled_extension):
    build_project()

    build_dir = new_project / 'dist'
    assert build_dir.is_dir()

    artifacts = list(build_dir.iterdir())
    assert len(artifacts) == 1
    wheel_file = artifacts[0]

    assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'

    extraction_directory = new_project.parent / '_archive'
    extraction_directory.mkdir()

    with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
        zip_archive.extractall(str(extraction_directory))

    root_paths = list(extraction_directory.iterdir())
    assert len(root_paths) == 3
    assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2
    assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1

    metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
    assert metadata_directory.is_dir()

    wheel_metadata_file = metadata_directory / 'WHEEL'
    assert wheel_metadata_file.is_file()
    assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')

    extracted_package_dir = extraction_directory / 'my_app'
    assert extracted_package_dir.is_dir()

    distributed_files = list(extracted_package_dir.iterdir())
    assert len(distributed_files) == 5

    root_files = 0
    fibonacci_files = 0
    for distributed_file in distributed_files:
        if distributed_file.name.startswith('__init__'):
            root_files += 1
        elif distributed_file.name.startswith('fib'):
            fibonacci_files += 1

    assert root_files == 2
    assert fibonacci_files == 2


def test_exclusion(new_project, compiled_extension):
    project_file = new_project / 'pyproject.toml'
    contents = project_file.read_text(encoding='utf-8')
    contents += '\nexclude = ["__main__.py"]'
    project_file.write_text(contents, encoding='utf-8')

    package_main = new_project / 'my_app' / '__main__.py'
    package_main.write_text(
        """\
if __name__ == '__main__':
    from .fib import fib
    print(fib())
""",
        encoding='utf-8',
    )

    build_project()

    build_dir = new_project / 'dist'
    assert build_dir.is_dir()

    artifacts = list(build_dir.iterdir())
    assert len(artifacts) == 1
    wheel_file = artifacts[0]

    assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'

    extraction_directory = new_project.parent / '_archive'
    extraction_directory.mkdir()

    with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
        zip_archive.extractall(str(extraction_directory))

    root_paths = list(extraction_directory.iterdir())
    assert len(root_paths) == 3
    assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2
    assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1

    metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
    assert metadata_directory.is_dir()

    wheel_metadata_file = metadata_directory / 'WHEEL'
    assert wheel_metadata_file.is_file()
    assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')

    extracted_package_dir = extraction_directory / 'my_app'
    assert extracted_package_dir.is_dir()

    distributed_files = list(extracted_package_dir.iterdir())
    assert len(distributed_files) == 6

    root_files = 0
    fibonacci_files = 0
    for distributed_file in distributed_files:
        if distributed_file.name.startswith('__init__'):
            root_files += 1
        elif distributed_file.name.startswith('fib'):
            fibonacci_files += 1

    assert root_files == 2
    assert fibonacci_files == 2


def test_separation(new_project):
    project_file = new_project / 'pyproject.toml'
    contents = project_file.read_text(encoding='utf-8')
    contents += '\noptions = { separate = true }'
    project_file.write_text(contents, encoding='utf-8')

    build_project()

    build_dir = new_project / 'dist'
    assert build_dir.is_dir()

    artifacts = list(build_dir.iterdir())
    assert len(artifacts) == 1
    wheel_file = artifacts[0]

    assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'

    extraction_directory = new_project.parent / '_archive'
    extraction_directory.mkdir()

    with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
        zip_archive.extractall(str(extraction_directory))

    root_paths = list(extraction_directory.iterdir())
    assert len(root_paths) == 2
    assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2

    metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
    assert metadata_directory.is_dir()

    wheel_metadata_file = metadata_directory / 'WHEEL'
    assert wheel_metadata_file.is_file()
    assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')

    extracted_package_dir = extraction_directory / 'my_app'
    assert extracted_package_dir.is_dir()

    distributed_files = list(extracted_package_dir.iterdir())
    assert len(distributed_files) == 5

    root_files = 0
    fibonacci_files = 0
    for distributed_file in distributed_files:
        if distributed_file.name.startswith('__init__'):
            root_files += 1
        elif distributed_file.name.startswith('fib'):
            fibonacci_files += 1

    assert root_files == 1
    assert fibonacci_files == 3


def test_src_layout(new_project, compiled_extension):
    project_file = new_project / 'pyproject.toml'
    contents = project_file.read_text(encoding='utf-8')
    contents = contents.replace('my_app', 'src/my_app')
    project_file.write_text(contents, encoding='utf-8')
    package_dir = new_project / 'my_app'
    src_dir = new_project / 'src'
    src_dir.mkdir()
    package_dir.replace(src_dir / 'my_app')

    build_project()

    build_dir = new_project / 'dist'
    assert build_dir.is_dir()

    artifacts = list(build_dir.iterdir())
    assert len(artifacts) == 1
    wheel_file = artifacts[0]

    assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'

    extraction_directory = new_project.parent / '_archive'
    extraction_directory.mkdir()

    with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
        zip_archive.extractall(str(extraction_directory))

    root_paths = list(extraction_directory.iterdir())
    assert len(root_paths) == 3
    assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2
    assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1

    metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
    assert metadata_directory.is_dir()

    wheel_metadata_file = metadata_directory / 'WHEEL'
    assert wheel_metadata_file.is_file()
    assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')

    extracted_package_dir = extraction_directory / 'my_app'
    assert extracted_package_dir.is_dir()

    distributed_files = list(extracted_package_dir.iterdir())
    assert len(distributed_files) == 5

    root_files = 0
    fibonacci_files = 0
    for distributed_file in distributed_files:
        if distributed_file.name.startswith('__init__'):
            root_files += 1
        elif distributed_file.name.startswith('fib'):
            fibonacci_files += 1

    assert root_files == 2
    assert fibonacci_files == 2


def test_dependency(new_project, compiled_extension):
    # Has py.typed and no transitive dependencies
    dependency = 'charset_normalizer'

    project_file = new_project / 'pyproject.toml'
    contents = project_file.read_text(encoding='utf-8')
    contents = contents.replace('dependencies = []', f'dependencies = ["{dependency}"]')
    contents += '\nrequire-runtime-dependencies = true'
    project_file.write_text(contents, encoding='utf-8')

    core_logic_file = new_project / 'my_app' / 'fib.py'
    core_logic_file.write_text(
        f"""\
import {dependency}
{core_logic_file.read_text()}
""",
        encoding='utf-8',
    )

    build_project()

    build_dir = new_project / 'dist'
    assert build_dir.is_dir()

    artifacts = list(build_dir.iterdir())
    assert len(artifacts) == 1
    wheel_file = artifacts[0]

    assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'

    extraction_directory = new_project.parent / '_archive'
    extraction_directory.mkdir()

    with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
        zip_archive.extractall(str(extraction_directory))

    root_paths = list(extraction_directory.iterdir())
    assert len(root_paths) == 3
    assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2
    assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1

    metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
    assert metadata_directory.is_dir()

    wheel_metadata_file = metadata_directory / 'WHEEL'
    assert wheel_metadata_file.is_file()
    assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')

    extracted_package_dir = extraction_directory / 'my_app'
    assert extracted_package_dir.is_dir()

    distributed_files = list(extracted_package_dir.iterdir())
    assert len(distributed_files) == 5

    root_files = 0
    fibonacci_files = 0
    for distributed_file in distributed_files:
        if distributed_file.name.startswith('__init__'):
            root_files += 1
        elif distributed_file.name.startswith('fib'):
            fibonacci_files += 1

    assert root_files == 2
    assert fibonacci_files == 2


def test_build_dir(new_project, compiled_extension):
    build_dir = os.urandom(8).hex()

    project_file = new_project / 'pyproject.toml'
    contents = project_file.read_text(encoding='utf-8')
    contents += f'\nbuild-dir = "{build_dir}"'
    project_file.write_text(contents, encoding='utf-8')

    build_project()

    build_dir = new_project / 'dist'
    assert build_dir.is_dir()

    artifacts = list(build_dir.iterdir())
    assert len(artifacts) == 1
    wheel_file = artifacts[0]

    assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl'

    extraction_directory = new_project.parent / '_archive'
    extraction_directory.mkdir()

    with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive:
        zip_archive.extractall(str(extraction_directory))

    root_paths = list(extraction_directory.iterdir())
    assert len(root_paths) == 3
    assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2
    assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1

    metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info'
    assert metadata_directory.is_dir()

    wheel_metadata_file = metadata_directory / 'WHEEL'
    assert wheel_metadata_file.is_file()
    assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8')

    extracted_package_dir = extraction_directory / 'my_app'
    assert extracted_package_dir.is_dir()

    distributed_files = list(extracted_package_dir.iterdir())
    assert len(distributed_files) == 5

    root_files = 0
    fibonacci_files = 0
    for distributed_file in distributed_files:
        if distributed_file.name.startswith('__init__'):
            root_files += 1
        elif distributed_file.name.startswith('fib'):
            fibonacci_files += 1

    assert root_files == 2
    assert fibonacci_files == 2

    intermediate_build_dir = new_project / build_dir
    assert intermediate_build_dir.is_dir()