Skip to content

Commit 3fbfd25

Browse files
Use pathlib.Path for file path handling (#514)
* 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
1 parent d6d9cb4 commit 3fbfd25

File tree

3 files changed

+25
-32
lines changed

3 files changed

+25
-32
lines changed

src/pytest_html/html_report.py

+17-23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import time
77
from collections import defaultdict
88
from collections import OrderedDict
9+
from pathlib import Path
910

1011
from py.xml import html
1112
from py.xml import raw
@@ -19,10 +20,10 @@
1920

2021
class HTMLReport:
2122
def __init__(self, logfile, config):
22-
logfile = os.path.expanduser(os.path.expandvars(logfile))
23-
self.logfile = os.path.abspath(logfile)
23+
logfile = Path(os.path.expandvars(logfile)).expanduser()
24+
self.logfile = logfile.absolute()
2425
self.test_logs = []
25-
self.title = os.path.basename(self.logfile)
26+
self.title = self.logfile.name
2627
self.results = []
2728
self.errors = self.failed = 0
2829
self.passed = self.skipped = 0
@@ -86,10 +87,8 @@ def _generate_report(self, session):
8687
numtests = self.passed + self.failed + self.xpassed + self.xfailed
8788
generated = datetime.datetime.now()
8889

89-
with open(
90-
os.path.join(os.path.dirname(__file__), "resources", "style.css")
91-
) as style_css_fp:
92-
self.style_css = style_css_fp.read()
90+
css_path = Path(__file__).parent / "resources" / "style.css"
91+
self.style_css = css_path.read_text()
9392

9493
if ansi_support():
9594
ansi_css = [
@@ -106,8 +105,7 @@ def _generate_report(self, session):
106105
self.style_css += "\n * CUSTOM CSS"
107106
self.style_css += f"\n * {path}"
108107
self.style_css += "\n ******************************/\n\n"
109-
with open(path) as f:
110-
self.style_css += f.read()
108+
self.style_css += Path(path).read_text()
111109

112110
css_href = "assets/style.css"
113111
html_css = html.link(href=css_href, rel="stylesheet", type="text/css")
@@ -177,10 +175,8 @@ def _generate_report(self, session):
177175
),
178176
]
179177

180-
with open(
181-
os.path.join(os.path.dirname(__file__), "resources", "main.js")
182-
) as main_js_fp:
183-
main_js = main_js_fp.read()
178+
main_js_path = Path(__file__).parent / "resources" / "main.js"
179+
main_js = main_js_path.read_text()
184180

185181
body = html.body(
186182
html.script(raw(main_js)),
@@ -253,19 +249,17 @@ def _is_redactable_environment_variable(self, environment_variable, config):
253249
return False
254250

255251
def _save_report(self, report_content):
256-
dir_name = os.path.dirname(self.logfile)
257-
assets_dir = os.path.join(dir_name, "assets")
252+
dir_name = self.logfile.parent
253+
assets_dir = dir_name / "assets"
258254

259-
os.makedirs(dir_name, exist_ok=True)
255+
dir_name.mkdir(parents=True, exist_ok=True)
260256
if not self.self_contained:
261-
os.makedirs(assets_dir, exist_ok=True)
257+
assets_dir.mkdir(parents=True, exist_ok=True)
262258

263-
with open(self.logfile, "w", encoding="utf-8") as f:
264-
f.write(report_content)
259+
self.logfile.write_text(report_content)
265260
if not self.self_contained:
266-
style_path = os.path.join(assets_dir, "style.css")
267-
with open(style_path, "w", encoding="utf-8") as f:
268-
f.write(self.style_css)
261+
style_path = assets_dir / "style.css"
262+
style_path.write_text(self.style_css)
269263

270264
def _post_process_reports(self):
271265
for test_name, test_reports in self.reports.items():
@@ -339,4 +333,4 @@ def pytest_sessionfinish(self, session):
339333
self._save_report(report_content)
340334

341335
def pytest_terminal_summary(self, terminalreporter):
342-
terminalreporter.write_sep("-", f"generated html file: file://{self.logfile}")
336+
terminalreporter.write_sep("-", f"generated html file: {self.logfile.as_uri()}")

src/pytest_html/plugin.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This Source Code Form is subject to the terms of the Mozilla Public
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4-
import os
4+
from pathlib import Path
55

66
import pytest
77

@@ -66,7 +66,7 @@ def pytest_configure(config):
6666
if htmlpath:
6767
missing_css_files = []
6868
for csspath in config.getoption("css"):
69-
if not os.path.exists(csspath):
69+
if not Path(csspath).exists():
7070
missing_css_files.append(csspath)
7171

7272
if missing_css_files:

src/pytest_html/result.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import json
2-
import os
32
import re
43
import time
54
import warnings
65
from base64 import b64decode
76
from base64 import b64encode
87
from html import escape
98
from os.path import isfile
9+
from pathlib import Path
1010

1111
from _pytest.logging import _remove_ansi_escape_sequences
1212
from py.xml import html
@@ -86,17 +86,16 @@ def create_asset(self, content, extra_index, test_index, file_extension, mode="w
8686
str(test_index),
8787
file_extension,
8888
)[-self.max_asset_filename_length :]
89-
asset_path = os.path.join(
90-
os.path.dirname(self.logfile), "assets", asset_file_name
91-
)
89+
asset_path = Path(self.logfile).parent / "assets" / asset_file_name
9290

93-
os.makedirs(os.path.dirname(asset_path), exist_ok=True)
91+
asset_path.parent.mkdir(exist_ok=True, parents=True)
9492

9593
relative_path = f"assets/{asset_file_name}"
9694

9795
kwargs = {"encoding": "utf-8"} if "b" not in mode else {}
98-
with open(asset_path, mode, **kwargs) as f:
99-
f.write(content)
96+
func = asset_path.write_bytes if "b" in mode else asset_path.write_text
97+
func(content, **kwargs)
98+
10099
return relative_path
101100

102101
def append_extra_html(self, extra, extra_index, test_index):

0 commit comments

Comments
 (0)