Skip to content

Commit 86216e1

Browse files
committed
Add tests for card generation
1 parent 04ec6df commit 86216e1

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
import io
4+
import pathlib
5+
6+
extensions = ['sphinxext.opengraph']
7+
8+
master_doc = 'index'
9+
exclude_patterns = ['_build']
10+
11+
html_theme = 'basic'
12+
13+
ogp_site_url = 'http://example.org/en/latest/'
14+
15+
16+
def setup(app):
17+
app.connect('generate-social-card', generate)
18+
19+
20+
def generate(
21+
app,
22+
contents,
23+
check_if_signature_exists,
24+
) -> None | tuple[io.BytesIO, str]:
25+
signature = 'custom-signature'
26+
check_if_signature_exists(signature)
27+
28+
return io.BytesIO(pathlib.Path(app.srcdir / 'pixel.png').read_bytes()), signature
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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 varius natoque penatibus et magnis dis parturient mauris.
95 Bytes
Loading

tests/test_options.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import pathlib
34
from typing import TYPE_CHECKING
45

56
import conftest
@@ -119,7 +120,7 @@ def test_image_alt(og_meta_tags):
119120

120121

121122
@pytest.mark.sphinx('html', testroot='simple')
122-
def test_image_social_cards(meta_tags):
123+
def test_image_social_cards(content, meta_tags):
123124
"""Social cards should automatically be added if no og:image is given."""
124125
pytest.importorskip('matplotlib')
125126
# Asserting `in` instead of `==` because of the hash that is generated
@@ -136,6 +137,39 @@ def test_image_social_cards(meta_tags):
136137
assert 'summary_large_image' in get_tag_content(
137138
meta_tags, 'card', kind='name', prefix='twitter'
138139
)
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'\x89PNG\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'\x89PNG\r\n\x1a\n'
172+
assert len(png_file.read_bytes()) == 95 # Size of the provided pixel.png
139173

140174

141175
@pytest.mark.sphinx('html', testroot='type')

0 commit comments

Comments
 (0)