test_diagram_aspect.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """No diagram picture is a tall, narrow strip.
  2. GitHub shows a wiki picture at its native pixel width up to the column width, so a
  3. picture taller than 1.6 times its width fills several screens with oversized text
  4. (issue #280: three flowcharts rendered 2 to 4.7 times taller than wide). Lay such
  5. a diagram out wide (flowchart LR, or subgraph rows) instead of raising the limit.
  6. Only generated diagrams are checked: those listed in docs/diagrams.json.
  7. """
  8. import json
  9. import struct
  10. from pathlib import Path
  11. ROOT = Path(__file__).resolve().parent.parent
  12. MAX_RATIO = 1.6
  13. # Diagram pictures allowed to exceed MAX_RATIO, each with a reason. Empty: none needed.
  14. ALLOWLIST: dict[str, str] = {}
  15. FIXED = (
  16. "docs/wiki/images/droplet-hardening-diagram-1.png",
  17. "docs/wiki/images/ci-tests-and-cost-gate-diagram-2.png",
  18. "docs/wiki/images/spacelift-diagram-1.png",
  19. )
  20. def png_size(path: Path) -> tuple[int, int]:
  21. """Width and height from the PNG IHDR chunk (standard library only; Pillow is not a test dependency)."""
  22. head = path.read_bytes()[:24]
  23. assert head[:8] == b"\x89PNG\r\n\x1a\n" and head[12:16] == b"IHDR", f"{path} is not a PNG"
  24. return struct.unpack(">II", head[16:24])
  25. def ratio(rel: str) -> float:
  26. width, height = png_size(ROOT / rel)
  27. return height / width
  28. def test_no_diagram_picture_is_taller_than_the_limit():
  29. manifest = json.loads((ROOT / "docs" / "diagrams.json").read_text())
  30. tall = {rel: round(ratio(rel), 2) for rel in manifest if rel not in ALLOWLIST and ratio(rel) > MAX_RATIO}
  31. assert not tall, f"diagram pictures taller than {MAX_RATIO} x their width (re-lay them out wide): {tall}"
  32. def test_the_allowlist_names_only_live_pictures_and_gives_reasons():
  33. manifest = json.loads((ROOT / "docs" / "diagrams.json").read_text())
  34. for rel, reason in ALLOWLIST.items():
  35. assert rel in manifest and reason.strip()
  36. def test_the_three_reported_diagrams_are_now_wide_enough():
  37. for rel in FIXED:
  38. assert ratio(rel) <= MAX_RATIO, rel
  39. def test_the_reader_sees_a_tall_picture(tmp_path):
  40. ihdr = struct.pack(">II", 100, 200) + b"\x08\x02\x00\x00\x00"
  41. tall = tmp_path / "t.png"
  42. tall.write_bytes(b"\x89PNG\r\n\x1a\n" + struct.pack(">I", 13) + b"IHDR" + ihdr)
  43. width, height = png_size(tall)
  44. assert height / width > MAX_RATIO