1
1
from __future__ import annotations
2
2
3
+ import pathlib
3
4
from typing import TYPE_CHECKING
4
5
5
6
import conftest
@@ -119,7 +120,7 @@ def test_image_alt(og_meta_tags):
119
120
120
121
121
122
@pytest .mark .sphinx ('html' , testroot = 'simple' )
122
- def test_image_social_cards (meta_tags ):
123
+ def test_image_social_cards (content , meta_tags ):
123
124
"""Social cards should automatically be added if no og:image is given."""
124
125
pytest .importorskip ('matplotlib' )
125
126
# Asserting `in` instead of `==` because of the hash that is generated
@@ -136,6 +137,39 @@ def test_image_social_cards(meta_tags):
136
137
assert 'summary_large_image' in get_tag_content (
137
138
meta_tags , 'card' , kind = 'name' , prefix = 'twitter'
138
139
)
140
+ png_file = (
141
+ pathlib .Path (content .outdir )
142
+ / get_tag_content (meta_tags , 'image' ).split ('/' , 5 )[- 1 ]
143
+ )
144
+ assert png_file .is_file ()
145
+ # https://en.wikipedia.org/wiki/List_of_file_signatures
146
+ assert png_file .read_bytes ()[:8 ] == b'\x89 PNG\r \n \x1a \n '
147
+ assert get_tag_content (meta_tags , 'image:width' ) == '1146'
148
+ assert get_tag_content (meta_tags , 'image:height' ) == '600'
149
+
150
+
151
+ @pytest .mark .sphinx ('html' , testroot = 'custom-social-card-generation' )
152
+ def test_image_social_cards_custom (content , meta_tags ):
153
+ """Providing a custom generation function for social cards."""
154
+ # Asserting `in` instead of `==` because of the hash that is generated
155
+ assert (
156
+ 'http://example.org/en/latest/_images/social_previews/summary_index'
157
+ in get_tag_content (meta_tags , 'image' )
158
+ )
159
+ # Make sure the extra tags are in the HTML
160
+ assert 'summary_large_image' in get_tag_content (
161
+ meta_tags , 'card' , kind = 'name' , prefix = 'twitter'
162
+ )
163
+ assert get_tag_content (meta_tags , 'image:width' ) == '1'
164
+ assert get_tag_content (meta_tags , 'image:height' ) == '1'
165
+ png_file = (
166
+ pathlib .Path (content .outdir )
167
+ / get_tag_content (meta_tags , 'image' ).split ('/' , 5 )[- 1 ]
168
+ )
169
+ assert png_file .is_file ()
170
+ # https://en.wikipedia.org/wiki/List_of_file_signatures
171
+ assert png_file .read_bytes ()[:8 ] == b'\x89 PNG\r \n \x1a \n '
172
+ assert len (png_file .read_bytes ()) == 95 # Size of the provided pixel.png
139
173
140
174
141
175
@pytest .mark .sphinx ('html' , testroot = 'type' )
0 commit comments