6
6
import time
7
7
from collections import defaultdict
8
8
from collections import OrderedDict
9
+ from pathlib import Path
9
10
10
11
from py .xml import html
11
12
from py .xml import raw
19
20
20
21
class HTMLReport :
21
22
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 ( )
24
25
self .test_logs = []
25
- self .title = os . path . basename ( self .logfile )
26
+ self .title = self .logfile . name
26
27
self .results = []
27
28
self .errors = self .failed = 0
28
29
self .passed = self .skipped = 0
@@ -86,10 +87,8 @@ def _generate_report(self, session):
86
87
numtests = self .passed + self .failed + self .xpassed + self .xfailed
87
88
generated = datetime .datetime .now ()
88
89
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 ()
93
92
94
93
if ansi_support ():
95
94
ansi_css = [
@@ -106,8 +105,7 @@ def _generate_report(self, session):
106
105
self .style_css += "\n * CUSTOM CSS"
107
106
self .style_css += f"\n * { path } "
108
107
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 ()
111
109
112
110
css_href = "assets/style.css"
113
111
html_css = html .link (href = css_href , rel = "stylesheet" , type = "text/css" )
@@ -177,10 +175,8 @@ def _generate_report(self, session):
177
175
),
178
176
]
179
177
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 ()
184
180
185
181
body = html .body (
186
182
html .script (raw (main_js )),
@@ -253,19 +249,17 @@ def _is_redactable_environment_variable(self, environment_variable, config):
253
249
return False
254
250
255
251
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"
258
254
259
- os . makedirs ( dir_name , exist_ok = True )
255
+ dir_name . mkdir ( parents = True , exist_ok = True )
260
256
if not self .self_contained :
261
- os . makedirs ( assets_dir , exist_ok = True )
257
+ assets_dir . mkdir ( parents = True , exist_ok = True )
262
258
263
- with open (self .logfile , "w" , encoding = "utf-8" ) as f :
264
- f .write (report_content )
259
+ self .logfile .write_text (report_content )
265
260
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 )
269
263
270
264
def _post_process_reports (self ):
271
265
for test_name , test_reports in self .reports .items ():
@@ -339,4 +333,4 @@ def pytest_sessionfinish(self, session):
339
333
self ._save_report (report_content )
340
334
341
335
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