1# #
2# # Copyright (c) 2024 Huawei Device Co., Ltd.
3# # Licensed under the Apache License, Version 2.0 (the "License");
4# # you may not use this file except in compliance with the License.
5# # You may obtain a copy of the License at
6# #
7# #     http://www.apache.org/licenses/LICENSE-2.0
8# #
9# # Unless required by applicable law or agreed to in writing, software
10# # distributed under the License is distributed on an "AS IS" BASIS,
11# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# # See the License for the specific language governing permissions and
13# # limitations under the License.
14# #
15
16import argparse
17import os
18import sys
19import unittest
20import config
21
22class RunTest:
23    args = None
24    suite = unittest.TestSuite()
25    loader = unittest.TestLoader()
26    hap_list = []
27
28    def __init__(self):
29        self.parse_args()
30        self.run_test()
31
32    def parse_args(self):
33        parser = argparse.ArgumentParser(description='Plugin test.')
34        parser.add_argument(
35            '-hdctool',
36            action='store',
37            required=True,
38            default=None,
39            help='Remote connnection tool.'
40        )
41        self.args = parser.parse_args()
42
43    def discover_cases(self):
44        current_dir = os.path.dirname(os.path.abspath(__file__))
45        case_dir = current_dir + os.sep + 'js_test_case'
46        discover = self.loader.discover(case_dir, pattern='*.py')
47        self.suite.addTest(discover)
48
49    def get_panda_list(self):
50        self.hap_list = config.hap_list
51
52    def run_test(self):
53        if self.args.hdctool is not None:
54            os.environ["HDCTool"] = self.args.hdctool
55        else:
56            print("please input the path to hdc tool!")
57            sys.exit(0)
58        self.get_panda_list()
59        for hap_name in self.hap_list:
60            if (hap_name != "com.example.myapplication1"):
61                os.environ["isRelease"] = "False"
62            else:
63                os.environ["isRelease"] = "True"
64            self.suite = unittest.TestSuite()
65            self.discover_cases()
66            os.environ["HapName"] = hap_name
67            runner = unittest.TextTestRunner(verbosity=2)
68            runner.run(self.suite)
69
70Main = RunTest
71
72if __name__ == '__main__':
73    Main()
74