This repository was archived by the owner on Sep 25, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathredirects_test.py
More file actions
90 lines (72 loc) · 3.62 KB
/
Copy pathredirects_test.py
File metadata and controls
90 lines (72 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""Check the redirect map against the site it is replacing.
This repository publishes redirects now, and once it is archived the artifact
from the last successful run is what GitHub Pages serves for good — Actions do
not run on an archived repository, so there is no second chance to fix a URL
that was left out. That makes the map worth checking while it can still change.
The check builds the mkdocs site the old way and compares the URLs it produces
against the redirect map, in both directions: a URL the old site served with no
redirect is a link that will break, and a redirect for a URL the old site never
served is a sign the map was edited by hand and drifted.
"""
from __future__ import annotations
import subprocess
import sys
import tempfile
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / "scripts"))
from build_redirects import OLD_PROJECT_DOCS, REDIRECTS, main # noqa: E402
def _urls_the_old_site_served() -> set[str]:
"""Build the mkdocs site and return every URL it publishes."""
with tempfile.TemporaryDirectory() as tmp:
result = subprocess.run(
[sys.executable, "-m", "mkdocs", "build", "--site-dir", tmp],
cwd=ROOT,
capture_output=True,
text=True,
)
if result.returncode != 0:
# Not a skip. A skip exits pytest 0, the workflow reads that as a
# pass and deploys redirects that were never checked against the
# site they replace — on the one deploy that cannot be redone.
pytest.fail(f"mkdocs build failed: {result.stderr.strip()[:200]}")
site = Path(tmp)
urls = set()
for page in site.rglob("index.html"):
rel = page.parent.relative_to(site).as_posix()
# ``relative_to`` gives "." for the site root, which is "/".
urls.add("/" if rel == "." else f"/{rel}/")
return urls
def test_every_published_url_has_a_redirect():
"""Nothing the old site served may be left without a forwarding address."""
missing = sorted(_urls_the_old_site_served() - set(REDIRECTS))
assert not missing, (
"these URLs are served by the current site but have no redirect, so "
"they will break when this repository is archived:\n "
+ "\n ".join(missing)
)
def test_no_redirect_points_at_a_url_that_never_existed():
"""A redirect for a URL the site never served means the map drifted."""
served = _urls_the_old_site_served()
invented = sorted(set(REDIRECTS) - served)
assert not invented, (
"these redirects are for URLs the site does not serve:\n "
+ "\n ".join(invented)
)
def test_old_project_docs_get_stubs_at_their_own_paths(tmp_path, monkeypatch):
"""The Sphinx pages are files (``cli_args.html``), not directories, and a
target that names a section must not get the reader's fragment appended."""
import build_redirects
monkeypatch.setattr(build_redirects, "__file__", str(tmp_path / "scripts" / "x.py"))
assert main() == 0
site = tmp_path / "site"
for old, new in OLD_PROJECT_DOCS.items():
rel = old.strip("/")
page = site / rel if rel.endswith(".html") else site / rel / "index.html"
html = page.read_text(encoding="utf-8")
assert f'href="https://commit-check.com{new}"' in html, old
assert "t.indexOf(\"#\") < 0" in (site / "commit-check" / "what-is-new.html").read_text()
def test_old_project_docs_do_not_shadow_the_mkdocs_redirects():
"""The two maps must stay disjoint, or one silently overwrites the other."""
assert not set(OLD_PROJECT_DOCS) & set(REDIRECTS)