Skip to content

Commit 2008a3a

Browse files
committed
[tests] Use BeautifulSoup to verify output
Using BeautifulSoup instead of regular expressions lets us assert that the expected elements are found with the right classes and attributes, without hard-coding whether e.g. `alt` is the first attribute written in the `img` tag. This will enable using these same assertions over HTML generated by ET.tostring in the RST implementation.
1 parent c8310ff commit 2008a3a

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

pelican/plugins/graphviz/test_graphviz.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,16 @@
2121
from tempfile import mkdtemp
2222
import unittest
2323

24+
from bs4 import BeautifulSoup, Tag
25+
2426
from pelican import Pelican
2527
from pelican.settings import read_settings
2628

2729
from . import graphviz
2830

2931
TEST_FILE_STEM = "test"
3032
TEST_DIR_PREFIX = "pelicantests."
31-
GRAPHVIZ_RE = (
32-
r'<{0} class="{1}"><img alt="{2}" '
33-
r'src="data:image/svg\+xml;base64,[0-9a-zA-Z+=]+"></{0}>'
34-
)
35-
36-
GRAPHVIZ_RE_XML = r'<svg width="\d+pt" height="\d+pt"'
33+
DIMENSION_ATTR_RE = re.compile(r"\d+pt")
3734

3835

3936
class TestGraphviz(unittest.TestCase):
@@ -105,26 +102,25 @@ def assert_expected_output(self):
105102
"""Test for default values of the configuration variables."""
106103
# Open the output HTML file
107104
with open(os.path.join(self.output_path, f"{TEST_FILE_STEM}.html")) as fid:
105+
# Keep content as a string so we can see full content in output
106+
# from failed asserts.
108107
content = fid.read()
109-
found = False
110-
# Iterate over the lines and look for the HTML element corresponding
111-
# to the generated Graphviz figure
112-
for line in content.splitlines():
113-
if self.expected_compressed:
114-
if re.search(
115-
GRAPHVIZ_RE.format(
116-
self.expected_html_element,
117-
self.expected_image_class,
118-
self.expected_alt_text,
119-
),
120-
line,
121-
):
122-
found = True
123-
break
124-
elif re.search(GRAPHVIZ_RE_XML, line):
125-
found = True
126-
break
127-
assert found, content
108+
soup = BeautifulSoup(content, "html.parser")
109+
if self.expected_compressed:
110+
elt = soup.find(
111+
self.expected_html_element, class_=self.expected_image_class
112+
)
113+
assert isinstance(elt, Tag), content
114+
115+
img = elt.find("img", attrs={"alt": self.expected_alt_text})
116+
assert img is not None, content
117+
else:
118+
svg = soup.find("svg")
119+
assert isinstance(svg, Tag), content
120+
121+
for attr in ["width", "height"]:
122+
assert attr in svg.attrs
123+
assert DIMENSION_ATTR_RE.fullmatch(str(svg.attrs[attr]))
128124

129125
def tearDown(self):
130126
"""Tidy up the test environment."""

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ lint = [
4545
"ruff>=0.9.1,<1.0.0",
4646
]
4747
test = [
48+
"beautifulsoup4>=4.13",
4849
"invoke>=2.2",
4950
"markdown>=3.4",
5051
"pytest>=7.0",

0 commit comments

Comments
 (0)