Skip to content

Commit

Permalink
First attempt to break __init__ in smaller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
werdeil committed Mar 23, 2020
1 parent ca9efc9 commit a064b92
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,37 +336,52 @@ def _append_video(self, extra, extra_index, test_index):
)
self.additional_html.append(html.div(html_div, class_="video"))

class EnvironmentTable:
class EnvironmentSection:
def __init__(self, config):
self.metadata = getattr(config, "_metadata", [])
self.config = config
self.environment = []
self.environment_table = []

rows = []

header_cells = [html.th("Key"), html.th("Value")]
self.config.hook.pytest_html_environment_table_header(cells=header_cells)
rows.append(header_cells)
if self.metadata:
self.rows = []
self._generate_environment_header()

keys = [k for k in self.metadata.keys()]
if not isinstance(self.metadata, OrderedDict):
keys.sort()

for key in keys:
value = self.metadata[key]
if isinstance(value, str) and value.startswith("http"):
value = html.a(value, href=value, target="_blank")
elif isinstance(value, (list, tuple, set)):
value = ", ".join(str(i) for i in sorted(map(str, value)))
elif isinstance(value, dict):
sorted_dict = {k: value[k] for k in sorted(value)}
value = json.dumps(sorted_dict)
raw_value_string = raw(str(value))
row_cells = html.tr(html.td(key), html.td(raw_value_string))
self.config.hook.pytest_html_environment_table_row(cells=row_cells)
rows.append(row_cells)

self.environment_table.append(html.table(rows, id="environment"))
self._generate_environment_row(key)

self._generate_environment_section()

def _generate_environment_header(self):
header_cells = [html.th("Key"), html.th("Value")]
self.config.hook.pytest_html_environment_table_header(cells=header_cells)
self.rows.append(header_cells)

def _generate_environment_row(self, key):
value = self.metadata[key]
if isinstance(value, str) and value.startswith("http"):
value = html.a(value, href=value, target="_blank")
elif isinstance(value, (list, tuple, set)):
value = ", ".join(str(i) for i in sorted(map(str, value)))
elif isinstance(value, dict):
sorted_dict = {k: value[k] for k in sorted(value)}
value = json.dumps(sorted_dict)
raw_value_string = raw(str(value))
row_cells = html.tr(html.td(key), html.td(raw_value_string))
self.config.hook.pytest_html_environment_table_row(cells=row_cells)
self.rows.append(row_cells)

def _generate_environment_table(self):
self.environment_table.append(html.table(self.rows, id="environment"))

def _generate_environment_section(self):
if self.environment_table:
self.environment = [html.h2("Environment")]
self.environment.append(self.environment_table)

def _appendrow(self, outcome, report):
result = self.TestResult(outcome, report, self.logfile, self.config)
Expand Down Expand Up @@ -564,7 +579,7 @@ def generate_summary_item(self):
onLoad="init()",
)

body.extend(self._generate_environment(session.config))
body.extend(self.EnvironmentSection(session.config).environment_table)

summary_prefix, summary_postfix = [], []
session.config.hook.pytest_html_results_summary(
Expand All @@ -582,15 +597,6 @@ def generate_summary_item(self):
unicode_doc = unicode_doc.encode("utf-8", errors="xmlcharrefreplace")
return unicode_doc.decode("utf-8")

def _generate_environment(self, config):
environment_table = self.EnvironmentTable(config).environment_table
if environment_table:
environment = [html.h2("Environment")]
environment.append(environment_table)
else:
environment = []
return environment

def _save_report(self, report_content):
dir_name = os.path.dirname(self.logfile)
assets_dir = os.path.join(dir_name, "assets")
Expand Down

0 comments on commit a064b92

Please sign in to comment.