Skip to content

Commit 0f7a9e2

Browse files
committed
move nextgen to sep module
1 parent 6dc58cf commit 0f7a9e2

File tree

2 files changed

+68
-64
lines changed

2 files changed

+68
-64
lines changed

src/pytest_html/nextgen.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import json
2+
from typing import Any
3+
from typing import Dict
4+
5+
import pytest
6+
7+
8+
class NextGenReport:
9+
def __init__(self, config, data_file):
10+
self._config = config
11+
self._data_file = data_file
12+
13+
self._title = "Next Gen Report"
14+
self._data = {
15+
"title": self._title,
16+
"collectedItems": 0,
17+
"environment": {},
18+
"tests": [],
19+
}
20+
21+
self._data_file.parent.mkdir(parents=True, exist_ok=True)
22+
23+
def _write(self):
24+
try:
25+
data = json.dumps(self._data)
26+
except TypeError:
27+
data = cleanup_unserializable(self._data)
28+
data = json.dumps(data)
29+
30+
with self._data_file.open("w", buffering=1, encoding="UTF-8") as f:
31+
f.write("const jsonData = ")
32+
f.write(data)
33+
f.write("\n")
34+
35+
@pytest.hookimpl(trylast=True)
36+
def pytest_sessionstart(self, session):
37+
config = session.config
38+
if hasattr(config, "_metadata") and config._metadata:
39+
metadata = config._metadata
40+
self._data["environment"] = metadata
41+
self._write()
42+
43+
@pytest.hookimpl(trylast=True)
44+
def pytest_collection_finish(self, session):
45+
self._data["collectedItems"] = len(session.items)
46+
self._write()
47+
48+
@pytest.hookimpl(trylast=True)
49+
def pytest_runtest_logreport(self, report):
50+
data = self._config.hook.pytest_report_to_serializable(
51+
config=self._config, report=report
52+
)
53+
self._data["tests"].append(data)
54+
self._write()
55+
56+
57+
def cleanup_unserializable(d: Dict[str, Any]) -> Dict[str, Any]:
58+
"""Return new dict with entries that are not json serializable by their str()."""
59+
result = {}
60+
for k, v in d.items():
61+
try:
62+
json.dumps({k: v})
63+
except TypeError:
64+
v = str(v)
65+
result[k] = v
66+
return result

src/pytest_html/plugin.py

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
from functools import lru_cache
1717
from html import escape
1818
from os.path import isfile
19-
from typing import Any
20-
from typing import Dict
2119

2220
import pytest
2321
from _pytest.logging import _remove_ansi_escape_sequences
@@ -28,6 +26,7 @@
2826
from . import __pypi_url__
2927
from . import __version__
3028
from . import extras
29+
from . import nextgen
3130

3231

3332
@lru_cache()
@@ -104,7 +103,7 @@ def pytest_configure(config):
104103
if not hasattr(config, "workerinput"):
105104
# prevent opening htmlpath on worker nodes (xdist)
106105
config._html = HTMLReport(htmlpath, config)
107-
config._next_gen = NextGenReport(config, Path("nextgendata.json"))
106+
config._next_gen = nextgen.NextGenReport(config, Path("nextgendata.js"))
108107
config.pluginmanager.register(config._html)
109108
config.pluginmanager.register(config._next_gen)
110109

@@ -767,64 +766,3 @@ def pytest_sessionfinish(self, session):
767766

768767
def pytest_terminal_summary(self, terminalreporter):
769768
terminalreporter.write_sep("-", f"generated html file: file://{self.logfile}")
770-
771-
772-
class NextGenReport:
773-
def __init__(self, config, data_file):
774-
self._config = config
775-
self._data_file = data_file
776-
777-
self._title = "Next Gen Report"
778-
self._data = {
779-
"title": self._title,
780-
"collectedItems": 0,
781-
"environment": {},
782-
"tests": [],
783-
}
784-
785-
self._data_file.parent.mkdir(parents=True, exist_ok=True)
786-
787-
def _write(self):
788-
try:
789-
data = json.dumps(self._data)
790-
except TypeError:
791-
data = cleanup_unserializable(self._data)
792-
data = json.dumps(data)
793-
794-
with self._data_file.open("w", buffering=1, encoding="UTF-8") as f:
795-
f.write("const jsonData = ")
796-
f.write(data)
797-
f.write("\n")
798-
799-
@pytest.hookimpl(trylast=True)
800-
def pytest_sessionstart(self, session):
801-
config = session.config
802-
if hasattr(config, "_metadata") and config._metadata:
803-
metadata = config._metadata
804-
self._data["environment"] = metadata
805-
self._write()
806-
807-
@pytest.hookimpl(trylast=True)
808-
def pytest_collection_finish(self, session):
809-
self._data["collectedItems"] = len(session.items)
810-
self._write()
811-
812-
@pytest.hookimpl(trylast=True)
813-
def pytest_runtest_logreport(self, report):
814-
data = self._config.hook.pytest_report_to_serializable(
815-
config=self._config, report=report
816-
)
817-
self._data["tests"].append(data)
818-
self._write()
819-
820-
821-
def cleanup_unserializable(d: Dict[str, Any]) -> Dict[str, Any]:
822-
"""Return new dict with entries that are not json serializable by their str()."""
823-
result = {}
824-
for k, v in d.items():
825-
try:
826-
json.dumps({k: v})
827-
except TypeError:
828-
v = str(v)
829-
result[k] = v
830-
return result

0 commit comments

Comments
 (0)