add custom_copy_assets

This commit is contained in:
Daniel 2023-12-09 14:45:56 +01:00
parent ad8f3a3891
commit cb6b166131

View file

@ -5,19 +5,36 @@ import json
import os
import pathlib
import re
import shutil
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup # type: ignore
from packaging import version
CHECKBOX_REGEX = r"^(?P<num>0|[1-9]\d*) (?P<txt1>.*)"
def merge_html_reports(in_path: str, out_path: str, title: str):
paths = get_html_files(in_path, out_path)
if not paths:
raise RuntimeError(f"Unable to find html files in {in_path}")
def custom_copy_assets(assets_dir_path: str, out_file_path: str):
"""custom function added for pytest_abra
assets_dir_path = get_assets_path(in_path)
copies every asset to asset folder. Exclude style.css as this is already handled by pytest_html_merger"""
assets_source_dir = pathlib.Path(assets_dir_path)
assets_source_files = [p for p in assets_source_dir.glob("*") if p.is_file() and p.name != "style.css"]
out_dir_path = pathlib.Path(out_file_path).parent
assets_target_dir = out_dir_path / "assets"
assets_target_dir.mkdir(exist_ok=True)
for asset in assets_source_files:
shutil.copy(asset, assets_target_dir / asset.name)
def merge_html_reports(in_dir_path: str, out_file_path: str, report_title: str):
paths = get_html_files(in_dir_path, out_file_path)
if not paths:
raise RuntimeError(f"Unable to find html files in {in_dir_path}")
assets_dir_path = get_assets_path(in_dir_path)
custom_copy_assets(assets_dir_path, out_file_path)
first_file = BeautifulSoup("".join(open(paths[0])), features="html.parser")
paths.pop(0)
@ -30,7 +47,7 @@ def merge_html_reports(in_path: str, out_path: str, title: str):
if assets_dir_path is None:
print(
f"Will assume css is embedded in the reports. If this is not the case, "
f"Please make sure that you have 'assets' directory inside {in_path} "
f"Please make sure that you have 'assets' directory inside {in_dir_path} "
f"which contains css files generated by pytest-html."
)
else:
@ -42,7 +59,7 @@ def merge_html_reports(in_path: str, out_path: str, title: str):
head.style.append(content)
h = first_file.find("h1")
h.string = title or os.path.basename(out_path)
h.string = report_title or os.path.basename(out_file_path)
ps = first_file.find_all("p")
pytest_version = ps[0].text.split(" ")[-1]
@ -106,7 +123,7 @@ def merge_html_reports(in_path: str, out_path: str, title: str):
for cb_type in cb_types:
set_checkbox_value(first_file, cb_type, cb_types[cb_type])
with open(out_path, "w") as f:
with open(out_file_path, "w") as f:
f.write(str(first_file))