Debian Codegraph @

tests/test_utils.py

raw symbols SCIP
1 """Tests for installer.utils"""
2
3 import base64
4 import hashlib
5 import textwrap
6 from email.message import Message
7 from io import BytesIO
8
9 import pytest
10
11 from installer.records import RecordEntry
12 from installer.utils import (
13 WheelFilename,
14 canonicalize_name,
15 construct_record_file,
16 copyfileobj_with_hashing,
17 fix_shebang,
18 get_stream_length,
19 parse_entrypoints,
20 parse_metadata_file,
21 parse_wheel_filename,
22 )
23
24
25 class TestParseMetadata:
26 def test_basics(self):
27 result = parse_metadata_file(
28 textwrap.dedent(
29 """\
30 Name: package
31 Version: 1.0.0
32 Multi-Use-Field: 1
33 Multi-Use-Field: 2
34 Multi-Use-Field: 3
35 """
36 )
37 )
38 assert isinstance(result, Message)
39 assert result.get("Name") == "package"
40 assert result.get("version") == "1.0.0"
41 assert result.get_all("MULTI-USE-FIELD") == ["1", "2", "3"]
42
43
44 class TestCanonicalizeDistributionName:
45 @pytest.mark.parametrize(
46 "string, expected",
47 [
48 # Noop
49 (
50 "package-1",
51 "package-1",
52 ),
53 # PEP 508 canonicalization
54 (
55 "ABC..12",
56 "abc-12",
57 ),
58 ],
59 )
60 def test_valid_cases(self, string, expected):
61 got = canonicalize_name(string)
62 assert expected == got, (expected, got)
63
64
65 class TestParseWheelFilename:
66 @pytest.mark.parametrize(
67 "string, expected",
68 [
69 # Crafted package name w/ a "complex" version and build tag
70 (
71 "package-1!1.0+abc.7-753-py3-none-any.whl",
72 WheelFilename("package", "1!1.0+abc.7", "753", "py3-none-any"),
73 ),
74 # Crafted package name w/ a "complex" version and no build tag
75 (
76 "package-1!1.0+abc.7-py3-none-any.whl",
77 WheelFilename("package", "1!1.0+abc.7", None, "py3-none-any"),
78 ),
79 # Use real tensorflow wheel names
80 (
81 "tensorflow-2.3.0-cp38-cp38-macosx_10_11_x86_64.whl",
82 WheelFilename(
83 "tensorflow", "2.3.0", None, "cp38-cp38-macosx_10_11_x86_64"
84 ),
85 ),
86 (
87 "tensorflow-2.3.0-cp38-cp38-manylinux2010_x86_64.whl",
88 WheelFilename(
89 "tensorflow", "2.3.0", None, "cp38-cp38-manylinux2010_x86_64"
90 ),
91 ),
92 (
93 "tensorflow-2.3.0-cp38-cp38-win_amd64.whl",
94 WheelFilename("tensorflow", "2.3.0", None, "cp38-cp38-win_amd64"),
95 ),
96 ],
97 )
98 def test_valid_cases(self, string, expected):
99 got = parse_wheel_filename(string)
100 assert expected == got, (expected, got)
101
102 @pytest.mark.parametrize(
103 "string",
104 [
105 # Not ".whl"
106 "pip-20.0.0-py2.py3-none-any.zip",
107 # No tag
108 "pip-20.0.0.whl",
109 # Empty tag
110 "pip-20.0.0---.whl",
111 ],
112 )
113 def test_invalid_cases(self, string):
114 with pytest.raises(ValueError):
115 parse_wheel_filename(string)
116
117
118 class TestCopyFileObjWithHashing:
119 def test_basic_functionality(self):
120 data = b"input data is this"
121 hash_ = (
122 base64.urlsafe_b64encode(hashlib.sha256(data).digest())
123 .decode("ascii")
124 .rstrip("=")
125 )
126 size = len(data)
127
128 with BytesIO(data) as source, BytesIO() as dest:
129 result = copyfileobj_with_hashing(source, dest, hash_algorithm="sha256")
130 written_data = dest.getvalue()
131
132 assert result == (hash_, size)
133 assert written_data == data
134
135
136 class TestGetStreamLength:
137 def test_basic_functionality(self):
138 data = b"input data is this"
139 size = len(data)
140
141 with BytesIO(data) as source:
142 result = get_stream_length(source)
143
144 assert result == size
145
146
147 class TestScript:
148 @pytest.mark.parametrize(
149 ("data", "expected"),
150 [
151 pytest.param(
152 b"#!python\ntest",
153 b"#!/my/python\ntest",
154 id="python",
155 ),
156 pytest.param(
157 b"#!pythonw\ntest",
158 b"#!/my/python\ntest",
159 id="pythonw",
160 ),
161 pytest.param(
162 b"#!python something\ntest",
163 b"#!/my/python\ntest",
164 id="python-with-args",
165 ),
166 pytest.param(
167 b"#!python",
168 b"#!/my/python\n",
169 id="python-no-content",
170 ),
171 ],
172 )
173 def test_replace_shebang(self, data, expected):
174 with BytesIO(data) as source, fix_shebang(source, "/my/python") as stream:
175 result = stream.read()
176 assert result == expected
177
178 @pytest.mark.parametrize(
179 "data",
180 [
181 b"#!py\ntest",
182 b"#!something\ntest",
183 b"#something\ntest",
184 b"#something",
185 b"something",
186 ],
187 )
188 def test_keep_data(self, data):
189 with BytesIO(data) as source, fix_shebang(source, "/my/python") as stream:
190 result = stream.read()
191 assert result == data
192
193
194 class TestConstructRecord:
195 def test_construct(self):
196 raw_records = [
197 ("a.py", "", ""),
198 ("a.py", "", "3144"),
199 ("a.py", "sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI", ""),
200 ("a.py", "sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI", "3144"),
201 ]
202 records = [
203 ("purelib", RecordEntry.from_elements(*elements))
204 for elements in raw_records
205 ]
206
207 assert construct_record_file(records).read() == (
208 b"a.py,,\n"
209 b"a.py,,3144\n"
210 b"a.py,sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI,\n"
211 b"a.py,sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI,3144\n"
212 )
213
214
215 class TestParseEntryPoints:
216 @pytest.mark.parametrize(
217 ("script", "expected"),
218 [
219 pytest.param("", [], id="empty"),
220 pytest.param(
221 """
222 [foo]
223 foo = foo.bar
224 """,
225 [],
226 id="unrelated",
227 ),
228 pytest.param(
229 """
230 [console_scripts]
231 package = package.__main__:package
232 """,
233 [
234 ("package", "package.__main__", "package", "console"),
235 ],
236 id="cli",
237 ),
238 pytest.param(
239 """
240 [gui_scripts]
241 package = package.__main__:package
242 """,
243 [
244 ("package", "package.__main__", "package", "gui"),
245 ],
246 id="gui",
247 ),
248 pytest.param(
249 """
250 [console_scripts]
251 magic-cli = magic.cli:main
252
253 [gui_scripts]
254 magic-gui = magic.gui:main
255 """,
256 [
257 ("magic-cli", "magic.cli", "main", "console"),
258 ("magic-gui", "magic.gui", "main", "gui"),
259 ],
260 id="cli-and-gui",
261 ),
262 ],
263 )
264 def test_valid(self, script, expected):
265 iterable = parse_entrypoints(textwrap.dedent(script))
266 assert list(iterable) == expected, expected