66import time
77from collections import defaultdict
88from collections import OrderedDict
9+ from pathlib import Path
910
1011from py .xml import html
1112from py .xml import raw
1920
2021class 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 () } " )
0 commit comments