Skip to content

Commit 16eb656

Browse files
committed
Fixing tests
1 parent 269d082 commit 16eb656

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

docs/source/index.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,3 @@
77
:hidden:
88
socialcards
99
```
10-
<!--
11-
```{image} _static/images/
12-
``` -->

noxfile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
@nox.session
88
def docs(session):
9+
"""Build the documentation. Use `-- live` to build with a live server."""
910
session.install("-e", ".")
1011
session.install("-r", "docs/requirements.txt")
1112
if "live" in session.posargs:
@@ -14,3 +15,10 @@ def docs(session):
1415
session.run(*split("sphinx-autobuild -b html docs/source docs/build/html"))
1516
else:
1617
session.run(*split("sphinx-build -nW --keep-going -b html docs/source docs/build/html"))
18+
19+
20+
@nox.session
21+
def test(session):
22+
"""Run the test suite."""
23+
session.install("-e", ".")
24+
session.run(*(["pytest"] + session.posargs))

sphinxext/opengraph/__init__.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,21 @@ def get_tags(
134134
image_url = fields["og:image"]
135135
ogp_use_first_image = False
136136
ogp_image_alt = fields.get("og:image:alt")
137-
fields.pop("og:image", None)
138-
# This will only be False if the user explicitly sets it
139-
elif app.config.ogp_social_cards.get("enable") is not False:
140-
# Set up our configuration and update it with new information
141-
config_social = DEFAULT_SOCIAL_CONFIG.copy()
142-
config_social.update(app.config.ogp_social_cards)
137+
fields.pop("og:image", None)
138+
else:
139+
image_url = config["ogp_image"]
140+
ogp_use_first_image = config["ogp_use_first_image"]
141+
ogp_image_alt = fields.get("og:image:alt", config["ogp_image_alt"])
142+
143+
# Decide whether to add social media card images for each page.
144+
# Only do this as a fallback if the user hasn't given any configuration
145+
# to add other images.
146+
config_social = DEFAULT_SOCIAL_CONFIG.copy()
147+
social_card_user_options = app.config.ogp_social_cards or {}
148+
config_social.update(social_card_user_options)
143149

150+
# This will only be False if the user explicitly sets it
151+
if not (image_url or ogp_use_first_image) and config_social.get("enable") is not False:
144152
# Description
145153
description_max_length = config_social.get(
146154
"description_max_length", DEFAULT_DESCRIPTION_LENGTH_SOCIAL_CARDS - 3
@@ -170,16 +178,17 @@ def get_tags(
170178
url_text,
171179
context["pagename"]
172180
)
173-
ogp_image_alt = description
174181
ogp_use_first_image = False
175182

183+
# Alt text is taken from description unless given
184+
if "og:image:alt" in fields:
185+
ogp_image_alt = fields.get("og:image:alt")
186+
else:
187+
ogp_image_alt = description
188+
176189
# Link the image in our page metadata
177190
url = app.config.ogp_site_url.strip("/")
178191
image_url = f"{url}/{image_path}"
179-
else:
180-
image_url = config["ogp_image"]
181-
ogp_use_first_image = config["ogp_use_first_image"]
182-
ogp_image_alt = fields.get("og:image:alt", config["ogp_image_alt"])
183192

184193
fields.pop("og:image:alt", None)
185194

tests/test_options.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ def test_image_alt(og_meta_tags):
9292
assert get_tag_content(og_meta_tags, "image:alt") == "Example's Docs!"
9393

9494

95+
@pytest.mark.sphinx("html", testroot="simple")
96+
def test_image_social_cards(og_meta_tags):
97+
"""Social cards should automatically be added if no og:image is given."""
98+
# Asserting `in` instead of `==` because of the hash that is generated
99+
assert (
100+
"http://example.org/en/latest/_images/social_previews/summary_index" in
101+
get_tag_content(og_meta_tags, "image")
102+
)
103+
# Image alt text should be taken from page content.
104+
assert (
105+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit." in
106+
get_tag_content(og_meta_tags, "image:alt")
107+
)
108+
95109
@pytest.mark.sphinx("html", testroot="type")
96110
def test_type(og_meta_tags):
97111
assert get_tag_content(og_meta_tags, "type") == "article"

0 commit comments

Comments
 (0)