Skip to content

Commit 76f248f

Browse files
committed
docs(fix[ext{sphinx_fonts}]): clean up partial file on download failure
why: urlretrieve can leave partial .woff2 on OSError mid-transfer, poisoning the cache and delivering corrupt fonts on subsequent builds. what: - Add dest.unlink() in except block to remove partial files - Add test for partial file cleanup behavior
1 parent e01d40f commit 76f248f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

docs/_ext/sphinx_fonts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def _download_font(url: str, dest: pathlib.Path) -> bool:
6363
urllib.request.urlretrieve(url, dest)
6464
logger.info("downloaded font: %s", dest.name)
6565
except (urllib.error.URLError, OSError):
66+
if dest.exists():
67+
dest.unlink()
6668
logger.warning("failed to download font: %s", url)
6769
return False
6870
return True

tests/docs/_ext/test_sphinx_fonts.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,27 @@ def fake_urlretrieve(url: str, filename: t.Any) -> t.NoReturn:
183183
assert any("failed" in r.message for r in warning_records)
184184

185185

186+
def test_download_font_partial_file_cleanup(
187+
tmp_path: pathlib.Path,
188+
monkeypatch: pytest.MonkeyPatch,
189+
) -> None:
190+
"""_download_font removes partial file on failure."""
191+
dest = tmp_path / "cache" / "partial.woff2"
192+
193+
msg = "disk full"
194+
195+
def fake_urlretrieve(url: str, filename: t.Any) -> t.NoReturn:
196+
pathlib.Path(filename).write_bytes(b"partial")
197+
raise OSError(msg)
198+
199+
monkeypatch.setattr("sphinx_fonts.urllib.request.urlretrieve", fake_urlretrieve)
200+
201+
result = sphinx_fonts._download_font("https://example.com/font.woff2", dest)
202+
203+
assert result is False
204+
assert not dest.exists()
205+
206+
186207
# --- _on_builder_inited tests ---
187208

188209

0 commit comments

Comments
 (0)