Skip to content

Commit 79e4f3c

Browse files
committed
chore: Better directory and class structure (pytest-dev#609)
1 parent 09abf2e commit 79e4f3c

File tree

4 files changed

+81
-76
lines changed

4 files changed

+81
-76
lines changed
Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import base64
2-
import binascii
31
import datetime
42
import json
53
import os
@@ -14,9 +12,9 @@
1412
from jinja2 import FileSystemLoader
1513
from jinja2 import select_autoescape
1614

17-
from . import __version__
18-
from . import extras
19-
from .util import cleanup_unserializable
15+
from pytest_html import __version__
16+
from pytest_html import extras
17+
from pytest_html.util import cleanup_unserializable
2018

2119

2220
try:
@@ -217,10 +215,7 @@ def _process_extras(self, report, test_id):
217215
content, asset_name=asset_name, mime_type=extra["mime_type"]
218216
)
219217

220-
if (
221-
extra["format_type"] == extras.FORMAT_IMAGE
222-
or extra["format_type"] == extras.FORMAT_VIDEO
223-
):
218+
if extra["format_type"] in [extras.FORMAT_IMAGE, extras.FORMAT_VIDEO]:
224219
extra["content"] = self._media_content(
225220
content, asset_name=asset_name, mime_type=extra["mime_type"]
226221
)
@@ -329,69 +324,6 @@ def pytest_runtest_logreport(self, report):
329324
self._generate_report()
330325

331326

332-
class NextGenReport(BaseReport):
333-
def __init__(self, report_path, config):
334-
super().__init__(report_path, config)
335-
self._assets_path = Path(self._report_path.parent, "assets")
336-
self._assets_path.mkdir(parents=True, exist_ok=True)
337-
self._css_path = Path(self._assets_path, "style.css")
338-
339-
with self._css_path.open("w", encoding="utf-8") as f:
340-
f.write(self._css)
341-
342-
@property
343-
def css(self):
344-
return Path(self._assets_path.name, "style.css")
345-
346-
def _data_content(self, content, asset_name, *args, **kwargs):
347-
content = content.encode("utf-8")
348-
return self._write_content(content, asset_name)
349-
350-
def _media_content(self, content, asset_name, *args, **kwargs):
351-
try:
352-
media_data = base64.b64decode(content.encode("utf-8"), validate=True)
353-
return self._write_content(media_data, asset_name)
354-
except binascii.Error:
355-
# if not base64 content, just return as it's a file or link
356-
return content
357-
358-
def _write_content(self, content, asset_name):
359-
content_relative_path = Path(self._assets_path, asset_name)
360-
content_relative_path.write_bytes(content)
361-
return str(content_relative_path.relative_to(self._report_path.parent))
362-
363-
364-
class NextGenSelfContainedReport(BaseReport):
365-
def __init__(self, report_path, config):
366-
super().__init__(report_path, config)
367-
368-
@property
369-
def css(self):
370-
return self._css
371-
372-
def _data_content(self, content, mime_type, *args, **kwargs):
373-
charset = "utf-8"
374-
data = base64.b64encode(content.encode(charset)).decode(charset)
375-
return f"data:{mime_type};charset={charset};base64,{data}"
376-
377-
def _media_content(self, content, mime_type, *args, **kwargs):
378-
try:
379-
# test if content is base64
380-
base64.b64decode(content.encode("utf-8"), validate=True)
381-
return f"data:{mime_type};base64,{content}"
382-
except binascii.Error:
383-
# if not base64, issue warning and just return as it's a file or link
384-
warnings.warn(
385-
"Self-contained HTML report "
386-
"includes link to external "
387-
f"resource: {content}"
388-
)
389-
return content
390-
391-
def _generate_report(self, *args, **kwargs):
392-
super()._generate_report(self_contained=True)
393-
394-
395327
def _process_css(default_css, extra_css):
396328
with open(default_css, encoding="utf-8") as f:
397329
css = f.read()

src/pytest_html/plugin.py

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

77
import pytest
88

9-
from .nextgen import NextGenReport
10-
from .nextgen import NextGenSelfContainedReport
9+
from pytest_html.report import Report
10+
from pytest_html.selfcontained_report import SelfContainedReport
1111

1212

1313
def pytest_addhooks(pluginmanager):
@@ -80,9 +80,9 @@ def pytest_configure(config):
8080
if not hasattr(config, "workerinput"):
8181
# prevent opening html_path on worker nodes (xdist)
8282
if config.getoption("self_contained_html"):
83-
html = NextGenSelfContainedReport(html_path, config)
83+
html = SelfContainedReport(html_path, config)
8484
else:
85-
html = NextGenReport(html_path, config)
85+
html = Report(html_path, config)
8686

8787
config.pluginmanager.register(html)
8888

src/pytest_html/report.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import base64
2+
import binascii
3+
from pathlib import Path
4+
5+
from pytest_html.basereport import BaseReport
6+
7+
8+
class Report(BaseReport):
9+
def __init__(self, report_path, config):
10+
super().__init__(report_path, config)
11+
self._assets_path = Path(self._report_path.parent, "assets")
12+
self._assets_path.mkdir(parents=True, exist_ok=True)
13+
self._css_path = Path(self._assets_path, "style.css")
14+
15+
with self._css_path.open("w", encoding="utf-8") as f:
16+
f.write(self._css)
17+
18+
@property
19+
def css(self):
20+
return Path(self._assets_path.name, "style.css")
21+
22+
def _data_content(self, content, asset_name, *args, **kwargs):
23+
content = content.encode("utf-8")
24+
return self._write_content(content, asset_name)
25+
26+
def _media_content(self, content, asset_name, *args, **kwargs):
27+
try:
28+
media_data = base64.b64decode(content.encode("utf-8"), validate=True)
29+
return self._write_content(media_data, asset_name)
30+
except binascii.Error:
31+
# if not base64 content, just return as it's a file or link
32+
return content
33+
34+
def _write_content(self, content, asset_name):
35+
content_relative_path = Path(self._assets_path, asset_name)
36+
content_relative_path.write_bytes(content)
37+
return str(content_relative_path.relative_to(self._report_path.parent))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import base64
2+
import binascii
3+
import warnings
4+
5+
from pytest_html.basereport import BaseReport
6+
7+
8+
class SelfContainedReport(BaseReport):
9+
def __init__(self, report_path, config):
10+
super().__init__(report_path, config)
11+
12+
@property
13+
def css(self):
14+
return self._css
15+
16+
def _data_content(self, content, mime_type, *args, **kwargs):
17+
charset = "utf-8"
18+
data = base64.b64encode(content.encode(charset)).decode(charset)
19+
return f"data:{mime_type};charset={charset};base64,{data}"
20+
21+
def _media_content(self, content, mime_type, *args, **kwargs):
22+
try:
23+
# test if content is base64
24+
base64.b64decode(content.encode("utf-8"), validate=True)
25+
return f"data:{mime_type};base64,{content}"
26+
except binascii.Error:
27+
# if not base64, issue warning and just return as it's a file or link
28+
warnings.warn(
29+
"Self-contained HTML report "
30+
"includes link to external "
31+
f"resource: {content}"
32+
)
33+
return content
34+
35+
def _generate_report(self, *args, **kwargs):
36+
super()._generate_report(self_contained=True)

0 commit comments

Comments
 (0)