Files
ConformalLabpp/scripts/check-markdown-links.py
Tarik Moussa a2eee9c279
Some checks failed
C++ Tests / test-fast (pull_request) Successful in 1m56s
API Docs / doc-build (pull_request) Successful in 58s
Markdown link check / check (pull_request) Successful in 45s
C++ Tests / test-cgal (pull_request) Failing after 13m14s
ci+quality: structural gates (CI: 3 new; local: 7 new + .clang-tidy)
CI gates (active on every PR via .gitea/workflows/)
───────────────────────────────────────────────────
1. test-count consistency
   cpp-tests.yml gains a step after test-cgal that runs
   `scripts/check-test-counts.sh` against the just-built ./build dir
   (reuse via new BUILD_DIR env var, ~5 s overhead).  Drift between
   `doc/api/tests.md` and ctest reality now fails the PR.

2. End-to-end smoke
   `scripts/try_it.sh` (the documented user quick-start) is now part of
   the CGAL job, so README quick-start regressions fail the PR rather
   than silently breaking when users land.

3. Internal markdown link checker
   New `.gitea/workflows/markdown-links.yml` + `scripts/check-markdown
   -links.py`.  PRs that touch any *.md file run the check; main pushes
   trigger it too; a weekly cron catches external link rot.  Pure
   Python, no third-party action.  Validated against the current tree:
   122 internal links across 37 *.md files, 0 broken.

Local quality scripts (`scripts/quality/`, not in CI)
─────────────────────────────────────────────────────
* `license-headers.sh`   — `SPDX-License-Identifier: MIT` audit over
                            code/{include,src,tests}/.  Currently
                            reports 60/66 files missing it — that's
                            a follow-up; the script captures the
                            structural gap.
* `sanitizers.sh`        — ASan + UBSan over the fast test suite.
* `coverage.sh`          — gcov/lcov line + branch coverage of
                            code/include/, HTML report under
                            build-coverage/lcov-html/.
* `clang-tidy.sh`        — runs the curated `.clang-tidy` policy over
                            every public header.
* `multi-compiler.sh`    — sequential build + test against every
                            detected g++/clang++ (auto-discovery or
                            explicit list).
* `cgal-version-matrix.sh`— sequential build + CGAL test suite against
                            every CGAL tree under `~/cgal/<ver>/` (or
                            via `CGAL_ROOTS=...` env var).
* `reproducible-build.sh`— two `Release -j1` builds, fail if any test
                            executable byte-differs.
* `run-all.sh`           — driver: `--fast` for the ~5-min subset,
                            no arg for the ~25–40 min full sweep;
                            captures per-gate logs to
                            build-quality-logs/.

+ `.clang-tidy`          — curated, deliberately-small policy (only
                            checks that fire on OUR code, never on
                            transitive CGAL/Eigen/Boost headers).

+ `scripts/quality/README.md` — explains the structure, lists each
                            gate's wall-time + prereqs, and codifies
                            the promotion path: a gate moves into CI
                            only when it's green on the dev machine
                            AND has a recovery-instructions paragraph
                            in `doc/release-policy.md`.

Doc updates
───────────
`doc/architecture/locked-vs-flexible.md` (reviewer-facing) gains 4
"closed" rows in the limitations table — the 3 CI gates above and the
local quality-script suite.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 08:25:09 +02:00

141 lines
4.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""scripts/check-markdown-links.py
Verify every internal `[text](target)` link in every markdown file under
the repo resolves to an existing file. Skips:
* `http(s)://`, `mailto:`, `ftp://` — external schemes
* pure-anchor links `#fragment`
* code blocks fenced by ``` ... ```
* link targets inside HTML tags that the markdown parser does not
treat as links (we operate on raw text — false positives are
explicitly excluded via the IGNORE patterns at the top of the file).
Walks `.` recursively. Skips `code/deps/`, `build*`, `.git/`, the
generated `doc/doxygen/` tree, and `node_modules/`.
Exit codes:
0 every internal link resolves
1 at least one link is broken — prints `file:line: target` for each
"""
from __future__ import annotations
import os, re, sys
ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
# Directories that are not part of our authored docs.
SKIP_DIR_PARTS = {"code/deps", ".git", "node_modules", "doc/doxygen"}
SKIP_DIR_PREFIXES = ("build", "build_T_", "test-reports")
# Files in those formats are sometimes generated.
SKIP_FILES = {"CHANGELOG-old.md"}
# Schemes to ignore (external).
EXTERNAL_SCHEME_RE = re.compile(r"^[a-z]+://|^mailto:", re.IGNORECASE)
# Standard markdown link: [text](target)
LINK_RE = re.compile(r"\[(?:[^\]]|\\\])+\]\(\s*<?([^)\s<>]+)>?\s*(?:\"[^\"]*\")?\)")
# Code-fence detection (``` or ~~~)
FENCE_RE = re.compile(r"^(?:```|~~~)")
def is_skipped_dir(rel: str) -> bool:
parts = rel.split(os.sep)
for skip in SKIP_DIR_PARTS:
skip_parts = skip.split("/")
for i in range(len(parts) - len(skip_parts) + 1):
if parts[i : i + len(skip_parts)] == skip_parts:
return True
for p in parts:
if any(p.startswith(pref) for pref in SKIP_DIR_PREFIXES):
return True
return False
def collect_md_files() -> list[str]:
md: list[str] = []
for dirpath, dirs, files in os.walk(ROOT):
rel = os.path.relpath(dirpath, ROOT)
if rel != "." and is_skipped_dir(rel):
dirs[:] = []
continue
for f in files:
if not f.lower().endswith(".md"):
continue
if f in SKIP_FILES:
continue
full = os.path.join(dirpath, f)
md.append(full)
return sorted(md)
def link_targets(text: str) -> list[tuple[int, str]]:
"""Yield (line_no, target) skipping code-fenced lines."""
out: list[tuple[int, str]] = []
in_fence = False
for line_no, line in enumerate(text.splitlines(), start=1):
if FENCE_RE.match(line.strip()):
in_fence = not in_fence
continue
if in_fence:
continue
for m in LINK_RE.finditer(line):
out.append((line_no, m.group(1)))
return out
def resolve(src_path: str, target: str) -> tuple[bool, str | None]:
"""Return (ok, expected_path).
ok=True → link resolves (file exists, or external/anchor we skip).
ok=False → file does not exist; expected_path is what we looked for.
"""
if not target:
return True, None
if EXTERNAL_SCHEME_RE.match(target):
return True, None
if target.startswith("#"):
# pure in-page anchor — we don't validate anchor existence
return True, None
# Strip any in-file anchor for file-existence check.
target_file = target.split("#", 1)[0]
if not target_file:
return True, None
if target_file.startswith("/"):
# Absolute-from-repo-root link.
full = os.path.join(ROOT, target_file.lstrip("/"))
else:
full = os.path.normpath(os.path.join(os.path.dirname(src_path), target_file))
return os.path.exists(full), full
def main() -> int:
files = collect_md_files()
broken: list[tuple[str, int, str, str]] = []
n_links = 0
for src in files:
try:
with open(src, encoding="utf-8") as f:
text = f.read()
except OSError as e:
print(f"WARN: cannot read {src}: {e}", file=sys.stderr)
continue
for line_no, target in link_targets(text):
n_links += 1
ok, expected = resolve(src, target)
if not ok:
broken.append((src, line_no, target, expected or "?"))
print(f"Scanned {len(files)} markdown files, {n_links} internal links.")
if broken:
print(f"\nBROKEN: {len(broken)} link(s) do not resolve:")
for src, line_no, target, expected in broken:
rel = os.path.relpath(src, ROOT)
print(f" {rel}:{line_no}: ({target}) → {os.path.relpath(expected, ROOT)} (missing)")
return 1
print("OK: every internal markdown link resolves.")
return 0
if __name__ == "__main__":
sys.exit(main())