Debian Codegraph @

tests/test_core.py

raw symbols SCIP
1 import hashlib
2 import textwrap
3 from io import BytesIO
4 from unittest import mock
5
6 import pytest
7
8 from installer import install
9 from installer.exceptions import InvalidWheelSource
10 from installer.records import RecordEntry
11 from installer.sources import WheelSource
12
13
14 # --------------------------------------------------------------------------------------
15 # Helpers
16 # --------------------------------------------------------------------------------------
17 def hash_and_size(data):
18 return hashlib.sha256(data).hexdigest(), len(data)
19
20
21 @pytest.fixture
22 def mock_destination():
23 retval = mock.Mock()
24
25 # A hacky approach to making sure we got the right objects going in.
26 def custom_write_file(scheme, path, stream, is_executable):
27 assert isinstance(stream, BytesIO)
28 return (path, scheme, 0)
29
30 def custom_write_script(name, module, attr, section):
31 return (name, module, attr, section)
32
33 retval.write_file.side_effect = custom_write_file
34 retval.write_script.side_effect = custom_write_script
35
36 return retval
37
38
39 class FakeWheelSource(WheelSource):
40 def __init__(self, *, distribution, version, regular_files, dist_info_files):
41 super().__init__(distribution, version)
42
43 self.dist_info_files = {
44 file: textwrap.dedent(content.decode("utf-8"))
45 for file, content in dist_info_files.items()
46 }
47 self.regular_files = {
48 file: textwrap.dedent(content.decode("utf-8")).encode("utf-8")
49 for file, content in regular_files.items()
50 }
51
52 # Compute RECORD file.
53 _records = [record for record, _, _ in self.get_contents()]
54 self.dist_info_files["RECORD"] = "\n".join(
55 sorted(
56 ",".join([file, "sha256=" + hash_, str(size)])
57 for file, hash_, size in _records
58 )
59 )
60
61 @property
62 def dist_info_filenames(self):
63 return list(self.dist_info_files)
64
65 def read_dist_info(self, filename):
66 return self.dist_info_files[filename]
67
68 def validate_record(self) -> None:
69 # Skip validation since the logic is different.
70 return
71
72 def get_contents(self):
73 # Sort for deterministic behaviour for Python versions that do not preserve
74 # insertion order for dictionaries.
75 for file, content in sorted(self.regular_files.items()):
76 hashed, size = hash_and_size(content)
77 record = (file, f"sha256={hashed}", str(size))
78 with BytesIO(content) as stream:
79 yield record, stream, False
80
81 # Sort for deterministic behaviour for Python versions that do not preserve
82 # insertion order for dictionaries.
83 for file, text in sorted(self.dist_info_files.items()):
84 content = text.encode("utf-8")
85 hashed, size = hash_and_size(content)
86 record = (
87 self.dist_info_dir + "/" + file,
88 f"sha256={hashed}",
89 str(size),
90 )
91 with BytesIO(content) as stream:
92 yield record, stream, False
93
94
95 # --------------------------------------------------------------------------------------
96 # Actual Tests
97 # --------------------------------------------------------------------------------------
98 class TestInstall:
99 def test_calls_destination_correctly(self, mock_destination):
100 # Create a fake wheel
101 source = FakeWheelSource(
102 distribution="fancy",
103 version="1.0.0",
104 regular_files={
105 "fancy/__init__.py": b"""\
106 def main():
107 print("I'm a fancy package")
108 """,
109 "fancy/__main__.py": b"""\
110 if __name__ == "__main__":
111 from . import main
112 main()
113 """,
114 },
115 dist_info_files={
116 "top_level.txt": b"""\
117 fancy
118 """,
119 "entry_points.txt": b"""\
120 [console_scripts]
121 fancy = fancy:main
122
123 [gui_scripts]
124 fancy-gui = fancy:main
125 """,
126 "WHEEL": b"""\
127 Wheel-Version: 1.0
128 Generator: magic (1.0.0)
129 Root-Is-Purelib: true
130 Tag: py3-none-any
131 """,
132 "METADATA": b"""\
133 Metadata-Version: 2.1
134 Name: fancy
135 Version: 1.0.0
136 Summary: A fancy package
137 Author: Agendaless Consulting
138 Author-email: [email protected]
139 License: MIT
140 Keywords: fancy amazing
141 Platform: UNKNOWN
142 Classifier: Intended Audience :: Developers
143 """,
144 },
145 )
146
147 install(
148 source=source,
149 destination=mock_destination,
150 additional_metadata={
151 "fun_file.txt": b"this should be in dist-info!",
152 },
153 )
154
155 mock_destination.assert_has_calls(
156 [
157 mock.call.write_script(
158 name="fancy",
159 module="fancy",
160 attr="main",
161 section="console",
162 ),
163 mock.call.write_script(
164 name="fancy-gui",
165 module="fancy",
166 attr="main",
167 section="gui",
168 ),
169 mock.call.write_file(
170 scheme="purelib",
171 path="fancy/__init__.py",
172 stream=mock.ANY,
173 is_executable=False,
174 ),
175 mock.call.write_file(
176 scheme="purelib",
177 path="fancy/__main__.py",
178 stream=mock.ANY,
179 is_executable=False,
180 ),
181 mock.call.write_file(
182 scheme="purelib",
183 path="fancy-1.0.0.dist-info/METADATA",
184 stream=mock.ANY,
185 is_executable=False,
186 ),
187 mock.call.write_file(
188 scheme="purelib",
189 path="fancy-1.0.0.dist-info/WHEEL",
190 stream=mock.ANY,
191 is_executable=False,
192 ),
193 mock.call.write_file(
194 scheme="purelib",
195 path="fancy-1.0.0.dist-info/entry_points.txt",
196 stream=mock.ANY,
197 is_executable=False,
198 ),
199 mock.call.write_file(
200 scheme="purelib",
201 path="fancy-1.0.0.dist-info/top_level.txt",
202 stream=mock.ANY,
203 is_executable=False,
204 ),
205 mock.call.write_file(
206 scheme="purelib",
207 path="fancy-1.0.0.dist-info/fun_file.txt",
208 stream=mock.ANY,
209 is_executable=False,
210 ),
211 mock.call.finalize_installation(
212 scheme="purelib",
213 record_file_path="fancy-1.0.0.dist-info/RECORD",
214 records=[
215 ("scripts", ("fancy", "fancy", "main", "console")),
216 ("scripts", ("fancy-gui", "fancy", "main", "gui")),
217 ("purelib", ("fancy/__init__.py", "purelib", 0)),
218 ("purelib", ("fancy/__main__.py", "purelib", 0)),
219 ("purelib", ("fancy-1.0.0.dist-info/METADATA", "purelib", 0)),
220 ("purelib", ("fancy-1.0.0.dist-info/WHEEL", "purelib", 0)),
221 (
222 "purelib",
223 ("fancy-1.0.0.dist-info/entry_points.txt", "purelib", 0),
224 ),
225 (
226 "purelib",
227 ("fancy-1.0.0.dist-info/top_level.txt", "purelib", 0),
228 ),
229 (
230 "purelib",
231 ("fancy-1.0.0.dist-info/fun_file.txt", "purelib", 0),
232 ),
233 (
234 "purelib",
235 RecordEntry("fancy-1.0.0.dist-info/RECORD", None, None),
236 ),
237 ],
238 ),
239 ]
240 )
241
242 def test_no_entrypoints_is_ok(self, mock_destination):
243 # Create a fake wheel
244 source = FakeWheelSource(
245 distribution="fancy",
246 version="1.0.0",
247 regular_files={
248 "fancy/__init__.py": b"""\
249 def main():
250 print("I'm a fancy package")
251 """,
252 "fancy/__main__.py": b"""\
253 if __name__ == "__main__":
254 from . import main
255 main()
256 """,
257 },
258 dist_info_files={
259 "top_level.txt": b"""\
260 fancy
261 """,
262 "WHEEL": b"""\
263 Wheel-Version: 1.0
264 Generator: magic (1.0.0)
265 Root-Is-Purelib: true
266 Tag: py3-none-any
267 """,
268 "METADATA": b"""\
269 Metadata-Version: 2.1
270 Name: fancy
271 Version: 1.0.0
272 Summary: A fancy package
273 Author: Agendaless Consulting
274 Author-email: [email protected]
275 License: MIT
276 Keywords: fancy amazing
277 Platform: UNKNOWN
278 Classifier: Intended Audience :: Developers
279 """,
280 },
281 )
282
283 install(
284 source=source,
285 destination=mock_destination,
286 additional_metadata={
287 "fun_file.txt": b"this should be in dist-info!",
288 },
289 )
290
291 mock_destination.assert_has_calls(
292 [
293 mock.call.write_file(
294 scheme="purelib",
295 path="fancy/__init__.py",
296 stream=mock.ANY,
297 is_executable=False,
298 ),
299 mock.call.write_file(
300 scheme="purelib",
301 path="fancy/__main__.py",
302 stream=mock.ANY,
303 is_executable=False,
304 ),
305 mock.call.write_file(
306 scheme="purelib",
307 path="fancy-1.0.0.dist-info/METADATA",
308 stream=mock.ANY,
309 is_executable=False,
310 ),
311 mock.call.write_file(
312 scheme="purelib",
313 path="fancy-1.0.0.dist-info/WHEEL",
314 stream=mock.ANY,
315 is_executable=False,
316 ),
317 mock.call.write_file(
318 scheme="purelib",
319 path="fancy-1.0.0.dist-info/top_level.txt",
320 stream=mock.ANY,
321 is_executable=False,
322 ),
323 mock.call.write_file(
324 scheme="purelib",
325 path="fancy-1.0.0.dist-info/fun_file.txt",
326 stream=mock.ANY,
327 is_executable=False,
328 ),
329 mock.call.finalize_installation(
330 scheme="purelib",
331 record_file_path="fancy-1.0.0.dist-info/RECORD",
332 records=[
333 ("purelib", ("fancy/__init__.py", "purelib", 0)),
334 ("purelib", ("fancy/__main__.py", "purelib", 0)),
335 ("purelib", ("fancy-1.0.0.dist-info/METADATA", "purelib", 0)),
336 ("purelib", ("fancy-1.0.0.dist-info/WHEEL", "purelib", 0)),
337 (
338 "purelib",
339 ("fancy-1.0.0.dist-info/top_level.txt", "purelib", 0),
340 ),
341 (
342 "purelib",
343 ("fancy-1.0.0.dist-info/fun_file.txt", "purelib", 0),
344 ),
345 (
346 "purelib",
347 RecordEntry("fancy-1.0.0.dist-info/RECORD", None, None),
348 ),
349 ],
350 ),
351 ]
352 )
353
354 def test_handles_platlib(self, mock_destination):
355 # Create a fake wheel
356 source = FakeWheelSource(
357 distribution="fancy",
358 version="1.0.0",
359 regular_files={
360 "fancy/__init__.py": b"""\
361 def main():
362 print("I'm a fancy package")
363 """,
364 "fancy/__main__.py": b"""\
365 if __name__ == "__main__":
366 from . import main
367 main()
368 """,
369 },
370 dist_info_files={
371 "top_level.txt": b"""\
372 fancy
373 """,
374 "entry_points.txt": b"""\
375 [console_scripts]
376 fancy = fancy:main
377
378 [gui_scripts]
379 fancy-gui = fancy:main
380 """,
381 "WHEEL": b"""\
382 Wheel-Version: 1.0
383 Generator: magic (1.0.0)
384 Root-Is-Purelib: false
385 Tag: py3-none-any
386 """,
387 "METADATA": b"""\
388 Metadata-Version: 2.1
389 Name: fancy
390 Version: 1.0.0
391 Summary: A fancy package
392 Author: Agendaless Consulting
393 Author-email: [email protected]
394 License: MIT
395 Keywords: fancy amazing
396 Platform: UNKNOWN
397 Classifier: Intended Audience :: Developers
398 """,
399 },
400 )
401
402 install(
403 source=source,
404 destination=mock_destination,
405 additional_metadata={
406 "fun_file.txt": b"this should be in dist-info!",
407 },
408 )
409
410 mock_destination.assert_has_calls(
411 [
412 mock.call.write_script(
413 name="fancy",
414 module="fancy",
415 attr="main",
416 section="console",
417 ),
418 mock.call.write_script(
419 name="fancy-gui",
420 module="fancy",
421 attr="main",
422 section="gui",
423 ),
424 mock.call.write_file(
425 scheme="platlib",
426 path="fancy/__init__.py",
427 stream=mock.ANY,
428 is_executable=False,
429 ),
430 mock.call.write_file(
431 scheme="platlib",
432 path="fancy/__main__.py",
433 stream=mock.ANY,
434 is_executable=False,
435 ),
436 mock.call.write_file(
437 scheme="platlib",
438 path="fancy-1.0.0.dist-info/METADATA",
439 stream=mock.ANY,
440 is_executable=False,
441 ),
442 mock.call.write_file(
443 scheme="platlib",
444 path="fancy-1.0.0.dist-info/WHEEL",
445 stream=mock.ANY,
446 is_executable=False,
447 ),
448 mock.call.write_file(
449 scheme="platlib",
450 path="fancy-1.0.0.dist-info/entry_points.txt",
451 stream=mock.ANY,
452 is_executable=False,
453 ),
454 mock.call.write_file(
455 scheme="platlib",
456 path="fancy-1.0.0.dist-info/top_level.txt",
457 stream=mock.ANY,
458 is_executable=False,
459 ),
460 mock.call.write_file(
461 scheme="platlib",
462 path="fancy-1.0.0.dist-info/fun_file.txt",
463 stream=mock.ANY,
464 is_executable=False,
465 ),
466 mock.call.finalize_installation(
467 scheme="platlib",
468 record_file_path="fancy-1.0.0.dist-info/RECORD",
469 records=[
470 ("scripts", ("fancy", "fancy", "main", "console")),
471 ("scripts", ("fancy-gui", "fancy", "main", "gui")),
472 ("platlib", ("fancy/__init__.py", "platlib", 0)),
473 ("platlib", ("fancy/__main__.py", "platlib", 0)),
474 ("platlib", ("fancy-1.0.0.dist-info/METADATA", "platlib", 0)),
475 ("platlib", ("fancy-1.0.0.dist-info/WHEEL", "platlib", 0)),
476 (
477 "platlib",
478 ("fancy-1.0.0.dist-info/entry_points.txt", "platlib", 0),
479 ),
480 (
481 "platlib",
482 ("fancy-1.0.0.dist-info/top_level.txt", "platlib", 0),
483 ),
484 (
485 "platlib",
486 ("fancy-1.0.0.dist-info/fun_file.txt", "platlib", 0),
487 ),
488 (
489 "platlib",
490 RecordEntry("fancy-1.0.0.dist-info/RECORD", None, None),
491 ),
492 ],
493 ),
494 ]
495 )
496
497 def test_accepts_newer_minor_wheel_versions(self, mock_destination):
498 # Create a fake wheel
499 source = FakeWheelSource(
500 distribution="fancy",
501 version="1.0.0",
502 regular_files={
503 "fancy/__init__.py": b"""\
504 def main():
505 print("I'm a fancy package")
506 """,
507 "fancy/__main__.py": b"""\
508 if __name__ == "__main__":
509 from . import main
510 main()
511 """,
512 },
513 dist_info_files={
514 "top_level.txt": b"""\
515 fancy
516 """,
517 "entry_points.txt": b"""\
518 [console_scripts]
519 fancy = fancy:main
520
521 [gui_scripts]
522 fancy-gui = fancy:main
523 """,
524 "WHEEL": b"""\
525 Wheel-Version: 1.1
526 Generator: magic (1.0.0)
527 Root-Is-Purelib: true
528 Tag: py3-none-any
529 """,
530 "METADATA": b"""\
531 Metadata-Version: 2.1
532 Name: fancy
533 Version: 1.0.0
534 Summary: A fancy package
535 Author: Agendaless Consulting
536 Author-email: [email protected]
537 License: MIT
538 Keywords: fancy amazing
539 Platform: UNKNOWN
540 Classifier: Intended Audience :: Developers
541 """,
542 },
543 )
544
545 install(
546 source=source,
547 destination=mock_destination,
548 additional_metadata={
549 "fun_file.txt": b"this should be in dist-info!",
550 },
551 )
552
553 # no assertions necessary, since we want to make sure this test didn't
554 # raises errors.
555 assert True
556
557 def test_rejects_newer_major_wheel_versions(self, mock_destination):
558 # Create a fake wheel
559 source = FakeWheelSource(
560 distribution="fancy",
561 version="1.0.0",
562 regular_files={
563 "fancy/__init__.py": b"""\
564 def main():
565 print("I'm a fancy package")
566 """,
567 "fancy/__main__.py": b"""\
568 if __name__ == "__main__":
569 from . import main
570 main()
571 """,
572 },
573 dist_info_files={
574 "top_level.txt": b"""\
575 fancy
576 """,
577 "entry_points.txt": b"""\
578 [console_scripts]
579 fancy = fancy:main
580
581 [gui_scripts]
582 fancy-gui = fancy:main
583 """,
584 "WHEEL": b"""\
585 Wheel-Version: 2.0
586 Generator: magic (1.0.0)
587 Root-Is-Purelib: true
588 Tag: py3-none-any
589 """,
590 "METADATA": b"""\
591 Metadata-Version: 2.1
592 Name: fancy
593 Version: 1.0.0
594 Summary: A fancy package
595 Author: Agendaless Consulting
596 Author-email: [email protected]
597 License: MIT
598 Keywords: fancy amazing
599 Platform: UNKNOWN
600 Classifier: Intended Audience :: Developers
601 """,
602 },
603 )
604
605 with pytest.raises(InvalidWheelSource) as ctx:
606 install(
607 source=source,
608 destination=mock_destination,
609 additional_metadata={
610 "fun_file.txt": b"this should be in dist-info!",
611 },
612 )
613
614 assert "Incompatible Wheel-Version" in str(ctx.value)
615
616 def test_handles_data_properly(self, mock_destination):
617 # Create a fake wheel
618 source = FakeWheelSource(
619 distribution="fancy",
620 version="1.0.0",
621 regular_files={
622 "fancy/__init__.py": b"""\
623 # put me in purelib
624 """,
625 "fancy-1.0.0.data/purelib/fancy/purelib.py": b"""\
626 # put me in purelib
627 """,
628 "fancy-1.0.0.data/platlib/fancy/platlib.py": b"""\
629 # put me in platlib
630 """,
631 "fancy-1.0.0.data/scripts/fancy/scripts.py": b"""\
632 # put me in scripts
633 """,
634 "fancy-1.0.0.data/headers/fancy/headers.py": b"""\
635 # put me in headers
636 """,
637 "fancy-1.0.0.data/data/fancy/data.py": b"""\
638 # put me in data
639 """,
640 },
641 dist_info_files={
642 "top_level.txt": b"""\
643 fancy
644 """,
645 "entry_points.txt": b"""\
646 [console_scripts]
647 fancy = fancy:main
648
649 [gui_scripts]
650 fancy-gui = fancy:main
651 """,
652 "WHEEL": b"""\
653 Wheel-Version: 1.0
654 Generator: magic (1.0.0)
655 Root-Is-Purelib: true
656 Tag: py3-none-any
657 """,
658 "METADATA": b"""\
659 Metadata-Version: 2.1
660 Name: fancy
661 Version: 1.0.0
662 Summary: A fancy package
663 Author: Agendaless Consulting
664 Author-email: [email protected]
665 License: MIT
666 Keywords: fancy amazing
667 Platform: UNKNOWN
668 Classifier: Intended Audience :: Developers
669 """,
670 },
671 )
672
673 install(
674 source=source,
675 destination=mock_destination,
676 additional_metadata={},
677 )
678
679 mock_destination.assert_has_calls(
680 [
681 mock.call.write_script(
682 name="fancy",
683 module="fancy",
684 attr="main",
685 section="console",
686 ),
687 mock.call.write_script(
688 name="fancy-gui",
689 module="fancy",
690 attr="main",
691 section="gui",
692 ),
693 mock.call.write_file(
694 scheme="data",
695 path="fancy/data.py",
696 stream=mock.ANY,
697 is_executable=False,
698 ),
699 mock.call.write_file(
700 scheme="headers",
701 path="fancy/headers.py",
702 stream=mock.ANY,
703 is_executable=False,
704 ),
705 mock.call.write_file(
706 scheme="platlib",
707 path="fancy/platlib.py",
708 stream=mock.ANY,
709 is_executable=False,
710 ),
711 mock.call.write_file(
712 scheme="purelib",
713 path="fancy/purelib.py",
714 stream=mock.ANY,
715 is_executable=False,
716 ),
717 mock.call.write_file(
718 scheme="scripts",
719 path="fancy/scripts.py",
720 stream=mock.ANY,
721 is_executable=False,
722 ),
723 mock.call.write_file(
724 scheme="purelib",
725 path="fancy/__init__.py",
726 stream=mock.ANY,
727 is_executable=False,
728 ),
729 mock.call.write_file(
730 scheme="purelib",
731 path="fancy-1.0.0.dist-info/METADATA",
732 stream=mock.ANY,
733 is_executable=False,
734 ),
735 mock.call.write_file(
736 scheme="purelib",
737 path="fancy-1.0.0.dist-info/WHEEL",
738 stream=mock.ANY,
739 is_executable=False,
740 ),
741 mock.call.write_file(
742 scheme="purelib",
743 path="fancy-1.0.0.dist-info/entry_points.txt",
744 stream=mock.ANY,
745 is_executable=False,
746 ),
747 mock.call.write_file(
748 scheme="purelib",
749 path="fancy-1.0.0.dist-info/top_level.txt",
750 stream=mock.ANY,
751 is_executable=False,
752 ),
753 mock.call.finalize_installation(
754 scheme="purelib",
755 record_file_path="fancy-1.0.0.dist-info/RECORD",
756 records=[
757 ("scripts", ("fancy", "fancy", "main", "console")),
758 ("scripts", ("fancy-gui", "fancy", "main", "gui")),
759 ("data", ("fancy/data.py", "data", 0)),
760 ("headers", ("fancy/headers.py", "headers", 0)),
761 ("platlib", ("fancy/platlib.py", "platlib", 0)),
762 ("purelib", ("fancy/purelib.py", "purelib", 0)),
763 ("scripts", ("fancy/scripts.py", "scripts", 0)),
764 ("purelib", ("fancy/__init__.py", "purelib", 0)),
765 ("purelib", ("fancy-1.0.0.dist-info/METADATA", "purelib", 0)),
766 ("purelib", ("fancy-1.0.0.dist-info/WHEEL", "purelib", 0)),
767 (
768 "purelib",
769 ("fancy-1.0.0.dist-info/entry_points.txt", "purelib", 0),
770 ),
771 (
772 "purelib",
773 ("fancy-1.0.0.dist-info/top_level.txt", "purelib", 0),
774 ),
775 (
776 "purelib",
777 RecordEntry("fancy-1.0.0.dist-info/RECORD", None, None),
778 ),
779 ],
780 ),
781 ]
782 )
783
784 def test_errors_out_when_given_invalid_scheme_in_data(self, mock_destination):
785 # Create a fake wheel
786 source = FakeWheelSource(
787 distribution="fancy",
788 version="1.0.0",
789 regular_files={
790 "fancy/__init__.py": b"""\
791 # put me in purelib
792 """,
793 "fancy-1.0.0.data/purelib/fancy/purelib.py": b"""\
794 # put me in purelib
795 """,
796 "fancy-1.0.0.data/invalid/fancy/invalid.py": b"""\
797 # i am invalid
798 """,
799 },
800 dist_info_files={
801 "top_level.txt": b"""\
802 fancy
803 """,
804 "entry_points.txt": b"""\
805 [console_scripts]
806 fancy = fancy:main
807
808 [gui_scripts]
809 fancy-gui = fancy:main
810 """,
811 "WHEEL": b"""\
812 Wheel-Version: 1.0
813 Generator: magic (1.0.0)
814 Root-Is-Purelib: true
815 Tag: py3-none-any
816 """,
817 "METADATA": b"""\
818 Metadata-Version: 2.1
819 Name: fancy
820 Version: 1.0.0
821 Summary: A fancy package
822 Author: Agendaless Consulting
823 Author-email: [email protected]
824 License: MIT
825 Keywords: fancy amazing
826 Platform: UNKNOWN
827 Classifier: Intended Audience :: Developers
828 """,
829 },
830 )
831
832 with pytest.raises(InvalidWheelSource) as ctx:
833 install(
834 source=source,
835 destination=mock_destination,
836 additional_metadata={},
837 )
838
839 assert "fancy-1.0.0.data/invalid/fancy/invalid.py" in str(ctx.value)
840
841 def test_ensure_non_executable_for_additional_metadata(self, mock_destination):
842 # Create a fake wheel
843 source = FakeWheelSource(
844 distribution="fancy",
845 version="1.0.0",
846 regular_files={
847 "fancy/__init__.py": b"""\
848 # put me in purelib
849 """,
850 },
851 dist_info_files={
852 "top_level.txt": b"""\
853 fancy
854 """,
855 "WHEEL": b"""\
856 Wheel-Version: 1.0
857 Generator: magic (1.0.0)
858 Root-Is-Purelib: true
859 Tag: py3-none-any
860 """,
861 "METADATA": b"""\
862 Metadata-Version: 2.1
863 Name: fancy
864 Version: 1.0.0
865 Summary: A fancy package
866 Author: Agendaless Consulting
867 Author-email: [email protected]
868 License: MIT
869 Keywords: fancy amazing
870 Platform: UNKNOWN
871 Classifier: Intended Audience :: Developers
872 """,
873 },
874 )
875 all_contents = list(source.get_contents())
876 source.get_contents = lambda: (
877 (*contents, True) for (*contents, _) in all_contents
878 )
879 install(
880 source=source,
881 destination=mock_destination,
882 additional_metadata={
883 "fun_file.txt": b"this should be in dist-info!",
884 },
885 )
886
887 mock_destination.assert_has_calls(
888 [
889 mock.call.write_file(
890 scheme="purelib",
891 path="fancy/__init__.py",
892 stream=mock.ANY,
893 is_executable=True,
894 ),
895 mock.call.write_file(
896 scheme="purelib",
897 path="fancy-1.0.0.dist-info/METADATA",
898 stream=mock.ANY,
899 is_executable=True,
900 ),
901 mock.call.write_file(
902 scheme="purelib",
903 path="fancy-1.0.0.dist-info/fun_file.txt",
904 stream=mock.ANY,
905 is_executable=False,
906 ),
907 ],
908 any_order=True,
909 )
910
911 def test_skips_pycache_and_warns(self, mock_destination):
912 """Files inside a __pycache__ directory should be skipped and a
913 RuntimeWarning emitted describing the skipped file.
914 """
915 top_pycache_path = "__pycache__/bad.cpython-312.pyc"
916 top_good_path = "good.py"
917 sub_pycache_path = "fancy/__pycache__/bad.cpython-312.pyc"
918 sub_good_path = "fancy/good.py"
919
920 # Create a fake wheel
921 source = FakeWheelSource(
922 distribution="fancy",
923 version="1.0.0",
924 regular_files={
925 top_pycache_path: b"""\
926 # compiled bytecode
927 """,
928 top_good_path: b"""\
929 # good source file
930 """,
931 sub_pycache_path: b"""\
932 # compiled bytecode
933 """,
934 sub_good_path: b"""\
935 # good source file
936 """,
937 },
938 dist_info_files={
939 "top_level.txt": b"""\
940 fancy
941 """,
942 "WHEEL": b"""\
943 Wheel-Version: 1.0
944 Generator: magic (1.0.0)
945 Root-Is-Purelib: true
946 Tag: py3-none-any
947 """,
948 "METADATA": b"""\
949 Metadata-Version: 2.1
950 Name: fancy
951 Version: 1.0.0
952 """,
953 },
954 )
955
956 # Installing should warn and skip the __pycache__ files.
957 with pytest.warns(RuntimeWarning) as record:
958 install(
959 source=source,
960 destination=mock_destination,
961 additional_metadata={},
962 )
963
964 # Ensure warnings mentioning the __pycache__ paths were emitted.
965 messages = [str(w.message) for w in record]
966 assert len(messages) == 2
967 assert any(top_pycache_path in m for m in messages)
968 assert any(sub_pycache_path in m for m in messages)
969
970 # Ensure write_file was not called for the __pycache__ paths.
971 written_files = {
972 kwargs.get("path")
973 for __, kwargs in mock_destination.write_file.call_args_list
974 }
975 assert top_good_path in written_files
976 assert sub_good_path in written_files
977 assert top_pycache_path not in written_files
978 assert sub_pycache_path not in written_files
979
980 # Ensure finalize_installation's records do not include __pycache__ paths.
981 assert mock_destination.finalize_installation.called
982 records = mock_destination.finalize_installation.call_args[1]["records"]
983 record_paths = {
984 rec.path if isinstance(rec, RecordEntry) else rec[0] for __, rec in records
985 }
986 assert top_good_path in record_paths
987 assert sub_good_path in record_paths
988 assert top_pycache_path not in record_paths
989 assert sub_pycache_path not in record_paths