@@ -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+
99199def 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