1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2023 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18import os
19import platform
20import sys
21
22from py.xml import html
23import pytest
24import time
25
26
27def pytest_html_report_title(report):
28    report.title = "OpenHarmony Build Test Report"
29
30
31@pytest.mark.optionalhook
32def pytest_metadata(metadata):
33    metadata.clear()
34
35    metadata['Python Version'] = sys.version
36    metadata['Cpu Count'] = os.cpu_count()
37    metadata["System Info"] = platform.platform()
38    try:
39        disk_info = os.statvfs('/')
40        total_disk = round(float(disk_info.f_frsize * disk_info.f_blocks) / (1024 ** 3), 4)
41        metadata["Disk Size"] = "{} GB".format(total_disk)
42
43        with open('/proc/meminfo', 'r') as f:
44            lines = f.readlines()
45        total_memory_line = [line for line in lines if line.startswith('MemTotal')]
46        total_memory = round(float(total_memory_line[0].split()[1]) / (1024 ** 2), 4) if total_memory_line else " "
47        metadata["Totla Memory"] = "{} GB".format(total_memory)
48    except Exception as e:
49        print(e)
50
51
52def pytest_html_results_table_header(cells):
53    cells.insert(2, html.th("Description", class_="sortable desc", col="desc"))
54    cells.insert(1, html.th("Time", class_="sortable time", col="time"))
55    cells.pop()
56
57
58def pytest_html_results_table_row(report, cells):
59    cells.insert(2, html.th(report.description))
60    cells.insert(1, html.th(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), class_="col-time"))
61    cells.pop()
62
63
64@pytest.hookimpl(hookwrapper=True)
65def pytest_runtest_makereport(item, call):
66    outcome = yield
67    report = outcome.get_result()
68    if str(item.function.__doc__) != "None":
69        report.description = str(item.function.__doc__)
70    else:
71        report.description = "this is description info"
72
73