Skip to content

Commit 9e65cf9

Browse files
committed
fix(ci): validate canonical README file links against candidate
1 parent 37311a1 commit 9e65cf9

2 files changed

Lines changed: 106 additions & 3 deletions

File tree

‎scripts/ci/check_readme_links.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Validate every local and HTTP link in the root README."""
1+
"""Validate README repository files against the candidate tree and external HTTP links."""
22

33
from __future__ import annotations
44

@@ -12,6 +12,7 @@
1212

1313
LINK = re.compile(r"(?<!!)\[[^]]+\]\((?P<target>[^)]+)\)")
1414
USER_AGENT = "ml4t-engineer-readme-check/1.0"
15+
REPOSITORY_FILE_PREFIX = "https://github.com/ml4t/engineer/blob/main/"
1516

1617

1718
def targets(readme: Path) -> tuple[str, ...]:
@@ -50,11 +51,13 @@ def validate_http(target: str, *, attempts: int = 3, retry_seconds: float = 1.0)
5051

5152

5253
def check(readme: Path, *, check_http: bool = True) -> None:
53-
"""Validate every Markdown link in *readme*."""
54+
"""Validate candidate file links locally and other HTTP links remotely."""
5455
root = readme.resolve().parent
5556
for target in targets(readme):
5657
scheme = urlparse(target).scheme
57-
if scheme in {"http", "https"}:
58+
if target.startswith(REPOSITORY_FILE_PREFIX):
59+
validate_local(root, target.removeprefix(REPOSITORY_FILE_PREFIX))
60+
elif scheme in {"http", "https"}:
5861
if check_http:
5962
validate_http(target)
6063
elif not scheme:

‎tests/test_release_candidate.py‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,106 @@ def open_url(request: Request, *, timeout: int) -> BytesIO:
9696
assert requests[0].get_header("User-agent") == check_readme_links.USER_AGENT
9797

9898

99+
@pytest.fixture
100+
def unavailable_http(monkeypatch: pytest.MonkeyPatch) -> list[str]:
101+
requests: list[str] = []
102+
103+
def open_url(request: Request, *, timeout: int) -> BytesIO:
104+
requests.append(request.full_url)
105+
assert timeout == 20
106+
raise HTTPError(request.full_url, 404, "Not Found", None, None)
107+
108+
monkeypatch.setattr(check_readme_links, "urlopen", open_url)
109+
monkeypatch.setattr(check_readme_links.time, "sleep", lambda _: None)
110+
return requests
111+
112+
113+
@pytest.fixture
114+
def published_http(monkeypatch: pytest.MonkeyPatch) -> list[str]:
115+
requests: list[str] = []
116+
117+
def open_url(request: Request, *, timeout: int) -> BytesIO:
118+
requests.append(request.full_url)
119+
assert timeout == 20
120+
response = BytesIO(b"published file")
121+
response.status = 200 # type: ignore[attr-defined]
122+
return response
123+
124+
monkeypatch.setattr(check_readme_links, "urlopen", open_url)
125+
return requests
126+
127+
128+
@pytest.mark.parametrize(
129+
"path,target",
130+
[
131+
("CONTRIBUTING.md", "https://github.com/ml4t/engineer/blob/main/CONTRIBUTING.md"),
132+
(
133+
"docs/new guide.md",
134+
"https://github.com/ml4t/engineer/blob/main/docs/new%20guide.md#intro",
135+
),
136+
],
137+
)
138+
def test_readme_canonical_main_links_validate_proposed_files(
139+
tmp_path: Path, unavailable_http: list[str], path: str, target: str
140+
) -> None:
141+
proposed = tmp_path / path
142+
proposed.parent.mkdir(parents=True, exist_ok=True)
143+
proposed.write_text("# Intro\n", encoding="utf-8")
144+
readme = tmp_path / "README.md"
145+
readme.write_text(f"[New guide]({target})\n", encoding="utf-8")
146+
check_readme_links.check(readme)
147+
assert unavailable_http == []
148+
149+
150+
@pytest.mark.parametrize("check_http", [False, True])
151+
def test_readme_canonical_main_links_reject_missing_candidate_files(
152+
tmp_path: Path, published_http: list[str], check_http: bool
153+
) -> None:
154+
readme = tmp_path / "README.md"
155+
readme.write_text(
156+
"[License](https://github.com/ml4t/engineer/blob/main/LICENSE)\n", encoding="utf-8"
157+
)
158+
with pytest.raises(ValueError, match="does not exist"):
159+
check_readme_links.check(readme, check_http=check_http)
160+
assert published_http == []
161+
162+
163+
@pytest.mark.parametrize("path", ["../outside.md", "%2e%2e/outside.md"])
164+
def test_readme_canonical_main_links_reject_repository_escape(
165+
tmp_path: Path, published_http: list[str], path: str
166+
) -> None:
167+
root = tmp_path / "repository"
168+
root.mkdir()
169+
(tmp_path / "outside.md").write_text("outside\n", encoding="utf-8")
170+
readme = root / "README.md"
171+
readme.write_text(
172+
f"[Outside](https://github.com/ml4t/engineer/blob/main/{path})\n", encoding="utf-8"
173+
)
174+
with pytest.raises(ValueError, match="escapes the repository"):
175+
check_readme_links.check(readme)
176+
assert published_http == []
177+
178+
179+
@pytest.mark.parametrize(
180+
"target",
181+
[
182+
"https://github.com/other/engineer/blob/main/CONTRIBUTING.md",
183+
"https://github.com/ml4t/other/blob/main/CONTRIBUTING.md",
184+
"https://github.com/ml4t/engineer/blob/feature/CONTRIBUTING.md",
185+
"https://github.com.evil.test/ml4t/engineer/blob/main/CONTRIBUTING.md",
186+
],
187+
)
188+
def test_readme_other_repository_urls_still_require_http_success(
189+
tmp_path: Path, unavailable_http: list[str], target: str
190+
) -> None:
191+
(tmp_path / "CONTRIBUTING.md").write_text("# Contributing\n", encoding="utf-8")
192+
readme = tmp_path / "README.md"
193+
readme.write_text(f"[Guide]({target})\n", encoding="utf-8")
194+
with pytest.raises(RuntimeError, match="README link failed"):
195+
check_readme_links.check(readme)
196+
assert unavailable_http == [target] * 3
197+
198+
99199
def test_docs_verifier_checks_release_identity(monkeypatch: pytest.MonkeyPatch) -> None:
100200
expected = {"commit": "a" * 40, "library": "engineer", "version": "0.1.4"}
101201
responses = iter(({**expected, "commit": "old"}, expected))

0 commit comments

Comments
 (0)