Skip to content

Commit 8e14277

Browse files
werdeilVincent VERDEIL
authored and
Vincent VERDEIL
committed
First attempt to break __init__ in smaller functions
1 parent 6731791 commit 8e14277

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

pytest_html/plugin.py

+36-30
Original file line numberDiff line numberDiff line change
@@ -341,37 +341,52 @@ def _append_video(self, extra, extra_index, test_index):
341341
)
342342
self.additional_html.append(html.div(html_div, class_="video"))
343343

344-
class EnvironmentTable:
344+
class EnvironmentSection:
345345
def __init__(self, config):
346346
self.metadata = getattr(config, "_metadata", [])
347347
self.config = config
348+
self.environment = []
348349
self.environment_table = []
349350

350-
rows = []
351-
352-
header_cells = [html.th("Key"), html.th("Value")]
353-
self.config.hook.pytest_html_environment_table_header(cells=header_cells)
354-
rows.append(header_cells)
355351
if self.metadata:
352+
self.rows = []
353+
self._generate_environment_header()
354+
356355
keys = [k for k in self.metadata.keys()]
357356
if not isinstance(self.metadata, OrderedDict):
358357
keys.sort()
359358

360359
for key in keys:
361-
value = self.metadata[key]
362-
if isinstance(value, str) and value.startswith("http"):
363-
value = html.a(value, href=value, target="_blank")
364-
elif isinstance(value, (list, tuple, set)):
365-
value = ", ".join(str(i) for i in sorted(map(str, value)))
366-
elif isinstance(value, dict):
367-
sorted_dict = {k: value[k] for k in sorted(value)}
368-
value = json.dumps(sorted_dict)
369-
raw_value_string = raw(str(value))
370-
row_cells = html.tr(html.td(key), html.td(raw_value_string))
371-
self.config.hook.pytest_html_environment_table_row(cells=row_cells)
372-
rows.append(row_cells)
373-
374-
self.environment_table.append(html.table(rows, id="environment"))
360+
self._generate_environment_row(key)
361+
362+
self._generate_environment_section()
363+
364+
def _generate_environment_header(self):
365+
header_cells = [html.th("Key"), html.th("Value")]
366+
self.config.hook.pytest_html_environment_table_header(cells=header_cells)
367+
self.rows.append(header_cells)
368+
369+
def _generate_environment_row(self, key):
370+
value = self.metadata[key]
371+
if isinstance(value, str) and value.startswith("http"):
372+
value = html.a(value, href=value, target="_blank")
373+
elif isinstance(value, (list, tuple, set)):
374+
value = ", ".join(str(i) for i in sorted(map(str, value)))
375+
elif isinstance(value, dict):
376+
sorted_dict = {k: value[k] for k in sorted(value)}
377+
value = json.dumps(sorted_dict)
378+
raw_value_string = raw(str(value))
379+
row_cells = html.tr(html.td(key), html.td(raw_value_string))
380+
self.config.hook.pytest_html_environment_table_row(cells=row_cells)
381+
self.rows.append(row_cells)
382+
383+
def _generate_environment_table(self):
384+
self.environment_table.append(html.table(self.rows, id="environment"))
385+
386+
def _generate_environment_section(self):
387+
if self.environment_table:
388+
self.environment = [html.h2("Environment")]
389+
self.environment.append(self.environment_table)
375390

376391
def _appendrow(self, outcome, report):
377392
result = self.TestResult(outcome, report, self.logfile, self.config)
@@ -569,7 +584,7 @@ def generate_summary_item(self):
569584
onLoad="init()",
570585
)
571586

572-
body.extend(self._generate_environment(session.config))
587+
body.extend(self.EnvironmentSection(session.config).environment_table)
573588

574589
summary_prefix, summary_postfix = [], []
575590
session.config.hook.pytest_html_results_summary(
@@ -587,15 +602,6 @@ def generate_summary_item(self):
587602
unicode_doc = unicode_doc.encode("utf-8", errors="xmlcharrefreplace")
588603
return unicode_doc.decode("utf-8")
589604

590-
def _generate_environment(self, config):
591-
environment_table = self.EnvironmentTable(config).environment_table
592-
if environment_table:
593-
environment = [html.h2("Environment")]
594-
environment.append(environment_table)
595-
else:
596-
environment = []
597-
return environment
598-
599605
def _save_report(self, report_content):
600606
dir_name = os.path.dirname(self.logfile)
601607
assets_dir = os.path.join(dir_name, "assets")

0 commit comments

Comments
 (0)