Skip to content

Commit

Permalink
Use pathlib.Path for file path handling (#514)
Browse files Browse the repository at this point in the history
* Change to pathlib.Path in html_report.py

* Change plugin.py to use Path

* Convert result.py

* Use Path for reading & writing files as well
  • Loading branch information
mrlegohead0x45 committed Jul 15, 2022
1 parent d6d9cb4 commit 3fbfd25
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
40 changes: 17 additions & 23 deletions src/pytest_html/html_report.py
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 = [
Expand All @@ -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")
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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()}")
4 changes: 2 additions & 2 deletions 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

Expand Down Expand Up @@ -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:
Expand Down
13 changes: 6 additions & 7 deletions src/pytest_html/result.py
@@ -1,12 +1,12 @@
import json
import os
import re
import time
import warnings
from base64 import b64decode
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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 3fbfd25

Please sign in to comment.