test_plans_state_drift_split.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # SPDX-License-Identifier: Apache-2.0
  2. """The plans, state, drift and recovery lesson is a short hub and parts, none of them long (issue #562).
  3. The lesson was one page of about 1,400 lines. It is now a hub that keeps the page name and URL, the intro and the
  4. banners, plus numbered part pages. The hub must list every part, each part must link back to the hub and stay under
  5. a line cap, the manifest must hold the pages as one series with the hub first, and every exercise the hub indexes must
  6. point at a part that really holds that heading. The text itself moved verbatim; that was proved once by script at the
  7. split, since the old single page no longer exists to compare against.
  8. """
  9. import re
  10. import sys
  11. from pathlib import Path
  12. sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts"))
  13. import gen_wiki_nav as gwn
  14. from wikitext import page_text
  15. ROOT = Path(__file__).resolve().parent.parent
  16. WIKI = ROOT / "docs" / "wiki"
  17. STEM = "Plans-State-Drift-and-Recovery"
  18. SERIES = "Plans, state, drift and recovery"
  19. MAX_PART_LINES = 450
  20. PARTS = (
  21. f"{STEM}-Part-1-Reading-Plans-and-State",
  22. f"{STEM}-Part-2-State-Locking-and-Drift-Exercises",
  23. f"{STEM}-Part-3-Moving-and-Losing-State",
  24. f"{STEM}-Part-4-The-Drill-and-the-Checkpoint",
  25. )
  26. LINK = re.compile(r"\]\(([A-Za-z0-9_-]+)\.md(?:#([^)]*))?\)")
  27. def part_names(wiki: Path) -> list[str]:
  28. return sorted(p.stem for p in wiki.glob(f"{STEM}-Part-*.md"))
  29. def split_problems(wiki: Path, cap: int = MAX_PART_LINES, expected: tuple[str, ...] = PARTS) -> list[str]:
  30. """Every way the split is broken: a part missing or extra, one the hub does not list, a long part, no way back."""
  31. parts = part_names(wiki)
  32. problems = []
  33. if tuple(parts) != tuple(sorted(expected)):
  34. problems.append(f"parts are {parts}, expected {sorted(expected)}")
  35. hub = page_text(wiki / f"{STEM}.md")
  36. linked = [name for name, _ in LINK.findall(hub)]
  37. for name in parts:
  38. if name not in linked:
  39. problems.append(f"the hub does not list {name}")
  40. text = page_text(wiki / f"{name}.md")
  41. lines = len(text.splitlines())
  42. if lines > cap:
  43. problems.append(f"{name} is {lines} lines, over {cap}")
  44. if f"]({STEM}.md)" not in text:
  45. problems.append(f"{name} does not link back to the hub")
  46. return problems
  47. def test_the_hub_lists_every_part_and_each_part_is_short():
  48. assert part_names(WIKI) == sorted(PARTS)
  49. assert split_problems(WIKI) == []
  50. def test_every_exercise_the_hub_indexes_is_a_heading_of_the_part_it_names():
  51. hub = page_text(WIKI / f"{STEM}.md")
  52. index = [(n, a) for n, a in LINK.findall(hub) if n.startswith(f"{STEM}-Part-") and a]
  53. assert len(index) >= 11, "the hub indexes fewer than the eleven exercises"
  54. for name, anchor in index:
  55. text = page_text(WIKI / f"{name}.md")
  56. assert anchor in {gwn.slug(h) for h in re.findall(r"^#{2,3} (.+)$", text, re.MULTILINE)}, (name, anchor)
  57. def test_the_manifest_holds_hub_and_parts_as_one_series_hub_first():
  58. m = gwn.read_manifest(ROOT)
  59. group = next(g["id"] for g in gwn.groups_of(m) if any(e["page"] == STEM for e in gwn.entries(m, g["id"])))
  60. rows = [e for e in gwn.entries(m, group) if e.get("series") == SERIES]
  61. assert [e["page"] for e in rows] == [STEM, *PARTS]
  62. def test_the_split_check_bites(tmp_path):
  63. wiki = tmp_path
  64. for name in (STEM, *(f"{STEM}-Part-{i}-X" for i in (1, 2, 3))):
  65. body = "\n".join(["line"] * 10)
  66. (wiki / f"{name}.md").write_text(f"# t\n\n{body}\n[hub]({STEM}.md)\n", encoding="utf-8")
  67. hub = "\n".join(f"- [p]({STEM}-Part-{i}-X.md)" for i in (1, 2, 3))
  68. (wiki / f"{STEM}.md").write_text(f"# hub\n\n{hub}\n", encoding="utf-8")
  69. fake = tuple(f"{STEM}-Part-{i}-X" for i in (1, 2, 3))
  70. assert split_problems(wiki, expected=fake) == []
  71. assert any("expected" in p for p in split_problems(wiki, expected=(*fake, f"{STEM}-Part-4-X")))
  72. (wiki / f"{STEM}-Part-2-X.md").write_text("\n".join(["line"] * 500) + f"\n[hub]({STEM}.md)\n", encoding="utf-8")
  73. assert any("over" in p for p in split_problems(wiki, expected=fake))
  74. (wiki / f"{STEM}.md").write_text(f"# hub\n\n- [p]({STEM}-Part-1-X.md)\n", encoding="utf-8")
  75. assert any("does not list" in p for p in split_problems(wiki, expected=fake))
  76. (wiki / f"{STEM}-Part-3-X.md").write_text("# t\n", encoding="utf-8")
  77. assert any("does not link back" in p for p in split_problems(wiki, expected=fake))