test_zip_unix_modes.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from __future__ import annotations
  2. import sys
  3. import zipfile
  4. from pathlib import Path
  5. TOOLS = Path(__file__).resolve().parents[3] / "tools"
  6. sys.path.insert(0, str(TOOLS))
  7. from package_hth_review import write_zip_file, zip_file_mode # noqa: E402
  8. from zip_unix_modes import extract_zip_with_unix_modes # noqa: E402
  9. def test_zip_file_mode_marks_scripts_executable(tmp_path):
  10. sh = tmp_path / "run.sh"
  11. sh.write_text("#!/bin/sh\n", encoding="utf-8")
  12. py = tmp_path / "tool.py"
  13. py.write_bytes(b"#!/usr/bin/env python3\nprint(1)\n")
  14. txt = tmp_path / "note.txt"
  15. txt.write_text("x\n", encoding="utf-8")
  16. assert zip_file_mode(sh) == 0o755
  17. assert zip_file_mode(py) == 0o755
  18. assert zip_file_mode(txt) == 0o644
  19. def test_extract_restores_unix_modes(tmp_path):
  20. zpath = tmp_path / "m.zip"
  21. with zipfile.ZipFile(zpath, "w") as zf:
  22. write_zip_file(zf, "bin/run.sh", b"#!/bin/sh\necho ok\n", 0o755)
  23. write_zip_file(zf, "readme.txt", b"hello\n", 0o644)
  24. dest = tmp_path / "out"
  25. extract_zip_with_unix_modes(zpath, dest)
  26. sh = dest / "bin" / "run.sh"
  27. txt = dest / "readme.txt"
  28. assert sh.is_file()
  29. assert txt.is_file()
  30. if sys.platform != "win32":
  31. assert sh.stat().st_mode & 0o777 == 0o755
  32. assert txt.stat().st_mode & 0o777 == 0o644