diff --git a/src/pytest_html/html_report.py b/src/pytest_html/html_report.py index 66e10f07..045c52c1 100644 --- a/src/pytest_html/html_report.py +++ b/src/pytest_html/html_report.py @@ -6,6 +6,7 @@ import time from collections import defaultdict from collections import OrderedDict +from pathlib import Path from py.xml import html from py.xml import raw @@ -19,10 +20,10 @@ class HTMLReport: def __init__(self, logfile, config): - logfile = os.path.expanduser(os.path.expandvars(logfile)) - self.logfile = os.path.abspath(logfile) + logfile = Path(os.path.expandvars(logfile)).expanduser() + self.logfile = logfile.absolute() self.test_logs = [] - self.title = os.path.basename(self.logfile) + self.title = self.logfile.name self.results = [] self.errors = self.failed = 0 self.passed = self.skipped = 0 @@ -86,10 +87,8 @@ def _generate_report(self, session): numtests = self.passed + self.failed + self.xpassed + self.xfailed generated = datetime.datetime.now() - with open( - os.path.join(os.path.dirname(__file__), "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 = [ @@ -106,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") @@ -177,10 +175,8 @@ def _generate_report(self, session): ), ] - with open( - os.path.join(os.path.dirname(__file__), "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)), @@ -253,19 +249,17 @@ def _is_redactable_environment_variable(self, environment_variable, config): return False def _save_report(self, report_content): - dir_name = os.path.dirname(self.logfile) - assets_dir = os.path.join(dir_name, "assets") + dir_name = self.logfile.parent + assets_dir = dir_name / "assets" - os.makedirs(dir_name, exist_ok=True) + dir_name.mkdir(parents=True, exist_ok=True) if not self.self_contained: - os.makedirs(assets_dir, exist_ok=True) + 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 = os.path.join(assets_dir, "style.css") - with open(style_path, "w", encoding="utf-8") as f: - f.write(self.style_css) + style_path = assets_dir / "style.css" + style_path.write_text(self.style_css) def _post_process_reports(self): for test_name, test_reports in self.reports.items(): @@ -339,4 +333,4 @@ def pytest_sessionfinish(self, session): self._save_report(report_content) def pytest_terminal_summary(self, terminalreporter): - terminalreporter.write_sep("-", f"generated html file: file://{self.logfile}") + terminalreporter.write_sep("-", f"generated html file: {self.logfile.as_uri()}") diff --git a/src/pytest_html/plugin.py b/src/pytest_html/plugin.py index 0034da19..3670a81f 100644 --- a/src/pytest_html/plugin.py +++ b/src/pytest_html/plugin.py @@ -1,7 +1,7 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -import os +from pathlib import Path import pytest @@ -66,7 +66,7 @@ def pytest_configure(config): if htmlpath: missing_css_files = [] for csspath in config.getoption("css"): - if not os.path.exists(csspath): + if not Path(csspath).exists(): missing_css_files.append(csspath) if missing_css_files: diff --git a/src/pytest_html/result.py b/src/pytest_html/result.py index f791e6d7..a9313c17 100644 --- a/src/pytest_html/result.py +++ b/src/pytest_html/result.py @@ -1,5 +1,4 @@ import json -import os import re import time import warnings @@ -7,6 +6,7 @@ from base64 import b64encode from html import escape from os.path import isfile +from pathlib import Path from _pytest.logging import _remove_ansi_escape_sequences from py.xml import html @@ -86,17 +86,16 @@ def create_asset(self, content, extra_index, test_index, file_extension, mode="w str(test_index), file_extension, )[-self.max_asset_filename_length :] - asset_path = os.path.join( - os.path.dirname(self.logfile), "assets", asset_file_name - ) + asset_path = Path(self.logfile).parent / "assets" / asset_file_name - os.makedirs(os.path.dirname(asset_path), exist_ok=True) + asset_path.parent.mkdir(exist_ok=True, parents=True) 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):