Skip to content

Commit

Permalink
As discussed, this deals with the code duplication introduced in #260.…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
ExaltedBagel committed Feb 4, 2020
1 parent f03b9d9 commit 588c41b
Showing 1 changed file with 24 additions and 34 deletions.
58 changes: 24 additions & 34 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,33 +181,7 @@ def create_asset(
def append_extra_html(self, extra, extra_index, test_index):
href = None
if extra.get("format") == extras.FORMAT_IMAGE:
content = extra.get("content")
try:
is_uri_or_path = content.startswith(("file", "http")) or isfile(
content
)
except ValueError:
# On Windows, os.path.isfile throws this exception when
# passed a b64 encoded image.
is_uri_or_path = False
if is_uri_or_path:
if self.self_contained:
warnings.warn(
"Self-contained HTML report "
"includes link to external "
"resource: {}".format(content)
)
html_div = html.a(html.img(src=content), href=content)
elif self.self_contained:
src = "data:{};base64,{}".format(extra.get("mime_type"), content)
html_div = html.img(src=src)
else:
content = b64decode(content.encode("utf-8"))
href = src = self.create_asset(
content, extra_index, test_index, extra.get("extension"), "wb"
)
html_div = html.a(html.img(src=src), href=href)
self.additional_html.append(html.div(html_div, class_="image"))
self._append_image(extra, extra_index, test_index)

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

def _append_video(self, extra, extra_index, test_index):
video_base = '<video controls><source src="{}" type="video/mp4"></video>'
def _make_media_html_div(
self, extra, extra_index, test_index, base_extra_string, base_extra_class
):
content = extra.get("content")
try:
is_uri_or_path = content.startswith(("file", "http")) or isfile(content)
Expand All @@ -293,20 +268,35 @@ def _append_video(self, extra, extra_index, test_index):
warnings.warn(
"Self-contained HTML report "
"includes link to external "
"resource: {}".format(content)
f"resource: {content}"
)

html_div = html.div(raw(video_base.format(extra.get("content"))))
html_div = html.a(
raw(base_extra_string.format(extra.get("content"))), href=content
)
elif self.self_contained:
src = "data:{};base64,{}".format(extra.get("mime_type"), content)
html_div = html.div(raw(video_base.format(src)))
src = f"data:{extra.get('mime_type')};base64,{content}"
html_div = raw(base_extra_string.format(src))
else:
content = b64decode(content.encode("utf-8"))
href = src = self.create_asset(
content, extra_index, test_index, extra.get("extension"), "wb"
)
html_div = html.a(class_=base_extra_class, target="_blank", href=href)
return html_div

def _append_image(self, extra, extra_index, test_index):
image_base = '<img src="{}"/>'
html_div = self._make_media_html_div(
extra, extra_index, test_index, image_base, "image"
)
self.additional_html.append(html.div(html_div, class_="image"))

html_div = html.a(video_base.format(src), href=href)
def _append_video(self, extra, extra_index, test_index):
video_base = '<video controls><source src="{}" type="video/mp4"></video>'
html_div = self._make_media_html_div(
extra, extra_index, test_index, video_base, "video"
)
self.additional_html.append(html.div(html_div, class_="video"))

def _appendrow(self, outcome, report):
Expand Down

0 comments on commit 588c41b

Please sign in to comment.