test_stale_sibling_statements.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # SPDX-License-Identifier: Apache-2.0
  2. """Pages that were written beside their siblings no longer say the siblings may not exist (issue #444).
  3. Several lessons were written while the pages they point at did not exist yet, so they carried notes such as "the link
  4. may not resolve until the pages merge", "named in plain text, not linked" or "planned lesson". Every one of those pages
  5. is in the wiki now. A note that says otherwise tells the reader a working link is dead, so each phrase below must be
  6. gone, and each page that named its siblings in plain text must link them to a file that exists. The pages with their own
  7. lesson test have their checks there; these are the pages that had none.
  8. """
  9. import re
  10. from pathlib import Path
  11. from wikitext import page_text
  12. WIKI = Path(__file__).resolve().parent.parent / "docs" / "wiki"
  13. LINK = re.compile(r"\]\(([A-Za-z0-9_-]+)\.md(?:#[^)]*)?\)")
  14. # page stem -> phrases that were true before the siblings existed and are false now
  15. STALE = {
  16. "LAMP-and-LEMP-From-Zero": ("may not be merged yet", "links can be dead"),
  17. "WordPress-and-PHP-Security-WAF-and-Static-Sites": ("may not resolve until they are merged",),
  18. "WordPress-at-Scale-CDN-WAF-and-CI": ("may not open yet", "the same caution about links"),
  19. "DNS-Cloudflare-and-Hosting-Your-Own-Website": ("If it is not merged yet", "lead nowhere", "written at the same time", "once it is merged"),
  20. "Rate-Limiting-at-Every-Layer": ("may not resolve until", "written at the same time"),
  21. "IDS-and-IPS-Snort-and-Suricata": ("may not resolve until", "are written at the same time", "They may not resolve"),
  22. "File-Integrity-and-Alerting-Tripwire-AIDE-Wazuh": ("may not resolve until", "same caveat"),
  23. "Packet-Filters-Compared-iptables-nftables-ufw-pf": ("may not resolve until", "being written at the same time"),
  24. "Security-Tools-Basics-Nmap-OpenVAS-Burp-Metasploit": ("may not resolve until", "written at the same time as this one"),
  25. "Red-and-Blue-Yin-and-Yang-and-Your-Lab-OS": ("may not resolve until lesson 2 merges", "written alongside this lesson", "when it merges"),
  26. "OpenTofu-From-Zero": ("may not resolve yet", "are written alongside this page"),
  27. "Plans-State-Drift-and-Recovery": ("may not be merged yet", "Planned lessons in this infrastructure-as-code track", "the link will resolve once it is"),
  28. "The-CIA-Triad-Confidentiality-Integrity-Availability": ("are not written yet", "named, not linked"),
  29. "Build-and-Destroy-Stack-Per-Service": ("being written at the same time", "a planned lesson", "once the lessons are merged"),
  30. "Spacelift-From-Zero": ("plain text here until they merge", "named but not linked", "not linked until it merges"),
  31. }
  32. # The infrastructure-as-code track: Spacelift from zero names all three of its siblings, and each must be a link.
  33. TRACK = ("OpenTofu-From-Zero", "Plans-State-Drift-and-Recovery", "Build-and-Destroy-Stack-Per-Service")
  34. def stale_phrases(body: str, phrases: tuple[str, ...]) -> list[str]:
  35. return [p for p in phrases if p in body]
  36. def test_no_page_still_says_its_existing_siblings_may_not_exist():
  37. assert STALE, "the guard collected no pages"
  38. for stem, phrases in STALE.items():
  39. path = WIKI / f"{stem}.md"
  40. assert path.is_file(), stem
  41. assert not stale_phrases(page_text(path), phrases), (stem, stale_phrases(page_text(path), phrases))
  42. def test_spacelift_from_zero_links_the_other_lessons_of_its_track():
  43. body = page_text(WIKI / "Spacelift-From-Zero.md")
  44. links = set(LINK.findall(body))
  45. for stem in TRACK:
  46. assert stem in links, f"{stem} exists and must be linked"
  47. assert (WIKI / f"{stem}.md").is_file(), stem
  48. def test_the_guard_bites():
  49. assert stale_phrases("their links may not resolve until the pages merge", ("may not resolve until",))
  50. assert not stale_phrases("their links resolve", ("may not resolve until",))
  51. assert "stem" not in set(LINK.findall("see [x](other.md)"))