1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (C) 2024 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import subprocess
17import pkg_resources
18import pytest
19import os
20import time
21
22
23def check_library_installation(library_name):
24    try:
25        pkg_resources.get_distribution(library_name)
26        return 0
27    except pkg_resources.DistributionNotFound:
28        print(f"\n\n{library_name} is not installed.\n\n")
29        print(f"try to use command below:")
30        print(f"pip install {library_name}")
31        return 1
32
33
34if __name__ == '__main__':
35    if check_library_installation("pytest"):
36        subprocess.check_call(["pip", "install", "-r", "requirements.txt"])
37        if check_library_installation("pytest"):
38            exit(1)
39
40    start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
41    pytest.main()
42    end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
43    report_time = time.strftime('%Y-%m-%d_%H_%M_%S', time.localtime(time.time()))
44    report_dir = os.path.join(os.getcwd(), "reports")
45    report_file = os.path.join(report_dir, f"{report_time}report.html")
46    print(f"Test over, the script version is {get_version},"
47        f" start at {start_time}, end at {end_time} \n"
48        f"=======>{report_file} is saved. \n"
49    )
50    input("=======>press [Enter] key to Show logs.")