Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use Path for reading & writing files as well
  • Loading branch information
mrlegohead0x45 committed May 6, 2022
commit bf3f0104c57e6ca4b10ca009a83543c03cb9589a
17 changes: 7 additions & 10 deletions src/pytest_html/html_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def _generate_report(self, session):
numtests = self.passed + self.failed + self.xpassed + self.xfailed
generated = datetime.datetime.now()

with open(Path(__file__).parent / "resources" / "style.css") as style_css_fp:
self.style_css = style_css_fp.read()
css_path = Path(__file__).parent / "resources" / "style.css"
self.style_css = css_path.read_text()

if ansi_support():
ansi_css = [
Expand All @@ -105,8 +105,7 @@ def _generate_report(self, session):
self.style_css += "\n * CUSTOM CSS"
self.style_css += f"\n * {path}"
self.style_css += "\n ******************************/\n\n"
with open(path) as f:
self.style_css += f.read()
self.style_css += Path(path).read_text()

css_href = "assets/style.css"
html_css = html.link(href=css_href, rel="stylesheet", type="text/css")
Expand Down Expand Up @@ -176,8 +175,8 @@ def _generate_report(self, session):
),
]

with open(Path(__file__).parent / "resources" / "main.js") as main_js_fp:
main_js = main_js_fp.read()
main_js_path = Path(__file__).parent / "resources" / "main.js"
main_js = main_js_path.read_text()

body = html.body(
html.script(raw(main_js)),
Expand Down Expand Up @@ -257,12 +256,10 @@ def _save_report(self, report_content):
if not self.self_contained:
assets_dir.mkdir(parents=True, exist_ok=True)

with open(self.logfile, "w", encoding="utf-8") as f:
f.write(report_content)
self.logfile.write_text(report_content)
if not self.self_contained:
style_path = assets_dir / "style.css"
with open(style_path, "w", encoding="utf-8") as f:
f.write(self.style_css)
style_path.write_text(self.style_css)

def _post_process_reports(self):
for test_name, test_reports in self.reports.items():
Expand Down
5 changes: 3 additions & 2 deletions src/pytest_html/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ def create_asset(self, content, extra_index, test_index, file_extension, mode="w
relative_path = f"assets/{asset_file_name}"

kwargs = {"encoding": "utf-8"} if "b" not in mode else {}
with open(asset_path, mode, **kwargs) as f:
f.write(content)
func = asset_path.write_bytes if "b" in mode else asset_path.write_text
func(content, **kwargs)

return relative_path

def append_extra_html(self, extra, extra_index, test_index):
Expand Down