test_wiki_navigation.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """Navigation checks for the wiki pages in docs/wiki/.
  2. A page nobody links to is a page nobody reads. These tests keep Contents (every page),
  3. the Reading-Order page and the "Next:" lines honest as pages are added.
  4. """
  5. import re
  6. import sys
  7. import tempfile
  8. from pathlib import Path
  9. ROOT = Path(__file__).resolve().parent.parent
  10. sys.path.insert(0, str(ROOT / "scripts"))
  11. import gen_wiki_nav as gwn
  12. WIKI = Path(__file__).resolve().parent.parent / "docs" / "wiki"
  13. PAGE_LINK = re.compile(r"\]\(([A-Za-z0-9_-]+)\.md(?:#[^)]*)?\)")
  14. EXEMPT = {"_Sidebar", "_Footer", "Home"}
  15. def pages() -> set[str]:
  16. return {p.stem for p in WIKI.glob("*.md")}
  17. def linked(name: str) -> list[str]:
  18. return PAGE_LINK.findall((WIKI / f"{name}.md").read_text())
  19. def test_every_page_is_in_contents_or_reading_order():
  20. reachable = set(linked("Contents")) | set(linked("Reading-Order"))
  21. missing = sorted(pages() - EXEMPT - reachable)
  22. assert not missing, f"not in Contents or Reading-Order: {missing}"
  23. def test_every_page_the_sidebar_links_exists():
  24. missing = sorted(set(linked("_Sidebar")) - pages())
  25. assert not missing, f"sidebar links to missing pages: {missing}"
  26. def test_every_page_named_in_a_next_line_exists():
  27. """The Next: lines are written by the publish step (issue #521), so they are read from its output."""
  28. problems, lines = [], 0
  29. with tempfile.TemporaryDirectory() as out:
  30. gwn.publish_pages(ROOT, Path(out))
  31. for page in sorted(Path(out).glob("*.md")):
  32. for line in page.read_text().splitlines():
  33. if "Next:" not in line:
  34. continue
  35. lines += 1
  36. for target in PAGE_LINK.findall(line):
  37. if target not in pages():
  38. problems.append(f"{page.name}: {target}")
  39. assert lines >= 200, f"the guard saw only {lines} Next lines"
  40. assert not problems, f"Next lines name missing pages: {problems}"
  41. def test_the_guard_for_next_lines_bites_on_a_missing_page():
  42. assert PAGE_LINK.findall("Previous: [A](A.md) / Next: [B](No-Such-Page-Here.md)") == ["A", "No-Such-Page-Here"]
  43. assert "No-Such-Page-Here" not in pages()
  44. def test_reading_order_lists_no_page_twice_within_one_path():
  45. text = (WIKI / "Reading-Order.md").read_text()
  46. sections = re.split(r"^## ", text, flags=re.MULTILINE)[1:]
  47. checked = 0
  48. for section in sections:
  49. title = section.splitlines()[0]
  50. rows = [
  51. line
  52. for line in section.splitlines()
  53. if re.match(r"\|\s*\d+\s*\|", line)
  54. ]
  55. if not rows:
  56. continue
  57. checked += 1
  58. targets = [PAGE_LINK.search(row).group(1) for row in rows if PAGE_LINK.search(row)]
  59. dupes = sorted({t for t in targets if targets.count(t) > 1})
  60. assert not dupes, f"{title}: listed twice: {dupes}"
  61. assert checked >= 5, "expected the core path and at least four tracks"
  62. def test_reading_order_pages_all_exist():
  63. missing = sorted(set(linked("Reading-Order")) - pages())
  64. assert not missing, f"Reading-Order links to missing pages: {missing}"