From cb6b1661314bdb8e58d37cdd6558b4f050441f09 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 9 Dec 2023 14:45:56 +0100 Subject: [PATCH] add custom_copy_assets --- pytest_abra/html_helper.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/pytest_abra/html_helper.py b/pytest_abra/html_helper.py index 6080b2f..6215350 100644 --- a/pytest_abra/html_helper.py +++ b/pytest_abra/html_helper.py @@ -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"^(?P0|[1-9]\d*) (?P.*)" -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))