1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2020 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# 18 19import os 20import copy 21from core.utils import get_file_list_by_postfix 22from core.config.config_manager import FilterConfigManager 23from xdevice import platform_logger 24 25LOG = platform_logger("TestcaseManager") 26 27TESTFILE_TYPE_DATA_DIC = { 28 "DEX": [], 29 "HAP": [], 30 "PYT": [], 31 "CXX": [], 32 "BIN": [], 33 "JST": [], 34} 35FILTER_SUFFIX_NAME_LIST = [".TOC", ".info", ".pyc"] 36 37 38class TestCaseManager(object): 39 def get_test_files(self, test_case_path, options): 40 LOG.info("test case path: " + test_case_path) 41 LOG.info("test type list: " + str(options.testtype)) 42 suit_file_dic = copy.deepcopy(TESTFILE_TYPE_DATA_DIC) 43 if os.path.exists(test_case_path): 44 if len(options.testtype) != 0: 45 test_type_list = options.testtype 46 suit_file_dic = self.get_test_file_data( 47 test_case_path, 48 test_type_list, 49 options) 50 else: 51 LOG.error("%s is not exist." % test_case_path) 52 return suit_file_dic 53 54 def get_test_file_data(self, test_case_path, test_type_list, options): 55 suit_file_dic = copy.deepcopy(TESTFILE_TYPE_DATA_DIC) 56 for test_type in test_type_list: 57 temp_dic = self.get_test_file_data_by_test_type( 58 test_case_path, 59 test_type, 60 options) 61 for key, value in suit_file_dic.items(): 62 suit_file_dic[key] = value + temp_dic[key] 63 return suit_file_dic 64 65 def get_test_file_data_by_test_type(self, test_case_path, 66 test_type, options): 67 suit_file_dictionary = copy.deepcopy(TESTFILE_TYPE_DATA_DIC) 68 test_case_out_path = os.path.join(test_case_path, test_type) 69 if os.path.exists(test_case_out_path): 70 LOG.info("The test case directory: %s" % test_case_out_path) 71 return self.get_all_test_file(test_case_out_path, options) 72 else: 73 LOG.error("Test case dir does not exist. %s" % test_case_out_path) 74 return suit_file_dictionary 75 76 def get_all_test_file(self, test_case_out_path, options): 77 suite_file_dictionary = copy.deepcopy(TESTFILE_TYPE_DATA_DIC) 78 filter_part_list = FilterConfigManager().get_filtering_list( 79 "subsystem_name", options.productform) 80 filter_list_test_file = FilterConfigManager().get_filtering_list( 81 "testfile_name", options.productform) 82 83 for part_name in os.listdir(test_case_out_path): 84 part_case_dir = os.path.join(test_case_out_path, part_name) 85 if not os.path.isdir(part_case_dir): 86 continue 87 88 if part_name in filter_part_list: 89 continue 90 91 suite_file_list = get_file_list_by_postfix(part_case_dir) 92 for suite_file in suite_file_list: 93 if -1 != suite_file.replace(test_case_out_path, "").find( 94 os.sep + "resource" + os.sep): 95 continue 96 97 file_name = os.path.basename(suite_file) 98 if file_name in filter_list_test_file: 99 continue 100 101 _, suffix_name = os.path.splitext(file_name) 102 if suffix_name in FILTER_SUFFIX_NAME_LIST: 103 continue 104 105 if not self.get_valid_suite_file(test_case_out_path, 106 suite_file, 107 options): 108 continue 109 110 if suffix_name == ".dex": 111 suite_file_dictionary["DEX"].append(suite_file) 112 elif suffix_name == ".hap": 113 suite_file_dictionary["JST"].append(suite_file) 114 elif suffix_name == ".py": 115 if not self.check_python_test_file(suite_file): 116 continue 117 suite_file_dictionary["PYT"].append(suite_file) 118 elif suffix_name == "": 119 suite_file_dictionary["CXX"].append(suite_file) 120 elif suffix_name == ".bin": 121 suite_file_dictionary["BIN"].append(suite_file) 122 123 return suite_file_dictionary 124 125 @classmethod 126 def get_valid_suite_file(cls, test_case_out_path, suite_file, options): 127 partlist = options.partname_list 128 testmodule = options.testmodule 129 testsuit = options.testsuit 130 131 if not suite_file.startswith(test_case_out_path): 132 return False 133 134 if testsuit != "": 135 short_name, _ = os.path.splitext(os.path.basename(suite_file)) 136 return short_name.startswith(testsuit) or \ 137 testsuit.startswith(short_name) 138 139 is_valid_status = False 140 suitfile_subpath = suite_file.replace(test_case_out_path, "") 141 suitfile_subpath = suitfile_subpath.strip(os.sep) 142 if len(partlist) == 0: 143 if testmodule != "": 144 temp_list = suitfile_subpath.split(os.sep) 145 if len(temp_list) > 2 and testmodule == temp_list[1]: 146 is_valid_status = True 147 else: 148 is_valid_status = True 149 else: 150 for partname in partlist: 151 if testmodule != "": 152 if suitfile_subpath.startswith( 153 partname + os.sep + testmodule + os.sep): 154 is_valid_status = True 155 break 156 else: 157 if suitfile_subpath.startswith(partname + os.sep): 158 is_valid_status = True 159 break 160 return is_valid_status 161 162 @classmethod 163 def check_python_test_file(cls, suite_file): 164 if suite_file.endswith(".py"): 165 filename = os.path.basename(suite_file) 166 if filename.startswith("test_"): 167 return True 168 return False 169