<commit_before>import json
import os
import platform
import shutil
import traceback
from codecs import open
from datetime import datetime
from xml.dom import minidom

from . import config
from .plogger import pconsole
from .testsuite import default_test_suite
from .enumeration import TestCaseStatus, TestCaseCountItem
from .utils import make_dirs, remove_tree, escape_html

current_dir = os.path.dirname(os.path.abspath(__file__))


def generate_xunit_xml(xml_file_path):
    pconsole.write_line("Generating xunit report...")
    doc = minidom.Document()
    test_suite_ele = doc.createElement("testsuite")
    doc.appendChild(test_suite_ele)
    status_count = default_test_suite.status_count
    test_suite_ele.setAttribute("name", default_test_suite.name)
    test_suite_ele.setAttribute("tests", str(status_count[TestCaseCountItem.TOTAL]))
    test_suite_ele.setAttribute("failures", str(status_count[TestCaseCountItem.FAILED]))
    test_suite_ele.setAttribute("skips", str(status_count[TestCaseCountItem.SKIPPED]))
    test_suite_ele.setAttribute("errors", "0")
    test_suite_ele.setAttribute("time", "%.3f" % default_test_suite.elapsed_time)
    test_suite_ele.setAttribute("timestamp", str(default_test_suite.start_time))

    for test_case in default_test_suite.test_cases:
        test_case_ele = doc.createElement("testcase")
        test_suite_ele.appendChild(test_case_ele)
        test_case_ele.setAttribute("name", test_case.name)
        test_case_ele.setAttribute("classname", test_case.test_class.full_name)
        test_case_ele.setAttribute("time", "%.3f" % test_case.elapsed_time)
        if test_case.status == TestCaseStatus.SKIPPED:
            skipped_ele = doc.createElement("skipped")
            test_case_ele.appendChild(skipped_ele)
            skipped_ele.setAttribute("message", test_case.skip_message)
        elif test_case.status == TestCaseStatus.FAILED:
            failure_ele = doc.createElement("failure")
            test_case_ele.appendChild(failure_ele)
            failure_ele.setAttribute("message", test_case.failure_message)
            failure_ele.setAttribute("type", test_case.failure_type)
            failure_ele.appendChild(doc.createTextNode(test_case.stack_trace))

    if os.path.exists(xml_file_path):
        pconsole.write_line("Cleaning old xunit report...")
        os.remove(xml_file_path)
    else:
        make_dirs(os.path.dirname(xml_file_path))

    f = open(xml_file_path, mode="w", encoding="utf-8")
    try:
        doc.writexml(f, "\t", "\t", "\n", "utf-8")
        pconsole.write_line("xunit report is generated at %s" % xml_file_path)
    except Exception as e:
        pconsole.write_line("Failed to generate xunit report.\n%s" % traceback.format_exc())
    finally:
        f.close()


def generate_html_report(report_dir):
    pconsole.write_line("Generating html report...")

    if os.path.exists(report_dir):
        pconsole.write_line("Cleaning old html report...")
        remove_tree(report_dir, remove_root=False)
    else:
        make_dirs(report_dir)

    html_template_dir = os.path.join(current_dir, "htmltemplate")

    # copy js and css file to report dir
    for fn in os.listdir(html_template_dir):
        file_full_path = os.path.join(html_template_dir, fn)
        _, file_ext = os.path.splitext(fn)
        if os.path.isfile(file_full_path) and file_ext in [".js", ".css"]:
            shutil.copy(file_full_path, report_dir)

    # copy screenshot from temp dir
    temp_dir = config.get_option("temp")
    for fn in os.listdir(temp_dir):
        file_full_path = os.path.join(temp_dir, fn)
        _, file_ext = os.path.splitext(fn)
        if os.path.isfile(file_full_path) and file_ext == ".png":
            shutil.copy(file_full_path, report_dir)

    with open(os.path.join(html_template_dir, "index.html"), encoding="utf-8") as f:
        index_page_template = f.read()

    current_time = datetime.now()
    system_info = "%s / Python %s / %s" % (platform.node(), platform.python_version(), platform.platform())
    test_suite_json = json.dumps(_get_test_suite_dict(default_test_suite))
    index_page_content = index_page_template.format(current_time=current_time, system_info=system_info,
                                                    test_suite_json=test_suite_json)

    f = open(os.path.join(report_dir, "index.html"), mode="w", encoding="utf-8")
    try:
        f.write(index_page_content)
        pconsole.write_line("html report is generated at %s" % os.path.abspath(report_dir))
    except Exception as e:
        pconsole.write_line("Failed to generate html report.\n%s" % traceback.format_exc())
    finally:
        f.close()


def _get_test_suite_dict(test_suite):
    test_suite_dict = {
        "name": escape_html(test_suite.name),
        "fullName": escape_html(test_suite.full_name),
        "type": "suite",
        "testModules": _get_test_module_dicts(test_suite.test_classes),
        "startTime": str(test_suite.start_time),
        "endTime": str(test_suite.end_time),
        "elapsedTime": test_suite.elapsed_time,
        "total": test_suite.status_count[TestCaseCountItem.TOTAL],
        "passed": test_suite.status_count[TestCaseCountItem.PASSED],<commit_msg>Display ptest version in html report.<commit_after>import json
import os
import platform
import shutil
import traceback
from codecs import open
from datetime import datetime
from xml.dom import minidom

from . import config, __version__
from .plogger import pconsole
from .testsuite import default_test_suite
from .enumeration import TestCaseStatus, TestCaseCountItem
from .utils import make_dirs, remove_tree, escape_html

current_dir = os.path.dirname(os.path.abspath(__file__))


def generate_xunit_xml(xml_file_path):
    pconsole.write_line("Generating xunit report...")
    doc = minidom.Document()
    test_suite_ele = doc.createElement("testsuite")
    doc.appendChild(test_suite_ele)
    status_count = default_test_suite.status_count
    test_suite_ele.setAttribute("name", default_test_suite.name)
    test_suite_ele.setAttribute("tests", str(status_count[TestCaseCountItem.TOTAL]))
    test_suite_ele.setAttribute("failures", str(status_count[TestCaseCountItem.FAILED]))
    test_suite_ele.setAttribute("skips", str(status_count[TestCaseCountItem.SKIPPED]))
    test_suite_ele.setAttribute("errors", "0")
    test_suite_ele.setAttribute("time", "%.3f" % default_test_suite.elapsed_time)
    test_suite_ele.setAttribute("timestamp", str(default_test_suite.start_time))

    for test_case in default_test_suite.test_cases:
        test_case_ele = doc.createElement("testcase")
        test_suite_ele.appendChild(test_case_ele)
        test_case_ele.setAttribute("name", test_case.name)
        test_case_ele.setAttribute("classname", test_case.test_class.full_name)
        test_case_ele.setAttribute("time", "%.3f" % test_case.elapsed_time)
        if test_case.status == TestCaseStatus.SKIPPED:
            skipped_ele = doc.createElement("skipped")
            test_case_ele.appendChild(skipped_ele)
            skipped_ele.setAttribute("message", test_case.skip_message)
        elif test_case.status == TestCaseStatus.FAILED:
            failure_ele = doc.createElement("failure")
            test_case_ele.appendChild(failure_ele)
            failure_ele.setAttribute("message", test_case.failure_message)
            failure_ele.setAttribute("type", test_case.failure_type)
            failure_ele.appendChild(doc.createTextNode(test_case.stack_trace))

    if os.path.exists(xml_file_path):
        pconsole.write_line("Cleaning old xunit report...")
        os.remove(xml_file_path)
    else:
        make_dirs(os.path.dirname(xml_file_path))

    f = open(xml_file_path, mode="w", encoding="utf-8")
    try:
        doc.writexml(f, "\t", "\t", "\n", "utf-8")
        pconsole.write_line("xunit report is generated at %s" % xml_file_path)
    except Exception as e:
        pconsole.write_line("Failed to generate xunit report.\n%s" % traceback.format_exc())
    finally:
        f.close()


def generate_html_report(report_dir):
    pconsole.write_line("Generating html report...")

    if os.path.exists(report_dir):
        pconsole.write_line("Cleaning old html report...")
        remove_tree(report_dir, remove_root=False)
    else:
        make_dirs(report_dir)

    html_template_dir = os.path.join(current_dir, "htmltemplate")

    # copy js and css file to report dir
    for fn in os.listdir(html_template_dir):
        file_full_path = os.path.join(html_template_dir, fn)
        _, file_ext = os.path.splitext(fn)
        if os.path.isfile(file_full_path) and file_ext in [".js", ".css"]:
            shutil.copy(file_full_path, report_dir)

    # copy screenshot from temp dir
    temp_dir = config.get_option("temp")
    for fn in os.listdir(temp_dir):
        file_full_path = os.path.join(temp_dir, fn)
        _, file_ext = os.path.splitext(fn)
        if os.path.isfile(file_full_path) and file_ext == ".png":
            shutil.copy(file_full_path, report_dir)

    with open(os.path.join(html_template_dir, "index.html"), encoding="utf-8") as f:
        index_page_template = f.read()

    current_time = datetime.now()
    system_info = "%s / Python %s / %s" % (platform.node(), platform.python_version(), platform.platform())
    test_suite_json = json.dumps(_get_test_suite_dict(default_test_suite))
    index_page_content = index_page_template.format(version=__version__, current_time=current_time, system_info=system_info,
                                                    test_suite_json=test_suite_json)

    f = open(os.path.join(report_dir, "index.html"), mode="w", encoding="utf-8")
    try:
        f.write(index_page_content)
        pconsole.write_line("html report is generated at %s" % os.path.abspath(report_dir))
    except Exception as e:
        pconsole.write_line("Failed to generate html report.\n%s" % traceback.format_exc())
    finally:
        f.close()


def _get_test_suite_dict(test_suite):
    test_suite_dict = {
        "name": escape_html(test_suite.name),
        "fullName": escape_html(test_suite.full_name),
        "type": "suite",
        "testModules": _get_test_module_dicts(test_suite.test_classes),
        "startTime": str(test_suite.start_time),
        "endTime": str(test_suite.end_time),
        "elapsedTime": test_suite.elapsed_time,
        "total": test_suite.status_count[TestCaseCountItem.TOTAL],
        "passed": test_suite.status_count[TestCaseCountItem.PASSED],
{'fraction_non_alphanumeric': 0.11123853211009174, 'fraction_numerical': 0.001337920489296636, 'mean_word_length': 3.7076023391812867, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2310295', 'n_tokens_mistral': 3324, 'n_tokens_neox': 3317, 'n_words': 570}