Debian Codegraph @

tests/test_records.py

raw symbols SCIP
1 from io import BytesIO
2 from pathlib import Path
3
4 import pytest
5
6 from installer.records import Hash, InvalidRecordEntry, RecordEntry, parse_record_file
7
8
9 #
10 # pytest fixture witchcraft
11 #
12 @pytest.fixture()
13 def record_simple_list():
14 return [
15 "file.py,sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI,3144",
16 "distribution-1.0.dist-info/RECORD,,",
17 ]
18
19
20 @pytest.fixture()
21 def record_simple_iter(record_simple_list):
22 return iter(record_simple_list)
23
24
25 @pytest.fixture()
26 def record_simple_file(tmpdir, record_simple_list):
27 p = tmpdir.join("RECORD")
28 p.write("\n".join(record_simple_list))
29 with Path(p).open() as f:
30 yield f
31
32
33 @pytest.fixture()
34 def record_input(request):
35 return request.getfixturevalue(request.param)
36
37
38 SAMPLE_RECORDS = [
39 (
40 "purelib",
41 ("test1.py", "sha256=Y0sCextp4SQtQNU-MSs7SsdxD1W-gfKJtUlEbvZ3i-4", 6),
42 b"test1\n",
43 True,
44 ),
45 (
46 "purelib",
47 ("test2.py", "sha256=fW_Xd08Nh2JNptzxbQ09EEwxkedx--LznIau1LK_Gg8", 6),
48 b"test2\n",
49 True,
50 ),
51 (
52 "purelib",
53 ("test3.py", "sha256=qwPDTx7OCCEf4qgDn9ZCQZmz9de1X_E7ETSzZHdsRcU", 6),
54 b"test3\n",
55 True,
56 ),
57 (
58 "purelib",
59 (
60 "test4.py",
61 "sha256=Y0sCextp4SQtQNU-MSs7SsdxD1W-gfKJtUlEbvZ3i-4",
62 None,
63 ),
64 b"test1\n",
65 True,
66 ),
67 ("purelib", ("test5.py", None, None), b"test1\n", True),
68 ("purelib", ("test6.py", None, 6), b"test1\n", True),
69 (
70 "purelib",
71 ("test7.py", "sha256=Y0sCextp4SQtQNU-MSs7SsdxD1W-gfKJtUlEbvZ3i-4", 7),
72 b"test1\n",
73 False,
74 ),
75 (
76 "purelib",
77 ("test7.py", "sha256=Y0sCextp4SQtQNU-MSs7SsdxD1W-gfKJtUlEbvZ3i-4", None),
78 b"not-test1\n",
79 False,
80 ),
81 ("purelib", ("test8.py", None, 10), b"test1\n", False),
82 ]
83
84
85 #
86 # Actual Tests
87 #
88 class TestRecordEntry:
89 @pytest.mark.parametrize(
90 "path, hash_, size, caused_by",
91 [
92 ("", "", "", ["path"]),
93 ("", "", "non-int", ["path", "size"]),
94 ("a.py", "", "non-int", ["size"]),
95 # Notice that we're explicitly allowing non-compliant hash values
96 ("a.py", "some-random-value", "non-int", ["size"]),
97 ],
98 )
99 def test_invalid_elements(self, path, hash_, size, caused_by):
100 with pytest.raises(InvalidRecordEntry) as exc_info:
101 RecordEntry.from_elements(path, hash_, size)
102
103 assert exc_info.value.elements == (path, hash_, size)
104 for word in caused_by:
105 assert word in str(exc_info.value)
106
107 @pytest.mark.parametrize(
108 "path, hash_, size",
109 [
110 ("a.py", "", ""),
111 ("a.py", "", "3144"),
112 ("a.py", "sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI", ""),
113 ("a.py", "sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI", "3144"),
114 ],
115 )
116 def test_valid_elements(self, path, hash_, size):
117 RecordEntry.from_elements(path, hash_, size)
118
119 @pytest.mark.parametrize(
120 ("scheme", "elements", "data", "passes_validation"), SAMPLE_RECORDS
121 )
122 def test_populates_attributes_correctly(
123 self, scheme, elements, data, passes_validation
124 ):
125 path, hash_string, size = elements
126
127 record = RecordEntry.from_elements(path, hash_string, size)
128
129 assert record.path == path
130 assert record.size == size
131
132 if record.hash_ is not None:
133 assert isinstance(record.hash_, Hash)
134 assert record.hash_.name == "sha256"
135 assert record.hash_.value == hash_string[len("sha256=") :]
136
137 @pytest.mark.parametrize(
138 ("scheme", "elements", "data", "passes_validation"), SAMPLE_RECORDS
139 )
140 def test_validation(self, scheme, elements, data, passes_validation):
141 record = RecordEntry.from_elements(*elements)
142 assert record.validate(data) == passes_validation
143
144 @pytest.mark.parametrize(
145 ("scheme", "elements", "data", "passes_validation"), SAMPLE_RECORDS
146 )
147 def test_validate_stream(self, scheme, elements, data, passes_validation):
148 record = RecordEntry.from_elements(*elements)
149
150 assert record.validate_stream(BytesIO(data)) == passes_validation
151
152 @pytest.mark.parametrize(
153 ("scheme", "elements", "data", "passes_validation"), SAMPLE_RECORDS
154 )
155 def test_string_representation(self, scheme, elements, data, passes_validation):
156 record = RecordEntry.from_elements(*elements)
157
158 expected_row = tuple(
159 [(str(elem) if elem is not None else "") for elem in elements]
160 )
161 assert record.to_row() == expected_row
162
163 @pytest.mark.parametrize(
164 ("scheme", "elements", "data", "passes_validation"), SAMPLE_RECORDS
165 )
166 def test_string_representation_with_prefix(
167 self, scheme, elements, data, passes_validation
168 ):
169 record = RecordEntry.from_elements(*elements)
170
171 expected_row = tuple(
172 [
173 (str(elem) if elem is not None else "")
174 for elem in ("prefix/" + elements[0], elements[1], elements[2])
175 ]
176 )
177 assert record.to_row("prefix/") == expected_row
178
179 def test_equality(self):
180 record = RecordEntry.from_elements(
181 "file.py",
182 "sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI",
183 "3144",
184 )
185 record_same = RecordEntry.from_elements(
186 "file.py",
187 "sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI",
188 "3144",
189 )
190 record_different_name = RecordEntry.from_elements(
191 "file2.py",
192 "sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI",
193 "3144",
194 )
195 record_different_hash_name = RecordEntry.from_elements(
196 "file.py",
197 "md5=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI",
198 "3144",
199 )
200 record_different_hash_value = RecordEntry.from_elements(
201 "file.py",
202 "sha256=qwertyuiodfdsflkgshdlkjghrefawrwerwffsdfflk29",
203 "3144",
204 )
205 record_different_size = RecordEntry.from_elements(
206 "file.py",
207 "sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI",
208 "10",
209 )
210
211 assert record == record_same
212
213 assert record != "random string"
214 assert record != record_different_name
215 assert record != record_different_hash_name
216 assert record != record_different_hash_value
217 assert record != record_different_size
218
219 # Ensure equality is based on current state
220 record_same.hash_ = None
221 assert record != record_same
222
223
224 class TestParseRecordFile:
225 def test_accepts_empty_iterable(self):
226 list(parse_record_file([]))
227
228 @pytest.mark.parametrize(
229 "record_input",
230 ["record_simple_list", "record_simple_iter", "record_simple_file"],
231 indirect=True,
232 )
233 def test_accepts_all_kinds_of_iterables(self, record_input):
234 """Should accepts any iterable, e.g. container, iterator, or file object."""
235 records = list(parse_record_file(record_input))
236 assert len(records) == 2
237
238 assert records == [
239 (
240 "file.py",
241 "sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI",
242 "3144",
243 ),
244 ("distribution-1.0.dist-info/RECORD", "", ""),
245 ]
246
247 @pytest.mark.parametrize(
248 "line, element_count",
249 [
250 pytest.param(
251 "file.py,sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI,3144,",
252 4,
253 id="four",
254 ),
255 pytest.param(
256 "distribution-1.0.dist-info/RECORD,,,,",
257 5,
258 id="five",
259 ),
260 ],
261 )
262 def test_rejects_wrong_element_count(self, line, element_count):
263 with pytest.raises(InvalidRecordEntry) as exc_info:
264 list(parse_record_file([line]))
265
266 message = f"expected 3 elements, got {element_count}"
267 assert message in str(exc_info.value)
268
269 def test_shows_correct_row_number(self):
270 record_lines = [
271 "file1.py,sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI,3144",
272 "file2.py,sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI,3144",
273 "file3.py,sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI,3144",
274 "distribution-1.0.dist-info/RECORD,,,,",
275 ]
276 with pytest.raises(InvalidRecordEntry) as exc_info:
277 list(parse_record_file(record_lines))
278
279 assert "Row Index 3" in str(exc_info.value)
280
281 def test_parse_record_entry_with_comma(self):
282 record_lines = [
283 '"file1,file2.txt",sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI,3144',
284 "distribution-1.0.dist-info/RECORD,,",
285 ]
286 records = list(parse_record_file(record_lines))
287 assert records == [
288 (
289 "file1,file2.txt",
290 "sha256=AVTFPZpEKzuHr7OvQZmhaU3LvwKz06AJw8mT\\_pNh2yI",
291 "3144",
292 ),
293 ("distribution-1.0.dist-info/RECORD", "", ""),
294 ]
295
296 def test_parse_record_entry_with_backslash_path(self):
297 record_lines = [
298 "distribution-1.0.dist-info\\RECORD,,",
299 ]
300 records = list(parse_record_file(record_lines))
301 assert records == [
302 ("distribution-1.0.dist-info/RECORD", "", ""),
303 ]