Skip to content

Commit e6a3d5c

Browse files
authored
fix: Replacing log HTML (#611)
1 parent 81dc803 commit e6a3d5c

File tree

3 files changed

+76
-44
lines changed

3 files changed

+76
-44
lines changed

src/pytest_html/basereport.py

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
from pytest_html import __version__
1616
from pytest_html import extras
17+
from pytest_html.table import Header
18+
from pytest_html.table import Html
19+
from pytest_html.table import Row
1720
from pytest_html.util import cleanup_unserializable
1821

19-
2022
try:
2123
from ansi2html import Ansi2HTMLConverter, style
2224

@@ -31,37 +33,6 @@
3133

3234

3335
class BaseReport:
34-
class Cells:
35-
def __init__(self):
36-
self._html = {}
37-
38-
def __delitem__(self, key):
39-
# This means the item should be removed
40-
self._html = None
41-
42-
@property
43-
def html(self):
44-
return self._html
45-
46-
def insert(self, index, html):
47-
# backwards-compat
48-
if not isinstance(html, str):
49-
if html.__module__.startswith("py."):
50-
warnings.warn(
51-
"The 'py' module is deprecated and support "
52-
"will be removed in a future release.",
53-
DeprecationWarning,
54-
)
55-
html = str(html)
56-
html = html.replace("col", "data-column-type")
57-
self._html[index] = html
58-
59-
def pop(self, *args):
60-
warnings.warn(
61-
"'pop' is deprecated and no longer supported.",
62-
DeprecationWarning,
63-
)
64-
6536
class Report:
6637
def __init__(self, title, config):
6738
self._config = config
@@ -100,15 +71,16 @@ def data(self):
10071
def set_data(self, key, value):
10172
self._data[key] = value
10273

103-
def add_test(self, test_data, report):
74+
def add_test(self, test_data, report, remove_log=False):
10475
# regardless of pass or fail we must add teardown logging to "call"
105-
if report.when == "teardown":
76+
if report.when == "teardown" and not remove_log:
10677
self.update_test_log(report)
10778

10879
# passed "setup" and "teardown" are not added to the html
10980
if report.when == "call" or _is_error(report):
110-
processed_logs = _process_logs(report)
111-
test_data["log"] = _handle_ansi(processed_logs)
81+
if not remove_log:
82+
processed_logs = _process_logs(report)
83+
test_data["log"] = _handle_ansi(processed_logs)
11284
self._data["tests"][report.nodeid].append(test_data)
11385
return True
11486

@@ -117,7 +89,7 @@ def add_test(self, test_data, report):
11789
def update_test_log(self, report):
11890
log = []
11991
for test in self._data["tests"][report.nodeid]:
120-
if test["testId"] == report.nodeid:
92+
if test["testId"] == report.nodeid and "log" in test:
12193
for section in report.sections:
12294
header, content = section
12395
if "teardown" in header:
@@ -260,7 +232,7 @@ def pytest_sessionstart(self, session):
260232

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

263-
header_cells = self.Cells()
235+
header_cells = Header()
264236
session.config.hook.pytest_html_results_table_header(cells=header_cells)
265237

266238
self._report.set_data("resultsTableHeader", header_cells.html)
@@ -301,28 +273,28 @@ def pytest_runtest_logreport(self, report):
301273
}
302274

303275
test_id = report.nodeid
276+
table_html = Html()
304277
if report.when == "call":
305-
row_cells = self.Cells()
278+
row_cells = Row()
306279
self._config.hook.pytest_html_results_table_row(
307280
report=report, cells=row_cells
308281
)
309282
if row_cells.html is None:
310283
return
311284
data["resultsTableRow"] = row_cells.html
312285

313-
table_html = []
314286
self._config.hook.pytest_html_results_table_html(
315287
report=report, data=table_html
316288
)
317-
data["tableHtml"] = table_html
289+
data["tableHtml"] = table_html.html["html"]
318290
else:
319291
test_id += f"::{report.when}"
320292
data["testId"] = test_id
321293

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

325-
if self._report.add_test(data, report):
297+
if self._report.add_test(data, report, table_html.replace_log):
326298
self._generate_report()
327299

328300

src/pytest_html/scripts/dom.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ const dom = {
9494

9595

9696
if (log) {
97-
// resultBody.querySelector('.log').innerText = log
9897
resultBody.querySelector('.log').innerHTML = log
98+
} else {
99+
resultBody.querySelector('.log').remove()
99100
}
100-
// if (collapsed || !longreprtext) {
101+
101102
if (collapsed) {
102103
resultBody.querySelector('.extras-row').classList.add('hidden')
103104
}

src/pytest_html/table.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import warnings
2+
3+
4+
class Table:
5+
def __init__(self):
6+
self._html = {}
7+
8+
@property
9+
def html(self):
10+
return self._html
11+
12+
13+
class Html(Table):
14+
def __init__(self):
15+
super().__init__()
16+
self.html.setdefault("html", [])
17+
self._replace_log = False
18+
19+
def __delitem__(self, key):
20+
# This means the log should be removed
21+
self._replace_log = True
22+
23+
@property
24+
def replace_log(self):
25+
return self._replace_log
26+
27+
def append(self, html):
28+
self.html["html"].append(html)
29+
30+
31+
class Cell(Table):
32+
def insert(self, index, html):
33+
# backwards-compat
34+
if not isinstance(html, str):
35+
if html.__module__.startswith("py."):
36+
warnings.warn(
37+
"The 'py' module is deprecated and support "
38+
"will be removed in a future release.",
39+
DeprecationWarning,
40+
)
41+
html = str(html)
42+
html = html.replace("col", "data-column-type")
43+
self._html[index] = html
44+
45+
def pop(self, *args):
46+
warnings.warn(
47+
"'pop' is deprecated and no longer supported.",
48+
DeprecationWarning,
49+
)
50+
51+
52+
class Header(Cell):
53+
pass
54+
55+
56+
class Row(Cell):
57+
def __delitem__(self, key):
58+
# This means the item should be removed
59+
self._html = None

0 commit comments

Comments
 (0)