Files
mandy/tests/test_docs_repo_paths.py
Vonsek c51e308305 docs: the XGameSave shim is #279's, not main's; test backticked repo paths
docs/saves.md and the tools/saves.py docstring named
runtime/shims/xgameruntime_services.cpp as the runtime's save store; that file
is only in the open #279, and on main XGameSave* imports get the loader's
default answers. docs/shader_validation.md named spirv_mini_interp without
its .py. tests/test_docs_repo_paths.py checks that backticked tools/,
runtime/, tests/ and scripts/ paths in docs/ name tracked files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 15:32:24 +01:00

80 lines
3.5 KiB
Python

"""Repository paths the docs name in backticks exist.
python -I tests/test_docs_repo_paths.py
`tests/test_docs_links.py` checks Markdown links; this checks the other way the docs point at code, a backticked
`tools/...`, `runtime/...`, `tests/...` or `scripts/...` path. A rename, or a page written against an unmerged pull
request, leaves such a path pointing at nothing (docs/saves.md described #279's `xgameruntime_services.cpp` as the
runtime's save store while #279 was still open). Reads tracked files only (`git ls-files`, with a directory walk when
git is not available); no game files.
"""
import os
import re
import subprocess
import unittest
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PATH = re.compile(r"`((?:tools|runtime|tests|scripts)/[A-Za-z0-9_./\-]+)`")
# Pages that quote another project's tree (SCARAB, americas-recomp): their paths are not this repository's.
OTHER_PROJECTS = {"docs/fssm_crosscheck.md", "docs/d3d12_xs_surface.md"}
# Build output, not tracked.
BUILD_OUTPUT = ("runtime/build",)
def tracked_files():
try:
out = subprocess.run(["git", "ls-files"], cwd=ROOT, capture_output=True, text=True, check=True).stdout
return [p for p in out.splitlines() if p]
except (OSError, subprocess.CalledProcessError):
found = []
for root, dirs, files in os.walk(ROOT):
dirs[:] = [d for d in dirs if d not in (".git", "build", "__pycache__")]
found += [os.path.relpath(os.path.join(root, f), ROOT).replace(os.sep, "/") for f in files]
return found
def missing_paths(pages, files):
"""[(page:line, path)] for backticked repository paths in `pages` ({name: text}) that `files` lacks."""
known = set(files)
for f in files:
parts = f.split("/")
known.update("/".join(parts[:i]) for i in range(1, len(parts)))
out = []
for page, text in sorted(pages.items()):
in_fence = False
for n, line in enumerate(text.split("\n"), 1):
if line.lstrip().startswith("```"):
in_fence = not in_fence
continue
if in_fence or "://" in line: # a link into another repository names its paths
continue
for m in PATH.finditer(line):
path = m.group(1).rstrip("/.")
if "*" in path or path.startswith(BUILD_OUTPUT) or path in known:
continue
out.append((f"{page}:{n}", path))
return out
class DocsRepoPaths(unittest.TestCase):
def test_checker_reports_only_the_missing_path(self):
pages = {"docs/a.md": "Run `tools/real.py`, then `tools/gone.py`.\n```\n`tools/in_fence.py`\n```\n"
"`runtime/build/x.exe` and `tests/` and [`tools/x.py`](https://example.org/tools/x.py)"}
self.assertEqual(missing_paths(pages, ["tools/real.py", "tests/t.py"]), [("docs/a.md:1", "tools/gone.py")])
def test_docs_name_existing_paths(self):
files = tracked_files()
pages = {}
for p in files:
if p.startswith("docs/") and p.endswith(".md") and p not in OTHER_PROJECTS:
with open(os.path.join(ROOT, p), encoding="utf-8") as f:
pages[p] = f.read()
self.assertTrue(pages)
missing = missing_paths(pages, files)
self.assertEqual(missing, [], "backticked paths that name no tracked file: " +
"; ".join(f"{where} {path}" for where, path in missing))
if __name__ == "__main__":
unittest.main()