# SPDX-License-Identifier: Apache-2.0 """The build and destroy lesson is a short hub and parts, none of them long (issue #562). The lesson was one page of about 1,300 lines. It is now a hub that keeps the page name and URL, the intro and the banners, plus numbered part pages. The hub must list every part, each part must link back to the hub and stay under a line cap, the manifest must hold the pages as one series with the hub first, and every exercise the hub indexes must point at a part that really holds that heading. The text itself moved verbatim: the parts are compared with a record of the pre-split page (its headings with their line counts and a digest of every fenced code line), not with themselves. """ import re import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts")) import gen_wiki_nav as gwn from test_stale_sibling_statements import split_inventory from wikitext import page_text ROOT = Path(__file__).resolve().parent.parent WIKI = ROOT / "docs" / "wiki" STEM = "Build-and-Destroy-Stack-Per-Service" SERIES = "Build and destroy a stack per service" MAX_PART_LINES = 450 PARTS = ( f"{STEM}-Part-1-Why-and-How-Stacks-Are-Split-and-the-Lab-Script", f"{STEM}-Part-2-The-PowerShell-Lab-and-Exercises-1-to-4", f"{STEM}-Part-3-Exercises-5-to-8-and-the-Cloud-Lab", f"{STEM}-Part-4-Spacelift-Break-Detect-Defend-and-Claims", ) LINK = re.compile(r"\]\(([A-Za-z0-9_-]+)\.md(?:#([^)]*))?\)") def part_names(wiki: Path) -> list[str]: return sorted(p.stem for p in wiki.glob(f"{STEM}-Part-*.md")) def split_problems(wiki: Path, cap: int = MAX_PART_LINES, expected: tuple[str, ...] = PARTS) -> list[str]: """Every way the split is broken: a part missing or extra, one the hub does not list, a long part, no way back.""" parts = part_names(wiki) problems = [] if tuple(parts) != tuple(sorted(expected)): problems.append(f"parts are {parts}, expected {sorted(expected)}") hub = page_text(wiki / f"{STEM}.md") linked = [name for name, _ in LINK.findall(hub)] for name in parts: if name not in linked: problems.append(f"the hub does not list {name}") text = page_text(wiki / f"{name}.md") lines = len(text.splitlines()) if lines > cap: problems.append(f"{name} is {lines} lines, over {cap}") if f"]({STEM}.md)" not in text: problems.append(f"{name} does not link back to the hub") return problems def test_the_hub_lists_every_part_and_each_part_is_short(): assert part_names(WIKI) == sorted(PARTS) assert split_problems(WIKI) == [] def test_every_exercise_the_hub_indexes_is_a_heading_of_the_part_it_names(): hub = page_text(WIKI / f"{STEM}.md") index = [(n, a) for n, a in LINK.findall(hub) if n.startswith(f"{STEM}-Part-") and a] assert len(index) >= 8, "the hub indexes fewer than the eight exercises" for name, anchor in index: text = page_text(WIKI / f"{name}.md") assert anchor in {gwn.slug(h) for h in re.findall(r"^#{2,3} (.+)$", text, re.MULTILINE)}, (name, anchor) def test_the_manifest_holds_hub_and_parts_as_one_series_hub_first(): m = gwn.read_manifest(ROOT) group = next(g["id"] for g in gwn.groups_of(m) if any(e["page"] == STEM for e in gwn.entries(m, g["id"]))) rows = [e for e in gwn.entries(m, group) if e.get("series") == SERIES] assert [e["page"] for e in rows] == [STEM, *PARTS] def test_the_split_check_bites(tmp_path): wiki = tmp_path for name in (STEM, *(f"{STEM}-Part-{i}-X" for i in (1, 2, 3))): body = "\n".join(["line"] * 10) (wiki / f"{name}.md").write_text(f"# t\n\n{body}\n[hub]({STEM}.md)\n", encoding="utf-8") hub = "\n".join(f"- [p]({STEM}-Part-{i}-X.md)" for i in (1, 2, 3)) (wiki / f"{STEM}.md").write_text(f"# hub\n\n{hub}\n", encoding="utf-8") fake = tuple(f"{STEM}-Part-{i}-X" for i in (1, 2, 3)) assert split_problems(wiki, expected=fake) == [] assert any("expected" in p for p in split_problems(wiki, expected=(*fake, f"{STEM}-Part-4-X"))) (wiki / f"{STEM}-Part-2-X.md").write_text("\n".join(["line"] * 500) + f"\n[hub]({STEM}.md)\n", encoding="utf-8") assert any("over" in p for p in split_problems(wiki, expected=fake)) (wiki / f"{STEM}.md").write_text(f"# hub\n\n- [p]({STEM}-Part-1-X.md)\n", encoding="utf-8") assert any("does not list" in p for p in split_problems(wiki, expected=fake)) (wiki / f"{STEM}-Part-3-X.md").write_text("# t\n", encoding="utf-8") assert any("does not link back" in p for p in split_problems(wiki, expected=fake)) # Record of the old single page (before the split into parts, issue #562), taken from the commit before it # (git show ce040cc:docs/wiki/Build-and-Destroy-Stack-Per-Service.md): each `##`/`###` heading with its non-blank lines. SPLIT_SECTIONS = ( ('What you will be able to do', 8), ('What the wiki already says (and the gap)', 6), ('Why one stack per service', 10), ('What splitting costs', 10), ('What the layout looks like', 8), ('Layout conventions worth copying', 10), ('Passing values between stacks', 9), ('Build order and reverse destroy order', 4), ('Tag everything, so teardown can be proven', 5), ('Cost counters: a number per stack, and one for the whole', 5), ('How this project does it', 8), ('If you are terraforming an entire infrastructure', 10), ('Lessons-learned: what makes a destroy fail or leave a bill', 10), ('Free path: five stacks in a scratch folder', 0), ('Cost and teardown (read first)', 11), ('What the lab is', 1), ('Step 0: write the lab', 393), ('Exercise 1: predict before you build', 12), ('Exercise 2: build all five, in order', 22), ('Exercise 3: destroy all five, in reverse, and prove it', 24), ('Exercise 4: five full cycles', 13), ('Exercise 5: destroy in the wrong order, on purpose', 39), ('Exercise 6: a forgotten resource and an untagged one', 30), ('Exercise 7: read how this project pins its addresses (read-only)', 13), ('Optional: the same shape on your own cloud account', 2), ('Cost and teardown (read first)', 77), ('The files', 152), ('Exercise 8: build and destroy the cloud set', 22), ('Optional: run the same stacks through Spacelift', 1), ('Build, break, detect, evade, defend', 10), ('Flip the board', 7), ('Common mistakes', 8), ('Claims register', 20), ) SPLIT_CODE_DIGEST = '31bdfbc61e6a7eac07fa0ab31298ddbac654722403e94ee276c171834a75a3c7' def part_inventory(texts: list[str]) -> tuple[list[tuple[str, int]], str]: """The sections of the parts, with the headings the split added folded into the section they continue.""" sections, digest = split_inventory(texts) merged: list[list] = [] for heading, count in sections: if heading.startswith("Free path, continued") and merged: merged[-1][1] += count else: merged.append([heading, count]) return [(h, n) for h, n in merged], digest def part_texts() -> list[str]: return [page_text(WIKI / f"{name}.md") for name in PARTS] def test_the_parts_still_hold_every_section_of_the_old_page_once(): sections, digest = part_inventory(part_texts()) assert tuple(sections) == SPLIT_SECTIONS assert digest == SPLIT_CODE_DIGEST assert sum(h.startswith("Exercise ") for h, _ in sections) == 8 def test_the_inventory_bites_when_a_section_is_lost_repeated_or_a_code_line_changes(): texts = part_texts() base = part_inventory(texts) cut = re.sub(r"### Exercise 5:.*?(?=### Exercise 6:)", "", texts[2], flags=re.DOTALL) assert cut != texts[2] assert part_inventory([texts[0], texts[1], cut, texts[3]]) != base assert part_inventory([*texts, texts[0]]) != base changed = texts[2].replace("```bash", "```bash\n# extra", 1) assert changed != texts[2] assert part_inventory([texts[0], texts[1], changed, texts[3]])[1] != base[1]