"""What the wiki states about the tools holds against them. Cited scripts and options exist, pipeline steps and skip flags match the run, samples and excerpts come from a real run. """ from __future__ import annotations import tests from tests.mdsections import FENCED from tests.sources import declared_options import ast import re import sys import unittest from pathlib import Path from tests.docpagessupport import ( MARKETING_WORDS, PUBLISHED, ROOT, WIKI, _page, _registry, _schema, _section, ) class TestSkeletonReadsTheResultShape(unittest.TestCase): """The skeleton indexed verify_platform's result as a list, and a test copied from it stopped on KeyError: 0 before any assertion.""" def test_every_key_it_reads_is_one_verify_platform_returns(self): from packs.coverage import verify_platform result = verify_platform( {"platform": "x", "verification_mode": "existence", "systems": {}}, {"files": {}, "indexes": {}}, emu_profiles={}, ) text = _page("testing-guide.md").split("Example skeleton:", 1)[1] code = re.search(r"```python\n(.*?)```", text, re.S).group(1) read = [ node.slice for node in ast.walk(ast.parse(code)) if isinstance(node, ast.Subscript) and isinstance(node.value, ast.Name) and node.value.id == "result" ] self.assertTrue(read, "the skeleton no longer reads the result") for key in read: self.assertIsInstance(key, ast.Constant) self.assertIn(key.value, result) class ToolsRunOnTheStatedFloor(unittest.TestCase): """tools.md and testing-guide.md state Python 3.9 for every script, and check_release_assets.py built `int | None` at import: a TypeError on 3.9 before any argument was read, in check_release.py too. CI runs 3.12 only, so this test reads the source for what 3.9 cannot evaluate.""" TYPES = {"int", "str", "float", "bytes", "bool", "dict", "list", "tuple", "set", "type"} def _type_like(self, node: ast.AST) -> bool: if isinstance(node, ast.Constant) and node.value is None: return True if isinstance(node, ast.Subscript): node = node.value if isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr): return self._type_like(node.left) or self._type_like(node.right) return isinstance(node, ast.Name) and node.id in self.TYPES def _runtime_unions(self, tree: ast.AST) -> list[int]: annotations: set[int] = set() for node in ast.walk(tree): for field in ("annotation", "returns"): value = getattr(node, field, None) if value is not None: annotations.update(id(n) for n in ast.walk(value)) return [ node.lineno for node in ast.walk(tree) if id(node) not in annotations and (isinstance(node, ast.Match) or isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr) and (self._type_like(node.left) or self._type_like(node.right))) ] def test_pages_state_the_floor(self): self.assertIn("Python 3.9 or newer", _page("tools.md")) self.assertIn("Python 3.9 or newer", _page("testing-guide.md")) def test_no_script_evaluates_310_syntax(self): offenders = [ f"{path.relative_to(ROOT)}:{line}" for path in sorted((ROOT / "scripts").rglob("*.py")) for line in self._runtime_unions(ast.parse(path.read_text(encoding="utf-8"))) ] self.assertEqual(offenders, [], "\n".join(offenders)) def test_the_check_catches_the_alias(self): self.assertTrue(self._runtime_unions(ast.parse("F = tuple[str, int | None]"))) self.assertFalse(self._runtime_unions(ast.parse("def f(x: int | None) -> str | None: ..."))) self.assertFalse(self._runtime_unions(ast.parse("mask = flags | other"))) class DeriveFiguresLiveWithTheTool(unittest.TestCase): """tools.md gave --derive about 90 s and 67 entries while its --help said about 35 s: two hand-kept figures for one run, measured at 33 s.""" def test_the_page_carries_no_duration_or_count(self): paragraph = next( p for p in _page("tools.md").split("\n\n") if "`--derive` reads" in p ) self.assertNotRegex(paragraph, r"\d+ ?s\b|\d+ entries") self.assertIn("derivable.json", paragraph) class EmulatorVerboseSample(unittest.TestCase): """advanced-usage.md showed `handy validates size=512 crc32=0x0d973c9d [src/handy/system.h:45]`, a format verify.py does not print and a reference handy.yml does not cite.""" def test_every_sample_line_is_printed(self): import subprocess section = _section(_page("advanced-usage.md"), "Per-emulator checks") sample = [body for lang, body in re.findall(r"(?ms)^```(\w*)\n(.*?)^```", section) if not lang][-1] printed = subprocess.run( [sys.executable, "scripts/verify.py", "--emulator", "handy", "--verbose"], cwd=ROOT, capture_output=True, text=True, check=True, ).stdout.splitlines() for line in sample.splitlines(): with self.subTest(line=line): self.assertIn(line, printed) class CiTriggersPointToTheirLists(unittest.TestCase): """The CI table listed six directories for validate.yml, which also runs on the installer, wiki/ and every other site input: a contributor touching only wiki/ read that no check would run.""" def test_the_table_does_not_copy_a_path_list(self): import yaml section = _section(_page("architecture.md"), "CI workflows") self.assertNotRegex(section, r"on bios/, platforms/|\(platforms, emulators, wiki, scripts\)") self.assertIn("`paths:`", section) self.assertIn("test_workflow_paths.py", section) workflow = yaml.safe_load((ROOT / ".github/workflows/validate.yml").read_text(encoding="utf-8")) paths = workflow.get("on", workflow.get(True))["pull_request"]["paths"] for named in ("wiki/**", "install.sh"): self.assertIn(named, paths) self.assertIn(f"`{named.rstrip('*')}`", section) class ScratchFilesGoToRepoTmp(unittest.TestCase): """adding-a-scraper.md wrote its comparison to /tmp while two other pages say /tmp is a RAM disk and send scratch files to the repo's tmp/.""" @staticmethod def _code_lines(text: str) -> list[str]: lines, inside = [], False for line in text.splitlines(): if line.strip().startswith("```"): inside = not inside elif inside: lines.append(line) return lines def test_no_command_writes_to_slash_tmp(self): offenders = [ f"{page.name}: {line.strip()}" for page in PUBLISHED for line in self._code_lines(page.read_text(encoding="utf-8")) if re.search(r"(?*{}]|myplatform|\bNAME\b|\.\.\.") # Folders the site build writes, ignored by git and absent from a fresh clone. BUILT = ("website/src/content/docs", "website/dist") def _schema_keys(schema: object, keys: set[str]) -> set[str]: if isinstance(schema, dict): keys.update(schema.get("properties", {})) for value in schema.values(): _schema_keys(value, keys) elif isinstance(schema, list): for value in schema: _schema_keys(value, keys) return keys def _code_spans(text: str) -> list[str]: """Inline code and every line of fenced code.""" spans = re.findall(r"`([^`\n]+)`", FENCED.sub("", text)) for block in FENCED.findall(text): spans.extend(block.splitlines()[1:-1]) return spans def missing_repo_paths(text: str) -> list[str]: missing = [] for span in _code_spans(text): for token in re.findall(r"[\w./-]+", span): if PLACEHOLDER.search(span) and "myplatform" in token: continue path = re.sub(r":[\d,-]+$", "", token).rstrip(".,") if path.startswith(REPO_DIRS) and not PLACEHOLDER.search(path): if not (ROOT / path).exists() and not path.startswith(BUILT): missing.append(path) elif re.fullmatch(r"test_\w+\.py", path) and not (ROOT / "tests" / path).exists(): missing.append(path) return sorted(set(missing)) def _script_source(line: str) -> tuple[str, str] | None: """The command a line runs and the source its options must come from.""" match = re.search(r"python3? (scripts/[\w/]+\.py)", line) if match: return match.group(1), (ROOT / match.group(1)).read_text(encoding="utf-8") \ if (ROOT / match.group(1)).is_file() else "" match = re.search(r"python3? -m (scripts\.scraper\.[\w.]+)", line) if match: module = match.group(1) path = ROOT / module.replace(".", "/") # A package runs its __main__ and reads its options across its modules. files = sorted(path.glob("*.py")) if path.is_dir() else [path.with_suffix(".py")] source = "".join(f.read_text(encoding="utf-8") for f in files if f.is_file()) if "scraper_cli" in source or "myplatform" in module: source += (ROOT / "scripts/scraper/base_scraper.py").read_text(encoding="utf-8") if "target_cli" in source: source += (ROOT / "scripts/scraper/targets/__init__.py").read_text(encoding="utf-8") return module, source return None def unknown_commands(text: str) -> list[str]: wrong = [] for span in _code_spans(text): for command in re.split(r"\||&&|;", span): found = _script_source(command) if found is None: continue name, source = found if not source: wrong.append(name) continue wrong.extend( f"{name} {option}" for option in re.findall(r"(? set[str]: """`key:` and `key: value` spans: the page naming a YAML key.""" return { m.group(1) for span in re.findall(r"`([^`\n]+)`", FENCED.sub("", text)) if (m := re.fullmatch(r"([a-z][a-z0-9_]*):(?:\s.*)?", span)) } class PagesNameWhatExists(unittest.TestCase): """The pages named a target scraper PLATFORM_NAME the discovery does not use, a docs/ folder the site left, exporter methods that no longer exist and profile fields in the wrong table. Every path, script and option a page names is looked up.""" def test_every_repository_path_exists(self): for name in CONTRIBUTOR_PAGES: with self.subTest(page=name): self.assertEqual(missing_repo_paths(_page(name)), []) def test_every_command_and_option_exists(self): for name in CONTRIBUTOR_PAGES: with self.subTest(page=name): self.assertEqual(unknown_commands(_page(name)), []) def test_the_checks_catch_the_old_text(self): self.assertEqual(missing_repo_paths("`scripts/scraper/targets/foo_targets.py`"), ["scripts/scraper/targets/foo_targets.py"]) self.assertEqual(unknown_commands("```bash\npython scripts/verify.py --no-such-flag\n```"), ["scripts/verify.py --no-such-flag"]) def test_every_yaml_key_a_page_names_is_a_schema_key(self): keys = set() for name in ("emulator", "platform", "registry"): _schema_keys(_schema(name), keys) keys |= {key for entry in _registry().values() for key in entry} for name in ("profiling.md", "reference-terms.md", "verification-modes.md"): with self.subTest(page=name): self.assertEqual(sorted(fields_of_colon_tokens(_page(name)) - keys), []) self.assertEqual(fields_of_colon_tokens("`bios_md5: abc`"), {"bios_md5"}) # A command a page gives: `python scripts/X.py ...` or `python -m scripts.x.y ...`, # up to a comment, a pipe or the end of the line. A placeholder module # (`myplatform_scraper`) is what a guide asks the reader to write. _COMMAND = re.compile( r"python3? (?:(scripts/[\w/]+\.py)|-m (scripts(?:\.\w+)+)(?![.*\w]))((?:[ \t]+[^\s#|&;`]+)*)" ) _OPTION = re.compile(r"(? list[str]: found = [] for number, target, module, _args in _commands(text): path = ROOT / (target.replace(".", "/") + ".py" if module else target) # `python -m` also runs a package by its __main__.py. package = ROOT / target.replace(".", "/") / "__main__.py" if module else None if not path.is_file() and not (package and package.is_file()): found.append(f"{number}: {target}") found += [ f"{number}: {name}" for number, line in enumerate(text.splitlines(), 1) for name in re.findall(r"(? str: key = (target, sub) if key not in cls._helps: import subprocess argv = [sys.executable, *(["-m", target] if module else [target])] proc = subprocess.run( [*argv, *([sub] if sub else []), "--help"], cwd=ROOT, capture_output=True, text=True, timeout=120, check=False, ) cls._helps[key] = proc.stdout if proc.returncode == 0 else "" return cls._helps[key] @classmethod def subcommands(cls, target: str, module: bool) -> list[str]: usage = cls.help_text(target, module).split("\n\n", 1)[0] choice = re.search(r"\{([\w,-]+)\}", usage) return choice.group(1).split(",") if choice else [] @classmethod def unknown(cls, text: str) -> list[str]: found = [] for number, target, module, args in _commands(text): subs = cls.subcommands(target, module) sub = args[0] if args and args[0] in subs else "" known = set(_OPTION.findall(cls.help_text(target, module, sub))) if not sub: for other in subs: if not other.startswith("_"): known |= set(_OPTION.findall(cls.help_text(target, module, other))) if not known: found.append(f"{number}: {target} prints no --help") continue found += [ f"{number}: {target} {sub} {option}".replace(" ", " ") for arg in args for option in _OPTION.findall(arg.split("=", 1)[0]) if arg.startswith(option) and option not in known ] return found def test_every_cited_option_is_in_the_parser(self): for page in sorted(WIKI.glob("*.md")): with self.subTest(page=page.name): self.assertEqual(self.unknown(page.read_text(encoding="utf-8")), []) def test_an_unknown_option_is_named(self): self.assertEqual( self.unknown("python scripts/campaign.py status --budget 6h\n"), ["1: scripts/campaign.py status --budget"], ) class PipelineStepsMatchTheRun(unittest.TestCase): """The step numbers the pages give are the ones pipeline.py prints. The testing guide called the integrity check "step 6/8" in a run of 24 steps numbered by label, and the tools table can lose or keep a step the run no longer has.""" @staticmethod def labels() -> list[str]: from orchestrate.labels import STEP_LABELS, step_number return [step_number(key) for key in STEP_LABELS] @staticmethod def cited(text: str) -> list[str]: """Step numbers a sentence attributes to the pipeline.""" found = [] for match in re.finditer( r"(?i)pipeline steps? ((?:\d[a-z0-9]*(?:/\d+)?(?:, | and | or )?)+)" r"|steps? (\d[a-z0-9]*(?:/\d+)?) of the pipeline", text, ): found += re.findall(r"\d[a-z0-9]*(?:/\d+)?", match.group(1) or match.group(2)) return found def test_the_table_lists_every_step_in_order(self): section = _section(_page("tools.md"), "Pipeline") table = re.findall(r"^\| (\d[a-z0-9]*) \|", section, re.M) self.assertEqual(table, self.labels()) def test_steps_named_in_the_pipeline_section_exist(self): section = _section(_page("tools.md"), "Pipeline") labels = set(self.labels()) named = [ step for clause in re.findall(r"(?i)\bsteps? ((?:\(?\d[a-z0-9]*\)?(?:, | and |, and )?)+)", section) for step in re.findall(r"\d[a-z0-9]*", clause) ] named += [step for group in re.findall(r"\(((?:\d[a-z0-9]*, )+\d[a-z0-9]*)\)", section) for step in group.split(", ")] self.assertTrue(named) self.assertEqual(sorted(set(named) - labels), []) def test_every_page_cites_a_step_the_run_has(self): labels = set(self.labels()) for page in sorted(WIKI.glob("*.md")): with self.subTest(page=page.name): self.assertEqual( [step for step in self.cited(page.read_text(encoding="utf-8")) if step not in labels], []) def test_a_fraction_of_steps_is_refused(self): self.assertEqual(self.cited("Integrated as pipeline step 6/8 (runs after"), ["6/8"]) self.assertEqual(self.cited("pipeline steps 3a and 4d"), ["3a", "4d"]) class SiteOutputIsTheStarlightProject(unittest.TestCase): """generate_site.py writes into website/ since the move to Starlight, while tools.md still gave `docs/` as the default of --docs-dir and as a path the pipeline holds, and the testing guide measured pages in `docs/`.""" PAGES = ("tools.md", "testing-guide.md", "release-process.md", "reading-reports.md", "measurements.md") BARE_DOCS = re.compile(r"(? 50_000_000) page = " ".join((ROOT / "wiki" / "release-process.md").read_text(encoding="utf-8").split()) said = re.search(r"Size does not decide it: (\w+) committed files are over 50 MB \(([^)]*)\)", page) self.assertIsNotNone(said) self.assertEqual(sorted(re.findall(r"`([^`]+)`", said.group(2))), large) self.assertEqual(said.group(1), {3: "three", 4: "four", 5: "five"}.get(len(large))) class SkipFlagsNameTheSteps(unittest.TestCase): """--skip-packs said "add --skip-docs to stop after verify" while 4d and 4e still ran, and the flag table left 8b out of --skip-docs.""" STEP = r"\b\d[a-z]?\d?\b" def test_the_flag_row_the_help_and_the_usage_agree_with_the_step_table(self): page = (ROOT / "wiki" / "tools.md").read_text(encoding="utf-8") table = page.split("Pipeline steps:", 1)[1].split("Pipeline flags:", 1)[0] source = (ROOT / "scripts" / "pipeline.py").read_text(encoding="utf-8") helps = {} for node in ast.walk(ast.parse(source)): if (isinstance(node, ast.Call) and getattr(node.func, "attr", "") == "add_argument" and node.args and isinstance(node.args[0], ast.Constant)): for kw in node.keywords: if kw.arg == "help": helps[node.args[0].value] = ast.literal_eval(kw.value) for flag in ("--skip-packs", "--skip-docs"): steps = set() for row in table.splitlines(): cells = [c.strip() for c in row.split("|")] if len(cells) > 3 and cells[3].startswith(f"`{flag}`"): steps.add(cells[1]) self.assertTrue(steps) row = re.search(rf"^\| `{flag}` \| skip steps ([^.|]*)", page, re.M).group(1) usage = re.search(rf"pipeline\.py {flag} +# skip steps (.*)$", source, re.M).group(1) said = helps[flag].split(":", 1)[0] for text in (row, usage, said): with self.subTest(flag=flag, text=text): self.assertEqual(set(re.findall(self.STEP, text)), steps) class TheRemovalProofsAreTheExportersOwn(unittest.TestCase): """tools.md listed the proofs that keep an entry out of removal_candidates without the platform's launcher records, which candidates.py checks right after a path.""" WORDS = {"path": "`path:`", "launcher": "`launchers:`", "name": "by name or alias", "hash": "by any hash", "directory": "`type: directory`", "data_directory": "a data directory"} def test_the_page_names_each_proof_in_order(self): from resolve.categories import _PROOF_ORDER page = (WIKI / "tools.md").read_text(encoding="utf-8") start = page.index("No entry the platform declares is removed.") section = page[start:page.index("removal_candidates", start)] self.assertEqual(set(self.WORDS), set(_PROOF_ORDER)) positions = [section.index(self.WORDS[kind]) for kind in _PROOF_ORDER] self.assertEqual(positions, sorted(positions)) class LandingAChangeNamesTheCommitRules(unittest.TestCase): """The page left out the review-tag rule scripts/commit_rules.py enforces, and never said where the rules live or how a clone runs them: the hook is not versioned.""" def _landing(self) -> str: return _section(_page("testing-guide.md"), "Landing a change") def test_every_refusal_is_on_the_page(self): from devtools import commitrules section = self._landing() words = re.search(r"\\b\((.*?)\)", commitrules.REFUSED_WORDS.pattern).group(1) for word in words.replace("|", " ").split(): if MARKETING_WORDS.search(word): # Named by "the marketing vocabulary above": spelled out, # the page would fail that test. continue with self.subTest(word=word): self.assertIn(word, section.lower()) self.assertIn("which review asked for it", section) self.assertIn("scripts/commit_rules.py", section) def test_the_hook_command_on_the_page_refuses_a_review_tag(self): import subprocess import tempfile block = re.search(r"```bash\n(.*?)```", self._landing(), re.S).group(1) with tempfile.TemporaryDirectory() as scratch: clone = Path(scratch) (clone / ".git" / "hooks").mkdir(parents=True) (clone / "scripts").mkdir() for name in ("commit_rules.py", "devtools/__init__.py", "devtools/commitrules.py"): (clone / "scripts" / name).parent.mkdir(exist_ok=True) (clone / "scripts" / name).write_bytes((ROOT / "scripts" / name).read_bytes()) subprocess.run(["sh", "-c", block], cwd=clone, check=True) message = clone / "MSG" message.write_text("fix: address review 3 notes\n", encoding="utf-8") refused = subprocess.run([str(clone / ".git" / "hooks" / "commit-msg"), "MSG"], cwd=clone, capture_output=True, text=True, check=False) message.write_text("fix: say what the change does\n", encoding="utf-8") accepted = subprocess.run([str(clone / ".git" / "hooks" / "commit-msg"), "MSG"], cwd=clone, capture_output=True, text=True, check=False) self.assertEqual(refused.returncode, 1, refused.stderr) self.assertIn("which review asked for it", refused.stderr) self.assertEqual(accepted.returncode, 0, accepted.stderr) # Families of test modules: a family is one subject split by size, so it sits # on one row of the guide's table. TEST_FAMILIES = ("test_site_", "test_install_", "test_tui_", "test_profile_sync_", "test_launchers_", "test_e2e_", "test_audit_") # A skip that names repository data: the test reads the working tree. WORKING_TREE_SKIP = re.compile( r"(?:skipTest|SkipTest|skipUnless|skipIf)\(.*(?:database\.json|bios/|romset|no pack found|dist/)" ) class TheTestingGuideNamesEveryModule(unittest.TestCase): """The guide described 18 modules of 144, and a contributor touching slots.py or the downloader found no module named for it. Later one row held 113 modules, the site, the doc pages, profile_sync and the launcher extractors together, and a family was split across rows.""" TESTS = ROOT / "tests" def _rows(self) -> list[list[str]]: section = _section(_page("testing-guide.md"), "Modules at a glance") return [re.findall(r"`(test_\w+)`", line.split("|")[3]) for line in section.splitlines() if line.startswith("| ") and line.count("|") >= 4 and "`test_" in line] def test_each_module_is_on_one_row_once(self): named = [name for row in self._rows() for name in row] modules = sorted(path.stem for path in self.TESTS.glob("test_*.py")) self.assertEqual(sorted(set(modules) - set(named)), [], "not in the table") self.assertEqual(sorted(set(named) - set(modules)), [], "no such module") self.assertEqual(sorted({n for n in named if named.count(n) > 1}), [], "named twice") def test_each_family_sits_on_one_row(self): rows = self._rows() for prefix in TEST_FAMILIES: with self.subTest(family=prefix): holding = [i for i, row in enumerate(rows) if any(n.startswith(prefix) for n in row)] self.assertEqual(len(holding), 1, f"{prefix} on rows {holding}") def test_every_helper_module_is_in_the_helpers_table(self): section = _section(_page("testing-guide.md"), "Shared helpers") named = set(re.findall(r"^\| `(\w+)` \|", section, re.M)) helpers = {path.stem for path in self.TESTS.glob("*.py") if not path.stem.startswith("test_")} helpers |= {path.parent.name for path in self.TESTS.glob("*/__init__.py")} self.assertEqual(sorted(helpers - named), [], "not in the table") self.assertEqual(sorted(named - helpers), [], "no such module") def test_every_module_reading_the_working_tree_is_listed(self): section = _section(_page("testing-guide.md"), "Tests that read the working tree") reading = sorted(path.stem for path in self.TESTS.glob("test_*.py") if WORKING_TREE_SKIP.search(path.read_text(encoding="utf-8"))) self.assertIn("test_archive_build", reading) self.assertEqual([name for name in reading if f"`{name}`" not in section], []) if __name__ == "__main__": unittest.main()