Debian Codegraph @

tests/test_options.py

raw
1 from __future__ import annotations
2
3 from typing import TYPE_CHECKING
4
5 import conftest
6 import pytest
7 from sphinx.errors import ExtensionError
8
9 if TYPE_CHECKING:
10 from sphinx.application import Sphinx
11
12
13 def get_tag(tags, tag_type, kind='property', prefix='og'):
14 return next(tag for tag in tags if tag.get(kind) == f'{prefix}:{tag_type}')
15
16
17 def get_tag_content(tags, tag_type, kind='property', prefix='og'):
18 # Gets the content of a specific ogp tag
19 return get_tag(tags, tag_type, kind, prefix).get('content', '')
20
21
22 def get_tag_content_text(tags, tag_type, kind='property', prefix='og'):
23 # Gets the content of a specific ogp tag
24 return get_tag(tags, tag_type, kind, prefix).get_text('content', '')
25
26
27 def get_meta_description(tags):
28 tag = next(tag for tag in tags if tag.get('name') == 'description')
29 return tag.get('content', '')
30
31
32 @pytest.mark.sphinx('html', testroot='simple')
33 def test_simple(og_meta_tags):
34 description = get_tag_content(og_meta_tags, 'description')
35
36 assert len(og_meta_tags) > 0
37 assert get_tag_content(og_meta_tags, 'type') == 'website'
38 assert len(description) == 200
39 assert (
40 description
41 == 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at lorem ornare, fringilla massa nec, venenatis mi. Donec erat sapien, tincidunt nec rhoncus nec, scelerisque id diam. Orci vari...'
42 )
43
44
45 @pytest.mark.sphinx('html', testroot='meta-name-description')
46 def test_meta_name_description(meta_tags):
47 og_description = get_tag_content(meta_tags, 'description')
48 description = get_meta_description(meta_tags)
49
50 assert description == og_description
51
52
53 @pytest.mark.sphinx('html', testroot='meta-name-description-escape')
54 def test_meta_name_description_escape(meta_tags):
55 og_description = get_tag_content(meta_tags, 'description')
56 og_description_text = get_tag_content_text(meta_tags, 'description')
57
58 assert '<' in og_description
59 assert '<' not in og_description_text
60
61
62 @pytest.mark.sphinx('html', testroot='meta-name-description-manual-description')
63 def test_meta_name_manual_description(meta_tags):
64 og_description = get_tag_content(meta_tags, 'description')
65 description = get_meta_description(meta_tags)
66
67 assert description != og_description
68 assert description == 'My manual description'
69
70
71 @pytest.mark.sphinx('html', testroot='meta-name-description-manual-og-description')
72 def test_meta_name_manual_og_description(meta_tags):
73 og_description = get_tag_content(meta_tags, 'description')
74 description = get_meta_description(meta_tags)
75
76 assert og_description != description
77 assert og_description == 'My manual og:description'
78
79
80 @pytest.mark.sphinx('html', testroot='simple')
81 def test_site_url(og_meta_tags):
82 # Uses the same directory as simple, because it already contains url for a minimal config
83 assert (
84 get_tag_content(og_meta_tags, 'url')
85 == 'http://example.org/en/latest/index.html'
86 )
87
88
89 @pytest.mark.sphinx('dirhtml', testroot='simple')
90 def test_dirhtml_url(og_meta_tags):
91 assert get_tag_content(og_meta_tags, 'url') == 'http://example.org/en/latest/'
92
93
94 @pytest.mark.sphinx('html', testroot='image')
95 def test_image(og_meta_tags):
96 assert (
97 get_tag_content(og_meta_tags, 'image')
98 == 'http://example.org/en/latest/image.png'
99 )
100
101
102 @pytest.mark.sphinx('html', testroot='local-image')
103 def test_local_image(og_meta_tags):
104 assert (
105 get_tag_content(og_meta_tags, 'image')
106 == 'http://example.org/en/latest/_static/sample.jpg'
107 )
108
109
110 @pytest.mark.sphinx('html', testroot='social-cards-svg')
111 def test_social_cards_svg(app: Sphinx, og_meta_tags):
112 """If the social cards image is an SVG, it should not be in the social card."""
113 assert app.statuscode == 0
114
115
116 @pytest.mark.sphinx('html', testroot='image')
117 def test_image_alt(og_meta_tags):
118 assert get_tag_content(og_meta_tags, 'image:alt') == "Example's Docs!"
119
120
121 @pytest.mark.sphinx('html', testroot='simple')
122 def test_image_social_cards(meta_tags):
123 """Social cards should automatically be added if no og:image is given."""
124 pytest.importorskip('matplotlib')
125 # Asserting `in` instead of `==` because of the hash that is generated
126 assert (
127 'http://example.org/en/latest/_images/social_previews/summary_index'
128 in get_tag_content(meta_tags, 'image')
129 )
130 # Image alt text should be taken from page content.
131 assert (
132 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
133 in get_tag_content(meta_tags, 'image:alt')
134 )
135 # Make sure the extra tags are in the HTML
136 assert 'summary_large_image' in get_tag_content(
137 meta_tags, 'card', kind='name', prefix='twitter'
138 )
139
140
141 @pytest.mark.sphinx('html', testroot='type')
142 def test_type(og_meta_tags):
143 assert get_tag_content(og_meta_tags, 'type') == 'article'
144
145
146 @pytest.mark.sphinx('html', testroot='description-length')
147 def test_description_length(og_meta_tags):
148 assert len(get_tag_content(og_meta_tags, 'description')) == 50
149
150
151 @pytest.mark.sphinx('html', testroot='sitename')
152 def test_site_name(og_meta_tags):
153 assert get_tag_content(og_meta_tags, 'site_name') == "Example's Docs!"
154
155
156 @pytest.mark.sphinx('html', testroot='sitename-from-project')
157 def test_site_name_project(og_meta_tags):
158 assert get_tag_content(og_meta_tags, 'site_name') == 'Project name'
159
160
161 @pytest.mark.sphinx('html', testroot='first-image')
162 def test_first_image(og_meta_tags):
163 assert (
164 get_tag_content(og_meta_tags, 'image')
165 == 'http://example.org/en/latest/image2.png'
166 )
167 assert get_tag_content(og_meta_tags, 'image:alt') == 'Test image alt text'
168
169
170 @pytest.mark.sphinx('html', testroot='first-image-no-image')
171 def test_first_image_no_image(og_meta_tags):
172 assert (
173 get_tag_content(og_meta_tags, 'image')
174 == 'http://example.org/en/latest/image33.png'
175 )
176 assert get_tag_content(og_meta_tags, 'image:alt') == 'TEST'
177
178
179 @pytest.mark.sphinx('html', testroot='image-rel-paths')
180 def test_image_rel_paths(og_meta_tags, og_meta_tags_sub):
181 assert (
182 get_tag_content(og_meta_tags, 'image')
183 == 'http://example.org/en/latest/_images/sample.jpg'
184 )
185 assert (
186 get_tag_content(og_meta_tags_sub, 'image')
187 == 'http://example.org/en/latest/_images/sample.jpg'
188 )
189
190
191 @pytest.mark.sphinx('html', testroot='skip-admonitions')
192 def test_skip_admonitions(og_meta_tags):
193 assert get_tag_content(og_meta_tags, 'description') == 'This is text.'
194
195
196 @pytest.mark.sphinx('html', testroot='skip-title')
197 def test_skip_first_title(og_meta_tags):
198 description = get_tag_content(og_meta_tags, 'description')
199 assert 'A Title' not in description
200 assert 'Another Title' in description
201
202
203 @pytest.mark.sphinx('html', testroot='skip-title')
204 def test_skip_title_punctuation(og_meta_tags):
205 description = get_tag_content(og_meta_tags, 'description')
206 assert 'Another Title:' in description
207
208
209 @pytest.mark.sphinx('html', testroot='double-spacing')
210 def test_remove_double_spacing(og_meta_tags):
211 description = get_tag_content(og_meta_tags, 'description')
212 assert ' ' not in description
213
214
215 @pytest.mark.sphinx('html', testroot='list')
216 def test_list_punctuation(og_meta_tags):
217 description = get_tag_content(og_meta_tags, 'description')
218 assert description == 'Item 1, Item 2, Item 3, Item 4.'
219
220
221 @pytest.mark.sphinx('html', testroot='nested-lists')
222 def test_nested_list_punctuation(og_meta_tags):
223 description = get_tag_content(og_meta_tags, 'description')
224 assert (
225 description == 'Item 1, Item 2- Nested Item 1, Nested Item 2., Item 3, Item 4.'
226 )
227
228
229 @pytest.mark.sphinx('html', testroot='skip-comments')
230 def test_skip_comments(og_meta_tags):
231 assert get_tag_content(og_meta_tags, 'description') == 'This is text.'
232
233
234 @pytest.mark.sphinx('html', testroot='custom-tags')
235 def test_custom_tags(og_meta_tags):
236 assert get_tag_content(og_meta_tags, 'ignore_canonical') == 'true'
237
238
239 @pytest.mark.sphinx('html', testroot='skip-raw')
240 def test_skip_raw(og_meta_tags):
241 description = get_tag_content(og_meta_tags, 'description')
242 assert '<p>' not in description
243 assert (
244 description
245 == 'This text should be included. This text should also be included.'
246 )
247
248
249 @pytest.mark.sphinx('html', testroot='skip-code-block')
250 def test_skip_code_block(og_meta_tags):
251 description = get_tag_content(og_meta_tags, 'description')
252 assert '<p>' not in description
253 assert (
254 description
255 == 'This text should be included. This text should also be included.'
256 )
257
258
259 @pytest.mark.sphinx('html', testroot='quotation-marks')
260 def test_quotation_marks(og_meta_tags):
261 # If smart quotes are disabled and the quotes aren't properly escaped, bs4 will fail to parse the tag and the content will be a empty string
262 description = get_tag_content(og_meta_tags, 'description')
263
264 assert (
265 description
266 == '"This text should appear in escaped quotation marks" This text should still appear as well "while this is once again in quotations"'
267 )
268
269
270 @pytest.mark.sphinx('html', testroot='overrides-simple')
271 def test_overrides_simple(og_meta_tags):
272 assert get_tag_content(og_meta_tags, 'description') == 'Overridden description'
273 assert get_tag_content(og_meta_tags, 'title') == 'Overridden Title'
274 assert get_tag_content(og_meta_tags, 'type') == 'article'
275 assert (
276 get_tag_content(og_meta_tags, 'image')
277 == 'http://example.org/en/latest/overridden-image.png'
278 )
279 # Make sure alt text still works even when overriding the image
280 assert get_tag_content(og_meta_tags, 'image:alt') == "Example's Docs!"
281
282
283 @pytest.mark.sphinx('html', testroot='overrides-complex')
284 def test_overrides_complex(og_meta_tags):
285 assert len(get_tag_content(og_meta_tags, 'description')) == 10
286 assert (
287 get_tag_content(og_meta_tags, 'image')
288 == 'http://example.org/en/latest/img/sample.jpg'
289 )
290 assert get_tag_content(og_meta_tags, 'image:alt') == 'Overridden Alt Text'
291
292
293 @pytest.mark.sphinx('html', testroot='overrides-disable')
294 def test_overrides_disable(og_meta_tags):
295 assert len(og_meta_tags) == 0
296
297
298 @pytest.mark.sphinx('html', testroot='arbitrary-tags')
299 def test_arbitrary_tags(og_meta_tags):
300 assert (
301 get_tag_content(og_meta_tags, 'video')
302 == 'http://example.org/en/latest/video.mp4'
303 )
304 assert get_tag_content(og_meta_tags, 'video:type') == 'video/mp4'
305
306
307 # use same as simple, as configuration is identical to overriden
308 @pytest.mark.sphinx('html', testroot='simple')
309 def test_rtd_override(app: Sphinx, monkeypatch):
310 monkeypatch.setenv('READTHEDOCS', 'True')
311 app.config.html_baseurl = 'https://failure.com/en/latest/'
312
313 app.build()
314 tags = conftest._og_meta_tags(app)
315
316 assert get_tag_content(tags, 'url') == 'http://example.org/en/latest/index.html'
317
318
319 @pytest.mark.sphinx('html', testroot='rtd-default')
320 def test_rtd_valid(app: Sphinx, monkeypatch):
321 monkeypatch.setenv('READTHEDOCS', 'True')
322 monkeypatch.setenv('READTHEDOCS_CANONICAL_URL', 'https://failure.com/en/latest/')
323
324 app.build()
325 tags = conftest._og_meta_tags(app)
326
327 assert get_tag_content(tags, 'url') == 'https://failure.com/en/latest/index.html'
328
329
330 # use rtd-default, as we are not changing configuration, but RTD variables
331 @pytest.mark.sphinx('html', testroot='rtd-invalid')
332 def test_rtd_invalid(app: Sphinx, monkeypatch):
333 monkeypatch.setenv('READTHEDOCS', 'True')
334 app.config.html_baseurl = None
335
336 with pytest.raises(ExtensionError, match='did not provide a valid canonical URL'):
337 app.build()
338
339
340 # Test no breakage with no configuration
341 @pytest.mark.sphinx('html', testroot='rtd-default')
342 def test_no_configuration_html(og_meta_tags):
343 assert get_tag_content(og_meta_tags, 'type') == 'website'
344
345
346 # Test no breakage with no configuration
347 @pytest.mark.sphinx('dirhtml', testroot='rtd-default')
348 def test_no_configuration_dirhtml(og_meta_tags):
349 assert get_tag_content(og_meta_tags, 'type') == 'website'