Debian Codegraph @

tests/unit/test_wheeltools.py

raw
1 from __future__ import annotations
2
3 import re
4 import shutil
5 import tempfile
6 import zipfile
7 from pathlib import Path
8
9 import pytest
10
11 from auditwheel._vendor.wheel.pkginfo import read_pkg_info
12 from auditwheel.architecture import Architecture
13 from auditwheel.error import NonPlatformWheelError
14 from auditwheel.libc import Libc
15 from auditwheel.wheeltools import (
16 InWheelCtx,
17 WheelToolsError,
18 add_platforms,
19 android_api_level,
20 get_wheel_architecture,
21 get_wheel_libc,
22 get_wheel_platforms,
23 )
24
25 HERE = Path(__file__).parent.resolve()
26
27
28 @pytest.mark.parametrize(
29 ("filename", "expected"),
30 [(f"foo-1.0-py3-none-linux_{arch}.whl", arch) for arch in Architecture]
31 + [
32 ("foo-1.0-py3-none-linux_x86_64.manylinux1_x86_64.whl", Architecture.x86_64),
33 ("foo-1.0-py3-none-android_21_x86_64.whl", Architecture.x86_64),
34 ("foo-1.0-py3-none-android_21_arm64_v8a.whl", Architecture.aarch64),
35 ],
36 )
37 def test_get_wheel_architecture(filename: str, expected: Architecture) -> None:
38 arch = get_wheel_architecture(filename)
39 assert arch == expected.baseline
40
41
42 def test_get_wheel_architecture_unknown() -> None:
43 with pytest.raises(WheelToolsError, match=re.escape("unknown architecture")):
44 get_wheel_architecture("foo-1.0-py3-none-linux_mipsel.whl")
45
46
47 def test_get_wheel_architecture_pure() -> None:
48 with pytest.raises(NonPlatformWheelError):
49 get_wheel_architecture("foo-1.0-py3-none-any.whl")
50
51
52 @pytest.mark.parametrize(
53 "filename",
54 [
55 "foo-1.0-py3-none-linux_x86_64.linux_aarch64.whl",
56 "foo-1.0-py3-none-linux_x86_64.linux_mipsel.whl",
57 "foo-1.0-py3-none-linux_x86_64.any.whl",
58 ],
59 )
60 def test_get_wheel_architecture_multiple(filename: str) -> None:
61 match = re.escape("multiple architectures are not supported")
62 with pytest.raises(WheelToolsError, match=match):
63 get_wheel_architecture(filename)
64
65
66 @pytest.mark.parametrize(
67 ("filename", "expected"),
68 [
69 ("foo-1.0-py3-none-manylinux1_x86_64.whl", Libc.GLIBC),
70 ("foo-1.0-py3-none-manylinux1_x86_64.manylinux2010_x86_64.whl", Libc.GLIBC),
71 ("foo-1.0-py3-none-musllinux_1_1_x86_64.whl", Libc.MUSL),
72 ("foo-1.0-py3-none-android_24_arm64_v8a.whl", Libc.ANDROID),
73 ],
74 )
75 def test_get_wheel_libc(filename: str, expected: Libc) -> None:
76 libc = get_wheel_libc(filename)
77 assert libc == expected
78
79
80 @pytest.mark.parametrize(
81 "filename",
82 ["foo-1.0-py3-none-any.whl", "foo-1.0-py3-none-something.whl"],
83 )
84 def test_get_wheel_libc_unknown(filename: str) -> None:
85 with pytest.raises(WheelToolsError, match=re.escape("unknown libc used")):
86 get_wheel_libc(filename)
87
88
89 @pytest.mark.parametrize(
90 "filename",
91 ["foo-1.0-py3-none-manylinux1_x86_64.musllinux_1_1_x86_64.whl"],
92 )
93 def test_get_wheel_libc_multiple(filename: str) -> None:
94 match = re.escape("multiple libc are not supported")
95 with pytest.raises(WheelToolsError, match=match):
96 get_wheel_libc(filename)
97
98
99 @pytest.mark.parametrize(
100 ("filename", "expected"),
101 [
102 ("foo-1.0-py3-none-manylinux1_x86_64.whl", ["manylinux1_x86_64"]),
103 ("foo-1.0-py3-none-any.any.whl", ["any"]),
104 ("foo-1.0-py3-none-linux_x86_64.any.whl", ["any", "linux_x86_64"]),
105 ("foo-1.0-py3-none-any.linux_x86_64.whl", ["any", "linux_x86_64"]),
106 ("foo-1.0-py2.py3-none-linux_x86_64.any.whl", ["any", "linux_x86_64"]),
107 ],
108 )
109 def test_get_wheel_platforms(filename: str, expected: list[str]) -> None:
110 assert get_wheel_platforms(filename) == expected
111
112
113 @pytest.mark.parametrize(
114 ("tag", "expected"),
115 [
116 ("android_21_arm64_v8a", 21),
117 ("android_21_x86_64", 21),
118 ("android_9_whatever", 9),
119 ("linux_x86_64", None),
120 ],
121 )
122 def test_android_api_level(tag, expected):
123 if expected is None:
124 with pytest.raises(ValueError, match=f"invalid tag: {tag}"):
125 android_api_level(tag)
126 else:
127 assert android_api_level(tag) == expected
128
129
130 def test_inwheel_tmpdir(tmp_path, monkeypatch):
131 wheel_path = (
132 HERE / "../integration/arch-wheels/glibc/testsimple-0.0.1-cp313-cp313-linux_x86_64.whl"
133 )
134 tmp_path = tmp_path.resolve(strict=True)
135 tmpdir = tmp_path / "tmpdir"
136 tmpdir.mkdir()
137 tmpdir_symlink = tmp_path / "symlink"
138 tmpdir_symlink.symlink_to(str(tmpdir), target_is_directory=True)
139 monkeypatch.setattr(tempfile, "gettempdir", lambda: str(tmpdir_symlink))
140 with InWheelCtx(wheel_path, tmp_path / wheel_path.name) as context:
141 Path(context._tmpdir.name).relative_to(tmpdir_symlink)
142 context.name.relative_to(tmpdir)
143
144
145 def test_inwheel_no_manager(tmp_path):
146 wheel_path = (
147 HERE / "../integration/arch-wheels/glibc/testsimple-0.0.1-cp313-cp313-linux_x86_64.whl"
148 )
149 context = InWheelCtx(wheel_path, tmp_path / wheel_path.name)
150 with pytest.raises(
151 ValueError,
152 match=re.escape("This function should be called from context manager"),
153 ):
154 next(context.iter_files())
155 with pytest.raises(
156 ValueError,
157 match=re.escape("This function should be called from wheel_ctx context manager"),
158 ):
159 add_platforms(context, [], [])
160
161
162 def test_add_platforms_no_duplicate_root_is_purelib(tmp_path):
163 """Regression test for #642: add_platforms should not duplicate Root-Is-Purelib."""
164 wheel_name = "testpkg-0.0.1-py3-none-any.whl"
165 wheel_path = tmp_path / wheel_name
166 dist_info = "testpkg-0.0.1.dist-info"
167
168 # Create a minimal wheel with Root-Is-Purelib already set to false
169 with zipfile.ZipFile(wheel_path, "w") as zf:
170 zf.writestr(
171 f"{dist_info}/WHEEL",
172 "Wheel-Version: 1.0\nGenerator: test\nRoot-Is-Purelib: false\nTag: py3-none-any\n",
173 )
174 zf.writestr(
175 f"{dist_info}/METADATA",
176 "Metadata-Version: 2.1\nName: testpkg\nVersion: 0.0.1\n",
177 )
178 zf.writestr(f"{dist_info}/RECORD", "")
179
180 out_wheel_path = tmp_path / "out" / wheel_name
181 out_wheel_path.parent.mkdir()
182
183 with InWheelCtx(wheel_path, out_wheel_path) as ctx:
184 add_platforms(ctx, ["manylinux_2_35_x86_64"])
185
186 # Find the output wheel (name will have changed)
187 out_wheels = list((tmp_path / "out").glob("*.whl"))
188 assert len(out_wheels) == 1
189
190 with zipfile.ZipFile(out_wheels[0]) as zf:
191 wheel_info = next(n for n in zf.namelist() if n.endswith("/WHEEL"))
192 info = read_pkg_info(zf.extract(wheel_info, tmp_path / "extracted"))
193
194 purelib_values = info.get_all("Root-Is-Purelib")
195 assert purelib_values is not None
196 assert len(purelib_values) == 1, (
197 f"Expected exactly one Root-Is-Purelib entry, got {len(purelib_values)}"
198 )
199
200
201 def test_inwheel_no_distinfo():
202 wheel_path = (
203 HERE / "../integration/arch-wheels/glibc/testsimple-0.0.1-cp313-cp313-linux_x86_64.whl"
204 )
205 with InWheelCtx(wheel_path, None) as context:
206 dist_info = list(context.path.glob("*.dist-info"))
207 assert len(dist_info) == 1
208 shutil.rmtree(dist_info[0])
209 with pytest.raises(
210 WheelToolsError,
211 match=re.escape("Should be exactly one `*.dist_info` directory"),
212 ):
213 next(context.iter_files())