Skip to content

Commit

Permalink
fix: Replacing log HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil committed Apr 1, 2023
1 parent 81dc803 commit e215f06
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 44 deletions.
56 changes: 14 additions & 42 deletions src/pytest_html/basereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

from pytest_html import __version__
from pytest_html import extras
from pytest_html.table import Header
from pytest_html.table import Html
from pytest_html.table import Row
from pytest_html.util import cleanup_unserializable


try:
from ansi2html import Ansi2HTMLConverter, style

Expand All @@ -31,37 +33,6 @@


class BaseReport:
class Cells:
def __init__(self):
self._html = {}

def __delitem__(self, key):
# This means the item should be removed
self._html = None

@property
def html(self):
return self._html

def insert(self, index, html):
# backwards-compat
if not isinstance(html, str):
if html.__module__.startswith("py."):
warnings.warn(
"The 'py' module is deprecated and support "
"will be removed in a future release.",
DeprecationWarning,
)
html = str(html)
html = html.replace("col", "data-column-type")
self._html[index] = html

def pop(self, *args):
warnings.warn(
"'pop' is deprecated and no longer supported.",
DeprecationWarning,
)

class Report:
def __init__(self, title, config):
self._config = config
Expand Down Expand Up @@ -100,15 +71,16 @@ def data(self):
def set_data(self, key, value):
self._data[key] = value

def add_test(self, test_data, report):
def add_test(self, test_data, report, remove_log=False):
# regardless of pass or fail we must add teardown logging to "call"
if report.when == "teardown":
if report.when == "teardown" and not remove_log:
self.update_test_log(report)

# passed "setup" and "teardown" are not added to the html
if report.when == "call" or _is_error(report):
processed_logs = _process_logs(report)
test_data["log"] = _handle_ansi(processed_logs)
if not remove_log:
processed_logs = _process_logs(report)
test_data["log"] = _handle_ansi(processed_logs)
self._data["tests"][report.nodeid].append(test_data)
return True

Expand All @@ -117,7 +89,7 @@ def add_test(self, test_data, report):
def update_test_log(self, report):
log = []
for test in self._data["tests"][report.nodeid]:
if test["testId"] == report.nodeid:
if test["testId"] == report.nodeid and "log" in test:
for section in report.sections:
header, content = section
if "teardown" in header:
Expand Down Expand Up @@ -260,7 +232,7 @@ def pytest_sessionstart(self, session):

session.config.hook.pytest_html_report_title(report=self._report)

header_cells = self.Cells()
header_cells = Header()
session.config.hook.pytest_html_results_table_header(cells=header_cells)

self._report.set_data("resultsTableHeader", header_cells.html)
Expand Down Expand Up @@ -301,28 +273,28 @@ def pytest_runtest_logreport(self, report):
}

test_id = report.nodeid
table_html = Html()
if report.when == "call":
row_cells = self.Cells()
row_cells = Row()
self._config.hook.pytest_html_results_table_row(
report=report, cells=row_cells
)
if row_cells.html is None:
return
data["resultsTableRow"] = row_cells.html

table_html = []
self._config.hook.pytest_html_results_table_html(
report=report, data=table_html
)
data["tableHtml"] = table_html
data["tableHtml"] = table_html.html["html"]
else:
test_id += f"::{report.when}"
data["testId"] = test_id

data["result"] = _process_outcome(report)
data["extras"] = self._process_extras(report, test_id)

if self._report.add_test(data, report):
if self._report.add_test(data, report, table_html.replace_log):
self._generate_report()


Expand Down
5 changes: 3 additions & 2 deletions src/pytest_html/scripts/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ const dom = {


if (log) {
// resultBody.querySelector('.log').innerText = log
resultBody.querySelector('.log').innerHTML = log
} else {
resultBody.querySelector('.log').remove()
}
// if (collapsed || !longreprtext) {

if (collapsed) {
resultBody.querySelector('.extras-row').classList.add('hidden')
}
Expand Down
59 changes: 59 additions & 0 deletions src/pytest_html/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import warnings


class Table:
def __init__(self):
self._html = {}

@property
def html(self):
return self._html


class Html(Table):
def __init__(self):
super().__init__()
self.html.setdefault("html", [])
self._replace_log = False

def __delitem__(self, key):
# This means the log should be removed
self._replace_log = True

@property
def replace_log(self):
return self._replace_log

def append(self, html):
self.html["html"].append(html)


class Cell(Table):
def insert(self, index, html):
# backwards-compat
if not isinstance(html, str):
if html.__module__.startswith("py."):
warnings.warn(
"The 'py' module is deprecated and support "
"will be removed in a future release.",
DeprecationWarning,
)
html = str(html)
html = html.replace("col", "data-column-type")
self._html[index] = html

def pop(self, *args):
warnings.warn(
"'pop' is deprecated and no longer supported.",
DeprecationWarning,
)


class Header(Cell):
pass


class Row(Cell):
def __delitem__(self, key):
# This means the item should be removed
self._html = None

0 comments on commit e215f06

Please sign in to comment.