MCPcopy
hub / github.com/pex-tool/pex / test_check

Function test_check

tests/test_pex_builder.py:522–607  ·  view source on GitHub ↗
(tmpdir)

Source from the content-addressed store, hash-verified

520 reason="The cat binary is required for this test.",
521)
522def test_check(tmpdir):
523 # type: (Any) -> None
524
525 def assert_perform_check_zip64_handling(
526 zipapp, # type: str
527 test_run=True, # type: bool
528 ):
529 # type: (...) -> None
530 assert Check.NONE.perform_check(Layout.ZIPAPP, zipapp) is None
531 assert Check.ERROR.perform_check(Layout.PACKED, zipapp) is None
532 assert Check.ERROR.perform_check(Layout.LOOSE, zipapp) is None
533
534 # Python 3.13 newly supports ZIP64 in `zipimport.zipimporter`.
535 if sys.version_info >= (3, 13):
536 assert Check.WARN.perform_check(Layout.ZIPAPP, zipapp) is True
537 assert Check.ERROR.perform_check(Layout.ZIPAPP, zipapp) is True
538 if test_run:
539 subprocess.check_call(args=[sys.executable, zipapp])
540 else:
541 expected_error_message_re = r"The PEX zip at {path} is not a valid zipapp: ".format(
542 path=re.escape(zipapp)
543 )
544 with pytest.warns(PEXWarning, match=expected_error_message_re):
545 assert Check.WARN.perform_check(Layout.ZIPAPP, zipapp) is False
546 with pytest.raises(InvalidZipAppError, match=expected_error_message_re):
547 Check.ERROR.perform_check(Layout.ZIPAPP, zipapp)
548
549 if test_run:
550 assert subprocess.call(args=[sys.executable, zipapp]) != 0
551
552 @contextmanager
553 def write_zipapp(path):
554 # type: (str) -> Iterator[ZipFile]
555 with open_zip(path, "w") as zip_file:
556 yield zip_file
557 zip_file.writestr("__main__.py", "print('BOOTED')")
558
559 # N.B.: This test creates very large files in /tmp; so to relieve possible memory pressure in
560 # tmpfs-based /tmp filesystems, we unlink files as soon as we don't need them instead of waiting
561 # for tmpdir fixture cleanup at the end of the test.
562
563 file_too_big = os.path.join(str(tmpdir), "file_too_big.py")
564 with open(file_too_big, "w") as fp:
565 fp.write("\n")
566 accum = file_too_big + ".accum"
567 for _ in range(32):
568 safe_rename(file_too_big, accum)
569 with open(file_too_big, "wb") as dest:
570 subprocess.check_call(args=["cat", accum, accum], stdout=dest.fileno())
571 os.unlink(accum)
572 assert os.path.getsize(file_too_big) > 0xFFFFFFFF, (
573 "ZIP64 must be used if file sizes are bigger than 0xFFFFFFFF per "
574 "https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT 4.3.9.2"
575 )
576
577 zipapp_too_big = os.path.join(str(tmpdir), "too-big.pyz")
578 with write_zipapp(zipapp_too_big) as zf:
579 zf.write(file_too_big, os.path.basename(file_too_big))

Callers

nothing calls this directly

Calls 8

safe_renameFunction · 0.90
write_zipappFunction · 0.85
perform_checkMethod · 0.80
joinMethod · 0.45
writeMethod · 0.45
encodeMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected