Debian Codegraph @

tests/test_options.py

raw
1 """Test suite for the sphinx-favicon extension."""
2
3 from itertools import chain
4 from pathlib import Path
5
6 import pytest
7
8
9 @pytest.mark.sphinx("html", testroot="list_of_three_dicts")
10 def test_list_of_three_dicts(favicon_tags):
11 """Run tests on a list of 3 dicts.
12
13 Args:
14 favicon_tags: Favicon tags in index.html page.
15 """
16 # this test should have 3 favicons
17 assert len(favicon_tags) == 3
18
19 # all favicons should have rel, href, type, and sizes attributes
20 for favicon_tag in favicon_tags:
21 assert favicon_tag["rel"]
22 assert favicon_tag["href"]
23 assert favicon_tag["type"]
24 assert favicon_tag["sizes"]
25
26 # check first favicon in more detail
27 assert favicon_tags[0]["rel"] == ["icon"]
28 assert (
29 favicon_tags[0]["href"]
30 == "https://raw.githubusercontent.com/tcmetzger/sphinx-favicon/main/docs/source/_static/favicon-16x16.png"
31 )
32 assert favicon_tags[0]["type"] == "image/png"
33 assert favicon_tags[0]["sizes"] == "16x16"
34
35
36 @pytest.mark.sphinx("html", testroot="list_of_three_icons_automated_values")
37 def test_list_of_three_icons_automated_values(favicon_tags):
38 """Run tests on a list of 3 dicts with automated values.
39
40 Args:
41 favicon_tags: Favicon tags in index.html page.
42 """
43 # this test should have 3 favicons
44 assert len(favicon_tags) == 3
45
46 # all favicons should have rel, href, type, and sizes attributes
47 for favicon_tag in favicon_tags:
48 assert favicon_tag["rel"]
49 assert favicon_tag["href"]
50 assert favicon_tag["type"]
51 assert favicon_tag["sizes"]
52
53 # check first favicon in more detail
54 assert favicon_tags[0]["rel"] == ["icon"]
55 assert (
56 favicon_tags[0]["href"]
57 == "https://raw.githubusercontent.com/tcmetzger/sphinx-favicon/main/docs/source/_static/favicon-16x16.png"
58 )
59 assert favicon_tags[0]["type"] == "image/png"
60 assert favicon_tags[0]["sizes"] == "16x16"
61
62
63 @pytest.mark.sphinx("html", testroot="single_dict")
64 def test_single_dict(favicon_tags):
65 """Run tests on a single dict.
66
67 Args:
68 favicon_tags: Favicon tags in index.html page.
69 """
70 # this test should have 1 favicon
71 assert len(favicon_tags) == 1
72
73 # check favicon
74 assert favicon_tags[0]["rel"] == ["apple-touch-icon"]
75 assert (
76 favicon_tags[0]["href"]
77 == "https://raw.githubusercontent.com/tcmetzger/sphinx-favicon/main/docs/source/_static/apple-touch-icon.png"
78 )
79 assert favicon_tags[0]["type"] == "image/png"
80 assert favicon_tags[0]["sizes"] == "180x180"
81
82
83 @pytest.mark.sphinx("html", testroot="list_of_urls")
84 def test_list_of_urls(favicon_tags):
85 """Run tests on a list of urls.
86
87 Args:
88 favicon_tags: Favicon tags in index.html page.
89 """
90 # this test should have 3 favicons
91 assert len(favicon_tags) == 3
92
93 # all favicons should have rel, href, and type attributes
94 for favicon_tag in favicon_tags:
95 assert favicon_tag["rel"]
96 assert favicon_tag["href"]
97 assert favicon_tag["type"]
98
99 # check first favicon in more detail
100 assert favicon_tags[0]["rel"] == ["icon"]
101 assert (
102 favicon_tags[0]["href"]
103 == "https://secure.example.com/favicon/favicon-16x16.gif"
104 )
105 assert favicon_tags[0]["type"] == "image/gif"
106
107
108 @pytest.mark.sphinx("html", testroot="static_files")
109 def test_static_files(app, favicon_tags, favicon_tags_for_nested):
110 """Run tests using static files.
111
112 Args:
113 app: the Sphinx application
114 favicon_tags: Favicon tags in index.html page.
115 favicon_tags_for_nested: Favicon tags in nested/page.html page.
116 """
117 expected_urls = [
118 "_static/square.svg",
119 "_static/nested/triangle.svg",
120 "_static/circle.svg",
121 ]
122
123 # this test should have 3 favicons
124 assert len(favicon_tags) == 3
125
126 # all favicons should have rel, href, and type attributes
127 for favicon_tag in chain(favicon_tags, favicon_tags_for_nested):
128 assert favicon_tag["rel"] == ["icon"]
129 assert "_static" in favicon_tag["href"]
130 assert favicon_tag["type"] == "image/svg+xml"
131 assert "static-file" not in favicon_tag
132
133 # check favicons in top level page (should be "_static/square.svg", etc.)
134 for favicon_tag in favicon_tags:
135 assert favicon_tag["href"] in expected_urls
136
137 # check favicon URLS for pages in subfolders (should be "../_static/square.svg", etc.)
138 for favicon_tag in favicon_tags_for_nested:
139 assert favicon_tag["href"] in [f"../{url}" for url in expected_urls]
140
141 static = Path(app.outdir, "_static")
142 assert (static / "square.svg").exists()
143 assert (static / "nested/triangle.svg").exists()
144 assert (static / "circle.svg").exists()
145
146
147 @pytest.mark.sphinx("html", testroot="href_and_static")
148 def test_href_and_static(app, favicon_tags, favicon_tags_for_nested):
149 """Run tests on a mix of static files and complete urls.
150
151 Args:
152 app: the Sphinx application
153 favicon_tags: Favicon tags in index.html page.
154 favicon_tags_for_nested: Favicon tags in nested/page.html page.
155 """
156 # this test should have 3 favicons
157 assert len(favicon_tags) == 2
158
159 # all favicons should have rel, href, type, and sizes attributes
160 for favicon_tag in chain(favicon_tags, favicon_tags_for_nested):
161 assert favicon_tag["rel"] == ["icon"]
162 assert "_static" in favicon_tag["href"]
163 assert favicon_tag["type"] == "image/svg+xml"
164 assert favicon_tag["sizes"]
165 assert "static-file" not in favicon_tag
166
167 for favicon_tag in favicon_tags:
168 assert favicon_tag["href"].startswith("_static")
169
170 for favicon_tag in favicon_tags_for_nested:
171 assert favicon_tag["href"].startswith("../_static")
172
173 # favicons should use relative paths, ignoring paths provided with `href`
174 static = Path(app.outdir, "_static")
175 assert (static / "square.svg").exists()
176 assert (static / "nested/triangle.svg").exists()
177
178
179 @pytest.mark.sphinx("html", testroot="msapp_meta")
180 def test_msapp_meta(favicon_tags, meta_tags):
181 """Run tests on a favicon configuration with meta.
182
183 Args:
184 favicon_tags: Favicon tags in index.html page.
185 meta_tags: Meta tags in index.html page.
186 """
187 # this test should have 1 link tag
188 assert len(favicon_tags) == 1
189
190 # get all values from meta tags and check for expected values
191 tag_values = []
192 for tag in meta_tags:
193 tag_values.extend(list(tag.attrs.values()))
194 assert "msapplication-TileColor" in tag_values
195 assert "#2d89ef" in tag_values
196 assert "theme-color" in tag_values
197 assert "#ffffff" in tag_values