Skip to content

Commit 588c41b

Browse files
committed
As discussed, this deals with the code duplication introduced in #260. These changes aimed at generelizing the
approach used to introduce media types to the project. Images and videos now use a method called _make_media_html_div which generates the media div in a streamlined manner. Both _append_image and _append_video use this method, to which they can pass their base string and class. Unit tests were left untouched, as the changes only affect private methods.
1 parent f03b9d9 commit 588c41b

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

pytest_html/plugin.py

+24-34
Original file line numberDiff line numberDiff line change
@@ -181,33 +181,7 @@ def create_asset(
181181
def append_extra_html(self, extra, extra_index, test_index):
182182
href = None
183183
if extra.get("format") == extras.FORMAT_IMAGE:
184-
content = extra.get("content")
185-
try:
186-
is_uri_or_path = content.startswith(("file", "http")) or isfile(
187-
content
188-
)
189-
except ValueError:
190-
# On Windows, os.path.isfile throws this exception when
191-
# passed a b64 encoded image.
192-
is_uri_or_path = False
193-
if is_uri_or_path:
194-
if self.self_contained:
195-
warnings.warn(
196-
"Self-contained HTML report "
197-
"includes link to external "
198-
"resource: {}".format(content)
199-
)
200-
html_div = html.a(html.img(src=content), href=content)
201-
elif self.self_contained:
202-
src = "data:{};base64,{}".format(extra.get("mime_type"), content)
203-
html_div = html.img(src=src)
204-
else:
205-
content = b64decode(content.encode("utf-8"))
206-
href = src = self.create_asset(
207-
content, extra_index, test_index, extra.get("extension"), "wb"
208-
)
209-
html_div = html.a(html.img(src=src), href=href)
210-
self.additional_html.append(html.div(html_div, class_="image"))
184+
self._append_image(extra, extra_index, test_index)
211185

212186
elif extra.get("format") == extras.FORMAT_HTML:
213187
self.additional_html.append(html.div(raw(extra.get("content"))))
@@ -279,8 +253,9 @@ def append_log_html(self, report, additional_html):
279253
log.append("No log output captured.")
280254
additional_html.append(log)
281255

282-
def _append_video(self, extra, extra_index, test_index):
283-
video_base = '<video controls><source src="{}" type="video/mp4"></video>'
256+
def _make_media_html_div(
257+
self, extra, extra_index, test_index, base_extra_string, base_extra_class
258+
):
284259
content = extra.get("content")
285260
try:
286261
is_uri_or_path = content.startswith(("file", "http")) or isfile(content)
@@ -293,20 +268,35 @@ def _append_video(self, extra, extra_index, test_index):
293268
warnings.warn(
294269
"Self-contained HTML report "
295270
"includes link to external "
296-
"resource: {}".format(content)
271+
f"resource: {content}"
297272
)
298273

299-
html_div = html.div(raw(video_base.format(extra.get("content"))))
274+
html_div = html.a(
275+
raw(base_extra_string.format(extra.get("content"))), href=content
276+
)
300277
elif self.self_contained:
301-
src = "data:{};base64,{}".format(extra.get("mime_type"), content)
302-
html_div = html.div(raw(video_base.format(src)))
278+
src = f"data:{extra.get('mime_type')};base64,{content}"
279+
html_div = raw(base_extra_string.format(src))
303280
else:
304281
content = b64decode(content.encode("utf-8"))
305282
href = src = self.create_asset(
306283
content, extra_index, test_index, extra.get("extension"), "wb"
307284
)
285+
html_div = html.a(class_=base_extra_class, target="_blank", href=href)
286+
return html_div
287+
288+
def _append_image(self, extra, extra_index, test_index):
289+
image_base = '<img src="{}"/>'
290+
html_div = self._make_media_html_div(
291+
extra, extra_index, test_index, image_base, "image"
292+
)
293+
self.additional_html.append(html.div(html_div, class_="image"))
308294

309-
html_div = html.a(video_base.format(src), href=href)
295+
def _append_video(self, extra, extra_index, test_index):
296+
video_base = '<video controls><source src="{}" type="video/mp4"></video>'
297+
html_div = self._make_media_html_div(
298+
extra, extra_index, test_index, video_base, "video"
299+
)
310300
self.additional_html.append(html.div(html_div, class_="video"))
311301

312302
def _appendrow(self, outcome, report):

0 commit comments

Comments
 (0)