Debian Codegraph @

tests/conftest.py

raw
1 """Configuration fixtures of the tests (compatible with modern Sphinx)."""
2
3 import re
4 from pathlib import Path
5
6 import pytest
7 from bs4 import BeautifulSoup
8
9 pytest_plugins = "sphinx.testing.fixtures"
10
11
12 @pytest.fixture(scope="session")
13 def rootdir() -> Path:
14 """The root directory for Sphinx test roots."""
15 return Path(__file__).resolve().parent / "roots"
16
17
18 @pytest.fixture(autouse=True)
19 def _stub_network_for_images(monkeypatch):
20 """Stub sphinx_favicon.requests.get to avoid network access during tests.
21
22 Returns minimal GIF bytes with the requested dimensions parsed from the URL
23 (e.g., "...16x16..." -> 16x16). Defaults to 16x16 if not found.
24 """
25
26 def _gif_bytes(w: int, h: int) -> bytes:
27 # Minimal GIF header: "GIF89a" + width + height (little-endian) + 3 padding bytes
28 return (
29 b"GIF89a"
30 + int(w).to_bytes(2, "little")
31 + int(h).to_bytes(2, "little")
32 + b"\x00\x00\x00"
33 )
34
35 def fake_get(url: str, *args, **kwargs):
36 m = re.search(r"(\d+)x(\d+)", url)
37 if m:
38 w, h = int(m.group(1)), int(m.group(2))
39 else:
40 # Sensible default for URLs without size hint; tests don't assert these sizes
41 w, h = 16, 16
42
43 class _Resp:
44 status_code = 200
45 content = _gif_bytes(w, h)
46
47 return _Resp()
48
49 monkeypatch.setattr("sphinx_favicon.requests.get", fake_get)
50
51
52 @pytest.fixture()
53 def content(app):
54 """The app build content."""
55 app.build()
56 yield app
57
58
59 def _link_tags(content, page):
60 """Link tags in a page content."""
61 c = (Path(content.outdir) / page).read_text()
62 return BeautifulSoup(c, "html.parser").find_all("link")
63
64
65 def _favicon_tags(content, page="index.html"):
66 """Favicon tags in the index.html page."""
67 return [
68 tag
69 for tag in _link_tags(content, page)
70 if tag.get("type", "").startswith("image")
71 ]
72
73
74 def _meta_tags(content, page):
75 """Link tags in a page content."""
76 c = (Path(content.outdir) / page).read_text()
77 return BeautifulSoup(c, "html.parser").find_all("meta")
78
79
80 @pytest.fixture()
81 def link_tags(content):
82 """Link tags in index.html page."""
83 return _link_tags(content, "index.html")
84
85
86 @pytest.fixture()
87 def meta_tags(content):
88 """Meta tags in index.html page."""
89 return _meta_tags(content, "index.html")
90
91
92 @pytest.fixture()
93 def favicon_tags(content):
94 """Favicon tags in index.html page."""
95 return _favicon_tags(content)
96
97
98 @pytest.fixture()
99 def favicon_tags_for_nested(content):
100 """Favicon tags in nested/page.html page."""
101 return _favicon_tags(content, "nested/page.html")
102
103
104 def pytest_configure(config):
105 """Add markers config to pytest."""
106 config.addinivalue_line("markers", "sphinx")